-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchitecture_test.go
More file actions
168 lines (145 loc) · 4.08 KB
/
architecture_test.go
File metadata and controls
168 lines (145 loc) · 4.08 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main_test
import (
"go/parser"
"go/token"
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
)
func TestPuzzlePackageStaysPure(t *testing.T) {
assertPackageDoesNotImport(t, "puzzle", []string{
"charm.land/bubbletea",
"charm.land/bubbles",
"charm.land/lipgloss",
"github.com/FelineStateMachine/puzzletea/theme",
"github.com/FelineStateMachine/puzzletea/export/pdf",
})
}
func TestStatsPackageAvoidsRenderingImports(t *testing.T) {
assertPackageDoesNotImport(t, "stats", []string{
"charm.land/bubbletea",
"charm.land/bubbles",
"charm.land/lipgloss",
"github.com/FelineStateMachine/puzzletea/theme",
"github.com/FelineStateMachine/puzzletea/ui",
})
}
func TestStorePackageDoesNotImportSchedulePackages(t *testing.T) {
assertPackageDoesNotImport(t, "store", []string{
"github.com/FelineStateMachine/puzzletea/daily",
"github.com/FelineStateMachine/puzzletea/weekly",
})
}
func TestGamePackageDoesNotImportPDFExport(t *testing.T) {
assertPackageDoesNotImport(t, "game", []string{
"github.com/FelineStateMachine/puzzletea/export/pdf",
})
}
func TestCatalogPackageDoesNotImportConcreteGames(t *testing.T) {
assertPackageDoesNotImport(t, "catalog", concreteGameImportPaths(t))
}
func TestBuiltinPrintDoesNotImportConcreteGames(t *testing.T) {
assertPackageDoesNotImport(t, "export/builtinprint", concreteGameImportPaths(t))
}
func TestSessionPackageDoesNotUseNameDerivedRunMetadata(t *testing.T) {
assertFilesDoNotContain(t, "session", []string{
"RunKindForName(",
"RunDateForName(",
"SeedTextForName(",
"WeeklyIdentityForName(",
})
}
func TestStoreCreateGameDoesNotUseNameDerivedRunMetadata(t *testing.T) {
assertFileDoesNotContain(t, filepath.Join("store", "store.go"), []string{
"RunKindForName(",
"RunDateForName(",
"SeedTextForName(",
"WeeklyIdentityForName(",
})
}
func assertPackageDoesNotImport(t *testing.T, dir string, forbidden []string) {
t.Helper()
files, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
t.Fatalf("glob %s: %v", dir, err)
}
fset := token.NewFileSet()
for _, path := range files {
if strings.HasSuffix(path, "_test.go") {
continue
}
file, err := parser.ParseFile(fset, path, nil, parser.ImportsOnly)
if err != nil {
t.Fatalf("parse %s: %v", path, err)
}
for _, imp := range file.Imports {
value := strings.Trim(imp.Path.Value, `"`)
for _, prefix := range forbidden {
if strings.HasPrefix(value, prefix) {
t.Fatalf("%s imports forbidden package %q", path, value)
}
}
}
}
}
func concreteGameImportPaths(t testing.TB) []string {
t.Helper()
pattern := filepath.Join(repoRoot(t), "games", "*", "gamemode.go")
matches, err := filepath.Glob(pattern)
if err != nil {
t.Fatalf("glob concrete game packages: %v", err)
}
importPaths := make([]string, 0, len(matches))
for _, match := range matches {
dir := filepath.Base(filepath.Dir(match))
importPaths = append(importPaths, "github.com/FelineStateMachine/puzzletea/games/"+dir)
}
slices.Sort(importPaths)
return importPaths
}
func repoRoot(t testing.TB) string {
t.Helper()
_, path, _, ok := runtime.Caller(0)
if !ok {
t.Fatal("runtime.Caller failed")
}
return filepath.Clean(filepath.Dir(path))
}
func assertFileDoesNotContain(t *testing.T, path string, forbidden []string) {
t.Helper()
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
content := string(data)
for _, pattern := range forbidden {
if strings.Contains(content, pattern) {
t.Fatalf("%s contains forbidden pattern %q", path, pattern)
}
}
}
func assertFilesDoNotContain(t *testing.T, dir string, forbidden []string) {
t.Helper()
files, err := filepath.Glob(filepath.Join(dir, "*.go"))
if err != nil {
t.Fatalf("glob %s: %v", dir, err)
}
for _, path := range files {
if strings.HasSuffix(path, "_test.go") {
continue
}
data, err := os.ReadFile(path)
if err != nil {
t.Fatalf("read %s: %v", path, err)
}
content := string(data)
for _, pattern := range forbidden {
if strings.Contains(content, pattern) {
t.Fatalf("%s contains forbidden pattern %q", path, pattern)
}
}
}
}