-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modulegen: create internal/dependabot (#1503)
* modulegen: create internal/dependabot Signed-off-by: Matthieu MOREL <[email protected]> * Apply suggestions from code review Co-authored-by: Manuel de la Peña <[email protected]> --------- Signed-off-by: Matthieu MOREL <[email protected]> Co-authored-by: Manuel de la Peña <[email protected]>
- Loading branch information
1 parent
94b4563
commit 41c24e0
Showing
11 changed files
with
367 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"sort" | ||
) | ||
|
||
type Context struct { | ||
RootDir string | ||
} | ||
|
||
func (ctx *Context) DependabotConfigFile() string { | ||
return filepath.Join(ctx.GithubDir(), "dependabot.yml") | ||
} | ||
|
||
func (ctx *Context) DocsDir() string { | ||
return filepath.Join(ctx.RootDir, "docs") | ||
} | ||
|
||
func (ctx *Context) GithubDir() string { | ||
return filepath.Join(ctx.RootDir, ".github") | ||
} | ||
|
||
func (ctx *Context) GithubWorkflowsDir() string { | ||
return filepath.Join(ctx.GithubDir(), "workflows") | ||
} | ||
|
||
func (ctx *Context) getModulesByBaseDir(baseDir string) ([]string, error) { | ||
dir := filepath.Join(ctx.RootDir, baseDir) | ||
|
||
allFiles, err := os.ReadDir(dir) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dirs := make([]string, 0) | ||
|
||
for _, f := range allFiles { | ||
if f.IsDir() { | ||
dirs = append(dirs, f.Name()) | ||
} | ||
} | ||
sort.Strings(dirs) | ||
return dirs, nil | ||
} | ||
|
||
func (ctx *Context) GetExamples() ([]string, error) { | ||
return ctx.getModulesByBaseDir("examples") | ||
} | ||
|
||
func (ctx *Context) GetModules() ([]string, error) { | ||
return ctx.getModulesByBaseDir("modules") | ||
} | ||
|
||
func (ctx *Context) MkdocsConfigFile() string { | ||
return filepath.Join(ctx.RootDir, "mkdocs.yml") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main_test | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"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")} | ||
|
||
githubDir := ctx.GithubDir() | ||
cfgFile := ctx.DependabotConfigFile() | ||
err := os.MkdirAll(githubDir, 0o777) | ||
require.NoError(t, err) | ||
|
||
err = os.WriteFile(cfgFile, []byte{}, 0o777) | ||
require.NoError(t, err) | ||
|
||
file := ctx.DependabotConfigFile() | ||
require.NotNil(t, file) | ||
|
||
assert.True(t, strings.HasSuffix(file, filepath.Join("testcontainers-go", ".github", "dependabot.yml"))) | ||
} | ||
|
||
func TestExamplesHasDependabotEntry(t *testing.T) { | ||
rootDir, err := getRootDir() | ||
require.NoError(t, err) | ||
ctx := &main.Context{RootDir: rootDir} | ||
examples, err := ctx.GetExamples() | ||
require.NoError(t, err) | ||
dependabotUpdates, err := dependabot.GetUpdates(ctx.DependabotConfigFile()) | ||
require.NoError(t, err) | ||
|
||
exampleUpdates := []dependabot.Update{} | ||
// exclude the Go modules from the examples updates | ||
for _, update := range dependabotUpdates { | ||
if strings.HasPrefix(update.Directory, "/examples/") { | ||
exampleUpdates = append(exampleUpdates, update) | ||
} | ||
} | ||
|
||
assert.Equal(t, len(exampleUpdates), len(examples)) | ||
|
||
// all example modules exist in the dependabot updates | ||
for _, example := range examples { | ||
found := false | ||
for _, exampleUpdate := range exampleUpdates { | ||
dependabotDir := "/examples/" + example | ||
|
||
assert.Equal(t, exampleUpdate.Schedule.Interval, "monthly") | ||
|
||
if dependabotDir == exampleUpdate.Directory { | ||
found = true | ||
continue | ||
} | ||
} | ||
assert.True(t, found, "example %s is not present in the dependabot updates", example) | ||
} | ||
} | ||
|
||
func TestModulesHasDependabotEntry(t *testing.T) { | ||
rootDir, err := getRootDir() | ||
require.NoError(t, err) | ||
ctx := &main.Context{RootDir: rootDir} | ||
modules, err := ctx.GetModules() | ||
require.NoError(t, err) | ||
dependabotUpdates, err := dependabot.GetUpdates(ctx.DependabotConfigFile()) | ||
require.NoError(t, err) | ||
|
||
moduleUpdates := []dependabot.Update{} | ||
// exclude the Go modules from the examples updates | ||
for _, update := range dependabotUpdates { | ||
if strings.HasPrefix(update.Directory, "/modules/") { | ||
moduleUpdates = append(moduleUpdates, update) | ||
} | ||
} | ||
assert.Equal(t, len(moduleUpdates), len(modules)) | ||
|
||
// all module modules exist in the dependabot updates | ||
for _, module := range modules { | ||
found := false | ||
for _, moduleUpdate := range moduleUpdates { | ||
dependabotDir := "/modules/" + module | ||
|
||
assert.Equal(t, moduleUpdate.Schedule.Interval, "monthly") | ||
|
||
if dependabotDir == moduleUpdate.Directory { | ||
found = true | ||
continue | ||
} | ||
} | ||
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 | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.