Skip to content

Commit 00e7002

Browse files
henripqtmdelapenya
andauthored
fix: update module container struct name and missing imports (#2831)
* fix: update module container struct name and missing imports * fix: update tests --------- Co-authored-by: Manuel de la Peña <mdelapenya@gmail.com>
1 parent fb6a4ba commit 00e7002

File tree

6 files changed

+33
-47
lines changed

6 files changed

+33
-47
lines changed

modulegen/_template/examples_test.go.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
{{ $entrypoint := Entrypoint }}{{ $image := Image }}{{ $lower := ToLower }}{{ $title := Title }}package {{ $lower }}_test
1+
{{ $entrypoint := Entrypoint }}{{ $image := Image }}{{ $lower := ToLower }}package {{ $lower }}_test
22

33
import (
44
"context"
55
"fmt"
66
"log"
77

8+
"github.com/testcontainers/testcontainers-go"
89
"github.com/testcontainers/testcontainers-go/{{ ParentDir }}/{{ $lower }}"
910
)
1011

1112
func Example{{ $entrypoint }}() {
12-
// run{{ $title }}Container {
1313
ctx := context.Background()
1414

1515
{{ $lower }}Container, err := {{ $lower }}.{{ $entrypoint }}(ctx, "{{ $image }}")

modulegen/_template/module.go.tmpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
"github.com/testcontainers/testcontainers-go"
88
)
99

10-
// {{ $containerName }} represents the {{ $title }} container type used in the module
11-
type {{ $containerName }} struct {
10+
// Container represents the {{ $title }} container type used in the module
11+
type Container struct {
1212
testcontainers.Container
1313
}
1414

1515
// {{ $entrypoint }} creates an instance of the {{ $title }} container type
16-
func {{ $entrypoint }}(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*{{ $containerName }}, error) {
16+
func {{ $entrypoint }}(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) {
1717
req := testcontainers.ContainerRequest{
1818
Image: img,
1919
}
@@ -30,9 +30,9 @@ func {{ $entrypoint }}(ctx context.Context, img string, opts ...testcontainers.C
3030
}
3131

3232
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
33-
var c *{{ $containerName }}
33+
var c *Container
3434
if container != nil {
35-
c = &{{ $containerName }}{Container: container}
35+
c = &Container{Container: container}
3636
}
3737

3838
if err != nil {

modulegen/_template/module.md.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{{ $lower := ToLower }}{{ $title := Title }}# {{ $title }}
1+
{{ $entrypoint := Entrypoint }}{{ $lower := ToLower }}{{ $title := Title }}# {{ $title }}
22

33
Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a>
44

@@ -17,7 +17,7 @@ go get github.com/testcontainers/testcontainers-go/{{ ParentDir }}/{{ $lower }}
1717
## Usage example
1818

1919
<!--codeinclude-->
20-
[Creating a {{ $title }} container](../../{{ ParentDir }}/{{ $lower }}/examples_test.go) inside_block:run{{ $title }}Container
20+
[Creating a {{ $title }} container](../../{{ ParentDir }}/{{ $lower }}/examples_test.go) inside_block:Example{{ $entrypoint }}
2121
<!--/codeinclude-->
2222

2323
## Module Reference

modulegen/_template/module_test.go.tmpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/stretchr/testify/require"
88

9+
"github.com/testcontainers/testcontainers-go"
910
"github.com/testcontainers/testcontainers-go/{{ ParentDir }}/{{ $lower }}"
1011
)
1112

modulegen/internal/context/types.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55
"regexp"
66
"strings"
7-
"unicode"
8-
"unicode/utf8"
97

108
"golang.org/x/text/cases"
119
"golang.org/x/text/language"
@@ -22,16 +20,7 @@ type TestcontainersModule struct {
2220
// ContainerName returns the name of the container, which is the lower-cased title of the example
2321
// If the title is set, it will be used instead of the name
2422
func (m *TestcontainersModule) ContainerName() string {
25-
name := m.Lower()
26-
27-
if m.IsModule {
28-
name = m.Title()
29-
} else if m.TitleName != "" {
30-
r, n := utf8.DecodeRuneInString(m.TitleName)
31-
name = string(unicode.ToLower(r)) + m.TitleName[n:]
32-
}
33-
34-
return name + "Container"
23+
return "Container"
3524
}
3625

3726
// Entrypoint returns the name of the entrypoint function, which is the lower-cased title of the example

modulegen/main_test.go

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ import (
1717

1818
func TestModule(t *testing.T) {
1919
tests := []struct {
20-
name string
21-
module context.TestcontainersModule
22-
expectedContainerName string
23-
expectedEntrypoint string
24-
expectedTitle string
20+
name string
21+
module context.TestcontainersModule
22+
expectedEntrypoint string
23+
expectedTitle string
2524
}{
2625
{
2726
name: "Module with title",
@@ -31,9 +30,8 @@ func TestModule(t *testing.T) {
3130
Image: "mongodb:latest",
3231
TitleName: "MongoDB",
3332
},
34-
expectedContainerName: "MongoDBContainer",
35-
expectedEntrypoint: "Run",
36-
expectedTitle: "MongoDB",
33+
expectedEntrypoint: "Run",
34+
expectedTitle: "MongoDB",
3735
},
3836
{
3937
name: "Module without title",
@@ -42,9 +40,8 @@ func TestModule(t *testing.T) {
4240
IsModule: true,
4341
Image: "mongodb:latest",
4442
},
45-
expectedContainerName: "MongodbContainer",
46-
expectedEntrypoint: "Run",
47-
expectedTitle: "Mongodb",
43+
expectedEntrypoint: "Run",
44+
expectedTitle: "Mongodb",
4845
},
4946
{
5047
name: "Example with title",
@@ -54,9 +51,8 @@ func TestModule(t *testing.T) {
5451
Image: "mongodb:latest",
5552
TitleName: "MongoDB",
5653
},
57-
expectedContainerName: "mongoDBContainer",
58-
expectedEntrypoint: "run",
59-
expectedTitle: "MongoDB",
54+
expectedEntrypoint: "run",
55+
expectedTitle: "MongoDB",
6056
},
6157
{
6258
name: "Example without title",
@@ -65,9 +61,9 @@ func TestModule(t *testing.T) {
6561
IsModule: false,
6662
Image: "mongodb:latest",
6763
},
68-
expectedContainerName: "mongodbContainer",
69-
expectedEntrypoint: "run",
70-
expectedTitle: "Mongodb",
64+
65+
expectedEntrypoint: "run",
66+
expectedTitle: "Mongodb",
7167
},
7268
}
7369

@@ -77,7 +73,7 @@ func TestModule(t *testing.T) {
7773

7874
assert.Equal(t, "mongodb", module.Lower())
7975
assert.Equal(t, test.expectedTitle, module.Title())
80-
assert.Equal(t, test.expectedContainerName, module.ContainerName())
76+
assert.Equal(t, "Container", module.ContainerName())
8177
assert.Equal(t, test.expectedEntrypoint, module.Entrypoint())
8278
})
8379
}
@@ -366,6 +362,7 @@ func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, m
366362

367363
lower := module.Lower()
368364
title := module.Title()
365+
entrypoint := module.Entrypoint()
369366

370367
data := sanitiseContent(content)
371368
assert.Equal(t, "# "+title, data[0])
@@ -376,7 +373,7 @@ func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, m
376373
assert.Equal(t, "Please run the following command to add the "+title+" module to your Go dependencies:", data[10])
377374
assert.Equal(t, "go get github.com/testcontainers/testcontainers-go/"+module.ParentDir()+"/"+lower, data[13])
378375
assert.Equal(t, "<!--codeinclude-->", data[18])
379-
assert.Equal(t, "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/examples_test.go) inside_block:run"+title+"Container", data[19])
376+
assert.Equal(t, "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/examples_test.go) inside_block:Example"+entrypoint, data[19])
380377
assert.Equal(t, "<!--/codeinclude-->", data[20])
381378
assert.Equal(t, "The "+title+" module exposes one entrypoint function to create the "+title+" container, and this function receives three parameters:", data[31])
382379
assert.True(t, strings.HasSuffix(data[34], "(*"+title+"Container, error)"))
@@ -391,13 +388,12 @@ func assertExamplesTestContent(t *testing.T, module context.TestcontainersModule
391388

392389
lower := module.Lower()
393390
entrypoint := module.Entrypoint()
394-
title := module.Title()
395391

396392
data := sanitiseContent(content)
397393
assert.Equal(t, "package "+lower+"_test", data[0])
398-
assert.Equal(t, "\t\"github.com/testcontainers/testcontainers-go/modules/"+lower+"\"", data[7])
399-
assert.Equal(t, "func Example"+entrypoint+"() {", data[10])
400-
assert.Equal(t, "\t// run"+title+"Container {", data[11])
394+
assert.Equal(t, "\t\"github.com/testcontainers/testcontainers-go\"", data[7])
395+
assert.Equal(t, "\t\"github.com/testcontainers/testcontainers-go/modules/"+lower+"\"", data[8])
396+
assert.Equal(t, "func Example"+entrypoint+"() {", data[11])
401397
assert.Equal(t, "\t"+lower+"Container, err := "+lower+"."+entrypoint+"(ctx, \""+module.Image+"\")", data[14])
402398
assert.Equal(t, "\tfmt.Println(state.Running)", data[32])
403399
assert.Equal(t, "\t// Output:", data[34])
@@ -411,8 +407,8 @@ func assertModuleTestContent(t *testing.T, module context.TestcontainersModule,
411407

412408
data := sanitiseContent(content)
413409
assert.Equal(t, "package "+module.Lower()+"_test", data[0])
414-
assert.Equal(t, "func Test"+module.Title()+"(t *testing.T) {", data[11])
415-
assert.Equal(t, "\tctr, err := "+module.Lower()+"."+module.Entrypoint()+"(ctx, \""+module.Image+"\")", data[14])
410+
assert.Equal(t, "func Test"+module.Title()+"(t *testing.T) {", data[12])
411+
assert.Equal(t, "\tctr, err := "+module.Lower()+"."+module.Entrypoint()+"(ctx, \""+module.Image+"\")", data[15])
416412
}
417413

418414
// assert content module
@@ -427,7 +423,7 @@ func assertModuleContent(t *testing.T, module context.TestcontainersModule, exam
427423

428424
data := sanitiseContent(content)
429425
require.Equal(t, "package "+lower, data[0])
430-
require.Equal(t, "// "+containerName+" represents the "+exampleName+" container type used in the module", data[9])
426+
require.Equal(t, "// Container represents the "+exampleName+" container type used in the module", data[9])
431427
require.Equal(t, "type "+containerName+" struct {", data[10])
432428
require.Equal(t, "// "+entrypoint+" creates an instance of the "+exampleName+" container type", data[14])
433429
require.Equal(t, "func "+entrypoint+"(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*"+containerName+", error) {", data[15])

0 commit comments

Comments
 (0)