Skip to content

Commit

Permalink
modulegen: create internal/module and internal/modfile (#1539)
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu MOREL <[email protected]>
  • Loading branch information
mmorel-35 authored Aug 29, 2023
1 parent e1db360 commit 987c6d0
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 73 deletions.
51 changes: 0 additions & 51 deletions modulegen/_template/go.mod.tmpl

This file was deleted.

4 changes: 4 additions & 0 deletions modulegen/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func (ctx *Context) GithubWorkflowsDir() string {
return filepath.Join(ctx.GithubDir(), "workflows")
}

func (ctx *Context) GoModFile() string {
return filepath.Join(ctx.RootDir, "go.mod")
}

func (ctx *Context) getModulesByBaseDir(baseDir string) ([]string, error) {
dir := filepath.Join(ctx.RootDir, baseDir)

Expand Down
1 change: 1 addition & 0 deletions modulegen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb
golang.org/x/mod v0.11.0
golang.org/x/text v0.12.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
2 changes: 2 additions & 0 deletions modulegen/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb h1:mIKbk8weKhSeLH2GmUTrvx8CjkyJmnU1wFmg59CUjFA=
golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
43 changes: 43 additions & 0 deletions modulegen/internal/modfile/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package modfile

import (
"path/filepath"

"golang.org/x/mod/modfile"
)

func GenerateModFile(exampleDir string, rootGoModFilePath string, directory string, tcVersion string) error {
rootGoMod, err := readModFile(rootGoModFilePath)
if err != nil {
return err
}
moduleStmt := rootGoMod.Module.Mod.Path + directory
goStmt := rootGoMod.Go.Version
tcPath := rootGoMod.Module.Mod.Path
file, err := newModFile(moduleStmt, goStmt, tcPath, tcVersion)
if err != nil {
return err
}
return writeModFile(filepath.Join(exampleDir, "go.mod"), file)
}

func newModFile(moduleStmt string, goStmt string, tcPath string, tcVersion string) (*modfile.File, error) {
file := &modfile.File{}
err := file.AddModuleStmt(moduleStmt)
if err != nil {
return nil, err
}
err = file.AddGoStmt(goStmt)
if err != nil {
return nil, err
}
err = file.AddRequire(tcPath, tcVersion)
if err != nil {
return nil, err
}
err = file.AddReplace(tcPath, "", "../..", "")
if err != nil {
return nil, err
}
return file, nil
}
15 changes: 15 additions & 0 deletions modulegen/internal/modfile/reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package modfile

import (
"os"

"golang.org/x/mod/modfile"
)

func readModFile(modFilePath string) (*modfile.File, error) {
file, err := os.ReadFile(modFilePath)
if err != nil {
return nil, err
}
return modfile.Parse(modFilePath, file, nil)
}
20 changes: 20 additions & 0 deletions modulegen/internal/modfile/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package modfile

import (
"os"
"path/filepath"

"golang.org/x/mod/modfile"
)

func writeModFile(modFilePath string, file *modfile.File) error {
err := os.MkdirAll(filepath.Dir(modFilePath), 0o755)
if err != nil {
return err
}
data, err := file.Format()
if err != nil {
return err
}
return os.WriteFile(modFilePath, data, 0o644)
}
26 changes: 26 additions & 0 deletions modulegen/internal/module/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package module

import (
"path/filepath"
"strings"
"text/template"

internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template"
)

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

err = internal_template.Generate(t, exampleFilePath, name, example)
if err != nil {
return err
}
}
return nil
}
15 changes: 6 additions & 9 deletions modulegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"

"github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs"
"github.com/testcontainers/testcontainers-go/modulegen/internal/tools"
)

Expand All @@ -19,7 +18,7 @@ var (
imageVar string
)

var templates = []string{"docs_example.md", "example_test.go", "example.go", "go.mod"}
var templates = []string{"docs_example.md"}

