Skip to content

Commit

Permalink
✅ RawFileChangerTest Allows Tests to Specify "No Changes" Case
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gilday committed Apr 16, 2024
1 parent c42a042 commit 2b1775a
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ private void verifySingleCase(
assertThat(result.getReferences(), is(not(empty())));

final var modifiedFile = Files.readString(tmpFilePath);
assertThat(modifiedFile, equalTo(Files.readString(filePathAfter)));
if (filePathAfter == null) {
// lack of an after file indicates that the before file should be unchanged
assertThat(modifiedFile, equalTo(Files.readString(filePathBefore)));
} else {
assertThat(modifiedFile, equalTo(Files.readString(filePathAfter)));
}
Files.deleteIfExists(tmpFilePath);
}

Expand All @@ -110,23 +115,31 @@ private void verifyCodemod(
final Path testResourceDir)
throws IOException {
// find all the sarif files
final var allSarifFiles =
Files.list(testResourceDir)
.filter(file -> file.getFileName().toString().endsWith(".sarif"))
.collect(Collectors.toList());
final List<Path> allSarifFiles;
try (final var files = Files.list(testResourceDir)) {
allSarifFiles =
files.filter(file -> file.getFileName().toString().endsWith(".sarif")).toList();
}

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

// grab all the .before and .after files in the dir
final var allBeforeFiles =
Files.list(testResourceDir)
.filter(file -> file.getFileName().toString().endsWith(".before"))
.toList();
final Map<String, Path> afterFilesMap =
Files.list(testResourceDir)
.filter(file -> file.getFileName().toString().endsWith(".after"))
.collect(Collectors.toMap(f -> trimExtension(f), f -> f));
final List<Path> allBeforeFiles;
try (final var files = Files.list(testResourceDir)) {
allBeforeFiles =
files.filter(file -> file.getFileName().toString().endsWith(".before")).toList();
}
final Map<String, Path> afterFilesMap;
try (final var files = Files.list(testResourceDir)) {
afterFilesMap =
files
.filter(file -> file.getFileName().toString().endsWith(".after"))
.collect(Collectors.toMap(RawFileCodemodTest::trimExtension, f -> f));
}
if (allBeforeFiles.isEmpty()) {
throw new IllegalArgumentException("No .before files found in " + testResourceDir);
}

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

0 comments on commit 2b1775a

Please sign in to comment.