diff --git a/modulegen/cmd/modules/example.go b/modulegen/cmd/modules/example.go index ddf907493a..5204f5be7b 100644 --- a/modulegen/cmd/modules/example.go +++ b/modulegen/cmd/modules/example.go @@ -11,14 +11,14 @@ var newExampleCmd = &cobra.Command{ Short: "Create a new Example", Long: "Create a new Example", RunE: func(cmd *cobra.Command, args []string) error { - return internal.Generate(*exampleVar, false) + return internal.Generate(tcModuleVar, false) }, } func init() { - newExampleCmd.Flags().StringVarP(&exampleVar.Name, nameFlag, "n", "", "Name of the example. Only alphabetical characters are allowed.") - newExampleCmd.Flags().StringVarP(&exampleVar.NameTitle, titleFlag, "t", "", "(Optional) Title of the example name, used to override the name in the case of mixed casing (Mongodb -> MongoDB). Use camel-case when needed. Only alphabetical characters are allowed.") - newExampleCmd.Flags().StringVarP(&exampleVar.Image, imageFlag, "i", "", "Fully-qualified name of the Docker image to be used by the example") + newExampleCmd.Flags().StringVarP(&tcModuleVar.Name, nameFlag, "n", "", "Name of the example. Only alphabetical characters are allowed.") + newExampleCmd.Flags().StringVarP(&tcModuleVar.NameTitle, titleFlag, "t", "", "(Optional) Title of the example name, used to override the name in the case of mixed casing (Mongodb -> MongoDB). Use camel-case when needed. Only alphabetical characters are allowed.") + newExampleCmd.Flags().StringVarP(&tcModuleVar.Image, imageFlag, "i", "", "Fully-qualified name of the Docker image to be used by the example") _ = newExampleCmd.MarkFlagRequired(imageFlag) _ = newExampleCmd.MarkFlagRequired(nameFlag) diff --git a/modulegen/cmd/modules/main.go b/modulegen/cmd/modules/main.go index 941e31ed3b..2e05db4ff4 100644 --- a/modulegen/cmd/modules/main.go +++ b/modulegen/cmd/modules/main.go @@ -6,7 +6,7 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/context" ) -var exampleVar = &context.ExampleVar{} +var tcModuleVar = context.TestcontainersModuleVar{} var NewCmd = &cobra.Command{ Use: "new", diff --git a/modulegen/cmd/modules/module.go b/modulegen/cmd/modules/module.go index 7904a3ef67..6fd809360a 100644 --- a/modulegen/cmd/modules/module.go +++ b/modulegen/cmd/modules/module.go @@ -11,14 +11,14 @@ var newModuleCmd = &cobra.Command{ Short: "Create a new Module", Long: "Create a new Module", RunE: func(cmd *cobra.Command, args []string) error { - return internal.Generate(*exampleVar, true) + return internal.Generate(tcModuleVar, true) }, } func init() { - newModuleCmd.Flags().StringVarP(&exampleVar.Name, nameFlag, "n", "", "Name of the module. Only alphabetical characters are allowed.") - newModuleCmd.Flags().StringVarP(&exampleVar.NameTitle, titleFlag, "t", "", "(Optional) Title of the module name, used to override the name in the case of mixed casing (Mongodb -> MongoDB). Use camel-case when needed. Only alphabetical characters are allowed.") - newModuleCmd.Flags().StringVarP(&exampleVar.Image, imageFlag, "i", "", "Fully-qualified name of the Docker image to be used by the module") + newModuleCmd.Flags().StringVarP(&tcModuleVar.Name, nameFlag, "n", "", "Name of the module. Only alphabetical characters are allowed.") + newModuleCmd.Flags().StringVarP(&tcModuleVar.NameTitle, titleFlag, "t", "", "(Optional) Title of the module name, used to override the name in the case of mixed casing (Mongodb -> MongoDB). Use camel-case when needed. Only alphabetical characters are allowed.") + newModuleCmd.Flags().StringVarP(&tcModuleVar.Image, imageFlag, "i", "", "Fully-qualified name of the Docker image to be used by the module") _ = newModuleCmd.MarkFlagRequired(imageFlag) _ = newModuleCmd.MarkFlagRequired(nameFlag) diff --git a/modulegen/context_test.go b/modulegen/context_test.go index de6cf714dd..e9d03f19ce 100644 --- a/modulegen/context_test.go +++ b/modulegen/context_test.go @@ -97,7 +97,7 @@ func TestModulesHasDependabotEntry(t *testing.T) { } } -func getTestRootContext(t *testing.T) *context.Context { +func getTestRootContext(t *testing.T) context.Context { current, err := os.Getwd() require.NoError(t, err) return context.New(filepath.Dir(current)) diff --git a/modulegen/internal/context/cmd.go b/modulegen/internal/context/cmd.go index 4521178819..3dd085eeab 100644 --- a/modulegen/internal/context/cmd.go +++ b/modulegen/internal/context/cmd.go @@ -1,6 +1,6 @@ package context -type ExampleVar struct { +type TestcontainersModuleVar struct { Name string NameTitle string Image string diff --git a/modulegen/internal/context/context.go b/modulegen/internal/context/context.go index 7e917bc4eb..c4d697b93b 100644 --- a/modulegen/internal/context/context.go +++ b/modulegen/internal/context/context.go @@ -10,27 +10,27 @@ type Context struct { RootDir string } -func (ctx *Context) DependabotConfigFile() string { +func (ctx Context) DependabotConfigFile() string { return filepath.Join(ctx.GithubDir(), "dependabot.yml") } -func (ctx *Context) DocsDir() string { +func (ctx Context) DocsDir() string { return filepath.Join(ctx.RootDir, "docs") } -func (ctx *Context) GithubDir() string { +func (ctx Context) GithubDir() string { return filepath.Join(ctx.RootDir, ".github") } -func (ctx *Context) GithubWorkflowsDir() string { +func (ctx Context) GithubWorkflowsDir() string { return filepath.Join(ctx.GithubDir(), "workflows") } -func (ctx *Context) GoModFile() string { +func (ctx Context) GoModFile() string { return filepath.Join(ctx.RootDir, "go.mod") } -func (ctx *Context) getModulesByBaseDir(baseDir string) ([]string, error) { +func (ctx Context) getModulesByBaseDir(baseDir string) ([]string, error) { dir := filepath.Join(ctx.RootDir, baseDir) allFiles, err := os.ReadDir(dir) @@ -49,7 +49,7 @@ func (ctx *Context) getModulesByBaseDir(baseDir string) ([]string, error) { return dirs, nil } -func (ctx *Context) getMarkdownsFromDir(baseDir string) ([]string, error) { +func (ctx Context) getMarkdownsFromDir(baseDir string) ([]string, error) { dir := filepath.Join(ctx.DocsDir(), baseDir) allFiles, err := os.ReadDir(dir) @@ -68,38 +68,38 @@ func (ctx *Context) getMarkdownsFromDir(baseDir string) ([]string, error) { return dirs, nil } -func (ctx *Context) GetExamples() ([]string, error) { +func (ctx Context) GetExamples() ([]string, error) { return ctx.getModulesByBaseDir("examples") } -func (ctx *Context) GetModules() ([]string, error) { +func (ctx Context) GetModules() ([]string, error) { return ctx.getModulesByBaseDir("modules") } -func (ctx *Context) GetExamplesDocs() ([]string, error) { +func (ctx Context) GetExamplesDocs() ([]string, error) { return ctx.getMarkdownsFromDir("examples") } -func (ctx *Context) GetModulesDocs() ([]string, error) { +func (ctx Context) GetModulesDocs() ([]string, error) { return ctx.getMarkdownsFromDir("modules") } -func (ctx *Context) MkdocsConfigFile() string { +func (ctx Context) MkdocsConfigFile() string { return filepath.Join(ctx.RootDir, "mkdocs.yml") } -func (ctx *Context) VSCodeWorkspaceFile() string { +func (ctx Context) VSCodeWorkspaceFile() string { return filepath.Join(ctx.RootDir, ".vscode", ".testcontainers-go.code-workspace") } -func New(dir string) *Context { - return &Context{RootDir: dir} +func New(dir string) Context { + return Context{RootDir: dir} } -func GetRootContext() (*Context, error) { +func GetRootContext() (Context, error) { current, err := os.Getwd() if err != nil { - return nil, err + return Context{}, err } return New(filepath.Dir(current)), nil } diff --git a/modulegen/internal/context/types.go b/modulegen/internal/context/types.go index 0a73fd42dd..b2c4a50fb9 100644 --- a/modulegen/internal/context/types.go +++ b/modulegen/internal/context/types.go @@ -11,25 +11,25 @@ import ( "golang.org/x/text/language" ) -type Example struct { +type TestcontainersModule struct { Image string // fully qualified name of the Docker image - IsModule bool // if true, the example will be generated as a Go module + IsModule bool // if true, the module will be generated as a Go module, otherwise an example Name string - TitleName string // title of the name: e.g. "mongodb" -> "MongoDB" + TitleName string // title of the name: m.g. "mongodb" -> "MongoDB" TCVersion string // Testcontainers for Go version } // ContainerName returns the name of the container, which is the lower-cased title of the example // If the title is set, it will be used instead of the name -func (e *Example) ContainerName() string { - name := e.Lower() +func (m *TestcontainersModule) ContainerName() string { + name := m.Lower() - if e.IsModule { - name = e.Title() + if m.IsModule { + name = m.Title() } else { - if e.TitleName != "" { - r, n := utf8.DecodeRuneInString(e.TitleName) - name = string(unicode.ToLower(r)) + e.TitleName[n:] + if m.TitleName != "" { + r, n := utf8.DecodeRuneInString(m.TitleName) + name = string(unicode.ToLower(r)) + m.TitleName[n:] } } @@ -38,48 +38,48 @@ func (e *Example) ContainerName() string { // Entrypoint returns the name of the entrypoint function, which is the lower-cased title of the example // If the example is a module, the entrypoint will be "RunContainer" -func (e *Example) Entrypoint() string { - if e.IsModule { +func (m *TestcontainersModule) Entrypoint() string { + if m.IsModule { return "RunContainer" } return "runContainer" } -func (e *Example) Lower() string { - return strings.ToLower(e.Name) +func (m *TestcontainersModule) Lower() string { + return strings.ToLower(m.Name) } -func (e *Example) ParentDir() string { - if e.IsModule { +func (m *TestcontainersModule) ParentDir() string { + if m.IsModule { return "modules" } return "examples" } -func (e *Example) Title() string { - if e.TitleName != "" { - return e.TitleName +func (m *TestcontainersModule) Title() string { + if m.TitleName != "" { + return m.TitleName } - return cases.Title(language.Und, cases.NoLower).String(e.Lower()) + return cases.Title(language.Und, cases.NoLower).String(m.Lower()) } -func (e *Example) Type() string { - if e.IsModule { +func (m *TestcontainersModule) Type() string { + if m.IsModule { return "module" } return "example" } -func (e *Example) Validate() error { - if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(e.Name) { - return fmt.Errorf("invalid name: %s. Only alphanumerical characters are allowed (leading character must be a letter)", e.Name) +func (m *TestcontainersModule) Validate() error { + if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(m.Name) { + return fmt.Errorf("invalid name: %s. Only alphanumerical characters are allowed (leading character must be a letter)", m.Name) } - if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(e.TitleName) { - return fmt.Errorf("invalid title: %s. Only alphanumerical characters are allowed (leading character must be a letter)", e.TitleName) + if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(m.TitleName) { + return fmt.Errorf("invalid title: %s. Only alphanumerical characters are allowed (leading character must be a letter)", m.TitleName) } return nil diff --git a/modulegen/internal/dependabot/main.go b/modulegen/internal/dependabot/main.go index db93755205..1ec4082254 100644 --- a/modulegen/internal/dependabot/main.go +++ b/modulegen/internal/dependabot/main.go @@ -1,21 +1,35 @@ package dependabot -import ( - "github.com/testcontainers/testcontainers-go/modulegen/internal/context" -) - -// update examples in dependabot -func GenerateDependabotUpdates(ctx *context.Context, example context.Example) error { - directory := "/" + example.ParentDir() + "/" + example.Lower() - return UpdateConfig(ctx.DependabotConfigFile(), directory, "gomod") -} +import "github.com/testcontainers/testcontainers-go/modulegen/internal/context" + +type Generator struct{} + +// AddModule update dependabot with the new module +func (g Generator) AddModule(ctx context.Context, tcModule context.TestcontainersModule) error { + configFile := ctx.DependabotConfigFile() -func UpdateConfig(configFile string, directory string, packageEcosystem string) error { config, err := readConfig(configFile) if err != nil { return err } + + packageEcosystem := "gomod" + directory := "/" + tcModule.ParentDir() + "/" + tcModule.Lower() + config.addUpdate(newUpdate(directory, packageEcosystem)) + + return writeConfig(configFile, config) +} + +// Generate generates dependabot config file from source +func (g Generator) Generate(ctx context.Context) error { + configFile := ctx.DependabotConfigFile() + + config, err := readConfig(configFile) + if err != nil { + return err + } + return writeConfig(configFile, config) } diff --git a/modulegen/internal/main.go b/modulegen/internal/main.go index 6f92c6ef84..a86969c27f 100644 --- a/modulegen/internal/main.go +++ b/modulegen/internal/main.go @@ -14,25 +14,25 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/workflow" ) -func Generate(exampleVar context.ExampleVar, isModule bool) error { +func Generate(moduleVar context.TestcontainersModuleVar, isModule bool) error { ctx, err := context.GetRootContext() if err != nil { return fmt.Errorf(">> could not get the root dir: %w", err) } - example := context.Example{ - Image: exampleVar.Image, + tcModule := context.TestcontainersModule{ + Image: moduleVar.Image, IsModule: isModule, - Name: exampleVar.Name, - TitleName: exampleVar.NameTitle, + Name: moduleVar.Name, + TitleName: moduleVar.NameTitle, } - err = GenerateFiles(ctx, example) + err = GenerateFiles(ctx, tcModule) if err != nil { - return fmt.Errorf(">> error generating the example: %w", err) + return fmt.Errorf(">> error generating the module: %w", err) } - cmdDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower()) + cmdDir := filepath.Join(ctx.RootDir, tcModule.ParentDir(), tcModule.Lower()) err = tools.GoModTidy(cmdDir) if err != nil { return fmt.Errorf(">> error synchronizing the dependencies: %w", err) @@ -48,40 +48,46 @@ func Generate(exampleVar context.ExampleVar, isModule bool) error { return nil } -func GenerateFiles(ctx *context.Context, example context.Example) error { - if err := example.Validate(); err != nil { - return err - } - // creates Makefile for example - err := make.GenerateMakefile(ctx, example) - if err != nil { - return err - } +type ProjectGenerator interface { + Generate(context.Context) error +} +type FileGenerator interface { + AddModule(context.Context, context.TestcontainersModule) error +} - err = module.GenerateGoModule(ctx, example) - if err != nil { +func GenerateFiles(ctx context.Context, tcModule context.TestcontainersModule) error { + if err := tcModule.Validate(); err != nil { return err } - // update github ci workflow - err = workflow.GenerateWorkflow(ctx) - if err != nil { - return err + fileGenerators := []FileGenerator{ + make.Generator{}, // creates Makefile for module + module.Generator{}, // creates go.mod for module + mkdocs.Generator{}, // update examples in mkdocs + dependabot.Generator{}, // update examples in dependabot } - // update examples in mkdocs - err = mkdocs.GenerateMkdocs(ctx, example) - if err != nil { - return err + + for _, generator := range fileGenerators { + err := generator.AddModule(ctx, tcModule) + if err != nil { + return err + } } - // update examples in dependabot - err = dependabot.GenerateDependabotUpdates(ctx, example) - if err != nil { - return err + + // they are based on the content of the modules in the project workspace, + // not in the new module to be added, that's why they happen after the actual + // module generation + projectGenerators := []ProjectGenerator{ + workflow.Generator{}, // update github ci workflow + vscode.Generator{}, // update vscode workspace } - // generate vscode workspace - err = vscode.GenerateVSCodeWorkspace(ctx) - if err != nil { - return err + + for _, generator := range projectGenerators { + err := generator.Generate(ctx) + if err != nil { + return err + } } + return nil } diff --git a/modulegen/internal/make/main.go b/modulegen/internal/make/main.go index 9c38735af6..521c5be8f8 100644 --- a/modulegen/internal/make/main.go +++ b/modulegen/internal/make/main.go @@ -8,21 +8,36 @@ import ( internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template" ) -// creates Makefile for example -func GenerateMakefile(ctx *context.Context, example context.Example) error { - exampleDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower()) - exampleName := example.Lower() - return Generate(exampleDir, exampleName) +type Generator struct{} + +// AddModule update dependabot with the new module +func (g Generator) AddModule(ctx context.Context, tcModule context.TestcontainersModule) error { + moduleDir := filepath.Join(ctx.RootDir, tcModule.ParentDir(), tcModule.Lower()) + moduleName := tcModule.Lower() + + name := "Makefile.tmpl" + t, err := template.New(name).ParseFiles(filepath.Join("_template", name)) + if err != nil { + return err + } + + moduleFilePath := filepath.Join(moduleDir, "Makefile") + + return internal_template.GenerateFile(t, moduleFilePath, name, moduleName) } -func Generate(exampleDir string, exampleName string) error { +// creates Makefile for example +func GenerateMakefile(ctx context.Context, tcModule context.TestcontainersModule) error { + moduleDir := filepath.Join(ctx.RootDir, tcModule.ParentDir(), tcModule.Lower()) + moduleName := tcModule.Lower() + name := "Makefile.tmpl" t, err := template.New(name).ParseFiles(filepath.Join("_template", name)) if err != nil { return err } - exampleFilePath := filepath.Join(exampleDir, "Makefile") + moduleFilePath := filepath.Join(moduleDir, "Makefile") - return internal_template.GenerateFile(t, exampleFilePath, name, exampleName) + return internal_template.GenerateFile(t, moduleFilePath, name, moduleName) } diff --git a/modulegen/internal/mkdocs/main.go b/modulegen/internal/mkdocs/main.go index 0f8d541f73..92b7843d7e 100644 --- a/modulegen/internal/mkdocs/main.go +++ b/modulegen/internal/mkdocs/main.go @@ -7,31 +7,33 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/context" ) -// update examples in mkdocs -func GenerateMkdocs(ctx *context.Context, example context.Example) error { - exampleMdFile := filepath.Join(ctx.DocsDir(), example.ParentDir(), example.Lower()+".md") +type Generator struct{} + +// AddModule update modules in mkdocs +func (g Generator) AddModule(ctx context.Context, tcModule context.TestcontainersModule) error { + moduleMdFile := filepath.Join(ctx.DocsDir(), tcModule.ParentDir(), tcModule.Lower()+".md") 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() }, + "Entrypoint": func() string { return tcModule.Entrypoint() }, + "ContainerName": func() string { return tcModule.ContainerName() }, + "ParentDir": func() string { return tcModule.ParentDir() }, + "ToLower": func() string { return tcModule.Lower() }, + "Title": func() string { return tcModule.Title() }, } - err := GenerateMdFile(exampleMdFile, funcMap, example) + err := GenerateMdFile(moduleMdFile, funcMap, tcModule) if err != nil { return err } - exampleMd := example.ParentDir() + "/" + example.Lower() + ".md" - indexMd := example.ParentDir() + "/index.md" - return UpdateConfig(ctx.MkdocsConfigFile(), example.IsModule, exampleMd, indexMd) -} + moduleMd := tcModule.ParentDir() + "/" + tcModule.Lower() + ".md" + indexMd := tcModule.ParentDir() + "/index.md" + + configFile := ctx.MkdocsConfigFile() + isModule := tcModule.IsModule -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) + config.addModule(isModule, moduleMd, indexMd) return writeConfig(configFile, config) } diff --git a/modulegen/internal/mkdocs/types.go b/modulegen/internal/mkdocs/types.go index 5e6529af79..d6145c3fd6 100644 --- a/modulegen/internal/mkdocs/types.go +++ b/modulegen/internal/mkdocs/types.go @@ -44,36 +44,36 @@ type Config struct { } `yaml:"extra"` } -func (c *Config) addExample(isModule bool, exampleMd string, indexMd string) { - mkdocsExamplesNav := c.Nav[4].Examples +func (c *Config) addModule(isModule bool, moduleMd string, indexMd string) { + mkdocsNavItems := c.Nav[4].Examples if isModule { - mkdocsExamplesNav = c.Nav[3].Modules + mkdocsNavItems = c.Nav[3].Modules } - if !slices.Contains(mkdocsExamplesNav, exampleMd) { + if !slices.Contains(mkdocsNavItems, moduleMd) { // make sure the index.md is the first element in the list of examples in the nav - examplesNav := make([]string, len(mkdocsExamplesNav)-1) + navItems := make([]string, len(mkdocsNavItems)-1) j := 0 - for _, exampleNav := range mkdocsExamplesNav { + for _, navItem := range mkdocsNavItems { // filter out the index.md file - if !strings.HasSuffix(exampleNav, "index.md") { - examplesNav[j] = exampleNav + if !strings.HasSuffix(navItem, "index.md") { + navItems[j] = navItem j++ } } - examplesNav = append(examplesNav, exampleMd) - sort.Strings(examplesNav) + navItems = append(navItems, moduleMd) + sort.Strings(navItems) // prepend the index.md file - examplesNav = append([]string{indexMd}, examplesNav...) + navItems = append([]string{indexMd}, navItems...) if isModule { - c.Nav[3].Modules = examplesNav + c.Nav[3].Modules = navItems } else { - c.Nav[4].Examples = examplesNav + c.Nav[4].Examples = navItems } } } diff --git a/modulegen/internal/module/main.go b/modulegen/internal/module/main.go index 7b2a36ac6d..40398c0654 100644 --- a/modulegen/internal/module/main.go +++ b/modulegen/internal/module/main.go @@ -12,27 +12,30 @@ import ( internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template" ) -func GenerateGoModule(ctx *context.Context, example context.Example) error { - exampleDir := filepath.Join(ctx.RootDir, example.ParentDir(), example.Lower()) - err := generateGoFiles(exampleDir, example) +type Generator struct{} + +// AddModule creates the go.mod file for the module +func (g Generator) AddModule(ctx context.Context, tcModule context.TestcontainersModule) error { + moduleDir := filepath.Join(ctx.RootDir, tcModule.ParentDir(), tcModule.Lower()) + err := generateGoFiles(moduleDir, tcModule) if err != nil { return err } - return generateGoModFile(exampleDir, example) + return generateGoModFile(moduleDir, tcModule) } -func generateGoFiles(exampleDir string, example context.Example) error { +func generateGoFiles(moduleDir string, tcModule context.TestcontainersModule) 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() }, + "Entrypoint": func() string { return tcModule.Entrypoint() }, + "ContainerName": func() string { return tcModule.ContainerName() }, + "ParentDir": func() string { return tcModule.ParentDir() }, + "ToLower": func() string { return tcModule.Lower() }, + "Title": func() string { return tcModule.Title() }, } - return GenerateFiles(exampleDir, example.Lower(), funcMap, example) + return GenerateFiles(moduleDir, tcModule.Lower(), funcMap, tcModule) } -func generateGoModFile(exampleDir string, example context.Example) error { +func generateGoModFile(moduleDir string, tcModule context.TestcontainersModule) error { rootCtx, err := context.GetRootContext() if err != nil { return err @@ -43,21 +46,21 @@ func generateGoModFile(exampleDir string, example context.Example) error { return err } rootGoModFile := rootCtx.GoModFile() - directory := "/" + example.ParentDir() + "/" + example.Lower() + directory := "/" + tcModule.ParentDir() + "/" + tcModule.Lower() tcVersion := mkdocsConfig.Extra.LatestVersion - return modfile.GenerateModFile(exampleDir, rootGoModFile, directory, tcVersion) + return modfile.GenerateModFile(moduleDir, rootGoModFile, directory, tcVersion) } -func GenerateFiles(exampleDir string, exampleName string, funcMap template.FuncMap, example any) error { +func GenerateFiles(moduleDir string, moduleName string, funcMap template.FuncMap, tcModule 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)) + moduleFilePath := filepath.Join(moduleDir, strings.ReplaceAll(tmpl, "example", moduleName)) - err = internal_template.GenerateFile(t, exampleFilePath, name, example) + err = internal_template.GenerateFile(t, moduleFilePath, name, tcModule) if err != nil { return err } diff --git a/modulegen/internal/vscode/main.go b/modulegen/internal/vscode/main.go index fa44529fbc..5128fb8a07 100644 --- a/modulegen/internal/vscode/main.go +++ b/modulegen/internal/vscode/main.go @@ -4,8 +4,10 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/context" ) -// print out the workspace for vscode -func GenerateVSCodeWorkspace(ctx *context.Context) error { +type Generator struct{} + +// Generate updates the workspace for vscode +func (g Generator) Generate(ctx context.Context) error { rootCtx, err := context.GetRootContext() if err != nil { return err @@ -19,11 +21,5 @@ func GenerateVSCodeWorkspace(ctx *context.Context) error { return err } - return Generate(ctx.VSCodeWorkspaceFile(), examples, modules) -} - -func Generate(exampleFilePath string, examples []string, modules []string) error { - config := newConfig(examples, modules) - - return writeConfig(exampleFilePath, config) + return writeConfig(ctx.VSCodeWorkspaceFile(), newConfig(examples, modules)) } diff --git a/modulegen/internal/workflow/main.go b/modulegen/internal/workflow/main.go index 15b48b0ecf..9cf75d259c 100644 --- a/modulegen/internal/workflow/main.go +++ b/modulegen/internal/workflow/main.go @@ -8,8 +8,10 @@ import ( internal_template "github.com/testcontainers/testcontainers-go/modulegen/internal/template" ) -// update github ci workflow -func GenerateWorkflow(ctx *context.Context) error { +type Generator struct{} + +// Generate updates github ci workflow +func (g Generator) Generate(ctx context.Context) error { rootCtx, err := context.GetRootContext() if err != nil { return err @@ -22,10 +24,9 @@ func GenerateWorkflow(ctx *context.Context) error { if err != nil { return err } - return Generate(ctx.GithubWorkflowsDir(), examples, modules) -} -func Generate(githubWorkflowsDir string, examples []string, modules []string) error { + githubWorkflowsDir := ctx.GithubWorkflowsDir() + projectDirectories := newProjectDirectories(examples, modules) name := "ci.yml.tmpl" t, err := template.New(name).ParseFiles(filepath.Join("_template", name)) diff --git a/modulegen/main_test.go b/modulegen/main_test.go index f8ab05a3c3..23516276b0 100644 --- a/modulegen/main_test.go +++ b/modulegen/main_test.go @@ -15,17 +15,17 @@ import ( "github.com/testcontainers/testcontainers-go/modulegen/internal/mkdocs" ) -func TestExample(t *testing.T) { +func TestModule(t *testing.T) { tests := []struct { name string - example context.Example + module context.TestcontainersModule expectedContainerName string expectedEntrypoint string expectedTitle string }{ { name: "Module with title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "mongoDB", IsModule: true, Image: "mongodb:latest", @@ -37,7 +37,7 @@ func TestExample(t *testing.T) { }, { name: "Module without title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "mongoDB", IsModule: true, Image: "mongodb:latest", @@ -48,7 +48,7 @@ func TestExample(t *testing.T) { }, { name: "Example with title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "mongoDB", IsModule: false, Image: "mongodb:latest", @@ -60,7 +60,7 @@ func TestExample(t *testing.T) { }, { name: "Example without title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "mongoDB", IsModule: false, Image: "mongodb:latest", @@ -73,48 +73,48 @@ func TestExample(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - example := test.example + module := test.module - assert.Equal(t, "mongodb", example.Lower()) - assert.Equal(t, test.expectedTitle, example.Title()) - assert.Equal(t, test.expectedContainerName, example.ContainerName()) - assert.Equal(t, test.expectedEntrypoint, example.Entrypoint()) + assert.Equal(t, "mongodb", module.Lower()) + assert.Equal(t, test.expectedTitle, module.Title()) + assert.Equal(t, test.expectedContainerName, module.ContainerName()) + assert.Equal(t, test.expectedEntrypoint, module.Entrypoint()) }) } } -func TestExample_Validate(outer *testing.T) { +func TestModule_Validate(outer *testing.T) { outer.Parallel() tests := []struct { name string - example context.Example + module context.TestcontainersModule expectedErr error }{ { name: "only alphabetical characters in name/title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "AmazingDB", TitleName: "AmazingDB", }, }, { name: "alphanumerical characters in name", - example: context.Example{ + module: context.TestcontainersModule{ Name: "AmazingDB4tw", TitleName: "AmazingDB", }, }, { name: "alphanumerical characters in title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "AmazingDB", TitleName: "AmazingDB4tw", }, }, { name: "non-alphanumerical characters in name", - example: context.Example{ + module: context.TestcontainersModule{ Name: "Amazing DB 4 The Win", TitleName: "AmazingDB", }, @@ -122,7 +122,7 @@ func TestExample_Validate(outer *testing.T) { }, { name: "non-alphanumerical characters in title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "AmazingDB", TitleName: "Amazing DB 4 The Win", }, @@ -130,7 +130,7 @@ func TestExample_Validate(outer *testing.T) { }, { name: "leading numerical character in name", - example: context.Example{ + module: context.TestcontainersModule{ Name: "1AmazingDB", TitleName: "AmazingDB", }, @@ -138,7 +138,7 @@ func TestExample_Validate(outer *testing.T) { }, { name: "leading numerical character in title", - example: context.Example{ + module: context.TestcontainersModule{ Name: "AmazingDB", TitleName: "1AmazingDB", }, @@ -148,12 +148,12 @@ func TestExample_Validate(outer *testing.T) { for _, test := range tests { outer.Run(test.name, func(t *testing.T) { - assert.Equal(t, test.expectedErr, test.example.Validate()) + assert.Equal(t, test.expectedErr, test.module.Validate()) }) } } -func TestGenerateWrongExampleName(t *testing.T) { +func TestGenerateWrongModuleName(t *testing.T) { tmpCtx := context.New(t.TempDir()) examplesTmp := filepath.Join(tmpCtx.RootDir, "examples") examplesDocTmp := filepath.Join(tmpCtx.DocsDir(), "examples") @@ -185,17 +185,17 @@ func TestGenerateWrongExampleName(t *testing.T) { } for _, test := range tests { - example := context.Example{ + module := context.TestcontainersModule{ Name: test.name, Image: "docker.io/example/" + test.name + ":latest", } - err = internal.GenerateFiles(tmpCtx, example) + err = internal.GenerateFiles(tmpCtx, module) assert.Error(t, err) } } -func TestGenerateWrongExampleTitle(t *testing.T) { +func TestGenerateWrongModuleTitle(t *testing.T) { tmpCtx := context.New(t.TempDir()) examplesTmp := filepath.Join(tmpCtx.RootDir, "examples") examplesDocTmp := filepath.Join(tmpCtx.DocsDir(), "examples") @@ -227,13 +227,13 @@ func TestGenerateWrongExampleTitle(t *testing.T) { } for _, test := range tests { - example := context.Example{ + module := context.TestcontainersModule{ Name: "foo", TitleName: test.title, Image: "docker.io/example/foo:latest", } - err = internal.GenerateFiles(tmpCtx, example) + err = internal.GenerateFiles(tmpCtx, module) assert.Error(t, err) } } @@ -263,41 +263,41 @@ func TestGenerate(t *testing.T) { originalDependabotConfigUpdates, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) assert.Nil(t, err) - example := context.Example{ + module := context.TestcontainersModule{ Name: "foodb4tw", TitleName: "FooDB4TheWin", IsModule: false, Image: "docker.io/example/foodb:latest", } - exampleNameLower := example.Lower() + moduleNameLower := module.Lower() - err = internal.GenerateFiles(tmpCtx, example) + err = internal.GenerateFiles(tmpCtx, module) assert.Nil(t, err) - exampleDirPath := filepath.Join(examplesTmp, exampleNameLower) + moduleDirPath := filepath.Join(examplesTmp, moduleNameLower) - exampleDirFileInfo, err := os.Stat(exampleDirPath) + moduleDirFileInfo, err := os.Stat(moduleDirPath) assert.Nil(t, err) // error nil implies the file exist - assert.True(t, exampleDirFileInfo.IsDir()) + assert.True(t, moduleDirFileInfo.IsDir()) - exampleDocFile := filepath.Join(examplesDocTmp, exampleNameLower+".md") - _, err = os.Stat(exampleDocFile) + moduleDocFile := filepath.Join(examplesDocTmp, moduleNameLower+".md") + _, err = os.Stat(moduleDocFile) assert.Nil(t, err) // error nil implies the file exist mainWorkflowFile := filepath.Join(githubWorkflowsTmp, "ci.yml") _, err = os.Stat(mainWorkflowFile) assert.Nil(t, err) // error nil implies the file exist - assertExampleDocContent(t, example, exampleDocFile) - assertExampleGithubWorkflowContent(t, example, mainWorkflowFile) + assertModuleDocContent(t, module, moduleDocFile) + assertModuleGithubWorkflowContent(t, module, 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, 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) + generatedTemplatesDir := filepath.Join(examplesTmp, moduleNameLower) + assertModuleTestContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+"_test.go")) + assertModuleContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+".go")) + assertGoModContent(t, module, originalConfig.Extra.LatestVersion, filepath.Join(generatedTemplatesDir, "go.mod")) + assertMakefileContent(t, module, filepath.Join(generatedTemplatesDir, "Makefile")) + assertMkdocsNavItems(t, module, originalConfig, tmpCtx) + assertDependabotUpdates(t, module, originalDependabotConfigUpdates, tmpCtx) } func TestGenerateModule(t *testing.T) { @@ -325,54 +325,54 @@ func TestGenerateModule(t *testing.T) { originalDependabotConfigUpdates, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) assert.Nil(t, err) - example := context.Example{ + module := context.TestcontainersModule{ Name: "foodb", TitleName: "FooDB", IsModule: true, Image: "docker.io/example/foodb:latest", } - exampleNameLower := example.Lower() + moduleNameLower := module.Lower() - err = internal.GenerateFiles(tmpCtx, example) + err = internal.GenerateFiles(tmpCtx, module) assert.Nil(t, err) - exampleDirPath := filepath.Join(modulesTmp, exampleNameLower) + moduleDirPath := filepath.Join(modulesTmp, moduleNameLower) - exampleDirFileInfo, err := os.Stat(exampleDirPath) + moduleDirFileInfo, err := os.Stat(moduleDirPath) assert.Nil(t, err) // error nil implies the file exist - assert.True(t, exampleDirFileInfo.IsDir()) + assert.True(t, moduleDirFileInfo.IsDir()) - exampleDocFile := filepath.Join(modulesDocTmp, exampleNameLower+".md") - _, err = os.Stat(exampleDocFile) + moduleDocFile := filepath.Join(modulesDocTmp, moduleNameLower+".md") + _, err = os.Stat(moduleDocFile) assert.Nil(t, err) // error nil implies the file exist mainWorkflowFile := filepath.Join(githubWorkflowsTmp, "ci.yml") _, err = os.Stat(mainWorkflowFile) assert.Nil(t, err) // error nil implies the file exist - assertExampleDocContent(t, example, exampleDocFile) - assertExampleGithubWorkflowContent(t, example, mainWorkflowFile) + assertModuleDocContent(t, module, moduleDocFile) + assertModuleGithubWorkflowContent(t, module, 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, 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) + generatedTemplatesDir := filepath.Join(modulesTmp, moduleNameLower) + assertModuleTestContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+"_test.go")) + assertModuleContent(t, module, filepath.Join(generatedTemplatesDir, moduleNameLower+".go")) + assertGoModContent(t, module, originalConfig.Extra.LatestVersion, filepath.Join(generatedTemplatesDir, "go.mod")) + assertMakefileContent(t, module, filepath.Join(generatedTemplatesDir, "Makefile")) + assertMkdocsNavItems(t, module, originalConfig, tmpCtx) + assertDependabotUpdates(t, module, originalDependabotConfigUpdates, tmpCtx) } -// assert content in the Examples nav from mkdocs.yml -func assertDependabotExamplesUpdates(t *testing.T, example context.Example, originalConfigUpdates dependabot.Updates, tmpCtx *context.Context) { - examples, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) +// assert content in the Dependabot descriptor file +func assertDependabotUpdates(t *testing.T, module context.TestcontainersModule, originalConfigUpdates dependabot.Updates, tmpCtx context.Context) { + modules, err := dependabot.GetUpdates(tmpCtx.DependabotConfigFile()) assert.Nil(t, err) - assert.Equal(t, len(originalConfigUpdates)+1, len(examples)) + assert.Equal(t, len(originalConfigUpdates)+1, len(modules)) - // the example should be in the dependabot updates + // the module should be in the dependabot updates found := false - for _, ex := range examples { - directory := "/" + example.ParentDir() + "/" + example.Lower() + for _, ex := range modules { + directory := "/" + module.ParentDir() + "/" + module.Lower() if directory == ex.Directory { found = true } @@ -381,25 +381,25 @@ func assertDependabotExamplesUpdates(t *testing.T, example context.Example, orig assert.True(t, found) // first item is the github-actions module - assert.Equal(t, "/", examples[0].Directory, examples) - assert.Equal(t, "github-actions", examples[0].PackageEcosystem, "PackageEcosystem should be github-actions") + assert.Equal(t, "/", modules[0].Directory, modules) + assert.Equal(t, "github-actions", modules[0].PackageEcosystem, "PackageEcosystem should be github-actions") // second item is the core module - assert.Equal(t, "/", examples[1].Directory, examples) - assert.Equal(t, "gomod", examples[1].PackageEcosystem, "PackageEcosystem should be gomod") + assert.Equal(t, "/", modules[1].Directory, modules) + assert.Equal(t, "gomod", modules[1].PackageEcosystem, "PackageEcosystem should be gomod") // third item is the pip module - assert.Equal(t, "/", examples[2].Directory, examples) - assert.Equal(t, "pip", examples[2].PackageEcosystem, "PackageEcosystem should be pip") + assert.Equal(t, "/", modules[2].Directory, modules) + assert.Equal(t, "pip", modules[2].PackageEcosystem, "PackageEcosystem should be pip") } -// assert content example file in the docs -func assertExampleDocContent(t *testing.T, example context.Example, exampleDocFile string) { - content, err := os.ReadFile(exampleDocFile) +// assert content module file in the docs +func assertModuleDocContent(t *testing.T, module context.TestcontainersModule, moduleDocFile string) { + content, err := os.ReadFile(moduleDocFile) assert.Nil(t, err) - lower := example.Lower() - title := example.Title() + lower := module.Lower() + title := module.Title() data := sanitiseContent(content) assert.Equal(t, data[0], "# "+title) @@ -408,38 +408,38 @@ func assertExampleDocContent(t *testing.T, example context.Example, exampleDocFi assert.Equal(t, data[6], "The Testcontainers module for "+title+".") assert.Equal(t, data[8], "## Adding this module to your project dependencies") assert.Equal(t, data[10], "Please run the following command to add the "+title+" module to your Go dependencies:") - assert.Equal(t, data[13], "go get github.com/testcontainers/testcontainers-go/"+example.ParentDir()+"/"+lower) + assert.Equal(t, data[13], "go get github.com/testcontainers/testcontainers-go/"+module.ParentDir()+"/"+lower) assert.Equal(t, data[18], "") - assert.Equal(t, data[19], "[Creating a "+title+" container](../../"+example.ParentDir()+"/"+lower+"/"+lower+".go)") + assert.Equal(t, data[19], "[Creating a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/"+lower+".go)") assert.Equal(t, data[20], "") assert.Equal(t, data[22], "") - assert.Equal(t, data[23], "[Test for a "+title+" container](../../"+example.ParentDir()+"/"+lower+"/"+lower+"_test.go)") + assert.Equal(t, data[23], "[Test for a "+title+" container](../../"+module.ParentDir()+"/"+lower+"/"+lower+"_test.go)") assert.Equal(t, data[24], "") assert.Equal(t, data[28], "The "+title+" module exposes one entrypoint function to create the "+title+" container, and this function receives two parameters:") assert.True(t, strings.HasSuffix(data[31], "(*"+title+"Container, error)")) - assert.Equal(t, "for "+title+". E.g. `testcontainers.WithImage(\""+example.Image+"\")`.", data[44]) + assert.Equal(t, "for "+title+". E.g. `testcontainers.WithImage(\""+module.Image+"\")`.", data[44]) } -// assert content example test -func assertExampleTestContent(t *testing.T, example context.Example, exampleTestFile string) { +// assert content module test +func assertModuleTestContent(t *testing.T, module context.TestcontainersModule, exampleTestFile string) { content, err := os.ReadFile(exampleTestFile) assert.Nil(t, err) data := sanitiseContent(content) - assert.Equal(t, data[0], "package "+example.Lower()) - assert.Equal(t, data[7], "func Test"+example.Title()+"(t *testing.T) {") - assert.Equal(t, data[10], "\tcontainer, err := "+example.Entrypoint()+"(ctx)") + 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 content example -func assertExampleContent(t *testing.T, example context.Example, exampleFile string) { +// assert content module +func assertModuleContent(t *testing.T, module context.TestcontainersModule, exampleFile string) { content, err := os.ReadFile(exampleFile) assert.Nil(t, err) - lower := example.Lower() - containerName := example.ContainerName() - exampleName := example.Title() - entrypoint := example.Entrypoint() + lower := module.Lower() + containerName := module.ContainerName() + exampleName := module.Title() + entrypoint := module.Entrypoint() data := sanitiseContent(content) assert.Equal(t, data[0], "package "+lower) @@ -447,13 +447,13 @@ func assertExampleContent(t *testing.T, example context.Example, exampleFile str assert.Equal(t, data[9], "type "+containerName+" struct {") assert.Equal(t, data[13], "// "+entrypoint+" creates an instance of the "+exampleName+" container type") assert.Equal(t, data[14], "func "+entrypoint+"(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*"+containerName+", error) {") - assert.Equal(t, data[16], "\t\tImage: \""+example.Image+"\",") + assert.Equal(t, data[16], "\t\tImage: \""+module.Image+"\",") assert.Equal(t, data[33], "\treturn &"+containerName+"{Container: container}, nil") } -// assert content GitHub workflow for the example -func assertExampleGithubWorkflowContent(t *testing.T, example context.Example, exampleWorkflowFile string) { - content, err := os.ReadFile(exampleWorkflowFile) +// assert content GitHub workflow for the module +func assertModuleGithubWorkflowContent(t *testing.T, module context.TestcontainersModule, moduleWorkflowFile string) { + content, err := os.ReadFile(moduleWorkflowFile) assert.Nil(t, err) data := sanitiseContent(content) @@ -469,46 +469,46 @@ func assertExampleGithubWorkflowContent(t *testing.T, example context.Example, e } // assert content go.mod -func assertGoModContent(t *testing.T, example context.Example, tcVersion string, goModFile string) { +func assertGoModContent(t *testing.T, module context.TestcontainersModule, 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, "module github.com/testcontainers/testcontainers-go/"+module.ParentDir()+"/"+module.Lower(), data[0]) 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 -func assertMakefileContent(t *testing.T, example context.Example, makefile string) { +func assertMakefileContent(t *testing.T, module context.TestcontainersModule, makefile string) { content, err := os.ReadFile(makefile) assert.Nil(t, err) data := sanitiseContent(content) - assert.Equal(t, data[4], "\t$(MAKE) test-"+example.Lower()) + assert.Equal(t, data[4], "\t$(MAKE) test-"+module.Lower()) } -// assert content in the Examples nav from mkdocs.yml -func assertMkdocsExamplesNav(t *testing.T, example context.Example, originalConfig *mkdocs.Config, tmpCtx *context.Context) { +// assert content in the nav items from mkdocs.yml +func assertMkdocsNavItems(t *testing.T, module context.TestcontainersModule, originalConfig *mkdocs.Config, tmpCtx context.Context) { config, err := mkdocs.ReadConfig(tmpCtx.MkdocsConfigFile()) assert.Nil(t, err) - parentDir := example.ParentDir() + parentDir := module.ParentDir() - examples := config.Nav[4].Examples + navItems := config.Nav[4].Examples expectedEntries := originalConfig.Nav[4].Examples - if example.IsModule { - examples = config.Nav[3].Modules + if module.IsModule { + navItems = config.Nav[3].Modules expectedEntries = originalConfig.Nav[3].Modules } - assert.Equal(t, len(expectedEntries)+1, len(examples)) + assert.Equal(t, len(expectedEntries)+1, len(navItems)) - // the example should be in the nav + // the module should be in the nav found := false - for _, ex := range examples { - markdownExample := example.ParentDir() + "/" + example.Lower() + ".md" - if markdownExample == ex { + for _, ex := range navItems { + markdownModule := module.ParentDir() + "/" + module.Lower() + ".md" + if markdownModule == ex { found = true } } @@ -516,7 +516,7 @@ func assertMkdocsExamplesNav(t *testing.T, example context.Example, originalConf assert.True(t, found) // first item is the index - assert.Equal(t, parentDir+"/index.md", examples[0], examples) + assert.Equal(t, parentDir+"/index.md", navItems[0], navItems) } func sanitiseContent(bytes []byte) []string { @@ -530,7 +530,7 @@ func sanitiseContent(bytes []byte) []string { return data } -func copyInitialDependabotConfig(t *testing.T, tmpCtx *context.Context) error { +func copyInitialDependabotConfig(t *testing.T, tmpCtx context.Context) error { ctx := getTestRootContext(t) return dependabot.CopyConfig(ctx.DependabotConfigFile(), tmpCtx.DependabotConfigFile()) } diff --git a/modulegen/mkdocs_test.go b/modulegen/mkdocs_test.go index fb3946dae9..358a0ce96f 100644 --- a/modulegen/mkdocs_test.go +++ b/modulegen/mkdocs_test.go @@ -56,7 +56,7 @@ func TestReadMkDocsConfig(t *testing.T) { assert.Greater(t, len(nav[4].Examples), 0) } -func TestExamples(t *testing.T) { +func TestNavItems(t *testing.T) { ctx := getTestRootContext(t) examples, err := ctx.GetExamples() require.NoError(t, err) @@ -81,7 +81,7 @@ func TestExamples(t *testing.T) { } } -func copyInitialMkdocsConfig(t *testing.T, tmpCtx *context.Context) error { +func copyInitialMkdocsConfig(t *testing.T, tmpCtx context.Context) error { ctx := getTestRootContext(t) return mkdocs.CopyConfig(ctx.MkdocsConfigFile(), tmpCtx.MkdocsConfigFile()) }