Skip to content

Commit c294715

Browse files
Make detail view have relative paths to runDir
1 parent 065608a commit c294715

File tree

6 files changed

+47
-34
lines changed

6 files changed

+47
-34
lines changed

messages/results-viewer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
%d. %s
44

5-
# summary.table.results-relative-to
5+
# summary.shared.results-relative-to
66

77
Violation file paths relative to '%s'.
88

src/lib/viewers/ResultsViewer.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ abstract class AbstractResultsDisplayer implements ResultsViewer {
4343
export class ResultsDetailDisplayer extends AbstractResultsDisplayer {
4444
protected _view(results: RunResults): void {
4545
const violations = sortViolations(results.getViolations());
46-
47-
this.displayDetails(violations);
46+
const runDir: string = results.getRunDirectory();
47+
this.display.displayLog(getMessage(BundleName.ResultsViewer, 'summary.shared.results-relative-to', [runDir]) + "\n");
48+
this.displayDetails(violations, runDir);
4849
}
4950

50-
private displayDetails(violations: Violation[]): void {
51+
private displayDetails(violations: Violation[], runDir: string): void {
5152
const styledViolations: string[] = violations
52-
.map((violation, idx) => this.styleViolation(violation, idx));
53+
.map((violation, idx) => this.styleViolation(violation, idx, runDir));
5354
this.display.displayLog(styledViolations.join('\n\n'));
5455
}
5556

56-
private styleViolation(violation: Violation, idx: number): string {
57+
private styleViolation(violation: Violation, idx: number, runDir: string): string {
5758
const rule = violation.getRule();
5859
const sev = rule.getSeverityLevel();
5960

@@ -69,10 +70,10 @@ export class ResultsDetailDisplayer extends AbstractResultsDisplayer {
6970
}
7071
const keys: string[] = ['severity', 'engine', 'message'];
7172
if (violation.getCodeLocations().length == 1) {
72-
body['location'] = stringifyLocation(violation.getCodeLocations()[0], false);
73+
body['location'] = stringifyLocation(violation.getCodeLocations()[0], false, runDir);
7374
keys.push('location');
7475
} else if (violation.getCodeLocations().length > 1) {
75-
body['locations'] = stringifyLocations(violation.getCodeLocations(), violation.getPrimaryLocationIndex());
76+
body['locations'] = stringifyLocations(violation.getCodeLocations(), violation.getPrimaryLocationIndex(), runDir);
7677
keys.push('locations');
7778
}
7879
if (violation.getResourceUrls().length == 1) {
@@ -86,14 +87,17 @@ export class ResultsDetailDisplayer extends AbstractResultsDisplayer {
8687
}
8788
}
8889

89-
function stringifyLocations(codeLocations: CodeLocation[], primaryIndex: number): string[] {
90+
function stringifyLocations(codeLocations: CodeLocation[], primaryIndex: number, runDir: string): string[] {
9091
return codeLocations.map((loc, idx) =>
91-
stringifyLocation(loc, codeLocations.length > 1 && primaryIndex === idx));
92+
stringifyLocation(loc, codeLocations.length > 1 && primaryIndex === idx, runDir));
9293
}
9394

94-
function stringifyLocation(loc: CodeLocation, displayMain: boolean): string {
95+
function stringifyLocation(loc: CodeLocation, displayMain: boolean, runDir: string): string {
9596
const mainPortion: string = displayMain ? '(main) ' : '';
96-
const commentPortion: string = loc.getComment() ? ` "${loc.getComment()}"` : '';
97+
let filePortion: string | undefined = loc.getFile();
98+
if (filePortion && filePortion.startsWith(runDir)) {
99+
filePortion = filePortion.slice(runDir.length);
100+
}
97101
let rangePortion: string = '';
98102
if (loc.getStartLine()) {
99103
rangePortion += ` (${loc.getStartLine()}:${loc.getStartColumn() || 1}`;
@@ -102,7 +106,9 @@ function stringifyLocation(loc: CodeLocation, displayMain: boolean): string {
102106
}
103107
rangePortion += ')';
104108
}
105-
return `${mainPortion}${loc.getFile()}${rangePortion}${commentPortion}`;
109+
const commentPortion: string = loc.getComment() ? ` "${loc.getComment()}"` : '';
110+
111+
return `${mainPortion}${filePortion}${rangePortion}${commentPortion}`;
106112
}
107113

108114
type ResultRow = {
@@ -151,7 +157,7 @@ export class ResultsTableDisplayer extends AbstractResultsDisplayer {
151157
}
152158
});
153159

154-
this.display.displayLog(getMessage(BundleName.ResultsViewer, 'summary.table.results-relative-to', [parentFolder]));
160+
this.display.displayLog(getMessage(BundleName.ResultsViewer, 'summary.shared.results-relative-to', [parentFolder]));
155161
this.display.displayTable(resultRows, TABLE_COLUMNS);
156162
}
157163
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

2+
Violation file paths relative to '{{RUNDIR}}'.
3+
24
=== 1. stub1RuleA
35
severity: 4 (Low)
46
engine: stubEngine1
57
message: This is a message
6-
location: __PATH_TO_SOME_FILE__ (1:1)
8+
location: test{{PATHSEP}}sample-code{{PATHSEP}}someFile.cls (1:1)
79
resources:
810
https://example.com/stub1RuleA
911
https://violation_specific.url
@@ -12,7 +14,7 @@
1214
severity: 4 (Low)
1315
engine: stubEngine1
1416
message: This is a message
15-
location: __PATH_TO_SOME_FILE__ (1:1)
17+
location: test{{PATHSEP}}sample-code{{PATHSEP}}someFile.cls (1:1)
1618
resources:
1719
https://example.com/stub1RuleA
1820
https://violation_specific.url
@@ -21,7 +23,7 @@
2123
severity: 4 (Low)
2224
engine: stubEngine1
2325
message: This is a message
24-
location: __PATH_TO_SOME_FILE__ (1:1)
26+
location: test{{PATHSEP}}sample-code{{PATHSEP}}someFile.cls (1:1)
2527
resources:
2628
https://example.com/stub1RuleA
2729
https://violation_specific.url
@@ -30,7 +32,7 @@
3032
severity: 4 (Low)
3133
engine: stubEngine1
3234
message: This is a message
33-
location: __PATH_TO_SOME_FILE__ (1:1)
35+
location: test{{PATHSEP}}sample-code{{PATHSEP}}someFile.cls (1:1)
3436
resources:
3537
https://example.com/stub1RuleA
3638
https://violation_specific.url
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11

2+
Violation file paths relative to '{{RUNDIR}}'.
3+
24
=== 1. stub1RuleB
35
severity: 2 (High)
46
engine: stubEngine1
57
message: This is a message
6-
location: __PATH_TO_FILE_Z__ (20:1)
8+
location: test{{PATHSEP}}sample-code{{PATHSEP}}fileZ.cls (20:1)
79
resource: https://example.com/stub1RuleB
810

911
=== 2. stub1RuleA
1012
severity: 4 (Low)
1113
engine: stubEngine1
1214
message: This is a message
13-
location: __PATH_TO_FILE_A__ (1:1)
15+
location: test{{PATHSEP}}sample-code{{PATHSEP}}fileA.cls (1:1)
1416
resource: https://example.com/stub1RuleA
1517

1618
=== 3. stub1RuleA
1719
severity: 4 (Low)
1820
engine: stubEngine1
1921
message: This is a message
20-
location: __PATH_TO_FILE_A__ (20:1)
22+
location: test{{PATHSEP}}sample-code{{PATHSEP}}fileA.cls (20:1)
2123
resource: https://example.com/stub1RuleA
2224

2325
=== 4. stub1RuleA
2426
severity: 4 (Low)
2527
engine: stubEngine1
2628
message: This is a message
27-
location: __PATH_TO_FILE_Z__ (1:1)
29+
location: test{{PATHSEP}}sample-code{{PATHSEP}}fileZ.cls (1:1)
2830
resource: https://example.com/stub1RuleA
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

2+
Violation file paths relative to '{{RUNDIR}}'.
3+
24
=== 1. stub1RuleA
35
severity: 4 (Low)
46
engine: stubEngine1
57
message: This is a message
68
locations:
7-
__PATH_TO_FILE_A__ (20:1)
8-
(main) __PATH_TO_FILE_Z__ (2:1) "This is a comment at Location 2"
9-
__PATH_TO_FILE_A__ (1:1-3:1) "This is a comment at Location 3"
9+
test{{PATHSEP}}sample-code{{PATHSEP}}fileA.cls (20:1)
10+
(main) test{{PATHSEP}}sample-code{{PATHSEP}}fileZ.cls (2:1) "This is a comment at Location 2"
11+
test{{PATHSEP}}sample-code{{PATHSEP}}fileA.cls (1:1-3:1) "This is a comment at Location 3"
1012
resource: https://example.com/stub1RuleA

test/lib/viewers/ResultsViewer.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ describe('ResultsViewer implementations', () => {
9090
// Rip off all of ansis's styling, so we're just comparing plain text.
9191
const actualEventText = ansis.strip(actualDisplayEvents.map(e => e.data).join('\n'));
9292
const expectedViolationDetails = (await readComparisonFile('four-identical-violations-details.txt'))
93-
.replace(/__PATH_TO_SOME_FILE__/g, PATH_TO_SOME_FILE);
94-
expect(actualEventText).toContain(expectedViolationDetails);
93+
.replaceAll("{{PATHSEP}}", path.sep)
94+
.replace("{{RUNDIR}}", results.getRunDirectory());
95+
expect(actualEventText).toEqual(expectedViolationDetails);
9596
});
9697

9798
// The reasoning behind this sorting order is so that the Detail view can function as a "show me the N most
@@ -128,9 +129,9 @@ describe('ResultsViewer implementations', () => {
128129
// Rip off all of ansis's styling, so we're just comparing plain text.
129130
const actualEventText = ansis.strip(actualDisplayEvents.map(e => e.data).join('\n'));
130131
const expectedViolationDetails = (await readComparisonFile('four-unique-violations-details.txt'))
131-
.replace(/__PATH_TO_FILE_A__/g, PATH_TO_FILE_A)
132-
.replace(/__PATH_TO_FILE_Z__/g, PATH_TO_FILE_Z);
133-
expect(actualEventText).toContain(expectedViolationDetails);
132+
.replaceAll("{{PATHSEP}}", path.sep)
133+
.replace("{{RUNDIR}}", results.getRunDirectory());
134+
expect(actualEventText).toEqual(expectedViolationDetails);
134135
});
135136

136137
it('Multi-location violations are correctly displayed', async () => {
@@ -177,8 +178,8 @@ describe('ResultsViewer implementations', () => {
177178
// Rip off all of ansis's styling, so we're just comparing plain text.
178179
const actualEventText = ansis.strip(actualDisplayEvents.map(e => e.data).join('\n'));
179180
const expectedViolationDetails = (await readComparisonFile('one-multilocation-violation-details.txt'))
180-
.replace(/__PATH_TO_FILE_A__/g, PATH_TO_FILE_A)
181-
.replace(/__PATH_TO_FILE_Z__/g, PATH_TO_FILE_Z);
181+
.replaceAll("{{PATHSEP}}", path.sep)
182+
.replace("{{RUNDIR}}", results.getRunDirectory());
182183
expect(actualEventText).toEqual(expectedViolationDetails);
183184
})
184185
});
@@ -229,7 +230,7 @@ describe('ResultsViewer implementations', () => {
229230
expect(displayEvents[0].type).toEqual(DisplayEventType.LOG);
230231
expect(displayEvents[0].data).toEqual('');
231232
expect(displayEvents[1].type).toEqual(DisplayEventType.LOG);
232-
expect(displayEvents[1].data).toEqual(getMessage(BundleName.ResultsViewer, 'summary.table.results-relative-to', [PATH_TO_SAMPLE_CODE]));
233+
expect(displayEvents[1].data).toEqual(getMessage(BundleName.ResultsViewer, 'summary.shared.results-relative-to', [PATH_TO_SAMPLE_CODE]));
233234
expect(displayEvents[2].type).toEqual(DisplayEventType.TABLE);
234235
expect(displayEvents[2].data).toEqual(`{"columns":["#","Severity","Rule","Location","Message"],"rows":[{"num":1,"location":"someFile.cls:1:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"},{"num":2,"location":"someFile.cls:1:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"},{"num":3,"location":"someFile.cls:1:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"},{"num":4,"location":"someFile.cls:1:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"}]}`);
235236
});
@@ -263,7 +264,7 @@ describe('ResultsViewer implementations', () => {
263264
expect(displayEvents[0].type).toEqual(DisplayEventType.LOG);
264265
expect(displayEvents[0].data).toEqual('');
265266
expect(displayEvents[1].type).toEqual(DisplayEventType.LOG);
266-
expect(displayEvents[1].data).toEqual(getMessage(BundleName.ResultsViewer, 'summary.table.results-relative-to', [PATH_TO_SAMPLE_CODE]));
267+
expect(displayEvents[1].data).toEqual(getMessage(BundleName.ResultsViewer, 'summary.shared.results-relative-to', [PATH_TO_SAMPLE_CODE]));
267268
expect(displayEvents[2].type).toEqual(DisplayEventType.TABLE);
268269
expect(displayEvents[2].data).toEqual(`{"columns":["#","Severity","Rule","Location","Message"],"rows":[{"num":1,"location":"fileZ.cls:20:1","rule":"stubEngine1:stub1RuleB","severity":"2 (High)","message":"This is a message"},{"num":2,"location":"fileA.cls:1:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"},{"num":3,"location":"fileA.cls:20:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"},{"num":4,"location":"fileZ.cls:1:1","rule":"stubEngine1:stub1RuleA","severity":"4 (Low)","message":"This is a message"}]}`);
269270
});

0 commit comments

Comments
 (0)