|
16 | 16 |
|
17 | 17 | import { codeCoverage } from "./codeCoverage"; |
18 | 18 | 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(); |
20 | 27 |
|
21 | 28 | const expectInstrumentation = instrumentWith( |
22 | 29 | codeCoverage(new ZeroEdgeIdStrategy()) |
@@ -160,4 +167,59 @@ describe("code coverage instrumentation", () => { |
160 | 167 | expectInstrumentation(input, output); |
161 | 168 | }); |
162 | 169 | }); |
| 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 | + }); |
163 | 225 | }); |
0 commit comments