diff --git a/modulegen/context.go b/modulegen/context.go index 470cd5113c..5eb386ce2c 100644 --- a/modulegen/context.go +++ b/modulegen/context.go @@ -45,6 +45,25 @@ func (ctx *Context) getModulesByBaseDir(baseDir string) ([]string, error) { return dirs, nil } +func (ctx *Context) getMarkdownsFromDir(baseDir string) ([]string, error) { + dir := filepath.Join(ctx.DocsDir(), baseDir) + + allFiles, err := os.ReadDir(dir) + if err != nil { + return nil, err + } + + dirs := make([]string, 0) + + for _, f := range allFiles { + if !f.IsDir() && filepath.Ext(f.Name()) == ".md" { + dirs = append(dirs, f.Name()) + } + } + sort.Strings(dirs) + return dirs, nil +} + func (ctx *Context) GetExamples() ([]string, error) { return ctx.getModulesByBaseDir("examples") } @@ -53,6 +72,18 @@ func (ctx *Context) GetModules() ([]string, error) { return ctx.getModulesByBaseDir("modules") } +func (ctx *Context) GetExamplesDocs() ([]string, error) { + return ctx.getMarkdownsFromDir("examples") +} + +func (ctx *Context) GetModulesDocs() ([]string, error) { + return ctx.getMarkdownsFromDir("modules") +} + func (ctx *Context) MkdocsConfigFile() string { return filepath.Join(ctx.RootDir, "mkdocs.yml") } + +func NewContext(dir string) *Context { + return &Context{RootDir: dir} +} diff --git a/modulegen/context_test.go b/modulegen/context_test.go index 101a1a33cc..b0812a5308 100644 --- a/modulegen/context_test.go +++ b/modulegen/context_test.go @@ -1,4 +1,4 @@ -package main_test +package main import ( "os" @@ -9,14 +9,11 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - main "github.com/testcontainers/testcontainers-go/modulegen" "github.com/testcontainers/testcontainers-go/modulegen/internal/dependabot" ) func TestGetDependabotConfigFile(t *testing.T) { - tmp := t.TempDir() - - ctx := &main.Context{RootDir: filepath.Join(tmp, "testcontainers-go")} + ctx := NewContext(filepath.Join(t.TempDir(), "testcontainers-go")) githubDir := ctx.GithubDir() cfgFile := ctx.DependabotConfigFile() @@ -33,9 +30,7 @@ func TestGetDependabotConfigFile(t *testing.T) { } func TestExamplesHasDependabotEntry(t *testing.T) { - rootDir, err := getRootDir() - require.NoError(t, err) - ctx := &main.Context{RootDir: rootDir} + ctx := getRootContext(t) examples, err := ctx.GetExamples() require.NoError(t, err) dependabotUpdates, err := dependabot.GetUpdates(ctx.DependabotConfigFile()) @@ -69,9 +64,7 @@ func TestExamplesHasDependabotEntry(t *testing.T) { } func TestModulesHasDependabotEntry(t *testing.T) { - rootDir, err := getRootDir() - require.NoError(t, err) - ctx := &main.Context{RootDir: rootDir} + ctx := getRootContext(t) modules, err := ctx.GetModules() require.NoError(t, err) dependabotUpdates, err := dependabot.GetUpdates(ctx.DependabotConfigFile()) @@ -102,12 +95,3 @@ func TestModulesHasDependabotEntry(t *testing.T) { assert.True(t, found, "module %s is not present in the dependabot updates", module) } } - -func getRootDir() (string, error) { - current, err := os.Getwd() - if err != nil { - return "", err - } - - return filepath.Dir(current), nil -} diff --git a/modulegen/go.mod b/modulegen/go.mod index df75acc582..a8af7dd31d 100644 --- a/modulegen/go.mod +++ b/modulegen/go.mod @@ -4,6 +4,7 @@ go 1.20 require ( github.com/stretchr/testify v1.8.4 + golang.org/x/exp v0.0.0-20230811145659-89c5cff77bcb golang.org/x/text v0.12.0 gopkg.in/yaml.v3 v3.0.1 ) diff --git a/modulegen/go.sum b/modulegen/go.sum index 9981e21553..0e13755d92 100644 --- a/modulegen/go.sum +++ b/modulegen/go.sum @@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 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/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= diff --git a/modulegen/internal/mkdocs/main.go b/modulegen/internal/mkdocs/main.go new file mode 100644 index 0000000000..2c49480963 --- /dev/null +++ b/modulegen/internal/mkdocs/main.go @@ -0,0 +1,18 @@ +package mkdocs + +func UpdateConfig(configFile string, isModule bool, exampleMd string, indexMd string) error { + config, err := ReadConfig(configFile) + if err != nil { + return err + } + config.addExample(isModule, exampleMd, indexMd) + return writeConfig(configFile, config) +} + +func CopyConfig(configFile string, tmpFile string) error { + config, err := ReadConfig(configFile) + if err != nil { + return err + } + return writeConfig(tmpFile, config) +} diff --git a/modulegen/internal/mkdocs/reader.go b/modulegen/internal/mkdocs/reader.go new file mode 100644 index 0000000000..650d51baea --- /dev/null +++ b/modulegen/internal/mkdocs/reader.go @@ -0,0 +1,23 @@ +package mkdocs + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +func ReadConfig(configFile string) (*Config, error) { + file, err := os.ReadFile(configFile) + if err != nil { + return nil, err + } + + config := &Config{} + + err = yaml.Unmarshal(file, config) + if err != nil { + return nil, err + } + + return config, nil +} diff --git a/modulegen/mkdocs.go b/modulegen/internal/mkdocs/types.go similarity index 55% rename from modulegen/mkdocs.go rename to modulegen/internal/mkdocs/types.go index ed41f3dfa8..5e6529af79 100644 --- a/modulegen/mkdocs.go +++ b/modulegen/internal/mkdocs/types.go @@ -1,13 +1,13 @@ -package main +package mkdocs import ( - "os" - "path/filepath" + "sort" + "strings" - "gopkg.in/yaml.v3" + "golang.org/x/exp/slices" ) -type MkDocsConfig struct { +type Config struct { SiteName string `yaml:"site_name"` SiteURL string `yaml:"site_url"` Plugins []string `yaml:"plugins"` @@ -44,59 +44,36 @@ type MkDocsConfig struct { } `yaml:"extra"` } -func getMkdocsConfigFile(rootDir string) string { - return filepath.Join(rootDir, "mkdocs.yml") -} - -func getExamples() ([]os.DirEntry, error) { - return getModulesOrExamples(false) -} - -func getExamplesDocs() ([]os.DirEntry, error) { - parent, err := getRootDir() - if err != nil { - return nil, err - } - - dir := filepath.Join(parent, "docs", "examples") - - return os.ReadDir(dir) -} - -func getRootDir() (string, error) { - current, err := os.Getwd() - if err != nil { - return "", err +func (c *Config) addExample(isModule bool, exampleMd string, indexMd string) { + mkdocsExamplesNav := c.Nav[4].Examples + if isModule { + mkdocsExamplesNav = c.Nav[3].Modules } - return filepath.Dir(current), nil -} - -func readMkdocsConfig(rootDir string) (*MkDocsConfig, error) { - configFile := getMkdocsConfigFile(rootDir) + if !slices.Contains(mkdocsExamplesNav, exampleMd) { - file, err := os.ReadFile(configFile) - if err != nil { - return nil, err - } + // make sure the index.md is the first element in the list of examples in the nav + examplesNav := make([]string, len(mkdocsExamplesNav)-1) + j := 0 - config := &MkDocsConfig{} + for _, exampleNav := range mkdocsExamplesNav { + // filter out the index.md file + if !strings.HasSuffix(exampleNav, "index.md") { + examplesNav[j] = exampleNav + j++ + } + } - err = yaml.Unmarshal(file, config) - if err != nil { - return nil, err - } + examplesNav = append(examplesNav, exampleMd) + sort.Strings(examplesNav) - return config, nil -} + // prepend the index.md file + examplesNav = append([]string{indexMd}, examplesNav...) -func writeMkdocsConfig(rootDir string, config *MkDocsConfig) error { - data, err := yaml.Marshal(config) - if err != nil { - return err + if isModule { + c.Nav[3].Modules = examplesNav + } else { + c.Nav[4].Examples = examplesNav + } } - - file := getMkdocsConfigFile(rootDir) - - return os.WriteFile(file, data, 0o777) } diff --git a/modulegen/internal/mkdocs/writer.go b/modulegen/internal/mkdocs/writer.go new file mode 100644 index 0000000000..a625d2ab27 --- /dev/null +++ b/modulegen/internal/mkdocs/writer.go @@ -0,0 +1,15 @@ +package mkdocs + +import ( + "os" + + "gopkg.in/yaml.v3" +) + +func writeConfig(configFile string, config *Config) error { + data, err := yaml.Marshal(config) + if err != nil { + return err + } + return os.WriteFile(configFile, data, 0o777) +} diff --git a/modulegen/main.go b/modulegen/main.go index 2b7eb9ad03..b33c67356e 100644 --- a/modulegen/main.go +++ b/modulegen/main.go @@ -17,6 +17,7 @@ import ( "golang.org/x/text/language" "github.com/testcontainers/testcontainers-go/modulegen/internal/dependabot" + "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" ) var ( @@ -131,9 +132,9 @@ func main() { os.Exit(1) } - rootDir := filepath.Dir(currentDir) + ctx := NewContext(filepath.Dir(currentDir)) - mkdocsConfig, err := readMkdocsConfig(rootDir) + mkdocsConfig, err := mkdocs.ReadConfig(ctx.MkdocsConfigFile()) if err != nil { fmt.Printf(">> could not read MkDocs config: %v\n", err) os.Exit(1) @@ -147,13 +148,13 @@ func main() { TCVersion: mkdocsConfig.Extra.LatestVersion, } - err = generate(example, rootDir) + err = generate(example, ctx) if err != nil { fmt.Printf(">> error generating the example: %v\n", err) os.Exit(1) } - cmdDir := filepath.Join(rootDir, example.ParentDir(), example.Lower()) + cmdDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower()) err = runGoCommand(cmdDir, "mod", "tidy") if err != nil { fmt.Printf(">> error synchronizing the dependencies: %v\n", err) @@ -170,14 +171,13 @@ func main() { fmt.Println("Thanks!") } -func generate(example Example, rootDir string) error { +func generate(example Example, ctx *Context) error { if err := example.Validate(); err != nil { return err } - ctx := &Context{RootDir: rootDir} githubWorkflowsDir := ctx.GithubWorkflowsDir() - outputDir := filepath.Join(rootDir, example.ParentDir()) + outputDir := filepath.Join(ctx.RootDir, example.ParentDir()) docsOuputDir := filepath.Join(ctx.DocsDir(), example.ParentDir()) funcMap := template.FuncMap{ @@ -264,7 +264,7 @@ func generate(example Example, rootDir string) error { } // update examples in mkdocs - err = generateMkdocs(rootDir, example) + err = generateMkdocs(ctx, example) if err != nil { return err } @@ -284,43 +284,11 @@ func generateDependabotUpdates(ctx *Context, example Example) error { return dependabot.UpdateConfig(ctx.DependabotConfigFile(), directory, "gomod") } -func generateMkdocs(rootDir string, example Example) error { +func generateMkdocs(ctx *Context, example Example) error { // update examples in mkdocs - mkdocsConfig, err := readMkdocsConfig(rootDir) - if err != nil { - return err - } - - mkdocsExamplesNav := mkdocsConfig.Nav[4].Examples - if example.IsModule { - mkdocsExamplesNav = mkdocsConfig.Nav[3].Modules - } - - // make sure the index.md is the first element in the list of examples in the nav - examplesNav := make([]string, len(mkdocsExamplesNav)-1) - j := 0 - - for _, exampleNav := range mkdocsExamplesNav { - // filter out the index.md file - if !strings.HasSuffix(exampleNav, "index.md") { - examplesNav[j] = exampleNav - j++ - } - } - - examplesNav = append(examplesNav, example.ParentDir()+"/"+example.Lower()+".md") - sort.Strings(examplesNav) - - // prepend the index.md file - examplesNav = append([]string{example.ParentDir() + "/index.md"}, examplesNav...) - - if example.IsModule { - mkdocsConfig.Nav[3].Modules = examplesNav - } else { - mkdocsConfig.Nav[4].Examples = examplesNav - } - - return writeMkdocsConfig(rootDir, mkdocsConfig) + exampleMd := example.ParentDir() + "/" + example.Lower() + ".md" + indexMd := example.ParentDir() + "/index.md" + return mkdocs.UpdateConfig(ctx.MkdocsConfigFile(), example.IsModule, exampleMd, indexMd) } func getModulesOrExamples(t bool) ([]os.DirEntry, error) { @@ -375,3 +343,12 @@ func runGoCommand(cmdDir string, args ...string) error { cmd.Dir = cmdDir return cmd.Run() } + +func getRootDir() (string, error) { + current, err := os.Getwd() + if err != nil { + return "", err + } + + return filepath.Dir(current), nil +} diff --git a/modulegen/main_test.go b/modulegen/main_test.go index 617998fdb7..9e9c104d43 100644 --- a/modulegen/main_test.go +++ b/modulegen/main_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/testcontainers/testcontainers-go/modulegen/internal/dependabot" + "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" ) func TestExample(t *testing.T) { @@ -151,10 +152,10 @@ func TestExample_Validate(outer *testing.T) { } func TestGenerateWrongExampleName(t *testing.T) { - rootTmp := t.TempDir() - examplesTmp := filepath.Join(rootTmp, "examples") - examplesDocTmp := filepath.Join(rootTmp, "docs", "examples") - githubWorkflowsTmp := filepath.Join(rootTmp, ".github", "workflows") + tmpCtx := NewContext(t.TempDir()) + examplesTmp := filepath.Join(tmpCtx.RootDir, "examples") + examplesDocTmp := filepath.Join(tmpCtx.DocsDir(), "examples") + githubWorkflowsTmp := tmpCtx.GithubWorkflowsDir() err := os.MkdirAll(examplesTmp, 0o777) assert.Nil(t, err) @@ -163,7 +164,7 @@ func TestGenerateWrongExampleName(t *testing.T) { err = os.MkdirAll(githubWorkflowsTmp, 0o777) assert.Nil(t, err) - err = copyInitialMkdocsConfig(t, rootTmp) + err = copyInitialMkdocsConfig(t, tmpCtx) assert.Nil(t, err) tests := []struct { @@ -188,16 +189,16 @@ func TestGenerateWrongExampleName(t *testing.T) { TCVersion: "v0.0.0-test", } - err = generate(example, rootTmp) + err = generate(example, tmpCtx) assert.Error(t, err) } } func TestGenerateWrongExampleTitle(t *testing.T) { - rootTmp := t.TempDir() - examplesTmp := filepath.Join(rootTmp, "examples") - examplesDocTmp := filepath.Join(rootTmp, "docs", "examples") - githubWorkflowsTmp := filepath.Join(rootTmp, ".github", "workflows") + tmpCtx := NewContext(t.TempDir()) + examplesTmp := filepath.Join(tmpCtx.RootDir, "examples") + examplesDocTmp := filepath.Join(tmpCtx.DocsDir(), "examples") + githubWorkflowsTmp := tmpCtx.GithubWorkflowsDir() err := os.MkdirAll(examplesTmp, 0o777) assert.Nil(t, err) @@ -206,7 +207,7 @@ func TestGenerateWrongExampleTitle(t *testing.T) { err = os.MkdirAll(githubWorkflowsTmp, 0o777) assert.Nil(t, err) - err = copyInitialMkdocsConfig(t, rootTmp) + err = copyInitialMkdocsConfig(t, tmpCtx) assert.Nil(t, err) tests := []struct { @@ -232,16 +233,15 @@ func TestGenerateWrongExampleTitle(t *testing.T) { TCVersion: "v0.0.0-test", } - err = generate(example, rootTmp) + err = generate(example, tmpCtx) assert.Error(t, err) } } func TestGenerate(t *testing.T) { - rootTmp := t.TempDir() - tmpCtx := &Context{RootDir: rootTmp} - examplesTmp := filepath.Join(rootTmp, "examples") - examplesDocTmp := filepath.Join(rootTmp, "docs", "examples") + tmpCtx := NewContext(t.TempDir()) + examplesTmp := filepath.Join(tmpCtx.RootDir, "examples") + examplesDocTmp := filepath.Join(tmpCtx.DocsDir(), "examples") githubWorkflowsTmp := tmpCtx.GithubWorkflowsDir() err := os.MkdirAll(examplesTmp, 0o777) @@ -251,13 +251,13 @@ func TestGenerate(t *testing.T) { err = os.MkdirAll(githubWorkflowsTmp, 0o777) assert.Nil(t, err) - err = copyInitialMkdocsConfig(t, rootTmp) + err = copyInitialMkdocsConfig(t, tmpCtx) assert.Nil(t, err) - originalConfig, err := readMkdocsConfig(rootTmp) + originalConfig, err := mkdocs.ReadConfig(tmpCtx.MkdocsConfigFile()) assert.Nil(t, err) - err = copyInitialDependabotConfig(t, tmpCtx.DependabotConfigFile()) + err = copyInitialDependabotConfig(t, tmpCtx) assert.Nil(t, err) originalDependabotConfigUpdates, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) @@ -272,7 +272,7 @@ func TestGenerate(t *testing.T) { } exampleNameLower := example.Lower() - err = generate(example, rootTmp) + err = generate(example, tmpCtx) assert.Nil(t, err) templatesDir, err := os.ReadDir(filepath.Join(".", "_template")) @@ -305,15 +305,14 @@ func TestGenerate(t *testing.T) { assertExampleContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+".go")) assertGoModContent(t, example, filepath.Join(generatedTemplatesDir, "go.mod")) assertMakefileContent(t, example, filepath.Join(generatedTemplatesDir, "Makefile")) - assertMkdocsExamplesNav(t, example, originalConfig, rootTmp) - assertDependabotExamplesUpdates(t, example, originalDependabotConfigUpdates, tmpCtx.DependabotConfigFile()) + assertMkdocsExamplesNav(t, example, originalConfig, tmpCtx) + assertDependabotExamplesUpdates(t, example, originalDependabotConfigUpdates, tmpCtx) } func TestGenerateModule(t *testing.T) { - rootTmp := t.TempDir() - tmpCtx := &Context{RootDir: rootTmp} - modulesTmp := filepath.Join(rootTmp, "modules") - modulesDocTmp := filepath.Join(rootTmp, "docs", "modules") + tmpCtx := NewContext(t.TempDir()) + modulesTmp := filepath.Join(tmpCtx.RootDir, "modules") + modulesDocTmp := filepath.Join(tmpCtx.DocsDir(), "modules") githubWorkflowsTmp := tmpCtx.GithubWorkflowsDir() err := os.MkdirAll(modulesTmp, 0o777) @@ -323,13 +322,13 @@ func TestGenerateModule(t *testing.T) { err = os.MkdirAll(githubWorkflowsTmp, 0o777) assert.Nil(t, err) - err = copyInitialMkdocsConfig(t, rootTmp) + err = copyInitialMkdocsConfig(t, tmpCtx) assert.Nil(t, err) - originalConfig, err := readMkdocsConfig(rootTmp) + originalConfig, err := mkdocs.ReadConfig(tmpCtx.MkdocsConfigFile()) assert.Nil(t, err) - err = copyInitialDependabotConfig(t, tmpCtx.DependabotConfigFile()) + err = copyInitialDependabotConfig(t, tmpCtx) assert.Nil(t, err) originalDependabotConfigUpdates, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) @@ -344,7 +343,7 @@ func TestGenerateModule(t *testing.T) { } exampleNameLower := example.Lower() - err = generate(example, rootTmp) + err = generate(example, tmpCtx) assert.Nil(t, err) templatesDir, err := os.ReadDir(filepath.Join(".", "_template")) @@ -377,13 +376,13 @@ func TestGenerateModule(t *testing.T) { assertExampleContent(t, example, filepath.Join(generatedTemplatesDir, exampleNameLower+".go")) assertGoModContent(t, example, filepath.Join(generatedTemplatesDir, "go.mod")) assertMakefileContent(t, example, filepath.Join(generatedTemplatesDir, "Makefile")) - assertMkdocsExamplesNav(t, example, originalConfig, rootTmp) - assertDependabotExamplesUpdates(t, example, originalDependabotConfigUpdates, tmpCtx.DependabotConfigFile()) + assertMkdocsExamplesNav(t, example, originalConfig, tmpCtx) + assertDependabotExamplesUpdates(t, example, originalDependabotConfigUpdates, tmpCtx) } // assert content in the Examples nav from mkdocs.yml -func assertDependabotExamplesUpdates(t *testing.T, example Example, originalConfigUpdates dependabot.Updates, tmpFile string) { - examples, err := dependabot.GetUpdates(tmpFile) +func assertDependabotExamplesUpdates(t *testing.T, example Example, originalConfigUpdates dependabot.Updates, tmpCtx *Context) { + examples, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) assert.Nil(t, err) assert.Equal(t, len(originalConfigUpdates)+1, len(examples)) @@ -476,14 +475,15 @@ func assertExampleGithubWorkflowContent(t *testing.T, example Example, exampleWo assert.Nil(t, err) data := sanitiseContent(content) + ctx := getRootContext(t) - modulesList, err := getModulesOrExamplesAsString(true) + modulesList, err := ctx.GetModules() assert.Nil(t, err) - assert.Equal(t, " module: ["+modulesList+"]", data[88]) + assert.Equal(t, " module: ["+strings.Join(modulesList, ", ")+"]", data[88]) - examplesList, err := getModulesOrExamplesAsString(false) + examplesList, err := ctx.GetExamples() assert.Nil(t, err) - assert.Equal(t, " module: ["+examplesList+"]", data[104]) + assert.Equal(t, " module: ["+strings.Join(examplesList, ", ")+"]", data[104]) } // assert content go.mod @@ -506,8 +506,8 @@ func assertMakefileContent(t *testing.T, example Example, makefile string) { } // assert content in the Examples nav from mkdocs.yml -func assertMkdocsExamplesNav(t *testing.T, example Example, originalConfig *MkDocsConfig, rootDir string) { - config, err := readMkdocsConfig(rootDir) +func assertMkdocsExamplesNav(t *testing.T, example Example, originalConfig *mkdocs.Config, tmpCtx *Context) { + config, err := mkdocs.ReadConfig(tmpCtx.MkdocsConfigFile()) assert.Nil(t, err) parentDir := example.ParentDir() @@ -547,11 +547,8 @@ func sanitiseContent(bytes []byte) []string { return data } -func copyInitialDependabotConfig(t *testing.T, tmpFile string) error { - projectDir, err := getRootDir() - assert.Nil(t, err) - - ctx := &Context{RootDir: projectDir} +func copyInitialDependabotConfig(t *testing.T, tmpCtx *Context) error { + ctx := getRootContext(t) - return dependabot.CopyConfig(ctx.DependabotConfigFile(), tmpFile) + return dependabot.CopyConfig(ctx.DependabotConfigFile(), tmpCtx.DependabotConfigFile()) } diff --git a/modulegen/mkdocs_test.go b/modulegen/mkdocs_test.go index d996ecac7c..b8314a12a0 100644 --- a/modulegen/mkdocs_test.go +++ b/modulegen/mkdocs_test.go @@ -8,36 +8,34 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + + "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" ) func TestGetMkDocsConfigFile(t *testing.T) { - tmp := t.TempDir() - - rootDir := filepath.Join(tmp, "testcontainers-go") - cfgFile := filepath.Join(rootDir, "mkdocs.yml") - err := os.MkdirAll(rootDir, 0o777) + tmpCtx := NewContext(filepath.Join(t.TempDir(), "testcontainers-go")) + cfgFile := tmpCtx.MkdocsConfigFile() + err := os.MkdirAll(tmpCtx.RootDir, 0o777) require.NoError(t, err) err = os.WriteFile(cfgFile, []byte{}, 0o777) require.NoError(t, err) - file := getMkdocsConfigFile(rootDir) + file := tmpCtx.MkdocsConfigFile() require.NotNil(t, file) assert.True(t, strings.HasSuffix(file, filepath.Join("testcontainers-go", "mkdocs.yml"))) } func TestReadMkDocsConfig(t *testing.T) { - tmp := t.TempDir() - - rootDir := filepath.Join(tmp, "testcontainers-go") - err := os.MkdirAll(rootDir, 0o777) + tmpCtx := NewContext(filepath.Join(t.TempDir(), "testcontainers-go")) + err := os.MkdirAll(tmpCtx.RootDir, 0o777) require.NoError(t, err) - err = copyInitialMkdocsConfig(t, rootDir) + err = copyInitialMkdocsConfig(t, tmpCtx) require.NoError(t, err) - config, err := readMkdocsConfig(rootDir) + config, err := mkdocs.ReadConfig(tmpCtx.MkdocsConfigFile()) require.NoError(t, err) require.NotNil(t, config) @@ -58,9 +56,10 @@ func TestReadMkDocsConfig(t *testing.T) { } func TestExamples(t *testing.T) { - examples, err := getExamples() + ctx := getRootContext(t) + examples, err := ctx.GetExamples() require.NoError(t, err) - examplesDocs, err := getExamplesDocs() + examplesDocs, err := ctx.GetExamplesDocs() require.NoError(t, err) // we have to remove the index.md file from the examples docs @@ -70,23 +69,25 @@ func TestExamples(t *testing.T) { for _, example := range examples { found := false for _, exampleDoc := range examplesDocs { - markdownName := example.Name() + ".md" + markdownName := example + ".md" - if markdownName == exampleDoc.Name() { + if markdownName == exampleDoc { found = true continue } } - assert.True(t, found, "example %s is not present in the docs", example.Name()) + assert.True(t, found, "example %s is not present in the docs", example) } } -func copyInitialMkdocsConfig(t *testing.T, tmpDir string) error { - projectDir, err := getRootDir() - require.NoError(t, err) +func copyInitialMkdocsConfig(t *testing.T, tmpCtx *Context) error { + ctx := getRootContext(t) - initialConfig, err := readMkdocsConfig(projectDir) - require.NoError(t, err) + return mkdocs.CopyConfig(ctx.MkdocsConfigFile(), tmpCtx.MkdocsConfigFile()) +} - return writeMkdocsConfig(tmpDir, initialConfig) +func getRootContext(t *testing.T) *Context { + current, err := os.Getwd() + require.NoError(t, err) + return NewContext(filepath.Dir(current)) }