Remove unnecessary nesting in external-api-usage-query.test.ts
This commit is contained in:
@@ -21,274 +21,272 @@ import { CodeQLCliServer } from "../../../../src/codeql-cli/cli";
|
|||||||
import { QueryRunner } from "../../../../src/query-server";
|
import { QueryRunner } from "../../../../src/query-server";
|
||||||
import { QueryOutputDir } from "../../../../src/run-queries-shared";
|
import { QueryOutputDir } from "../../../../src/run-queries-shared";
|
||||||
|
|
||||||
describe("external api usage query", () => {
|
describe("runModelEditorQueries", () => {
|
||||||
describe("runModelEditorQueries", () => {
|
const language = Object.keys(fetchExternalApiQueries)[
|
||||||
const language = Object.keys(fetchExternalApiQueries)[
|
Math.floor(Math.random() * Object.keys(fetchExternalApiQueries).length)
|
||||||
Math.floor(Math.random() * Object.keys(fetchExternalApiQueries).length)
|
] as QueryLanguage;
|
||||||
] as QueryLanguage;
|
|
||||||
|
|
||||||
const queryDir = dirSync({ unsafeCleanup: true }).name;
|
const queryDir = dirSync({ unsafeCleanup: true }).name;
|
||||||
|
|
||||||
it("should log an error", async () => {
|
it("should log an error", async () => {
|
||||||
const showAndLogExceptionWithTelemetrySpy: jest.SpiedFunction<
|
const showAndLogExceptionWithTelemetrySpy: jest.SpiedFunction<
|
||||||
typeof showAndLogExceptionWithTelemetry
|
typeof showAndLogExceptionWithTelemetry
|
||||||
> = jest.spyOn(log, "showAndLogExceptionWithTelemetry");
|
> = jest.spyOn(log, "showAndLogExceptionWithTelemetry");
|
||||||
|
|
||||||
const outputDir = new QueryOutputDir(join((await file()).path, "1"));
|
const outputDir = new QueryOutputDir(join((await file()).path, "1"));
|
||||||
|
|
||||||
const query = fetchExternalApiQueries[language];
|
const query = fetchExternalApiQueries[language];
|
||||||
if (!query) {
|
if (!query) {
|
||||||
throw new Error(`No query found for language ${language}`);
|
throw new Error(`No query found for language ${language}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const options = {
|
|
||||||
cliServer: mockedObject<CodeQLCliServer>({
|
|
||||||
resolveQlpacks: jest.fn().mockResolvedValue({
|
|
||||||
"my/extensions": "/a/b/c/",
|
|
||||||
}),
|
|
||||||
resolveQueriesInSuite: jest
|
|
||||||
.fn()
|
|
||||||
.mockResolvedValue(["/a/b/c/ApplicationModeEndpoints.ql"]),
|
|
||||||
packPacklist: jest
|
|
||||||
.fn()
|
|
||||||
.mockResolvedValue([
|
|
||||||
"/a/b/c/qlpack.yml",
|
|
||||||
"/a/b/c/qlpack.lock.yml",
|
|
||||||
"/a/b/c/qlpack2.yml",
|
|
||||||
]),
|
|
||||||
}),
|
|
||||||
queryRunner: mockedObject<QueryRunner>({
|
|
||||||
createQueryRun: jest.fn().mockReturnValue({
|
|
||||||
evaluate: jest.fn().mockResolvedValue({
|
|
||||||
resultType: QueryResultType.CANCELLATION,
|
|
||||||
}),
|
|
||||||
outputDir,
|
|
||||||
}),
|
|
||||||
logger: createMockLogger(),
|
|
||||||
}),
|
|
||||||
databaseItem: mockedObject<DatabaseItem>({
|
|
||||||
databaseUri: mockedUri("/a/b/c/src.zip"),
|
|
||||||
contents: {
|
|
||||||
kind: DatabaseKind.Database,
|
|
||||||
name: "foo",
|
|
||||||
datasetUri: mockedUri(),
|
|
||||||
},
|
|
||||||
language,
|
|
||||||
}),
|
|
||||||
queryStorageDir: "/tmp/queries",
|
|
||||||
queryDir,
|
|
||||||
progress: jest.fn(),
|
|
||||||
token: {
|
|
||||||
isCancellationRequested: false,
|
|
||||||
onCancellationRequested: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(
|
|
||||||
await runModelEditorQueries(Mode.Application, options),
|
|
||||||
).toBeUndefined();
|
|
||||||
expect(showAndLogExceptionWithTelemetrySpy).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
undefined,
|
|
||||||
expect.any(RedactableError),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should run query for random language", async () => {
|
|
||||||
const outputDir = new QueryOutputDir(join((await file()).path, "1"));
|
|
||||||
|
|
||||||
const query = fetchExternalApiQueries[language];
|
|
||||||
if (!query) {
|
|
||||||
throw new Error(`No query found for language ${language}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = {
|
|
||||||
cliServer: mockedObject<CodeQLCliServer>({
|
|
||||||
resolveQlpacks: jest.fn().mockResolvedValue({
|
|
||||||
"my/extensions": "/a/b/c/",
|
|
||||||
}),
|
|
||||||
resolveQueriesInSuite: jest
|
|
||||||
.fn()
|
|
||||||
.mockResolvedValue(["/a/b/c/ApplicationModeEndpoints.ql"]),
|
|
||||||
packPacklist: jest
|
|
||||||
.fn()
|
|
||||||
.mockResolvedValue([
|
|
||||||
"/a/b/c/qlpack.yml",
|
|
||||||
"/a/b/c/qlpack.lock.yml",
|
|
||||||
"/a/b/c/qlpack2.yml",
|
|
||||||
]),
|
|
||||||
bqrsInfo: jest.fn().mockResolvedValue({
|
|
||||||
"result-sets": [],
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
queryRunner: mockedObject<QueryRunner>({
|
|
||||||
createQueryRun: jest.fn().mockReturnValue({
|
|
||||||
evaluate: jest.fn().mockResolvedValue({
|
|
||||||
resultType: QueryResultType.SUCCESS,
|
|
||||||
outputDir,
|
|
||||||
}),
|
|
||||||
outputDir,
|
|
||||||
}),
|
|
||||||
logger: createMockLogger(),
|
|
||||||
}),
|
|
||||||
databaseItem: mockedObject<DatabaseItem>({
|
|
||||||
databaseUri: mockedUri("/a/b/c/src.zip"),
|
|
||||||
contents: {
|
|
||||||
kind: DatabaseKind.Database,
|
|
||||||
name: "foo",
|
|
||||||
datasetUri: mockedUri(),
|
|
||||||
},
|
|
||||||
language,
|
|
||||||
}),
|
|
||||||
queryStorageDir: "/tmp/queries",
|
|
||||||
queryDir,
|
|
||||||
progress: jest.fn(),
|
|
||||||
token: {
|
|
||||||
isCancellationRequested: false,
|
|
||||||
onCancellationRequested: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
const result = await runModelEditorQueries(Mode.Framework, options);
|
|
||||||
|
|
||||||
expect(result).not.toBeUndefined;
|
|
||||||
|
|
||||||
expect(options.cliServer.resolveQlpacks).toHaveBeenCalledTimes(1);
|
|
||||||
expect(options.cliServer.resolveQlpacks).toHaveBeenCalledWith([], true);
|
|
||||||
expect(options.queryRunner.createQueryRun).toHaveBeenCalledWith(
|
|
||||||
"/a/b/c/src.zip",
|
|
||||||
{
|
|
||||||
queryPath: expect.stringMatching(/\S*ModeEndpoints\.ql/),
|
|
||||||
quickEvalPosition: undefined,
|
|
||||||
quickEvalCountOnly: false,
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
[],
|
|
||||||
["my/extensions"],
|
|
||||||
{},
|
|
||||||
"/tmp/queries",
|
|
||||||
undefined,
|
|
||||||
undefined,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("readQueryResults", () => {
|
|
||||||
const options = {
|
const options = {
|
||||||
cliServer: {
|
cliServer: mockedObject<CodeQLCliServer>({
|
||||||
bqrsInfo: jest.fn(),
|
resolveQlpacks: jest.fn().mockResolvedValue({
|
||||||
bqrsDecode: jest.fn(),
|
"my/extensions": "/a/b/c/",
|
||||||
|
}),
|
||||||
|
resolveQueriesInSuite: jest
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue(["/a/b/c/ApplicationModeEndpoints.ql"]),
|
||||||
|
packPacklist: jest
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue([
|
||||||
|
"/a/b/c/qlpack.yml",
|
||||||
|
"/a/b/c/qlpack.lock.yml",
|
||||||
|
"/a/b/c/qlpack2.yml",
|
||||||
|
]),
|
||||||
|
}),
|
||||||
|
queryRunner: mockedObject<QueryRunner>({
|
||||||
|
createQueryRun: jest.fn().mockReturnValue({
|
||||||
|
evaluate: jest.fn().mockResolvedValue({
|
||||||
|
resultType: QueryResultType.CANCELLATION,
|
||||||
|
}),
|
||||||
|
outputDir,
|
||||||
|
}),
|
||||||
|
logger: createMockLogger(),
|
||||||
|
}),
|
||||||
|
databaseItem: mockedObject<DatabaseItem>({
|
||||||
|
databaseUri: mockedUri("/a/b/c/src.zip"),
|
||||||
|
contents: {
|
||||||
|
kind: DatabaseKind.Database,
|
||||||
|
name: "foo",
|
||||||
|
datasetUri: mockedUri(),
|
||||||
|
},
|
||||||
|
language,
|
||||||
|
}),
|
||||||
|
queryStorageDir: "/tmp/queries",
|
||||||
|
queryDir,
|
||||||
|
progress: jest.fn(),
|
||||||
|
token: {
|
||||||
|
isCancellationRequested: false,
|
||||||
|
onCancellationRequested: jest.fn(),
|
||||||
},
|
},
|
||||||
bqrsPath: "/tmp/results.bqrs",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let showAndLogExceptionWithTelemetrySpy: jest.SpiedFunction<
|
expect(
|
||||||
typeof showAndLogExceptionWithTelemetry
|
await runModelEditorQueries(Mode.Application, options),
|
||||||
>;
|
).toBeUndefined();
|
||||||
|
expect(showAndLogExceptionWithTelemetrySpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
undefined,
|
||||||
|
expect.any(RedactableError),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(() => {
|
it("should run query for random language", async () => {
|
||||||
showAndLogExceptionWithTelemetrySpy = jest.spyOn(
|
const outputDir = new QueryOutputDir(join((await file()).path, "1"));
|
||||||
log,
|
|
||||||
"showAndLogExceptionWithTelemetry",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns undefined when there are no results", async () => {
|
const query = fetchExternalApiQueries[language];
|
||||||
options.cliServer.bqrsInfo.mockResolvedValue({
|
if (!query) {
|
||||||
"result-sets": [],
|
throw new Error(`No query found for language ${language}`);
|
||||||
});
|
}
|
||||||
|
|
||||||
expect(await readQueryResults(options)).toBeUndefined();
|
const options = {
|
||||||
expect(showAndLogExceptionWithTelemetrySpy).toHaveBeenCalledWith(
|
cliServer: mockedObject<CodeQLCliServer>({
|
||||||
expect.anything(),
|
resolveQlpacks: jest.fn().mockResolvedValue({
|
||||||
undefined,
|
"my/extensions": "/a/b/c/",
|
||||||
expect.any(RedactableError),
|
}),
|
||||||
);
|
resolveQueriesInSuite: jest
|
||||||
});
|
.fn()
|
||||||
|
.mockResolvedValue(["/a/b/c/ApplicationModeEndpoints.ql"]),
|
||||||
|
packPacklist: jest
|
||||||
|
.fn()
|
||||||
|
.mockResolvedValue([
|
||||||
|
"/a/b/c/qlpack.yml",
|
||||||
|
"/a/b/c/qlpack.lock.yml",
|
||||||
|
"/a/b/c/qlpack2.yml",
|
||||||
|
]),
|
||||||
|
bqrsInfo: jest.fn().mockResolvedValue({
|
||||||
|
"result-sets": [],
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
queryRunner: mockedObject<QueryRunner>({
|
||||||
|
createQueryRun: jest.fn().mockReturnValue({
|
||||||
|
evaluate: jest.fn().mockResolvedValue({
|
||||||
|
resultType: QueryResultType.SUCCESS,
|
||||||
|
outputDir,
|
||||||
|
}),
|
||||||
|
outputDir,
|
||||||
|
}),
|
||||||
|
logger: createMockLogger(),
|
||||||
|
}),
|
||||||
|
databaseItem: mockedObject<DatabaseItem>({
|
||||||
|
databaseUri: mockedUri("/a/b/c/src.zip"),
|
||||||
|
contents: {
|
||||||
|
kind: DatabaseKind.Database,
|
||||||
|
name: "foo",
|
||||||
|
datasetUri: mockedUri(),
|
||||||
|
},
|
||||||
|
language,
|
||||||
|
}),
|
||||||
|
queryStorageDir: "/tmp/queries",
|
||||||
|
queryDir,
|
||||||
|
progress: jest.fn(),
|
||||||
|
token: {
|
||||||
|
isCancellationRequested: false,
|
||||||
|
onCancellationRequested: jest.fn(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
it("returns undefined when there are multiple result sets", async () => {
|
const result = await runModelEditorQueries(Mode.Framework, options);
|
||||||
options.cliServer.bqrsInfo.mockResolvedValue({
|
|
||||||
"result-sets": [
|
|
||||||
{
|
|
||||||
name: "#select",
|
|
||||||
rows: 10,
|
|
||||||
columns: [
|
|
||||||
{ name: "usage", kind: "e" },
|
|
||||||
{ name: "apiName", kind: "s" },
|
|
||||||
{ kind: "s" },
|
|
||||||
{ kind: "s" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "#select2",
|
|
||||||
rows: 10,
|
|
||||||
columns: [
|
|
||||||
{ name: "usage", kind: "e" },
|
|
||||||
{ name: "apiName", kind: "s" },
|
|
||||||
{ kind: "s" },
|
|
||||||
{ kind: "s" },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(await readQueryResults(options)).toBeUndefined();
|
expect(result).not.toBeUndefined;
|
||||||
expect(showAndLogExceptionWithTelemetrySpy).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
undefined,
|
|
||||||
expect.any(RedactableError),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("gets the result set", async () => {
|
expect(options.cliServer.resolveQlpacks).toHaveBeenCalledTimes(1);
|
||||||
options.cliServer.bqrsInfo.mockResolvedValue({
|
expect(options.cliServer.resolveQlpacks).toHaveBeenCalledWith([], true);
|
||||||
"result-sets": [
|
expect(options.queryRunner.createQueryRun).toHaveBeenCalledWith(
|
||||||
{
|
"/a/b/c/src.zip",
|
||||||
name: "#select",
|
{
|
||||||
rows: 10,
|
queryPath: expect.stringMatching(/\S*ModeEndpoints\.ql/),
|
||||||
columns: [
|
quickEvalPosition: undefined,
|
||||||
{ name: "usage", kind: "e" },
|
quickEvalCountOnly: false,
|
||||||
{ name: "apiName", kind: "s" },
|
},
|
||||||
{ kind: "s" },
|
false,
|
||||||
{ kind: "s" },
|
[],
|
||||||
],
|
["my/extensions"],
|
||||||
},
|
{},
|
||||||
],
|
"/tmp/queries",
|
||||||
"compatible-query-kinds": ["Table", "Tree", "Graph"],
|
undefined,
|
||||||
});
|
undefined,
|
||||||
const decodedResultSet = {
|
);
|
||||||
columns: [
|
});
|
||||||
{ name: "usage", kind: "e" },
|
});
|
||||||
{ name: "apiName", kind: "s" },
|
|
||||||
{ kind: "s" },
|
describe("readQueryResults", () => {
|
||||||
{ kind: "s" },
|
const options = {
|
||||||
],
|
cliServer: {
|
||||||
tuples: [
|
bqrsInfo: jest.fn(),
|
||||||
[
|
bqrsDecode: jest.fn(),
|
||||||
"java.io.PrintStream#println(String)",
|
},
|
||||||
true,
|
bqrsPath: "/tmp/results.bqrs",
|
||||||
{
|
};
|
||||||
label: "println(...)",
|
|
||||||
url: {
|
let showAndLogExceptionWithTelemetrySpy: jest.SpiedFunction<
|
||||||
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
|
typeof showAndLogExceptionWithTelemetry
|
||||||
startLine: 29,
|
>;
|
||||||
startColumn: 9,
|
|
||||||
endLine: 29,
|
beforeEach(() => {
|
||||||
endColumn: 49,
|
showAndLogExceptionWithTelemetrySpy = jest.spyOn(
|
||||||
},
|
log,
|
||||||
},
|
"showAndLogExceptionWithTelemetry",
|
||||||
],
|
);
|
||||||
],
|
});
|
||||||
};
|
|
||||||
options.cliServer.bqrsDecode.mockResolvedValue(decodedResultSet);
|
it("returns undefined when there are no results", async () => {
|
||||||
|
options.cliServer.bqrsInfo.mockResolvedValue({
|
||||||
const result = await readQueryResults(options);
|
"result-sets": [],
|
||||||
expect(result).toEqual(decodedResultSet);
|
});
|
||||||
expect(options.cliServer.bqrsInfo).toHaveBeenCalledWith(options.bqrsPath);
|
|
||||||
expect(options.cliServer.bqrsDecode).toHaveBeenCalledWith(
|
expect(await readQueryResults(options)).toBeUndefined();
|
||||||
options.bqrsPath,
|
expect(showAndLogExceptionWithTelemetrySpy).toHaveBeenCalledWith(
|
||||||
"#select",
|
expect.anything(),
|
||||||
);
|
undefined,
|
||||||
});
|
expect.any(RedactableError),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns undefined when there are multiple result sets", async () => {
|
||||||
|
options.cliServer.bqrsInfo.mockResolvedValue({
|
||||||
|
"result-sets": [
|
||||||
|
{
|
||||||
|
name: "#select",
|
||||||
|
rows: 10,
|
||||||
|
columns: [
|
||||||
|
{ name: "usage", kind: "e" },
|
||||||
|
{ name: "apiName", kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "#select2",
|
||||||
|
rows: 10,
|
||||||
|
columns: [
|
||||||
|
{ name: "usage", kind: "e" },
|
||||||
|
{ name: "apiName", kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(await readQueryResults(options)).toBeUndefined();
|
||||||
|
expect(showAndLogExceptionWithTelemetrySpy).toHaveBeenCalledWith(
|
||||||
|
expect.anything(),
|
||||||
|
undefined,
|
||||||
|
expect.any(RedactableError),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("gets the result set", async () => {
|
||||||
|
options.cliServer.bqrsInfo.mockResolvedValue({
|
||||||
|
"result-sets": [
|
||||||
|
{
|
||||||
|
name: "#select",
|
||||||
|
rows: 10,
|
||||||
|
columns: [
|
||||||
|
{ name: "usage", kind: "e" },
|
||||||
|
{ name: "apiName", kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
"compatible-query-kinds": ["Table", "Tree", "Graph"],
|
||||||
|
});
|
||||||
|
const decodedResultSet = {
|
||||||
|
columns: [
|
||||||
|
{ name: "usage", kind: "e" },
|
||||||
|
{ name: "apiName", kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
{ kind: "s" },
|
||||||
|
],
|
||||||
|
tuples: [
|
||||||
|
[
|
||||||
|
"java.io.PrintStream#println(String)",
|
||||||
|
true,
|
||||||
|
{
|
||||||
|
label: "println(...)",
|
||||||
|
url: {
|
||||||
|
uri: "file:/home/runner/work/sql2o-example/sql2o-example/src/main/java/org/example/HelloController.java",
|
||||||
|
startLine: 29,
|
||||||
|
startColumn: 9,
|
||||||
|
endLine: 29,
|
||||||
|
endColumn: 49,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
],
|
||||||
|
};
|
||||||
|
options.cliServer.bqrsDecode.mockResolvedValue(decodedResultSet);
|
||||||
|
|
||||||
|
const result = await readQueryResults(options);
|
||||||
|
expect(result).toEqual(decodedResultSet);
|
||||||
|
expect(options.cliServer.bqrsInfo).toHaveBeenCalledWith(options.bqrsPath);
|
||||||
|
expect(options.cliServer.bqrsDecode).toHaveBeenCalledWith(
|
||||||
|
options.bqrsPath,
|
||||||
|
"#select",
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user