Skip to content

Commit

Permalink
modulegen: create internal/dependabot (#1503)
Browse files Browse the repository at this point in the history
* modulegen: create internal/dependabot

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>

* Apply suggestions from code review

Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>

---------

Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
Co-authored-by: Manuel de la Peña <social.mdelapenya@gmail.com>
  • Loading branch information
mmorel-35 and mdelapenya committed Aug 21, 2023
1 parent 94b4563 commit 41c24e0
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 279 deletions.
58 changes: 58 additions & 0 deletions modulegen/context.go
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")
}
113 changes: 113 additions & 0 deletions modulegen/context_test.go
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
}
138 changes: 0 additions & 138 deletions modulegen/dependabot.go

This file was deleted.

Loading

0 comments on commit 41c24e0

Please sign in to comment.