Skip to content

Commit 2b1775a

Browse files
committed
✅ RawFileChangerTest Allows Tests to Specify "No Changes" Case
The `LLMVerifyingCodemodTestMixin` and `CodemodTestMixin` types allow the test author to specify cases where the codemod should not act on a file. This change adds the same capability to the `RawFileChangerTest` fixture. Also, this enhances the test fixture to detect the case where the user has improperly set-up their test. /towards ISS-834
1 parent c42a042 commit 2b1775a

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

framework/codemodder-testutils/src/main/java/io/codemodder/testutils/RawFileCodemodTest.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ private void verifySingleCase(
9393
assertThat(result.getReferences(), is(not(empty())));
9494

9595
final var modifiedFile = Files.readString(tmpFilePath);
96-
assertThat(modifiedFile, equalTo(Files.readString(filePathAfter)));
96+
if (filePathAfter == null) {
97+
// lack of an after file indicates that the before file should be unchanged
98+
assertThat(modifiedFile, equalTo(Files.readString(filePathBefore)));
99+
} else {
100+
assertThat(modifiedFile, equalTo(Files.readString(filePathAfter)));
101+
}
97102
Files.deleteIfExists(tmpFilePath);
98103
}
99104

@@ -110,23 +115,31 @@ private void verifyCodemod(
110115
final Path testResourceDir)
111116
throws IOException {
112117
// find all the sarif files
113-
final var allSarifFiles =
114-
Files.list(testResourceDir)
115-
.filter(file -> file.getFileName().toString().endsWith(".sarif"))
116-
.collect(Collectors.toList());
118+
final List<Path> allSarifFiles;
119+
try (final var files = Files.list(testResourceDir)) {
120+
allSarifFiles =
121+
files.filter(file -> file.getFileName().toString().endsWith(".sarif")).toList();
122+
}
117123

118124
final Map<String, List<RuleSarif>> map =
119125
SarifParser.create().parseIntoMap(allSarifFiles, tmpDir);
120126

121127
// grab all the .before and .after files in the dir
122-
final var allBeforeFiles =
123-
Files.list(testResourceDir)
124-
.filter(file -> file.getFileName().toString().endsWith(".before"))
125-
.toList();
126-
final Map<String, Path> afterFilesMap =
127-
Files.list(testResourceDir)
128-
.filter(file -> file.getFileName().toString().endsWith(".after"))
129-
.collect(Collectors.toMap(f -> trimExtension(f), f -> f));
128+
final List<Path> allBeforeFiles;
129+
try (final var files = Files.list(testResourceDir)) {
130+
allBeforeFiles =
131+
files.filter(file -> file.getFileName().toString().endsWith(".before")).toList();
132+
}
133+
final Map<String, Path> afterFilesMap;
134+
try (final var files = Files.list(testResourceDir)) {
135+
afterFilesMap =
136+
files
137+
.filter(file -> file.getFileName().toString().endsWith(".after"))
138+
.collect(Collectors.toMap(RawFileCodemodTest::trimExtension, f -> f));
139+
}
140+
if (allBeforeFiles.isEmpty()) {
141+
throw new IllegalArgumentException("No .before files found in " + testResourceDir);
142+
}
130143

131144
for (var beforeFile : allBeforeFiles) {
132145
final var afterFile = afterFilesMap.get(trimExtension(beforeFile));

0 commit comments

Comments
 (0)