-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathrun-cli.test.ts
More file actions
187 lines (164 loc) · 5.88 KB
/
run-cli.test.ts
File metadata and controls
187 lines (164 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { authentication, Uri } from "vscode";
import { join } from "path";
import { SemVer } from "semver";
import type {
CodeQLCliServer,
QueryInfoByLanguage,
} from "../../../src/codeql-cli/cli";
import { itWithCodeQL } from "../cli";
import { getOnDiskWorkspaceFolders } from "../../../src/common/vscode/workspace-folders";
import {
KeyType,
resolveContextualQueries,
} from "../../../src/language-support";
import { faker } from "@faker-js/faker";
import { getActivatedExtension } from "../global.helper";
import type { BaseLogger } from "../../../src/common/logging";
import { getQlPackForDbscheme } from "../../../src/databases/qlpack";
import { languageToDbScheme } from "../../../src/common/query-language";
/**
* Perform proper integration tests by running the CLI
*/
describe("Use cli", () => {
let cli: CodeQLCliServer;
let supportedLanguages: string[];
let logSpy: jest.SpiedFunction<BaseLogger["log"]>;
beforeEach(async () => {
const extension = await getActivatedExtension();
cli = extension.cliServer;
supportedLanguages = await cli.getSupportedLanguages();
logSpy = jest.spyOn(cli.logger, "log");
});
if (process.env.CLI_VERSION && process.env.CLI_VERSION !== "nightly") {
it("should have the correct version of the cli", async () => {
expect((await cli.getVersion()).toString()).toBe(
new SemVer(process.env.CLI_VERSION || "").toString(),
);
});
}
it("should resolve ram", async () => {
const result = await (cli as any).resolveRam(8192);
expect(result.length).toEqual(2);
expect(result[0]).toMatch(/^-J-Xmx\d+M$/);
expect(result[1]).toMatch(/^--off-heap-ram=\d+$/);
});
describe("silent logging", () => {
it("should log command output", async () => {
const queryDir = getOnDiskWorkspaceFolders()[0];
await cli.resolveQueries(queryDir);
expect(logSpy).toHaveBeenCalled();
});
it("shouldn't log command output if the `silent` flag is set", async () => {
const queryDir = getOnDiskWorkspaceFolders()[0];
const silent = true;
await cli.resolveQueries(queryDir, silent);
expect(logSpy).not.toHaveBeenCalled();
});
});
itWithCodeQL()("should resolve query packs", async () => {
const qlpacks = await cli.resolveQlpacks(getOnDiskWorkspaceFolders());
// Depending on the version of the CLI, the qlpacks may have different names
// (e.g. "codeql/javascript-all" vs "codeql-javascript"),
// so we just check that the expected languages are included.
for (const expectedLanguage of supportedLanguages) {
expect(Object.keys(qlpacks).includes(expectedLanguage));
}
});
itWithCodeQL()("should support the expected languages", async () => {
// Just check a few examples that definitely are/aren't supported.
expect(supportedLanguages).toEqual(
expect.arrayContaining(["go", "javascript", "python"]),
);
expect(supportedLanguages).not.toEqual(
expect.arrayContaining(["xml", "properties"]),
);
});
itWithCodeQL()("should resolve query by language", async () => {
const queryPath = join(__dirname, "data", "simple-javascript-query.ql");
const queryInfo: QueryInfoByLanguage = await cli.resolveQueryByLanguage(
getOnDiskWorkspaceFolders(),
Uri.file(queryPath),
);
expect(Object.keys(queryInfo.byLanguage)[0]).toEqual("javascript");
});
itWithCodeQL()(
"should resolve printAST queries for supported languages",
async () => {
for (let lang of supportedLanguages) {
if (lang === "go") {
// The codeql-go submodule is not available in the integration tests.
return;
}
if (lang === "actions") {
// The actions queries use the javascript dbscheme.
lang = "javascript";
}
console.log(`resolving printAST queries for ${lang}`);
const pack = await getQlPackForDbscheme(cli, languageToDbScheme[lang]);
expect(pack.dbschemePack).toContain(lang);
if (pack.dbschemePackIsLibraryPack) {
expect(pack.queryPack).toContain(lang);
}
const result = await resolveContextualQueries(
cli,
pack,
KeyType.PrintAstQuery,
);
// It doesn't matter what the name or path of the query is, only
// that we have found exactly one query.
expect(result.length).toBe(1);
}
},
);
describe("github authentication", () => {
itWithCodeQL()(
"should not use authentication if there are no credentials",
async () => {
const getSession = jest
.spyOn(authentication, "getSession")
.mockResolvedValue(undefined);
await cli.packDownload(["codeql/tutorial"]);
expect(getSession).toHaveBeenCalledTimes(1);
expect(getSession).toHaveBeenCalledWith(
"github",
expect.arrayContaining(["read:packages"]),
{
createIfNone: false,
},
);
},
);
itWithCodeQL()(
"should use authentication if there are credentials",
async () => {
const getSession = jest
.spyOn(authentication, "getSession")
.mockResolvedValue({
id: faker.string.uuid(),
accessToken: "gho_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
account: {
id: faker.string.uuid(),
label: "Account",
},
scopes: ["read:packages"],
});
await cli.packDownload(["codeql/tutorial"]);
expect(getSession).toHaveBeenCalledTimes(2);
expect(getSession).toHaveBeenCalledWith(
"github",
expect.arrayContaining(["read:packages"]),
{
createIfNone: false,
},
);
expect(getSession).toHaveBeenCalledWith(
"github",
expect.arrayContaining(["read:packages"]),
{
createIfNone: true,
},
);
},
);
});
});