-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy patharchitecture_test.go
More file actions
154 lines (137 loc) · 3.83 KB
/
Copy patharchitecture_test.go
File metadata and controls
154 lines (137 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//go:build architecture
package kubemq
import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
)
// TestNoProtoInPublicAPI verifies that no public API file in the root
// package imports gRPC or protobuf packages directly. This enforces
// the layer boundary: only internal/transport/ may import those packages.
func TestNoProtoInPublicAPI(t *testing.T) {
forbiddenPrefixes := []string{
"google.golang.org/grpc",
"google.golang.org/protobuf",
"github.com/kubemq-io/protobuf",
"github.com/gogo/protobuf",
"go.opencensus.io",
"github.com/go-resty/resty",
"github.com/gorilla/websocket",
"go.uber.org/atomic",
}
entries, err := os.ReadDir(".")
if err != nil {
t.Fatalf("failed to read directory: %v", err)
}
fset := token.NewFileSet()
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
if !strings.HasSuffix(name, ".go") {
continue
}
if strings.HasSuffix(name, "_test.go") {
continue
}
f, err := parser.ParseFile(fset, name, nil, parser.ImportsOnly)
if err != nil {
t.Errorf("failed to parse %s: %v", name, err)
continue
}
for _, imp := range f.Imports {
importPath := strings.Trim(imp.Path.Value, `"`)
for _, prefix := range forbiddenPrefixes {
if strings.HasPrefix(importPath, prefix) {
t.Errorf("file %s imports forbidden package %q (violates layer boundary)", name, importPath)
}
}
}
}
}
// TestInternalDirectoryExists verifies the internal package structure.
func TestInternalDirectoryExists(t *testing.T) {
dirs := []string{
"internal/transport",
"internal/middleware",
"internal/types",
"internal/testutil",
"internal/otel",
}
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
t.Errorf("expected directory %s to exist", dir)
}
}
}
// TestTransportInterfaceExists verifies that the Transport interface is defined.
func TestTransportInterfaceExists(t *testing.T) {
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "internal/transport/transport.go", nil, parser.ParseComments)
if err != nil {
t.Fatalf("failed to parse internal/transport/transport.go: %v", err)
}
found := false
ast.Inspect(f, func(n ast.Node) bool {
ts, ok := n.(*ast.TypeSpec)
if ok && ts.Name.Name == "Transport" {
if _, isIface := ts.Type.(*ast.InterfaceType); isIface {
found = true
}
}
return true
})
if !found {
t.Error("Transport interface not found in internal/transport/transport.go")
}
}
// TestNoPublicAPIImportsGRPC scans all .go files in the root directory
// (excluding test files and internal/) and verifies none import gRPC.
func TestNoPublicAPIImportsGRPC(t *testing.T) {
matches, err := filepath.Glob("*.go")
if err != nil {
t.Fatalf("failed to glob: %v", err)
}
fset := token.NewFileSet()
for _, name := range matches {
if strings.HasSuffix(name, "_test.go") {
continue
}
f, err := parser.ParseFile(fset, name, nil, parser.ImportsOnly)
if err != nil {
t.Errorf("failed to parse %s: %v", name, err)
continue
}
for _, imp := range f.Imports {
importPath := strings.Trim(imp.Path.Value, `"`)
if strings.HasPrefix(importPath, "google.golang.org/grpc") {
t.Errorf("%s: imports gRPC package %q — public API must not import gRPC", name, importPath)
}
}
}
}
// TestFileMaxLines checks that no .go file in the root package exceeds
// the 500-line hard limit.
func TestFileMaxLines(t *testing.T) {
const hardLimit = 500
matches, err := filepath.Glob("*.go")
if err != nil {
t.Fatalf("failed to glob: %v", err)
}
for _, name := range matches {
data, err := os.ReadFile(name)
if err != nil {
t.Errorf("failed to read %s: %v", name, err)
continue
}
lines := strings.Count(string(data), "\n") + 1
if lines > hardLimit {
t.Errorf("%s has %d lines, exceeding the %d-line hard limit", name, lines, hardLimit)
}
}
}