Skip to content

Commit

Permalink
Merge branch 'main' into kafka-module
Browse files Browse the repository at this point in the history
* main:
  chore: always generate the examples including a explicit image (testcontainers#1611)
  chore: support linting all modules locally (testcontainers#1609)
  • Loading branch information
mdelapenya committed Sep 12, 2023
2 parents c03bfcc + b25ee5a commit 7de1787
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 14 deletions.
6 changes: 6 additions & 0 deletions commons-test.mk
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))

.PHONY: dependencies-scan
dependencies-scan:
@echo ">> Scanning dependencies in $(CURDIR)..."
go list -json -m all | docker run --rm -i sonatypecommunity/nancy:latest sleuth --skip-update-check

.PHONY: lint
lint:
golangci-lint run --out-format=github-actions --path-prefix=. --verbose -c $(ROOT_DIR)/.golangci.yml --fix

.PHONY: test-%
test-%:
@echo "Running $* tests..."
Expand Down
4 changes: 4 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ include ../commons-test.mk
dependencies-scan-examples:
@find . -type f -name Makefile -execdir make dependencies-scan \;

.PHONY: lint-examples
lint-examples:
@find . -type f -name Makefile -execdir make lint \;

.PHONY: tidy-examples
tidy-examples:
@find . -type f -name Makefile -execdir make tools-tidy \;
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
4 changes: 4 additions & 0 deletions modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ include ../commons-test.mk
dependencies-scan-modules:
@find . -type f -name Makefile -execdir make dependencies-scan \;

.PHONY: lint-modules
lint-modules:
@find . -type f -name Makefile -execdir make lint \;

.PHONY: tidy-modules
tidy-modules:
@find . -type f -name Makefile -execdir make tools-tidy \;
2 changes: 1 addition & 1 deletion modules/compose/compose_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (dc *LocalDockerCompose) applyStrategyToRunningContainer() error {
containerListOptions := types.ContainerListOptions{Filters: f, All: true}
containers, err := cli.ContainerList(context.Background(), containerListOptions)
if err != nil {
return fmt.Errorf("error %w occured while filtering the service %s: %d by name and published port", err, k.service, k.publishedPort)
return fmt.Errorf("error %w occurred while filtering the service %s: %d by name and published port", err, k.service, k.publishedPort)
}

if len(containers) == 0 {
Expand Down

0 comments on commit 7de1787

Please sign in to comment.