Skip to content

Commit 7693903

Browse files
nahsracarlosu7
andauthored
Create shared utility for verifying fix locations (#358)
The different codemod test mixins need to start being brought together, and this is a small step towards that. --------- Co-authored-by: Carlos Uscanga <[email protected]>
1 parent 189133b commit 7693903

File tree

5 files changed

+76
-19
lines changed

5 files changed

+76
-19
lines changed

core-codemods/src/main/java/io/codemodder/codemods/MavenSecureURLCodemod.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@ private CodemodFileScanningResult processXml(final Path file, final List<Result>
106106
if (matchingResult.isPresent()) {
107107
String id =
108108
SarifFindingKeyUtil.buildFindingId(matchingResult.get(), file, line);
109-
return CodemodChange.from(line, new FixedFinding(id, detectorRule()));
109+
Integer sarifLine =
110+
matchingResult
111+
.get()
112+
.getLocations()
113+
.get(0)
114+
.getPhysicalLocation()
115+
.getRegion()
116+
.getStartLine();
117+
return CodemodChange.from(sarifLine, new FixedFinding(id, detectorRule()));
110118
}
111119
return CodemodChange.from(line);
112120
})

core-codemods/src/test/java/io/codemodder/codemods/MavenSecureURLCodemodTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
import io.codemodder.testutils.Metadata;
44
import io.codemodder.testutils.RawFileCodemodTest;
55

6+
/**
7+
* This test needs to be fixed once this bug is addressed:
8+
* https://github.com/pixee/codemodder-java/issues/359
9+
*/
610
@Metadata(
711
codemodType = MavenSecureURLCodemod.class,
812
testResourceDir = "maven-non-https-url",
13+
// expectingFixesAtLines = {22, 26, 30},
914
dependencies = {})
1015
final class MavenSecureURLCodemodTest implements RawFileCodemodTest {}

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,8 @@ private void verifyCodemod(
187187
assertThat(change.getDescription(), is(not(blankOrNullString())));
188188
}
189189

190-
List<CodeTFChange> changes =
191-
changeset.stream().map(CodeTFChangesetEntry::getChanges).flatMap(List::stream).toList();
192-
193-
if (codemod.getChanger() instanceof FixOnlyCodeChanger) {
194-
assertThat(changes.stream().anyMatch(c -> !c.getFixedFindings().isEmpty()), is(true));
195-
}
196-
197-
for (int expectedFixLine : expectedFixLines) {
198-
assertThat(changes.stream().anyMatch(c -> c.getLineNumber() == expectedFixLine), is(true));
199-
}
200-
201-
List<UnfixedFinding> unfixedFindings = result.getUnfixedFindings();
202-
for (int expectedFailedFixLine : expectingFailedFixesAtLines) {
203-
assertThat(
204-
unfixedFindings.stream().noneMatch(c -> c.getLine() == expectedFailedFixLine), is(true));
205-
}
190+
ExpectedFixes.verifyExpectedFixes(
191+
result, codemod.getChanger(), expectedFixLines, expectingFailedFixesAtLines);
206192

207193
// make sure that some of the basics are being reported
208194
assertThat(result.getSummary(), is(not(blankOrNullString())));
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.codemodder.testutils;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.is;
5+
6+
import io.codemodder.CodeChanger;
7+
import io.codemodder.FixOnlyCodeChanger;
8+
import io.codemodder.codetf.CodeTFChange;
9+
import io.codemodder.codetf.CodeTFChangesetEntry;
10+
import io.codemodder.codetf.CodeTFResult;
11+
import io.codemodder.codetf.UnfixedFinding;
12+
import java.util.List;
13+
14+
/** Utilities for verifying expected fixes. */
15+
final class ExpectedFixes {
16+
17+
private ExpectedFixes() {}
18+
19+
/** Verify the expected fix metadata. */
20+
static void verifyExpectedFixes(
21+
final CodeTFResult result,
22+
final CodeChanger changer,
23+
final int[] expectedFixLines,
24+
final int[] expectingFailedFixesAtLines) {
25+
List<CodeTFChange> changes =
26+
result.getChangeset().stream()
27+
.map(CodeTFChangesetEntry::getChanges)
28+
.flatMap(List::stream)
29+
.toList();
30+
31+
if (changer instanceof FixOnlyCodeChanger) {
32+
assertThat(changes.stream().anyMatch(c -> !c.getFixedFindings().isEmpty()), is(true));
33+
}
34+
35+
for (int expectedFixLine : expectedFixLines) {
36+
assertThat(changes.stream().anyMatch(c -> c.getLineNumber() == expectedFixLine), is(true));
37+
}
38+
39+
List<UnfixedFinding> unfixedFindings = result.getUnfixedFindings();
40+
for (int expectedFailedFixLine : expectingFailedFixesAtLines) {
41+
assertThat(
42+
unfixedFindings.stream().noneMatch(c -> c.getLine() == expectedFailedFixLine), is(true));
43+
}
44+
}
45+
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ private void verifySingleCase(
4141
final Metadata metadata,
4242
final Path filePathBefore,
4343
final Path filePathAfter,
44-
final Map<String, List<RuleSarif>> ruleSarifMap)
44+
final Map<String, List<RuleSarif>> ruleSarifMap,
45+
final int[] expectedFixLines,
46+
final int[] expectingFailedFixesAtLines)
4547
throws IOException {
4648

4749
String tmpFileName = trimExtension(filePathBefore);
@@ -101,6 +103,9 @@ private void verifySingleCase(
101103
assertThat(modifiedFile, equalTo(Files.readString(filePathAfter)));
102104
}
103105
Files.deleteIfExists(tmpFilePath);
106+
107+
ExpectedFixes.verifyExpectedFixes(
108+
result, pair.getChanger(), expectedFixLines, expectingFailedFixesAtLines);
104109
}
105110

106111
private static String trimExtension(final Path path) {
@@ -145,7 +150,15 @@ private void verifyCodemod(
145150
for (var beforeFile : allBeforeFiles) {
146151
final var afterFile = afterFilesMap.get(trimExtension(beforeFile));
147152
// run the codemod
148-
verifySingleCase(codemod, tmpDir, metadata, beforeFile, afterFile, map);
153+
verifySingleCase(
154+
codemod,
155+
tmpDir,
156+
metadata,
157+
beforeFile,
158+
afterFile,
159+
map,
160+
metadata.expectingFixesAtLines(),
161+
metadata.expectingFailedFixesAtLines());
149162
}
150163
}
151164
}

0 commit comments

Comments
 (0)