Skip to content

Commit

Permalink
Move QL pack root path to the QlPackDetails (#3267)
Browse files Browse the repository at this point in the history
  • Loading branch information
charisk authored Jan 23, 2024
1 parent 9285b02 commit 67e2ff3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions extensions/ql-vscode/src/variant-analysis/ql-pack-details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
*/
export interface QlPackDetails {
queryFile: string;

// The path to the QL pack that is used for triggering a variant analysis.
// If there is no query pack, this is the same as the directory of the query files.
qlPackRootPath: string;
}
4 changes: 2 additions & 2 deletions extensions/ql-vscode/src/variant-analysis/run-remote-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ async function generateQueryPack(
): Promise<GeneratedQueryPack> {
const queryFile = qlPackDetails.queryFile;

const originalPackRoot = await findPackRoot(queryFile);
const originalPackRoot = qlPackDetails.qlPackRootPath;
const packRelativePath = relative(originalPackRoot, queryFile);
const workspaceFolders = getOnDiskWorkspaceFolders();
const extensionPacks = await getExtensionPacksToInject(
Expand Down Expand Up @@ -262,7 +262,7 @@ async function copyExistingQueryPack(
await fixPackFile(queryPackDir, packRelativePath);
}

async function findPackRoot(queryFile: string): Promise<string> {
export async function findPackRoot(queryFile: string): Promise<string> {
// recursively find the directory containing qlpack.yml or codeql-pack.yml
let dir = dirname(queryFile);
while (!(await getQlPackFilePath(dir))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ import type {
LoadResultsOptions,
VariantAnalysisResultsManager,
} from "./variant-analysis-results-manager";
import { getQueryName, prepareRemoteQueryRun } from "./run-remote-query";
import {
findPackRoot,
getQueryName,
prepareRemoteQueryRun,
} from "./run-remote-query";
import {
mapVariantAnalysis,
mapVariantAnalysisRepositoryTask,
Expand Down Expand Up @@ -273,6 +277,7 @@ export class VariantAnalysisManager
// for multiple queries.
const qlPackDetails: QlPackDetails = {
queryFile: problemQueries[0],
qlPackRootPath: packDir,
};

await this.runVariantAnalysis(
Expand Down Expand Up @@ -308,8 +313,10 @@ export class VariantAnalysisManager

private async runVariantAnalysisCommand(uri: Uri): Promise<void> {
// Build up details to pass to the functions that run the variant analysis.
const qlPackRootPath = await findPackRoot(uri.fsPath);
const qlPackDetails: QlPackDetails = {
queryFile: uri.fsPath,
qlPackRootPath,
};

return withProgress(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ describe("Variant Analysis Manager", () => {
});

it("should run a variant analysis that is part of a qlpack", async () => {
const filePath = getFile("data-remote-qlpack/in-pack.ql");
const filePath = getFileOrDir("data-remote-qlpack/in-pack.ql");
const qlPackDetails: QlPackDetails = {
queryFile: filePath,
qlPackRootPath: join(baseDir, "data-remote-qlpack"),
};

await variantAnalysisManager.runVariantAnalysis(
Expand All @@ -125,9 +126,10 @@ describe("Variant Analysis Manager", () => {
});

it("should run a remote query that is not part of a qlpack", async () => {
const filePath = getFile("data-remote-no-qlpack/in-pack.ql");
const filePath = getFileOrDir("data-remote-no-qlpack/in-pack.ql");
const qlPackDetails: QlPackDetails = {
queryFile: filePath,
qlPackRootPath: join(baseDir, "data-remote-no-qlpack"),
};

await variantAnalysisManager.runVariantAnalysis(
Expand All @@ -149,11 +151,12 @@ describe("Variant Analysis Manager", () => {
});

it("should run a remote query that is nested inside a qlpack", async () => {
const filePath = getFile(
const filePath = getFileOrDir(
"data-remote-qlpack-nested/subfolder/in-pack.ql",
);
const qlPackDetails: QlPackDetails = {
queryFile: filePath,
qlPackRootPath: join(baseDir, "data-remote-qlpack-nested"),
};

await variantAnalysisManager.runVariantAnalysis(
Expand All @@ -175,9 +178,10 @@ describe("Variant Analysis Manager", () => {
});

it("should cancel a run before uploading", async () => {
const filePath = getFile("data-remote-no-qlpack/in-pack.ql");
const filePath = getFileOrDir("data-remote-no-qlpack/in-pack.ql");
const qlPackDetails: QlPackDetails = {
queryFile: filePath,
qlPackRootPath: join(baseDir, "data-remote-no-qlpack"),
};

const promise = variantAnalysisManager.runVariantAnalysis(
Expand Down Expand Up @@ -218,6 +222,7 @@ describe("Variant Analysis Manager", () => {
it("should run a remote query that is part of a qlpack", async () => {
await doVariantAnalysisTest({
queryPath: "data-remote-qlpack/in-pack.ql",
qlPackRootPath: "data-remote-qlpack",
expectedPackName: "github/remote-query-pack",
filesThatExist: ["in-pack.ql", "lib.qll"],
filesThatDoNotExist: [],
Expand All @@ -228,6 +233,7 @@ describe("Variant Analysis Manager", () => {
it("should run a remote query that is not part of a qlpack", async () => {
await doVariantAnalysisTest({
queryPath: "data-remote-no-qlpack/in-pack.ql",
qlPackRootPath: "data-remote-no-qlpack",
expectedPackName: "codeql-remote/query",
filesThatExist: ["in-pack.ql"],
filesThatDoNotExist: ["lib.qll", "not-in-pack.ql"],
Expand All @@ -238,6 +244,7 @@ describe("Variant Analysis Manager", () => {
it("should run a remote query that is nested inside a qlpack", async () => {
await doVariantAnalysisTest({
queryPath: "data-remote-qlpack-nested/subfolder/in-pack.ql",
qlPackRootPath: "data-remote-qlpack-nested",
expectedPackName: "github/remote-query-pack",
filesThatExist: ["subfolder/in-pack.ql", "otherfolder/lib.qll"],
filesThatDoNotExist: ["subfolder/not-in-pack.ql"],
Expand All @@ -255,6 +262,7 @@ describe("Variant Analysis Manager", () => {
await cli.setUseExtensionPacks(true);
await doVariantAnalysisTest({
queryPath: "data-remote-qlpack-nested/subfolder/in-pack.ql",
qlPackRootPath: "data-remote-qlpack-nested",
expectedPackName: "github/remote-query-pack",
filesThatExist: [
"subfolder/in-pack.ql",
Expand Down Expand Up @@ -299,12 +307,11 @@ describe("Variant Analysis Manager", () => {
? ["Telemetry/ExtractorInformation.ql"]
: [];

const qlPackRootPath = join(process.env.TEST_CODEQL_PATH, "java/ql/src");
const queryPath = join(qlPackRootPath, queryToRun);
await doVariantAnalysisTest({
queryPath: join(
process.env.TEST_CODEQL_PATH,
"java/ql/src",
queryToRun,
),
queryPath,
qlPackRootPath,
expectedPackName: "codeql/java-queries",
filesThatExist: [queryToRun, ...extraQueries],
filesThatDoNotExist: [],
Expand All @@ -317,6 +324,7 @@ describe("Variant Analysis Manager", () => {

async function doVariantAnalysisTest({
queryPath,
qlPackRootPath,
expectedPackName,
filesThatExist,
qlxFilesThatExist,
Expand All @@ -328,16 +336,18 @@ describe("Variant Analysis Manager", () => {
checkVersion = true,
}: {
queryPath: string;
qlPackRootPath: string;
expectedPackName: string;
filesThatExist: string[];
qlxFilesThatExist: string[];
filesThatDoNotExist: string[];
dependenciesToCheck?: string[];
checkVersion?: boolean;
}) {
const filePath = getFile(queryPath);
const filePath = getFileOrDir(queryPath);
const qlPackDetails: QlPackDetails = {
queryFile: filePath,
qlPackRootPath: getFileOrDir(qlPackRootPath),
};

await variantAnalysisManager.runVariantAnalysis(
Expand Down Expand Up @@ -416,11 +426,11 @@ describe("Variant Analysis Manager", () => {
);
}

function getFile(file: string): string {
if (isAbsolute(file)) {
return file;
function getFileOrDir(path: string): string {
if (isAbsolute(path)) {
return path;
} else {
return join(baseDir, file);
return join(baseDir, path);
}
}
});
Expand Down

0 comments on commit 67e2ff3

Please sign in to comment.