Fix determineSelectedQuery tests

This commit is contained in:
Dave Bartolomeo
2023-04-11 11:50:44 -04:00
parent 19e083e473
commit 1cbfd0159e
2 changed files with 26 additions and 19 deletions

View File

@@ -1,7 +1,10 @@
import { resolve, join } from "path";
import * as vscode from "vscode";
import { Uri } from "vscode";
import { determineSelectedQuery } from "../../../src/run-queries-shared";
import {
getQuickEvalContext,
validateQueryUri,
} from "../../../src/run-queries-shared";
async function showQlDocument(name: string): Promise<vscode.TextDocument> {
const folderPath = vscode.workspace.workspaceFolders![0].uri.fsPath;
@@ -14,43 +17,47 @@ async function showQlDocument(name: string): Promise<vscode.TextDocument> {
export function run() {
describe("Determining selected query", () => {
it("should allow ql files to be queried", async () => {
const q = await determineSelectedQuery(
const queryPath = validateQueryUri(
Uri.parse("file:///tmp/queryname.ql"),
false,
);
expect(q.queryPath).toBe(join("/", "tmp", "queryname.ql"));
expect(q.quickEvalPosition).toBeUndefined();
expect(queryPath).toBe(join("/", "tmp", "queryname.ql"));
});
it("should allow ql files to be quick-evaled", async () => {
const doc = await showQlDocument("query.ql");
const q = await determineSelectedQuery(doc.uri, true);
await showQlDocument("query.ql");
const q = await getQuickEvalContext(undefined);
expect(
q.queryPath.endsWith(join("ql-vscode", "test", "data", "query.ql")),
q.quickEvalPosition.fileName.endsWith(
join("ql-vscode", "test", "data", "query.ql"),
),
).toBe(true);
});
it("should allow qll files to be quick-evaled", async () => {
const doc = await showQlDocument("library.qll");
const q = await determineSelectedQuery(doc.uri, true);
await showQlDocument("library.qll");
const q = await getQuickEvalContext(undefined);
expect(
q.queryPath.endsWith(join("ql-vscode", "test", "data", "library.qll")),
q.quickEvalPosition.fileName.endsWith(
join("ql-vscode", "test", "data", "library.qll"),
),
).toBe(true);
});
it("should reject non-ql files when running a query", async () => {
await expect(
determineSelectedQuery(Uri.parse("file:///tmp/queryname.txt"), false),
).rejects.toThrow("The selected resource is not a CodeQL query file");
await expect(
determineSelectedQuery(Uri.parse("file:///tmp/queryname.qll"), false),
).rejects.toThrow("The selected resource is not a CodeQL query file");
expect(() =>
validateQueryUri(Uri.parse("file:///tmp/queryname.txt"), false),
).toThrow("The selected resource is not a CodeQL query file");
expect(() =>
validateQueryUri(Uri.parse("file:///tmp/queryname.qll"), false),
).toThrow("The selected resource is not a CodeQL query file");
});
it("should reject non-ql[l] files when running a quick eval", async () => {
await expect(
determineSelectedQuery(Uri.parse("file:///tmp/queryname.txt"), true),
).rejects.toThrow("The selected resource is not a CodeQL file");
await showQlDocument("textfile.txt");
await expect(getQuickEvalContext(undefined)).rejects.toThrow(
"The selected resource is not a CodeQL file",
);
});
});
}