Skip to content

Commit e03c2b1

Browse files
author
Dave Bartolomeo
committed
Custom message for assertions about files existing in packs
1 parent 9c42c6a commit e03c2b1

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-manager.test.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,47 @@ import { ExtensionApp } from "../../../../src/common/vscode/vscode-app";
2020
import { DbConfigStore } from "../../../../src/databases/config/db-config-store";
2121
import { mockedQuickPickItem } from "../../utils/mocking.helpers";
2222
import { QueryLanguage } from "../../../../src/common/query-language";
23+
import type { QueryPackFS } from "../../utils/bundled-pack-helpers";
2324
import { readBundledPack } from "../../utils/bundled-pack-helpers";
2425
import { load } from "js-yaml";
2526
import type { ExtensionPackMetadata } from "../../../../src/model-editor/extension-pack-metadata";
2627
import type { QlPackLockFile } from "../../../../src/packaging/qlpack-lock-file";
28+
import { expect } from "@jest/globals";
29+
import type { ExpectationResult } from "expect";
30+
31+
/**
32+
* Custom Jest matcher to check if a file exists in a query pack.
33+
*/
34+
function toExistInPack(
35+
this: jest.MatcherContext,
36+
actual: unknown,
37+
packFS: QueryPackFS,
38+
): ExpectationResult {
39+
if (typeof actual !== "string") {
40+
throw new TypeError("Expected actual value to be a string.");
41+
}
42+
43+
const pass = packFS.fileExists(actual);
44+
if (pass) {
45+
return {
46+
pass: true,
47+
message: () => `expected ${actual} not to exist in pack`,
48+
};
49+
} else {
50+
return {
51+
pass: false,
52+
message: () => `expected ${actual} to exist in pack`,
53+
};
54+
}
55+
}
56+
57+
expect.extend({ toExistInPack });
58+
59+
declare module "expect" {
60+
interface Matchers<R> {
61+
toExistInPack(packFS: QueryPackFS): R;
62+
}
63+
}
2764

2865
describe("Variant Analysis Manager", () => {
2966
let cli: CodeQLCliServer;
@@ -331,14 +368,14 @@ describe("Variant Analysis Manager", () => {
331368

332369
const packFS = await readBundledPack(request.query.pack);
333370
filesThatExist.forEach((file) => {
334-
expect(packFS.fileExists(file)).toBe(true);
371+
expect(file).toExistInPack(packFS);
335372
});
336373

337374
qlxFilesThatExist.forEach((file) => {
338-
expect(packFS.fileExists(file)).toBe(true);
375+
expect(file).toExistInPack(packFS);
339376
});
340377
filesThatDoNotExist.forEach((file) => {
341-
expect(packFS.fileExists(file)).toBe(false);
378+
expect(file).not.toExistInPack(packFS);
342379
});
343380

344381
expect(

extensions/ql-vscode/test/vscode-tests/utils/bundled-pack-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { extract as tar_extract } from "tar-stream";
44
import { pipeline } from "stream/promises";
55
import { createGunzip } from "zlib";
66

7-
interface QueryPackFS {
7+
export interface QueryPackFS {
88
fileExists: (name: string) => boolean;
99
fileContents: (name: string) => Buffer;
1010
directoryContents: (name: string) => string[];

0 commit comments

Comments
 (0)