Skip to content

Commit

Permalink
chore: always generate the examples including a explicit image (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Sep 12, 2023
1 parent fec0b4d commit b25ee5a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions modulegen/_template/examples_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{{ $entrypoint := Entrypoint }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}_test
{{ $entrypoint := Entrypoint }}{{ $image := Image }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}_test

import (
"context"
"fmt"

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

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

{{ $lower }}Container, err := {{ $lower }}.{{ $entrypoint }}(ctx)
{{ $lower }}Container, err := {{ $lower }}.{{ $entrypoint }}(ctx, testcontainers.WithImage("{{ $image }}"))
if err != nil {
panic(err)
}
Expand Down
6 changes: 4 additions & 2 deletions modulegen/_template/module_test.go.tmpl
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{{ $entrypoint := Entrypoint }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}
{{ $entrypoint := Entrypoint }}{{ $image := Image }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}

import (
"context"
"testing"

"github.com/testcontainers/testcontainers-go"
)

func Test{{ $title }}(t *testing.T) {
ctx := context.Background()

container, err := {{ $entrypoint }}(ctx)
container, err := {{ $entrypoint }}(ctx, testcontainers.WithImage("{{ $image }}"))
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions modulegen/internal/module/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func generateGoFiles(moduleDir string, tcModule context.TestcontainersModule) er
funcMap := template.FuncMap{
"Entrypoint": tcModule.Entrypoint,
"ContainerName": tcModule.ContainerName,
"Image": func() string { return tcModule.Image },
"ParentDir": tcModule.ParentDir,
"ToLower": tcModule.Lower,
"Title": tcModule.Title,
Expand Down
19 changes: 10 additions & 9 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,14 @@ func assertExamplesTestContent(t *testing.T, module context.TestcontainersModule

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.Equal(t, data[6], "\t\"github.com/testcontainers/testcontainers-go\"")
assert.Equal(t, data[7], "\t\"github.com/testcontainers/testcontainers-go/modules/"+lower+"\"")
assert.Equal(t, data[10], "func Example"+entrypoint+"() {")
assert.Equal(t, data[11], "\t// run"+title+"Container {")
assert.Equal(t, data[14], "\t"+lower+"Container, err := "+lower+"."+entrypoint+"(ctx, testcontainers.WithImage(\""+module.Image+"\"))")
assert.Equal(t, data[32], "\tfmt.Println(state.Running)")
assert.Equal(t, data[34], "\t// Output:")
assert.Equal(t, data[35], "\t// true")
}

// assert content module test
Expand All @@ -446,8 +447,8 @@ func assertModuleTestContent(t *testing.T, module context.TestcontainersModule,

data := sanitiseContent(content)
assert.Equal(t, data[0], "package "+module.Lower())
assert.Equal(t, data[7], "func Test"+module.Title()+"(t *testing.T) {")
assert.Equal(t, data[10], "\tcontainer, err := "+module.Entrypoint()+"(ctx)")
assert.Equal(t, data[9], "func Test"+module.Title()+"(t *testing.T) {")
assert.Equal(t, data[12], "\tcontainer, err := "+module.Entrypoint()+"(ctx, testcontainers.WithImage(\""+module.Image+"\"))")
}

// assert content module
Expand Down

0 comments on commit b25ee5a

Please sign in to comment.