package configkit import ( "go/parser" "go/token" "io/fs" "path/filepath" "strconv" "strings" "testing" ) func TestDependencyImportBoundary(t *testing.T) { t.Parallel() err := filepath.WalkDir(".", func(path string, entry fs.DirEntry, walkErr error) error { if walkErr != nil { return walkErr } if entry.IsDir() { if path == ".git" { return filepath.SkipDir } return nil } if filepath.Ext(path) != ".go" { return nil } file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly) if err != nil { return err } for _, imported := range file.Imports { importPath, err := strconv.Unquote(imported.Path.Value) if err != nil { return err } if strings.HasPrefix(importPath, "go.yaml.in/yaml/") && !strings.HasPrefix(filepath.ToSlash(path), "internal/yaml/") { t.Errorf("%s imports private YAML dependency %s", path, importPath) } if strings.HasPrefix(importPath, "github.com/google/go-cmp/") && !strings.HasSuffix(path, "_test.go") { t.Errorf("%s imports test-only dependency %s", path, importPath) } production := !strings.HasSuffix(path, "_test.go") rootProduction := production && filepath.Dir(path) == "." internalProduction := production && strings.HasPrefix(filepath.ToSlash(path), "internal/") if rootProduction && strings.HasPrefix(importPath, "gitea.maximumdirect.net/eric/ConfigKit/internal/") && importPath != "gitea.maximumdirect.net/eric/ConfigKit/internal/engine" { t.Errorf("%s bypasses the engine boundary through %s", path, importPath) } if internalProduction && importPath == "gitea.maximumdirect.net/eric/ConfigKit" { t.Errorf("%s imports the public root package", path) } } return nil }) if err != nil { t.Fatal(err) } }