-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathResultsWriter.test.ts
More file actions
93 lines (78 loc) · 3.32 KB
/
ResultsWriter.test.ts
File metadata and controls
93 lines (78 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import fs from 'node:fs';
import {OutputFormat} from '@salesforce/code-analyzer-core';
import {ResultsFileWriter, CompositeResultsWriter} from '../../../src/lib/writers/ResultsWriter';
import * as StubRunResults from '../../stubs/StubRunResults';
describe('ResultsWriter implementations', () => {
let writeFileSpy: jest.SpyInstance;
let writeFileInvocations: { file: fs.PathOrFileDescriptor, contents: string | ArrayBufferView }[];
beforeEach(() => {
writeFileInvocations = [];
writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockImplementation(async (file, contents) => {
writeFileInvocations.push({file, contents});
});
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('ResultsFileWriter', () => {
it.each([
{ext: '.csv', expectedOutput: `Results formatted as ${OutputFormat.CSV}`},
{ext: '.html', expectedOutput: `Results formatted as ${OutputFormat.HTML}`},
{ext: '.htm', expectedOutput: `Results formatted as ${OutputFormat.HTML}`},
{ext: '.json', expectedOutput: `Results formatted as ${OutputFormat.JSON}`},
{ext: '.sarif', expectedOutput: `Results formatted as ${OutputFormat.SARIF}`},
{ext: '.sarif.json', expectedOutput: `Results formatted as ${OutputFormat.SARIF}`},
{ext: '.xml', expectedOutput: `Results formatted as ${OutputFormat.XML}`}
])('Accepts and outputs valid file format: *$ext', ({ext, expectedOutput}) => {
const validFile = `beep${ext}`;
const outputFileWriter = new ResultsFileWriter(validFile);
const stubbedResults = new StubRunResults.StubNonEmptyResults();
outputFileWriter.write(stubbedResults);
expect(writeFileSpy).toHaveBeenCalled();
expect(writeFileInvocations).toEqual([{
file: validFile,
contents: expectedOutput
}]);
});
it('Writes file even when results are empty', () => {
const expectations = {
file: 'beep.csv',
contents: `Results formatted as ${OutputFormat.CSV}`
};
const outputFileWriter = new ResultsFileWriter(expectations.file);
const stubbedResults = new StubRunResults.StubEmptyResults();
outputFileWriter.write(stubbedResults);
expect(writeFileSpy).toHaveBeenCalled();
expect(writeFileInvocations).toEqual([expectations]);
});
it('Rejects invalid file format: *.txt', () => {
const invalidFile = 'beep.txt';
expect(() => new ResultsFileWriter(invalidFile)).toThrow(invalidFile);
});
});
describe('CompositeResultsWriter', () => {
it('Does a no-op when there are no files to write to', () => {
const outputFileWriter = CompositeResultsWriter.fromFiles([]);
const stubbedResults = new StubRunResults.StubNonEmptyResults();
outputFileWriter.write(stubbedResults);
expect(writeFileSpy).not.toHaveBeenCalled();
});
it('When given multiple files, outputs to all of them', () => {
const expectations = [{
file: 'beep.csv',
contents: `Results formatted as ${OutputFormat.CSV}`
}, {
file: 'beep.xml',
contents: `Results formatted as ${OutputFormat.XML}`
}, {
file: 'beep.json',
contents: `Results formatted as ${OutputFormat.JSON}`
}];
const outputFileWriter = CompositeResultsWriter.fromFiles(expectations.map(i => i.file));
const stubbedResults = new StubRunResults.StubNonEmptyResults();
outputFileWriter.write(stubbedResults);
expect(writeFileSpy).toHaveBeenCalledTimes(3);
expect(writeFileInvocations).toEqual(expectations);
});
})
});