Skip to content

Commit bacf244

Browse files
committed
instrumentor: add unit test for sync file ID strategy
1 parent d0cf311 commit bacf244

1 file changed

Lines changed: 63 additions & 1 deletion

File tree

packages/instrumentor/plugins/codeCoverage.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616

1717
import { codeCoverage } from "./codeCoverage";
1818
import { instrumentWith } from "./testhelpers";
19-
import { ZeroEdgeIdStrategy } from "../edgeIdStrategy";
19+
import { FileSyncIdStrategy, ZeroEdgeIdStrategy } from "../edgeIdStrategy";
20+
import { Instrumentor } from "../instrument";
21+
22+
import * as tmp from "tmp";
23+
import * as fs from "fs";
24+
import * as os from "os";
25+
26+
tmp.setGracefulCleanup();
2027

2128
const expectInstrumentation = instrumentWith(
2229
codeCoverage(new ZeroEdgeIdStrategy())
@@ -160,4 +167,59 @@ describe("code coverage instrumentation", () => {
160167
expectInstrumentation(input, output);
161168
});
162169
});
170+
171+
describe("FileSyncIdStrategy", () => {
172+
it("should add correct number of edges", () => {
173+
const idSyncFile = tmp.fileSync({
174+
mode: 0o600,
175+
prefix: "jazzer.js",
176+
postfix: "idSync",
177+
});
178+
fs.closeSync(idSyncFile.fd);
179+
180+
const testCases: { file: string; code: string }[] = [
181+
{
182+
file: "foo.js",
183+
code: "if (1 < 2) { true; } else { false; }",
184+
},
185+
{
186+
file: "bar.js",
187+
code: "for (let i = 0; i < 100; i++) { counter++; }",
188+
},
189+
{
190+
file: "do_not_instrument.js",
191+
code: "some invalid code to throw a SyntaxError if we try to instrument it",
192+
},
193+
{
194+
file: "baz.js",
195+
code: "switch(a) {case 1: true; case 2: false; break; default: true;}",
196+
},
197+
];
198+
199+
const instrumentor = new Instrumentor(
200+
["*"],
201+
["do_not_instrument"],
202+
new FileSyncIdStrategy(idSyncFile.name)
203+
);
204+
205+
for (const testCase of testCases) {
206+
instrumentor.instrument(testCase.code, testCase.file);
207+
}
208+
209+
for (let i = 0; i < 100; i++) {
210+
// Randomly select a file to instrument. At this point all files should have been instrumented
211+
// and thus instrumenting new files should not change the ID sync file.
212+
const testCase = testCases[Math.floor(Math.random() * 4)];
213+
instrumentor.instrument(testCase.code, testCase.file);
214+
}
215+
216+
expect(
217+
fs
218+
.readFileSync(idSyncFile.name)
219+
.toString()
220+
.split(os.EOL)
221+
.filter((line) => line !== "")
222+
).toEqual(["foo.js,0,3", "bar.js,3,2", "baz.js,5,4"]);
223+
});
224+
});
163225
});

0 commit comments

Comments
 (0)