| 1 | import chai from "chai";
|
|---|
| 2 | import fs from "fs";
|
|---|
| 3 | import path from "path";
|
|---|
| 4 | import { FunctionCov, mergeFunctionCovs, mergeProcessCovs, mergeScriptCovs, ProcessCov, ScriptCov } from "../lib";
|
|---|
| 5 |
|
|---|
| 6 | const REPO_ROOT: string = path.join(__dirname, "..", "..", "..", "..");
|
|---|
| 7 | const BENCHES_INPUT_DIR: string = path.join(REPO_ROOT, "benches");
|
|---|
| 8 | const BENCHES_DIR: string = path.join(REPO_ROOT, "test-data", "merge", "benches");
|
|---|
| 9 | const RANGES_DIR: string = path.join(REPO_ROOT, "test-data", "merge", "ranges");
|
|---|
| 10 | const BENCHES_TIMEOUT: number = 20000; // 20sec
|
|---|
| 11 |
|
|---|
| 12 | interface MergeRangeItem {
|
|---|
| 13 | name: string;
|
|---|
| 14 | status: "run" | "skip" | "only";
|
|---|
| 15 | inputs: ProcessCov[];
|
|---|
| 16 | expected: ProcessCov;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | const FIXTURES_DIR: string = path.join(REPO_ROOT, "test-data", "bugs");
|
|---|
| 20 | function loadFixture(name: string) {
|
|---|
| 21 | const content: string = fs.readFileSync(
|
|---|
| 22 | path.resolve(FIXTURES_DIR, `${name}.json`),
|
|---|
| 23 | {encoding: "UTF-8"},
|
|---|
| 24 | );
|
|---|
| 25 | return JSON.parse(content);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | describe("merge", () => {
|
|---|
| 29 | describe("Various", () => {
|
|---|
| 30 | it("accepts empty arrays for `mergeProcessCovs`", () => {
|
|---|
| 31 | const inputs: ProcessCov[] = [];
|
|---|
| 32 | const expected: ProcessCov = {result: []};
|
|---|
| 33 | const actual: ProcessCov = mergeProcessCovs(inputs);
|
|---|
| 34 | chai.assert.deepEqual(actual, expected);
|
|---|
| 35 | });
|
|---|
| 36 |
|
|---|
| 37 | it("accepts empty arrays for `mergeScriptCovs`", () => {
|
|---|
| 38 | const inputs: ScriptCov[] = [];
|
|---|
| 39 | const expected: ScriptCov | undefined = undefined;
|
|---|
| 40 | const actual: ScriptCov | undefined = mergeScriptCovs(inputs);
|
|---|
| 41 | chai.assert.deepEqual(actual, expected);
|
|---|
| 42 | });
|
|---|
| 43 |
|
|---|
| 44 | it("accepts empty arrays for `mergeFunctionCovs`", () => {
|
|---|
| 45 | const inputs: FunctionCov[] = [];
|
|---|
| 46 | const expected: FunctionCov | undefined = undefined;
|
|---|
| 47 | const actual: FunctionCov | undefined = mergeFunctionCovs(inputs);
|
|---|
| 48 | chai.assert.deepEqual(actual, expected);
|
|---|
| 49 | });
|
|---|
| 50 |
|
|---|
| 51 | it("accepts arrays with a single item for `mergeProcessCovs`", () => {
|
|---|
| 52 | const inputs: ProcessCov[] = [
|
|---|
| 53 | {
|
|---|
| 54 | result: [
|
|---|
| 55 | {
|
|---|
| 56 | scriptId: "123",
|
|---|
| 57 | url: "/lib.js",
|
|---|
| 58 | functions: [
|
|---|
| 59 | {
|
|---|
| 60 | functionName: "test",
|
|---|
| 61 | isBlockCoverage: true,
|
|---|
| 62 | ranges: [
|
|---|
| 63 | {startOffset: 0, endOffset: 4, count: 2},
|
|---|
| 64 | {startOffset: 1, endOffset: 2, count: 1},
|
|---|
| 65 | {startOffset: 2, endOffset: 3, count: 1},
|
|---|
| 66 | ],
|
|---|
| 67 | },
|
|---|
| 68 | ],
|
|---|
| 69 | },
|
|---|
| 70 | ],
|
|---|
| 71 | },
|
|---|
| 72 | ];
|
|---|
| 73 | const expected: ProcessCov = {
|
|---|
| 74 | result: [
|
|---|
| 75 | {
|
|---|
| 76 | scriptId: "0",
|
|---|
| 77 | url: "/lib.js",
|
|---|
| 78 | functions: [
|
|---|
| 79 | {
|
|---|
| 80 | functionName: "test",
|
|---|
| 81 | isBlockCoverage: true,
|
|---|
| 82 | ranges: [
|
|---|
| 83 | {startOffset: 0, endOffset: 4, count: 2},
|
|---|
| 84 | {startOffset: 1, endOffset: 3, count: 1},
|
|---|
| 85 | ],
|
|---|
| 86 | },
|
|---|
| 87 | ],
|
|---|
| 88 | },
|
|---|
| 89 | ],
|
|---|
| 90 | };
|
|---|
| 91 | const actual: ProcessCov = mergeProcessCovs(inputs);
|
|---|
| 92 | chai.assert.deepEqual(actual, expected);
|
|---|
| 93 | });
|
|---|
| 94 |
|
|---|
| 95 | describe("mergeProcessCovs", () => {
|
|---|
| 96 | // see: https://github.com/demurgos/v8-coverage/issues/2
|
|---|
| 97 | it("handles function coverage merged into block coverage", () => {
|
|---|
| 98 | const blockCoverage: ProcessCov = loadFixture("issue-2-block-coverage");
|
|---|
| 99 | const functionCoverage: ProcessCov = loadFixture("issue-2-func-coverage");
|
|---|
| 100 | const inputs: ProcessCov[] = [
|
|---|
| 101 | functionCoverage,
|
|---|
| 102 | blockCoverage,
|
|---|
| 103 | ];
|
|---|
| 104 | const expected: ProcessCov = loadFixture("issue-2-expected");
|
|---|
| 105 | const actual: ProcessCov = mergeProcessCovs(inputs);
|
|---|
| 106 | chai.assert.deepEqual(actual, expected);
|
|---|
| 107 | });
|
|---|
| 108 |
|
|---|
| 109 | // see: https://github.com/demurgos/v8-coverage/issues/2
|
|---|
| 110 | it("handles block coverage merged into function coverage", () => {
|
|---|
| 111 | const blockCoverage: ProcessCov = loadFixture("issue-2-block-coverage");
|
|---|
| 112 | const functionCoverage: ProcessCov = loadFixture("issue-2-func-coverage");
|
|---|
| 113 | const inputs: ProcessCov[] = [
|
|---|
| 114 | blockCoverage,
|
|---|
| 115 | functionCoverage,
|
|---|
| 116 | ];
|
|---|
| 117 | const expected: ProcessCov = loadFixture("issue-2-expected");
|
|---|
| 118 | const actual: ProcessCov = mergeProcessCovs(inputs);
|
|---|
| 119 | chai.assert.deepEqual(actual, expected);
|
|---|
| 120 | });
|
|---|
| 121 | });
|
|---|
| 122 |
|
|---|
| 123 | it("accepts arrays with a single item for `mergeScriptCovs`", () => {
|
|---|
| 124 | const inputs: ScriptCov[] = [
|
|---|
| 125 | {
|
|---|
| 126 | scriptId: "123",
|
|---|
| 127 | url: "/lib.js",
|
|---|
| 128 | functions: [
|
|---|
| 129 | {
|
|---|
| 130 | functionName: "test",
|
|---|
| 131 | isBlockCoverage: true,
|
|---|
| 132 | ranges: [
|
|---|
| 133 | {startOffset: 0, endOffset: 4, count: 2},
|
|---|
| 134 | {startOffset: 1, endOffset: 2, count: 1},
|
|---|
| 135 | {startOffset: 2, endOffset: 3, count: 1},
|
|---|
| 136 | ],
|
|---|
| 137 | },
|
|---|
| 138 | ],
|
|---|
| 139 | },
|
|---|
| 140 | ];
|
|---|
| 141 | const expected: ScriptCov | undefined = {
|
|---|
| 142 | scriptId: "123",
|
|---|
| 143 | url: "/lib.js",
|
|---|
| 144 | functions: [
|
|---|
| 145 | {
|
|---|
| 146 | functionName: "test",
|
|---|
| 147 | isBlockCoverage: true,
|
|---|
| 148 | ranges: [
|
|---|
| 149 | {startOffset: 0, endOffset: 4, count: 2},
|
|---|
| 150 | {startOffset: 1, endOffset: 3, count: 1},
|
|---|
| 151 | ],
|
|---|
| 152 | },
|
|---|
| 153 | ],
|
|---|
| 154 | };
|
|---|
| 155 | const actual: ScriptCov | undefined = mergeScriptCovs(inputs);
|
|---|
| 156 | chai.assert.deepEqual(actual, expected);
|
|---|
| 157 | });
|
|---|
| 158 |
|
|---|
| 159 | it("accepts arrays with a single item for `mergeFunctionCovs`", () => {
|
|---|
| 160 | const inputs: FunctionCov[] = [
|
|---|
| 161 | {
|
|---|
| 162 | functionName: "test",
|
|---|
| 163 | isBlockCoverage: true,
|
|---|
| 164 | ranges: [
|
|---|
| 165 | {startOffset: 0, endOffset: 4, count: 2},
|
|---|
| 166 | {startOffset: 1, endOffset: 2, count: 1},
|
|---|
| 167 | {startOffset: 2, endOffset: 3, count: 1},
|
|---|
| 168 | ],
|
|---|
| 169 | },
|
|---|
| 170 | ];
|
|---|
| 171 | const expected: FunctionCov = {
|
|---|
| 172 | functionName: "test",
|
|---|
| 173 | isBlockCoverage: true,
|
|---|
| 174 | ranges: [
|
|---|
| 175 | {startOffset: 0, endOffset: 4, count: 2},
|
|---|
| 176 | {startOffset: 1, endOffset: 3, count: 1},
|
|---|
| 177 | ],
|
|---|
| 178 | };
|
|---|
| 179 | const actual: FunctionCov | undefined = mergeFunctionCovs(inputs);
|
|---|
| 180 | chai.assert.deepEqual(actual, expected);
|
|---|
| 181 | });
|
|---|
| 182 | });
|
|---|
| 183 |
|
|---|
| 184 | describe("ranges", () => {
|
|---|
| 185 | for (const sourceFile of getSourceFiles()) {
|
|---|
| 186 | const relPath: string = path.relative(RANGES_DIR, sourceFile);
|
|---|
| 187 | describe(relPath, () => {
|
|---|
| 188 | const content: string = fs.readFileSync(sourceFile, {encoding: "UTF-8"});
|
|---|
| 189 | const items: MergeRangeItem[] = JSON.parse(content);
|
|---|
| 190 | for (const item of items) {
|
|---|
| 191 | const test: () => void = () => {
|
|---|
| 192 | const actual: ProcessCov | undefined = mergeProcessCovs(item.inputs);
|
|---|
| 193 | chai.assert.deepEqual(actual, item.expected);
|
|---|
| 194 | };
|
|---|
| 195 | switch (item.status) {
|
|---|
| 196 | case "run":
|
|---|
| 197 | it(item.name, test);
|
|---|
| 198 | break;
|
|---|
| 199 | case "only":
|
|---|
| 200 | it.only(item.name, test);
|
|---|
| 201 | break;
|
|---|
| 202 | case "skip":
|
|---|
| 203 | it.skip(item.name, test);
|
|---|
| 204 | break;
|
|---|
| 205 | default:
|
|---|
| 206 | throw new Error(`Unexpected status: ${item.status}`);
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 | });
|
|---|
| 210 | }
|
|---|
| 211 | });
|
|---|
| 212 |
|
|---|
| 213 | describe("benches", () => {
|
|---|
| 214 | for (const bench of getBenches()) {
|
|---|
| 215 | const BENCHES_TO_SKIP: Set<string> = new Set();
|
|---|
| 216 | if (process.env.CI === "true") {
|
|---|
| 217 | // Skip very large benchmarks when running continuous integration
|
|---|
| 218 | BENCHES_TO_SKIP.add("node@10.11.0");
|
|---|
| 219 | BENCHES_TO_SKIP.add("npm@6.4.1");
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | const name: string = path.basename(bench);
|
|---|
| 223 |
|
|---|
| 224 | if (BENCHES_TO_SKIP.has(name)) {
|
|---|
| 225 | it.skip(`${name} (skipped: too large for CI)`, testBench);
|
|---|
| 226 | } else {
|
|---|
| 227 | it(name, testBench);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | async function testBench(this: Mocha.Context) {
|
|---|
| 231 | this.timeout(BENCHES_TIMEOUT);
|
|---|
| 232 |
|
|---|
| 233 | const inputFileNames: string[] = await fs.promises.readdir(bench);
|
|---|
| 234 | const inputPromises: Promise<ProcessCov>[] = [];
|
|---|
| 235 | for (const inputFileName of inputFileNames) {
|
|---|
| 236 | const resolved: string = path.join(bench, inputFileName);
|
|---|
| 237 | inputPromises.push(fs.promises.readFile(resolved).then(buffer => JSON.parse(buffer.toString("UTF-8"))));
|
|---|
| 238 | }
|
|---|
| 239 | const inputs: ProcessCov[] = await Promise.all(inputPromises);
|
|---|
| 240 | const expectedPath: string = path.join(BENCHES_DIR, `${name}.json`);
|
|---|
| 241 | const expectedContent: string = await fs.promises.readFile(expectedPath, {encoding: "UTF-8"}) as string;
|
|---|
| 242 | const expected: ProcessCov = JSON.parse(expectedContent);
|
|---|
| 243 | const startTime: number = Date.now();
|
|---|
| 244 | const actual: ProcessCov | undefined = mergeProcessCovs(inputs);
|
|---|
| 245 | const endTime: number = Date.now();
|
|---|
| 246 | console.error(`Time (${name}): ${(endTime - startTime) / 1000}`);
|
|---|
| 247 | chai.assert.deepEqual(actual, expected);
|
|---|
| 248 | console.error(`OK: ${name}`);
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | });
|
|---|
| 252 | });
|
|---|
| 253 |
|
|---|
| 254 | function getSourceFiles() {
|
|---|
| 255 | return getSourcesFrom(RANGES_DIR);
|
|---|
| 256 |
|
|---|
| 257 | function* getSourcesFrom(dir: string): Iterable<string> {
|
|---|
| 258 | const names: string[] = fs.readdirSync(dir);
|
|---|
| 259 | for (const name of names) {
|
|---|
| 260 | const resolved: string = path.join(dir, name);
|
|---|
| 261 | const stat: fs.Stats = fs.statSync(resolved);
|
|---|
| 262 | if (stat.isDirectory()) {
|
|---|
| 263 | yield* getSourcesFrom(dir);
|
|---|
| 264 | } else {
|
|---|
| 265 | yield resolved;
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | function* getBenches(): Iterable<string> {
|
|---|
| 272 | const names: string[] = fs.readdirSync(BENCHES_INPUT_DIR);
|
|---|
| 273 | for (const name of names) {
|
|---|
| 274 | const resolved: string = path.join(BENCHES_INPUT_DIR, name);
|
|---|
| 275 | const stat: fs.Stats = fs.statSync(resolved);
|
|---|
| 276 | if (stat.isDirectory()) {
|
|---|
| 277 | yield resolved;
|
|---|
| 278 | }
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|