1package cef
2
3import (
4 "go/parser"
5 "go/token"
6 "os"
7 "path/filepath"
8 "runtime"
9 "strconv"
10 "strings"
11 "testing"
12)
13
14func TestCEF2GTKImportStaysInInfrastructureCEF(t *testing.T) {
15 root := repoRoot(t)
16 if err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
17 if err != nil {
18 return err
19 }
20 if d.IsDir() {
21 switch d.Name() {
22 case ".git", ".worktrees", "vendor", "dist", "tmp":
23 return filepath.SkipDir
24 }
25 return nil
26 }
27 if filepath.Ext(path) != ".go" || strings.HasSuffix(path, "_test.go") {
28 return nil
29 }
30 importsBridge, err := importsCEF2GTK(path)
31 if err != nil {
32 return err
33 }
34 if !importsBridge {
35 return nil
36 }
37 rel, err := filepath.Rel(root, path)
38 if err != nil {
39 return err
40 }
41 if strings.HasPrefix(filepath.ToSlash(rel), "internal/infrastructure/cef/") {
42 return nil
43 }
44 t.Fatalf("purego-cef2gtk imported outside cef infrastructure: %s", rel)
45 return nil
46 }); err != nil {
47 t.Fatalf("scan imports: %v", err)
48 }
49}
50
51func importsCEF2GTK(path string) (bool, error) {
52 file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly)
53 if err != nil {
54 return false, err
55 }
56 for _, spec := range file.Imports {
57 importPath, err := strconv.Unquote(spec.Path.Value)
58 if err == nil && strings.HasPrefix(importPath, "github.com/bnema/purego-cef2gtk") {
59 return true, nil
60 }
61 }
62 return false, nil
63}
64
65func repoRoot(t *testing.T) string {
66 t.Helper()
67 _, filename, _, ok := runtime.Caller(0)
68 if !ok {
69 t.Fatal("resolve caller path")
70 }
71 dir := filepath.Dir(filename)
72 for {
73 if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
74 return dir
75 }
76 parent := filepath.Dir(dir)
77 if parent == dir {
78 t.Fatalf("go.mod not found from %s", filename)
79 }
80 dir = parent
81 }
82}