task-5: 同名面普查落地 8 例(RED 实录 8 格全红);实测 3 组同名与点名 10 裸名交集为 0、主源码 973 支/970 唯一名,N20 收紧的前提得证
This commit is contained in:
@@ -95,6 +95,57 @@ public final class SourceScan {
|
||||
return line;
|
||||
}
|
||||
|
||||
/** {@code src/main/java} 的绝对根。定位不到必须抛——理由与 {@link #mainSources()} 同一条。 */
|
||||
public static Path mainSourcesRoot() {
|
||||
Path root = moduleRoot().resolve("src/main/java");
|
||||
if (!Files.isDirectory(root)) {
|
||||
throw new IllegalStateException("源码根不存在: " + root + "(cwd=" + Path.of("").toAbsolutePath() + ")");
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/** 主源码面全部 {@code .java},按相对路径字典序(与 {@link #censusSourceTextCallSites} 同一条排序键)。 */
|
||||
public static List<Path> mainSourceFiles() {
|
||||
return collectJavaFiles(mainSourcesRoot());
|
||||
}
|
||||
|
||||
/** 同名面普查:{@code src/main/java} 下裸文件名出现 >1 次的那些。 */
|
||||
public static java.util.Map<String, List<String>> duplicateMainSourceNames() {
|
||||
return duplicateSourceNames(mainSourcesRoot());
|
||||
}
|
||||
|
||||
/**
|
||||
* 分组键是<b>裸文件名</b>——因为 {@link #sourceText(String)} 就是按裸名找文件的,
|
||||
* 两个包下放同名件,那个助手就会命中 >1(N20 要把它改成歧义即抛)。
|
||||
* 只算 {@code .java}:{@code Foo.md} 与 {@code Foo.java} 不同名,把文档也算进来会让聚合面多吃一支。
|
||||
*/
|
||||
public static java.util.Map<String, List<String>> duplicateSourceNames(Path treeRoot) {
|
||||
Path root = treeRoot.toAbsolutePath();
|
||||
if (!Files.isDirectory(root)) {
|
||||
throw new IllegalStateException("同名面普查的树根不是目录: " + root + "(cwd="
|
||||
+ Path.of("").toAbsolutePath() + ")");
|
||||
}
|
||||
java.util.Map<String, List<String>> grouped = new java.util.TreeMap<>();
|
||||
for (Path file : collectJavaFiles(root)) {
|
||||
grouped.computeIfAbsent(file.getFileName().toString(), k -> new java.util.ArrayList<>())
|
||||
.add(root.relativize(file).toString().replace('\\', '/'));
|
||||
}
|
||||
grouped.entrySet().removeIf(entry -> entry.getValue().size() < 2);
|
||||
return java.util.Map.copyOf(grouped);
|
||||
}
|
||||
|
||||
private static List<Path> collectJavaFiles(Path root) {
|
||||
try (Stream<Path> walk = Files.walk(root)) {
|
||||
return walk.filter(Files::isRegularFile)
|
||||
.filter(p -> p.getFileName().toString().endsWith(".java"))
|
||||
.sorted(java.util.Comparator.comparing(
|
||||
p -> root.relativize(p).toString().replace('\\', '/')))
|
||||
.toList();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException("遍历 " + root + " 失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/** 按文件名(不含目录)取 {@code src/main/java} 下那份源码的<b>全文字符串</b>。
|
||||
* ⚠️ 返回 String,与 InternalTokenSingleOutletTest 里那个返回 Path 的私有 {@code sourceOf} 不是一回事,
|
||||
* 别指望复用它的——那个是 private。 */
|
||||
|
||||
@@ -175,4 +175,91 @@ class SourceScanTest {
|
||||
.hasMessageContaining("cwd=")
|
||||
.hasMessageContaining(wrong.toAbsolutePath().toString());
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- N5 同名面普查
|
||||
|
||||
@Test
|
||||
void duplicateNames_实测三组且不多不少() {
|
||||
assertThat(SourceScan.duplicateMainSourceNames().keySet())
|
||||
.as("src/main/java 下同名 >1 的裸文件名——实测就这三组,多一组说明有新落点,少一组说明有人搬走了")
|
||||
.containsExactlyInAnyOrder(
|
||||
"BoundedImageCache.java",
|
||||
"GroupDeletionGuard.java",
|
||||
"SkipPriceAsinPageVo.java");
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_与点名单交集为空() {
|
||||
Set<String> duplicates = SourceScan.duplicateMainSourceNames().keySet();
|
||||
|
||||
assertThat(duplicates)
|
||||
.as("这是 N20 把 sourceText 收紧成「歧义即抛」的前提证明:今天被点名的 10 个裸名"
|
||||
+ "全部单命中,所以当场不会打红。若未来第 11 个点名撞上这三组之一,"
|
||||
+ "N20 会当场红——那是正确行为,不是回归")
|
||||
.doesNotContainAnyElementsOf(REGISTERED_BARE_NAMES);
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_全树java数不少于900() {
|
||||
assertThat(SourceScan.mainSourceFiles())
|
||||
.as("分母下限:扫到 0 支必须被读成尺子坏了,而不是「主源码空了」")
|
||||
.hasSizeGreaterThanOrEqualTo(900);
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_唯一名数小于总文件数() {
|
||||
List<Path> all = SourceScan.mainSourceFiles();
|
||||
long uniqueNames = all.stream()
|
||||
.map(p -> p.getFileName().toString())
|
||||
.distinct()
|
||||
.count();
|
||||
|
||||
assertThat(uniqueNames)
|
||||
.as("973 支文件 / 970 个唯一名 ⇒ 确有 3 组同名。相等就说明同名面被搬空了,N5 的台账要同步改")
|
||||
.isLessThan((long) all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_空目录返回空集合() throws IOException {
|
||||
Path empty = Files.createDirectories(tmp.resolve("空树"));
|
||||
|
||||
assertThat(SourceScan.duplicateSourceNames(empty))
|
||||
.as("目录存在且没有 .java ⇒ 空 Map(与 N4 的「语料真空」同一条口径)")
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_同名恰好三支时计数为3() throws IOException {
|
||||
for (String dir : new String[] {"a", "b/c", "d/e/f"}) {
|
||||
Path sub = Files.createDirectories(tmp.resolve("三支").resolve(dir));
|
||||
Files.writeString(sub.resolve("Trio.java"), "class Trio {}", StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
assertThat(SourceScan.duplicateSourceNames(tmp.resolve("三支")))
|
||||
.as("三支同名必须聚成一组、值为 3 条路径;分组写成「每对相邻比较」就会数出 3 组")
|
||||
.containsEntry("Trio.java", List.of(
|
||||
"a/Trio.java", "b/c/Trio.java", "d/e/f/Trio.java"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_忽略非java文件同名() throws IOException {
|
||||
Path tree = Files.createDirectories(tmp.resolve("混放树"));
|
||||
Files.createDirectories(tree.resolve("docs"));
|
||||
Files.writeString(tree.resolve("Foo.java"), "class Foo {}", StandardCharsets.UTF_8);
|
||||
Files.writeString(tree.resolve("docs").resolve("Foo.md"), "# Foo", StandardCharsets.UTF_8);
|
||||
|
||||
assertThat(SourceScan.duplicateSourceNames(tree))
|
||||
.as("`Foo.md` 与 `Foo.java` 不同名——只按裸名分组会把文档也算成同名,聚合面就多吃一支")
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateNames_路径不可读时抛不静默跳过() throws IOException {
|
||||
Path notADirectory = Files.writeString(tmp.resolve("我是文件不是目录"), "x", StandardCharsets.UTF_8);
|
||||
|
||||
assertThatThrownBy(() -> SourceScan.duplicateSourceNames(notADirectory))
|
||||
.as("传进来的是文件而非目录 ⇒ 必须抛。静默返回空 Map 会被读成「主源码没有同名件」")
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("cwd=");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user