|
| 1 | +package architecture_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/parser" |
| 5 | + "go/token" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// Rule defines an architectural constraint |
| 13 | +type Rule struct { |
| 14 | + Package string |
| 15 | + ShouldNot []string // Substrings of import paths that are forbidden |
| 16 | + Description string |
| 17 | +} |
| 18 | + |
| 19 | +func TestSeparationOfConcerns(t *testing.T) { |
| 20 | + modulePrefix := "github.com/Prescott-Data/nexus-framework/nexus-broker" |
| 21 | + |
| 22 | + rules := []Rule{ |
| 23 | + { |
| 24 | + Package: "internal/domain", |
| 25 | + ShouldNot: []string{ |
| 26 | + "net/http", |
| 27 | + "github.com/jmoiron/sqlx", |
| 28 | + "github.com/lib/pq", |
| 29 | + modulePrefix + "/pkg/handlers", |
| 30 | + modulePrefix + "/internal/service", |
| 31 | + modulePrefix + "/internal/repository", |
| 32 | + }, |
| 33 | + Description: "Domain models must be pure and ignorant of HTTP, database drivers, and other layers.", |
| 34 | + }, |
| 35 | + { |
| 36 | + Package: "internal/repository", |
| 37 | + ShouldNot: []string{ |
| 38 | + "net/http", |
| 39 | + modulePrefix + "/pkg/handlers", |
| 40 | + modulePrefix + "/internal/service", |
| 41 | + }, |
| 42 | + Description: "Repositories must not depend on HTTP handlers or business logic services.", |
| 43 | + }, |
| 44 | + { |
| 45 | + Package: "internal/service", |
| 46 | + ShouldNot: []string{ |
| 47 | + modulePrefix + "/pkg/handlers", |
| 48 | + "github.com/jmoiron/sqlx", |
| 49 | + }, |
| 50 | + Description: "Services must not depend on HTTP handlers or raw SQL drivers (use repositories instead).", |
| 51 | + }, |
| 52 | + { |
| 53 | + Package: "pkg/handlers", |
| 54 | + ShouldNot: []string{ |
| 55 | + modulePrefix + "/internal/repository", |
| 56 | + }, |
| 57 | + Description: "HTTP Handlers must not bypass the Service layer to talk directly to Repositories.", |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + basePath := ".." // We are inside internal, so .. is the broker root |
| 62 | + |
| 63 | + for _, rule := range rules { |
| 64 | + t.Run(rule.Package, func(t *testing.T) { |
| 65 | + targetDir := filepath.Join(basePath, rule.Package) |
| 66 | + |
| 67 | + // Walk the target directory |
| 68 | + err := filepath.Walk(targetDir, func(path string, info os.FileInfo, err error) error { |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + |
| 73 | + // Only analyze Go files, skip tests |
| 74 | + if info.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + fset := token.NewFileSet() |
| 79 | + node, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("failed to parse %s: %v", path, err) |
| 82 | + } |
| 83 | + |
| 84 | + for _, imp := range node.Imports { |
| 85 | + importPath := strings.Trim(imp.Path.Value, `"`) |
| 86 | + |
| 87 | + for _, forbidden := range rule.ShouldNot { |
| 88 | + if strings.Contains(importPath, forbidden) { |
| 89 | + t.Errorf("\nViolation in %s\nRule: %s\nForbidden Import Found: %s", |
| 90 | + path, rule.Description, importPath) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return nil |
| 95 | + }) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + // If the directory doesn't exist yet, we just skip (e.g. if we are running from a different root) |
| 99 | + if os.IsNotExist(err) { |
| 100 | + t.Logf("Directory %s does not exist, skipping.", targetDir) |
| 101 | + } else { |
| 102 | + t.Fatalf("error walking directory %s: %v", targetDir, err) |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
0 commit comments