func init() {
flag.StringVar(&nameVar, "name", "", "Name of the example. Only alphabetical characters are allowed.")
Expand Down Expand Up @@ -50,18 +49,11 @@ func main() {

ctx := NewContext(filepath.Dir(currentDir))

mkdocsConfig, err := mkdocs.ReadConfig(ctx.MkdocsConfigFile())
if err != nil {
fmt.Printf(">> could not read MkDocs config: %v\n", err)
os.Exit(1)
}

example := Example{
Image: imageVar,
IsModule: asModuleVar,
Name: nameVar,
TitleName: nameTitleVar,
TCVersion: mkdocsConfig.Extra.LatestVersion,
}

err = generate(example, ctx)
Expand Down Expand Up @@ -159,6 +151,11 @@ func generate(example Example, ctx *Context) error {
return err
}

err = generateGoModule(ctx, example)
if err != nil {
return err
}

// update github ci workflow
err = generateWorkFlow(ctx)
if err != nil {
Expand Down
21 changes: 9 additions & 12 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,8 @@ func TestGenerateWrongExampleName(t *testing.T) {

for _, test := range tests {
example := Example{
Name: test.name,
Image: "docker.io/example/" + test.name + ":latest",
TCVersion: "v0.0.0-test",
Name: test.name,
Image: "docker.io/example/" + test.name + ":latest",
}

err = generate(example, tmpCtx)
Expand Down Expand Up @@ -230,7 +229,6 @@ func TestGenerateWrongExampleTitle(t *testing.T) {
Name: "foo",
TitleName: test.title,
Image: "docker.io/example/foo:latest",
TCVersion: "v0.0.0-test",
}

err = generate(example, tmpCtx)
Expand Down Expand Up @@ -268,7 +266,6 @@ func TestGenerate(t *testing.T) {
TitleName: "FooDB4TheWin",
IsModule: false,
Image: "docker.io/example/foodb:latest",
TCVersion: "v0.0.0-test",
}
exampleNameLower := example.Lower()

Expand All @@ -295,15 +292,15 @@ func TestGenerate(t *testing.T) {
assert.Nil(t, err) // error nil implies the file exist

// check the number of template files is equal to examples + 2 (the doc and the github workflow)
assert.Equal(t, len(newExampleDir)+2, len(templatesDir))
assert.Equal(t, len(newExampleDir)+1, len(templatesDir))

assertExampleDocContent(t, example, exampleDocFile)
assertExampleGithubWorkflowContent(t, example, mainWorkflowFile)

generatedTemplatesDir := filepath.Join(examplesTmp, exampleNameLower)
assertExampleTestContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+"_test.go"))
assertExampleContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+".go"))
assertGoModContent(t, example, filepath.Join(generatedTemplatesDir, "go.mod"))
assertGoModContent(t, example, originalConfig.Extra.LatestVersion, filepath.Join(generatedTemplatesDir, "go.mod"))
assertMakefileContent(t, example, filepath.Join(generatedTemplatesDir, "Makefile"))
assertMkdocsExamplesNav(t, example, originalConfig, tmpCtx)
assertDependabotExamplesUpdates(t, example, originalDependabotConfigUpdates, tmpCtx)
Expand Down Expand Up @@ -339,7 +336,6 @@ func TestGenerateModule(t *testing.T) {
TitleName: "FooDB",
IsModule: true,
Image: "docker.io/example/foodb:latest",
TCVersion: "v0.0.0-test",
}
exampleNameLower := example.Lower()

Expand All @@ -366,15 +362,15 @@ func TestGenerateModule(t *testing.T) {
assert.Nil(t, err) // error nil implies the file exist

// check the number of template files is equal to examples + 2 (the doc and the github workflow)
assert.Equal(t, len(newExampleDir)+2, len(templatesDir))
assert.Equal(t, len(newExampleDir)+1, len(templatesDir))

assertExampleDocContent(t, example, exampleDocFile)
assertExampleGithubWorkflowContent(t, example, mainWorkflowFile)

generatedTemplatesDir := filepath.Join(modulesTmp, exampleNameLower)
assertExampleTestContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+"_test.go"))
assertExampleContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+".go"))
assertGoModContent(t, example, filepath.Join(generatedTemplatesDir, "go.mod"))
assertGoModContent(t, example, originalConfig.Extra.LatestVersion, filepath.Join(generatedTemplatesDir, "go.mod"))
assertMakefileContent(t, example, filepath.Join(generatedTemplatesDir, "Makefile"))
assertMkdocsExamplesNav(t, example, originalConfig, tmpCtx)
assertDependabotExamplesUpdates(t, example, originalDependabotConfigUpdates, tmpCtx)
Expand Down Expand Up @@ -487,13 +483,14 @@ func assertExampleGithubWorkflowContent(t *testing.T, example Example, exampleWo
}

