Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions modulegen/_template/examples_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{ $entrypoint := Entrypoint }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}_test
Copy link
Copy Markdown
Member Author

@mdelapenya mdelapenya Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


import (
"context"
"fmt"

"github.com/testcontainers/testcontainers-go/modules/{{ $lower }}"
)

func Example{{ $entrypoint }}() {
// run{{ $title }}Container {
ctx := context.Background()

{{ $lower }}Container, err := {{ $lower }}.{{ $entrypoint }}(ctx)
if err != nil {
panic(err)
}

// Clean up the container after the test is complete
defer func() {
if err := {{ $lower }}Container.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

state, err := {{ $lower }}Container.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ go get github.com/testcontainers/testcontainers-go/{{ ParentDir }}/{{ $lower }}
## Usage example

<!--codeinclude-->
[Creating a {{ $title }} container](../../{{ ParentDir }}/{{ $lower }}/{{ $lower }}.go)
<!--/codeinclude-->

<!--codeinclude-->
[Test for a {{ $title }} container](../../{{ ParentDir }}/{{ $lower }}/{{ $lower }}_test.go)
[Creating a {{ $title }} container](../../{{ ParentDir }}/{{ $lower }}/examples_test.go) inside_block:run{{ $title }}Container
<!--/codeinclude-->

## Module reference
Expand Down
2 changes: 1 addition & 1 deletion modulegen/internal/mkdocs/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func GenerateMdFile(filePath string, funcMap template.FuncMap, example any) error {
name := "example.md.tmpl"
name := "module.md.tmpl"
t, err := template.New(name).Funcs(funcMap).ParseFiles(filepath.Join("_template", name))
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions modulegen/internal/module/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ func generateGoModFile(moduleDir string, tcModule context.TestcontainersModule)
}

func GenerateFiles(moduleDir string, moduleName string, funcMap template.FuncMap, tcModule any) error {
for _, tmpl := range []string{"example_test.go", "example.go"} {
for _, tmpl := range []string{"examples_test.go", "module_test.go", "module.go"} {
name := tmpl + ".tmpl"
t, err := template.New(name).Funcs(funcMap).ParseFiles(filepath.Join("_template", name))
if err != nil {
return err
}
moduleFilePath := filepath.Join(moduleDir, strings.ReplaceAll(tmpl, "example", moduleName))
moduleFilePath := filepath.Join(moduleDir, strings.ReplaceAll(tmpl, "module", moduleName))

err = internal_template.GenerateFile(t, moduleFilePath, name, tcModule)
if err != nil {
Expand Down
33 changes: 26 additions & 7 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func TestGenerate(t *testing.T) {
assertModuleGithubWorkflowContent(t, module, mainWorkflowFile)

generatedTemplatesDir := filepath.Join(examplesTmp, moduleNameLower)
assertExamplesTestContent(t, module, filepath.Join(generatedTemplatesDir, "examples_test.go"))
assertModuleTestContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+"_test.go"))
assertModuleContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+".go"))
assertGoModContent(t, module, originalConfig.Extra.LatestVersion, filepath.Join(generatedTemplatesDir, "go.mod"))
Expand Down Expand Up @@ -354,6 +355,7 @@ func TestGenerateModule(t *testing.T) {
assertModuleGithubWorkflowContent(t, module, mainWorkflowFile)

generatedTemplatesDir := filepath.Join(modulesTmp, moduleNameLower)
assertExamplesTestContent(t, module, filepath.Join(generatedTemplatesDir, "examples_test.go"))
assertModuleTestContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+"_test.go"))
assertModuleContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+".go"))
assertGoModContent(t, module, originalConfig.Extra.LatestVersion, filepath.Join(generatedTemplatesDir, "go.mod"))
Expand Down Expand Up @@ -410,14 +412,31 @@ func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, m
assert.Equal(t, data[10], "Please run the following command to add the "+title+" module to your Go dependencies:")
assert.Equal(t, data[13], "go get github.com/testcontainers/testcontainers-go/"+module.ParentDir()+"/"+lower)
assert.Equal(t, data[18], "<!--codeinclude-->")
assert.Equal(t, data[19], "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/"+lower+".go)")
assert.Equal(t, data[19], "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/examples_test.go) inside_block:run"+title+"Container")
assert.Equal(t, data[20], "<!--/codeinclude-->")
assert.Equal(t, data[22], "<!--codeinclude-->")
assert.Equal(t, data[23], "[Test for a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/"+lower+"_test.go)")
assert.Equal(t, data[24], "<!--/codeinclude-->")
assert.Equal(t, data[28], "The "+title+" module exposes one entrypoint function to create the "+title+" container, and this function receives two parameters:")
assert.True(t, strings.HasSuffix(data[31], "(*"+title+"Container, error)"))
assert.Equal(t, "for "+title+". E.g. `testcontainers.WithImage(\""+module.Image+"\")`.", data[44])
assert.Equal(t, data[24], "The "+title+" module exposes one entrypoint function to create the "+title+" container, and this function receives two parameters:")
assert.True(t, strings.HasSuffix(data[27], "(*"+title+"Container, error)"))
assert.Equal(t, "for "+title+". E.g. `testcontainers.WithImage(\""+module.Image+"\")`.", data[40])
}

// assert content module test
func assertExamplesTestContent(t *testing.T, module context.TestcontainersModule, examplesTestFile string) {
content, err := os.ReadFile(examplesTestFile)
assert.Nil(t, err)

lower := module.Lower()
entrypoint := module.Entrypoint()
title := module.Title()

data := sanitiseContent(content)
assert.Equal(t, data[0], "package "+lower+"_test")
assert.Equal(t, data[6], "\t\"github.com/testcontainers/testcontainers-go/modules/"+lower+"\"")
assert.Equal(t, data[9], "func Example"+entrypoint+"() {")
assert.Equal(t, data[10], "\t// run"+title+"Container {")
assert.Equal(t, data[13], "\t"+lower+"Container, err := "+lower+"."+entrypoint+"(ctx)")
assert.Equal(t, data[31], "\tfmt.Println(state.Running)")
assert.Equal(t, data[33], "\t// Output:")
assert.Equal(t, data[34], "\t// true")
}

// assert content module test
Expand Down
11 changes: 9 additions & 2 deletions modules/elasticsearch/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ func ExampleRunContainer() {
panic(err)
}
defer func() {
_ = elasticsearchContainer.Terminate(ctx)
if err := elasticsearchContainer.Terminate(ctx); err != nil {
panic(err)
}
}()
// }

fmt.Println(strings.HasPrefix(elasticsearchContainer.Settings.Address, "https://"))
state, err := elasticsearchContainer.State(ctx)
if err != nil {
panic(err)
}

fmt.Println(state.Running)

// Output:
// true
Expand Down