-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCanonicalSurfaceGuardTest.java
More file actions
232 lines (203 loc) · 9.9 KB
/
CanonicalSurfaceGuardTest.java
File metadata and controls
232 lines (203 loc) · 9.9 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.demcha.documentation;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
class CanonicalSurfaceGuardTest {
private static final Path PROJECT_ROOT = Path.of("").toAbsolutePath().normalize();
private static final List<String> FORBIDDEN_TOKENS = List.of(
"com.demcha.templates",
"com.demcha.compose.v2",
"TemplateBuilder",
"GraphCompose.pdf(",
"PdfComposer",
"MainPageCV",
"MainPageCvDTO",
"ModuleYml",
"ModuleSummary");
private static final Set<String> MAIN_CANONICAL_SOURCE_ALLOWLIST = Set.of();
private static final Set<String> DOCUMENTATION_ALLOWLIST = Set.of(
"src/test/java/com/demcha/documentation/CanonicalSurfaceGuardTest.java",
"src/test/java/com/demcha/documentation/DocumentationCoverageTest.java");
private static final Set<String> CANONICAL_DOCUMENT_TEST_ALLOWLIST = Set.of(
"src/test/java/com/demcha/compose/document/templates/architecture/CanonicalTemplateComposerPdfBoundaryTest.java");
private static final Set<String> CANONICAL_BENCHMARK_ALLOWLIST = Set.of();
// Add an entry here only when a public markdown document genuinely
// needs to name retired legacy surface (com.demcha.templates.*,
// com.demcha.compose.v2.*, GraphCompose.pdf(...), PdfComposer) —
// i.e. an audit / migration / parity log. Internal planning docs
// should live outside the public docs surface (see .gitignore →
// docs/private/).
private static final Set<String> PUBLIC_MARKDOWN_ALLOWLIST = Set.of(
// Lists every retired V1 CV / cover-letter class so callers
// can find the v2 replacement. Naming the legacy surface is
// the explicit purpose of a migration log.
"docs/migration-v1-5-to-v1-6.md");
private static final List<String> FORBIDDEN_PUBLIC_AUTHORING_IMPORTS = List.of(
"import com.demcha.compose.engine.");
@Test
void canonicalMainSourcesShouldAvoidLegacySurfaceOutsideTransitionMappers() throws IOException {
assertNoForbiddenReferences(
PROJECT_ROOT.resolve("src/main/java/com/demcha/compose/document"),
MAIN_CANONICAL_SOURCE_ALLOWLIST);
}
@Test
void runnableExamplesShouldStayOnCanonicalSurface() throws IOException {
assertNoForbiddenReferences(
PROJECT_ROOT.resolve("examples/src/main/java/com/demcha/examples"),
Set.of());
}
@Test
void documentationTestsShouldAvoidLegacySurfaceOutsideGuardFiles() throws IOException {
assertNoForbiddenReferences(
PROJECT_ROOT.resolve("src/test/java/com/demcha/documentation"),
DOCUMENTATION_ALLOWLIST);
}
@Test
void canonicalDocumentTestsShouldAvoidLegacySurfaceOutsideCompatibilityParity() throws IOException {
assertNoForbiddenReferences(
PROJECT_ROOT.resolve("src/test/java/com/demcha/compose/document"),
CANONICAL_DOCUMENT_TEST_ALLOWLIST);
}
@Test
void canonicalBenchmarkEntryPointsShouldAvoidLegacySurface() throws IOException {
assertNoForbiddenReferences(
PROJECT_ROOT.resolve("src/test/java/com/demcha/compose"),
path -> {
String fileName = path.getFileName().toString();
return fileName.endsWith("Benchmark.java")
|| fileName.endsWith("StressTest.java")
|| fileName.endsWith("EnduranceTest.java");
},
CANONICAL_BENCHMARK_ALLOWLIST);
}
@Test
void publicMarkdownDocsShouldAvoidLegacySurfaceOutsideHistoricalAuditNotes() throws IOException {
assertNoForbiddenMarkdownReferences(
List.of(
PROJECT_ROOT.resolve("README.md"),
PROJECT_ROOT.resolve("CONTRIBUTING.md"),
PROJECT_ROOT.resolve("examples/README.md"),
PROJECT_ROOT.resolve("docs")),
PUBLIC_MARKDOWN_ALLOWLIST);
}
@Test
void publicAuthoringDocsAndExamplesShouldNotImportEngineInternals() throws IOException {
assertNoForbiddenAuthoringImports(
List.of(
PROJECT_ROOT.resolve("README.md"),
PROJECT_ROOT.resolve("docs/getting-started.md"),
PROJECT_ROOT.resolve("docs/recipes.md"),
PROJECT_ROOT.resolve("docs/layout-snapshot-testing.md"),
PROJECT_ROOT.resolve("examples/src/main/java/com/demcha/examples"),
PROJECT_ROOT.resolve("src/test/java/com/demcha/documentation/DocumentationExamplesTest.java")));
}
@Test
void semanticAuthoringValuePackagesShouldNotImportEngineInternals() throws IOException {
assertNoForbiddenAuthoringImports(
List.of(
PROJECT_ROOT.resolve("src/main/java/com/demcha/compose/document/node"),
PROJECT_ROOT.resolve("src/main/java/com/demcha/compose/document/dsl"),
PROJECT_ROOT.resolve("src/main/java/com/demcha/compose/document/style"),
PROJECT_ROOT.resolve("src/main/java/com/demcha/compose/document/table"),
PROJECT_ROOT.resolve("src/main/java/com/demcha/compose/document/image")));
}
private void assertNoForbiddenReferences(Path root, Set<String> allowlist) throws IOException {
assertNoForbiddenReferences(root, path -> true, allowlist);
}
private void assertNoForbiddenReferences(Path root,
Predicate<Path> include,
Set<String> allowlist) throws IOException {
try (var paths = Files.walk(root)) {
List<String> violations = new TreeSet<>(paths
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".java"))
.filter(include)
.filter(path -> !allowlist.contains(relative(path)))
.filter(this::containsForbiddenToken)
.map(this::relative)
.collect(Collectors.toList()))
.stream()
.toList();
assertThat(violations)
.describedAs("Files under %s must stay on the canonical document surface", relative(root))
.isEmpty();
}
}
private void assertNoForbiddenMarkdownReferences(List<Path> roots, Set<String> allowlist) throws IOException {
Set<String> violations = new TreeSet<>();
for (Path root : roots) {
if (Files.isRegularFile(root)) {
if (!allowlist.contains(relative(root)) && containsForbiddenToken(root)) {
violations.add(relative(root));
}
continue;
}
try (var paths = Files.walk(root)) {
paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".md"))
// docs/private/ is gitignored — it holds local-only
// planning notes, audits, and roadmap drafts that
// are not part of the public docs surface and so
// are not subject to this guard.
.filter(path -> !relative(path).startsWith("docs/private/"))
.filter(path -> !allowlist.contains(relative(path)))
.filter(this::containsForbiddenToken)
.map(this::relative)
.forEach(violations::add);
}
}
assertThat(violations)
.describedAs("Public markdown docs must stay on the canonical document surface")
.isEmpty();
}
private boolean containsForbiddenToken(Path path) {
try {
String source = Files.readString(path);
return FORBIDDEN_TOKENS.stream().anyMatch(source::contains);
} catch (IOException e) {
throw new RuntimeException("Failed to inspect " + path, e);
}
}
private void assertNoForbiddenAuthoringImports(List<Path> roots) throws IOException {
Set<String> violations = new TreeSet<>();
for (Path root : roots) {
if (Files.notExists(root)) {
continue;
}
if (Files.isRegularFile(root)) {
if (containsForbiddenAuthoringImport(root)) {
violations.add(relative(root));
}
continue;
}
try (var paths = Files.walk(root)) {
paths.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".java") || path.toString().endsWith(".md"))
.filter(this::containsForbiddenAuthoringImport)
.map(this::relative)
.forEach(violations::add);
}
}
assertThat(violations)
.describedAs("Public authoring docs and runnable examples should not import engine internals")
.isEmpty();
}
private boolean containsForbiddenAuthoringImport(Path path) {
try {
String source = Files.readString(path);
return FORBIDDEN_PUBLIC_AUTHORING_IMPORTS.stream().anyMatch(source::contains);
} catch (IOException e) {
throw new RuntimeException("Failed to inspect " + path, e);
}
}
private String relative(Path path) {
return PROJECT_ROOT.relativize(path).toString().replace('\\', '/');
}
}