// assert content go.mod
func assertGoModContent(t *testing.T, example Example, goModFile string) {
func assertGoModContent(t *testing.T, example Example, tcVersion string, goModFile string) {
content, err := os.ReadFile(goModFile)
assert.Nil(t, err)

data := sanitiseContent(content)
assert.Equal(t, "module github.com/testcontainers/testcontainers-go/"+example.ParentDir()+"/"+example.Lower(), data[0])
assert.Equal(t, "\tjackfan.us.kg/testcontainers/testcontainers-go "+example.TCVersion, data[5])
assert.Equal(t, "require github.com/testcontainers/testcontainers-go "+tcVersion, data[4])
assert.Equal(t, "replace github.com/testcontainers/testcontainers-go => ../..", data[6])
}

// assert content Makefile
Expand Down
47 changes: 47 additions & 0 deletions modulegen/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"path/filepath"
"text/template"

"github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs"
"github.com/testcontainers/testcontainers-go/modulegen/internal/modfile"
"github.com/testcontainers/testcontainers-go/modulegen/internal/module"
)

func generateGoModule(ctx *Context, example Example) error {
exampleDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower())
err := generateGoFiles(exampleDir, example)
if err != nil {
return err
}
return generateGoModFile(exampleDir, example)
}

func generateGoFiles(exampleDir string, example Example) error {
funcMap := template.FuncMap{
"Entrypoint": func() string { return example.Entrypoint() },
"ContainerName": func() string { return example.ContainerName() },
"ParentDir": func() string { return example.ParentDir() },
"ToLower": func() string { return example.Lower() },
"Title": func() string { return example.Title() },
}
return module.GenerateFiles(exampleDir, example.Lower(), funcMap, example)
}

func generateGoModFile(exampleDir string, example Example) error {
rootCtx, err := getRootContext()
if err != nil {
return err
}
mkdocsConfig, err := mkdocs.ReadConfig(rootCtx.MkdocsConfigFile())
if err != nil {
fmt.Printf(">> could not read MkDocs config: %v\n", err)
return err
}
rootGoModFile := rootCtx.GoModFile()
directory := "/" + example.ParentDir() + "/" + example.Lower()
tcVersion := mkdocsConfig.Extra.LatestVersion
return modfile.GenerateModFile(exampleDir, rootGoModFile, directory, tcVersion)
}
1 change: 0 additions & 1 deletion scripts/bump-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function main() {
for modFile in $(find "${ROOT_DIR}" -name "go.mod" -not -path "${ROOT_DIR}/vendor/*" -not -path "${ROOT_DIR}/.git/*"); do
bumpModFile "${modFile}" "${escapedCurrentGoVersion}" "${escapedGoVersion}"
done
bumpModFile "${ROOT_DIR}/modulegen/_template/go.mod.tmpl" "${escapedCurrentGoVersion}" "${escapedGoVersion}"

# bump markdown files
for f in $(find "${ROOT_DIR}" -name "*.md"); do
Expand Down

0 comments on commit 987c6d0

Please sign in to comment.