diff --git a/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScan.java b/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScan.java index 8697083ed..204048bc6 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScan.java +++ b/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScan.java @@ -22,6 +22,9 @@ public final class SourceScan { public record CallSite(String bareName, String relativeFile, int line) { } + /** 本仓主源码的包基址:模块目录实际相对的是它,不是 src/main/java 本身。 */ + private static final String PACKAGE_BASE = "com/nanri/aiimage"; + private static final Pattern CALL_SITE = Pattern.compile("sourceText\\(\\s*\"([^\"]+)\"\\s*\\)"); /** {@code src/test/java} 的绝对根,与 {@link #mainSources()} 同一条定位分支。 */ @@ -134,7 +137,59 @@ public final class SourceScan { return java.util.Map.copyOf(grouped); } + /** + * 模块聚合入口(N6 定「正常返回」):列出 {@code src/main/java} 下该模块目录里所有 {@code .java}。 + * + *

为什么需要它:归属类护栏此前只能 {@code sourceText("XxxService.java")} 逐文件点,而一座上帝类拆成 + * {@code service/} + {@code service/support/} 之后,被禁写法只要搬进 support 文件,旧格子照样绿。 + * 目录口径才是这条护栏的成立前提,所以这里按目录求值,且递归任意深度(N7)。

+ * + *

本格只定「目录存在 ⇒ 返回不可变、按相对路径字典序的 {@code List}」。 + * 「目录不存在 ⇒ 抛」由 N10 定义、「0 支 .java ⇒ 抛」由 N11 定义(零文件绝不许读成零违规)。

+ */ + public static List moduleSources(String moduleRelDir) { + return moduleSources(mainSourcesRoot(), moduleRelDir); + } + + /** 可注入语料根的同义重载:合成树场景(中文目录、越界、空目录)必须在不动业务树的前提下被测。 */ + public static List moduleSources(Path javaRoot, String moduleRelDir) { + Path root = javaRoot.toAbsolutePath(); + if (!Files.isDirectory(root)) { + throw new IllegalStateException("源码根不存在: " + root + "(cwd=" + Path.of("").toAbsolutePath() + ")"); + } + String normalized = moduleRelDir == null ? "" : moduleRelDir.replace('\\', '/'); + while (normalized.startsWith("/")) { + normalized = normalized.substring(1); + } + while (normalized.endsWith("/")) { + normalized = normalized.substring(0, normalized.length() - 1); + } + Path module = root.resolve(normalized).normalize(); + if (!Files.isDirectory(module)) { + /* + * ⚠ plan N6 原文写「相对 {@code src/main/java} 解析目录」,但本仓的模块目录实际相对的是 + * 包基址:真实路径是 {@code src/main/java/com/nanri/aiimage/modules/appearancepatent}。 + * 只按 plan 字面解析会得到 0 支文件——而那正是 N11 要防的死法(读不到被当成没违规)。 + * 所以这里先按字面试、再按包基址试;两条都不存在时保留字面路径,交给 N10 的「目录不存在 ⇒ 抛」。 + */ + Path underBase = root.resolve(PACKAGE_BASE).resolve(normalized).normalize(); + if (Files.isDirectory(underBase)) { + module = underBase; + } + } + if (!normalized.isEmpty() && !module.startsWith(root)) { + throw new IllegalArgumentException("模块目录越界出 src/main/java: 请求「" + moduleRelDir + + "」解析为 " + module + "(根=" + root + ",cwd=" + Path.of("").toAbsolutePath() + ")" + + " —— 聚合面只许吃主源码树,越界就等于把整台机器读进来"); + } + if (!Files.isDirectory(module)) { + return List.of(); // N10 会把它改成抛;N6 只定正常返回 + } + return List.copyOf(collectJavaFiles(module)); + } + private static List collectJavaFiles(Path root) { + try (Stream walk = Files.walk(root)) { return walk.filter(Files::isRegularFile) .filter(p -> p.getFileName().toString().endsWith(".java")) diff --git a/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java b/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java index 9b4e7090c..4454ce7ec 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/testutil/SourceScanTest.java @@ -262,4 +262,96 @@ class SourceScanTest { .isInstanceOf(IllegalStateException.class) .hasMessageContaining("cwd="); } + + // ---------------------------------------------------------------- N6 moduleSources + + @Test + void moduleSources_appearancepatent返回28支含既有support件() { + List files = SourceScan.moduleSources("modules/appearancepatent"); + + assertThat(files) + .as("真实集成:对真仓目录求值。28 支=1 controller+1 client+1 service 主件+3 spi/cache+" + + "5 service/support+16 model(dto7/vo9),与 find 实测同号") + .hasSize(28); + assertThat(files.stream().map(p -> p.toString().replace('\\', '/')).toList()) + .as("support 目录里的既有件必须在面里——聚合入口存在的理由就是拆分后的新文件要自动被看到") + .anyMatch(rel -> rel.endsWith("service/support/AppearancePatentRowNormalizer.java")); + } + + @Test + void moduleSources_返回列表不可变() { + List files = SourceScan.moduleSources("modules/appearancepatent"); + + assertThatThrownBy(() -> files.add(Path.of("塞一支进去.java"))) + .as("调用方改坏面 ⇒ 同一轮里后一格读到的就不是真面了") + .isInstanceOf(UnsupportedOperationException.class); + } + + @Test + void moduleSources_相对路径与绝对路径两种写法都接受() { + List plain = SourceScan.moduleSources("modules/appearancepatent"); + + assertThat(SourceScan.moduleSources("/modules/appearancepatent")) + .as("前导斜杠写法与裸相对写法必须同面,否则同一个模块会有两种口径") + .isEqualTo(plain); + assertThat(SourceScan.moduleSources("modules/appearancepatent/")) + .as("尾斜杠同理") + .isEqualTo(plain); + assertThat(SourceScan.moduleSources("modules\\appearancepatent")) + .as("Windows 反斜杠写法") + .isEqualTo(plain); + } + + @Test + void moduleSources_空目录场景留给N11本task先返回空() throws IOException { + Path javaRoot = Files.createDirectories(tmp.resolve("N6空场景树/src/main/java")); + Files.createDirectories(javaRoot.resolve("modules/zzz_empty")); + + assertThat(SourceScan.moduleSources(javaRoot, "modules/zzz_empty")) + .as("N6 只定「正常返回」:目录存在且为空 ⇒ 先返回空表不抛。" + + "N11 会把这条改成抛(零文件绝不许读成零违规),届时更新本例") + .isEmpty(); + } + + @Test + void moduleSources_深层相对路径越界到src外时拒绝() { + assertThatThrownBy(() -> SourceScan.moduleSources("modules/../../../Windows")) + .as("`..` 逃逸出 src/main/java ⇒ 必须挡。挡不住就等于聚合面能把整台机器读进来") + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("越界"); + } + + @Test + void moduleSources_路径含中文时正确解析() throws IOException { + Path javaRoot = Files.createDirectories(tmp.resolve("N6中文树/src/main/java")); + Path module = Files.createDirectories(javaRoot.resolve("modules/模块中文")); + Files.writeString(module.resolve("中文件.java"), "class A {}", StandardCharsets.UTF_8); + + List chinese = SourceScan.moduleSources(javaRoot, "modules/模块中文"); + + assertThat(chinese) + .as("主源码树今天没有中文名件,但语料根与相对目录都得过 UTF-8 这条道:这里用合成树把它钉住") + .hasSize(1); + assertThat(chinese.get(0).getFileName().toString()).isEqualTo("中文件.java"); + } + + @Test + void moduleSources_返回类型是Path不是字符串() { + List files = SourceScan.moduleSources("modules/appearancepatent"); + + assertThat(files).allSatisfy(p -> assertThat(p).isInstanceOf(Path.class)); + assertThat(files).allSatisfy(p -> assertThat(p.isAbsolute()).isTrue()); + assertThat(files).allSatisfy(p -> assertThat(Files.isRegularFile(p)).isTrue()); + } + + @Test + void moduleSources_找不到源码根时抛并回显cwd() { + Path wrong = tmp.resolve("少爬了一层/src/main/java"); + + assertThatThrownBy(() -> SourceScan.moduleSources(wrong, "modules/appearancepatent")) + .as("根目录不存在必须抛并回显 cwd——静默返回空表会让整条聚合护栏恒绿") + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("cwd=") + .hasMessageContaining(Path.of("").toAbsolutePath().toString()); + } }