package cef import ( "go/parser" "go/token" "os" "path/filepath" "runtime" "strconv" "strings" "testing" ) func TestCEF2GTKImportStaysInInfrastructureCEF(t *testing.T) { root := repoRoot(t) if err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error { if err != nil { return err } if d.IsDir() { switch d.Name() { case ".git", ".worktrees", "vendor", "dist", "tmp": return filepath.SkipDir } return nil } if filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") { return nil } importsBridge, err := importsCEF2GTK(path) if err != nil { return err } if !importsBridge { return nil } rel, err := filepath.Rel(root, path) if err != nil { return err } if strings.HasPrefix(filepath.ToSlash(rel), "internal/infrastructure/cef/") { return nil } t.Fatalf("purego-cef2gtk imported outside cef infrastructure: %s", rel) return nil }); err != nil { t.Fatalf("scan imports: %v", err) } } func importsCEF2GTK(path string) (bool, error) { file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly) if err != nil { return false, err } for _, spec := range file.Imports { importPath, err := strconv.Unquote(spec.Path.Value) if err == nil && strings.HasPrefix(importPath, "github.com/bnema/purego-cef2gtk") { return true, nil } } return false, nil } func repoRoot(t *testing.T) string { t.Helper() _, filename, _, ok := runtime.Caller(0) if !ok { t.Fatal("resolve caller path") } dir := filepath.Dir(filename) for { if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil { return dir } parent := filepath.Dir(dir) if parent == dir { t.Fatalf("go.mod not found from %s", filename) } dir = parent } }