Skip to content

Commit 2c5b44c

Browse files
committed
Fix standard query lockfile detection
1 parent 2a75e17 commit 2c5b44c

2 files changed

Lines changed: 38 additions & 36 deletions

File tree

extensions/ql-vscode/src/local-queries/standard-queries.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { CodeQLCliServer } from "../codeql-cli/cli";
22
import { QLPACK_FILENAMES, QLPACK_LOCK_FILENAMES } from "../common/ql";
33
import { basename, dirname, resolve } from "path";
44
import { extLogger } from "../common/logging/vscode";
5-
import { promises } from "fs-extra";
5+
import { pathExists, promises } from "fs-extra";
66
import type { BaseLogger } from "../common/logging";
77

88
type LockFileForStandardQueryResult = {
@@ -36,9 +36,14 @@ export async function createLockFileForStandardQuery(
3636
);
3737
}
3838
const packPath = dirname(packFilePath);
39-
const lockFilePath = packContents.find((p) =>
40-
QLPACK_LOCK_FILENAMES.includes(basename(p)),
41-
);
39+
let lockFilePath: string | undefined;
40+
for (const lockFileName of QLPACK_LOCK_FILENAMES) {
41+
const candidateLockFilePath = resolve(packPath, lockFileName);
42+
if (await pathExists(candidateLockFilePath)) {
43+
lockFilePath = candidateLockFilePath;
44+
break;
45+
}
46+
}
4247

4348
let cleanup: (() => Promise<void>) | undefined = undefined;
4449

extensions/ql-vscode/test/vscode-tests/no-workspace/local-queries/standard-queries.test.ts

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { DirectoryResult } from "tmp-promise";
44
import { dir } from "tmp-promise";
55
import { join } from "path";
66
import { createLockFileForStandardQuery } from "../../../../src/local-queries/standard-queries";
7-
import { outputFile, pathExists } from "fs-extra";
7+
import { outputFile, pathExists, readFile } from "fs-extra";
88

99
describe("createLockFileForStandardQuery", () => {
1010
let tmpDir: DirectoryResult;
@@ -41,37 +41,34 @@ describe("createLockFileForStandardQuery", () => {
4141
});
4242

4343
describe("when the lock file exists", () => {
44-
let lockfilePath: string;
45-
46-
beforeEach(async () => {
47-
lockfilePath = join(packPath, "qlpack.lock.yml");
48-
49-
packPacklist.mockResolvedValue([qlpackPath, lockfilePath, queryPath]);
50-
});
51-
52-
it("does not resolve or install dependencies", async () => {
53-
expect(await createLockFileForStandardQuery(mockCli, queryPath)).toEqual({
54-
cleanup: undefined,
55-
});
56-
57-
expect(packResolveDependencies).not.toHaveBeenCalled();
58-
expect(clearCache).not.toHaveBeenCalled();
59-
expect(packInstall).not.toHaveBeenCalled();
60-
});
61-
62-
it("does not resolve or install dependencies with a codeql-pack.lock.yml", async () => {
63-
lockfilePath = join(packPath, "codeql-pack.lock.yml");
64-
65-
packPacklist.mockResolvedValue([qlpackPath, lockfilePath, queryPath]);
66-
67-
expect(await createLockFileForStandardQuery(mockCli, queryPath)).toEqual({
68-
cleanup: undefined,
69-
});
70-
71-
expect(packResolveDependencies).not.toHaveBeenCalled();
72-
expect(clearCache).not.toHaveBeenCalled();
73-
expect(packInstall).not.toHaveBeenCalled();
74-
});
44+
it.each(["qlpack.lock.yml", "codeql-pack.lock.yml"])(
45+
"does not resolve or install dependencies with %s",
46+
async (lockFileName) => {
47+
const lockFilePath = join(packPath, lockFileName);
48+
const lockFileContents = `${lockFileName} contents`;
49+
await outputFile(lockFilePath, lockFileContents);
50+
51+
const { cleanup } = await createLockFileForStandardQuery(
52+
mockCli,
53+
queryPath,
54+
);
55+
56+
expect({
57+
cleanup,
58+
packResolveDependenciesCallCount:
59+
packResolveDependencies.mock.calls.length,
60+
clearCacheCallCount: clearCache.mock.calls.length,
61+
packInstallCallCount: packInstall.mock.calls.length,
62+
lockFileContents: await readFile(lockFilePath, "utf8"),
63+
}).toEqual({
64+
cleanup: undefined,
65+
packResolveDependenciesCallCount: 0,
66+
clearCacheCallCount: 0,
67+
packInstallCallCount: 0,
68+
lockFileContents,
69+
});
70+
},
71+
);
7572
});
7673

7774
describe("when the lock file does not exist", () => {

0 commit comments

Comments
 (0)