-
Notifications
You must be signed in to change notification settings - Fork 451
Expand file tree
/
Copy pathcodeql.test.ts
More file actions
298 lines (258 loc) · 8.71 KB
/
codeql.test.ts
File metadata and controls
298 lines (258 loc) · 8.71 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import * as github from "@actions/github";
import * as toolcache from "@actions/tool-cache";
import test from "ava";
import nock from "nock";
import * as path from "path";
import sinon from "sinon";
import * as api from "./api-client";
import * as codeql from "./codeql";
import * as defaults from "./defaults.json"; // Referenced from codeql-action-sync-tool!
import { Language } from "./languages";
import { getRunnerLogger } from "./logging";
import { setupTests } from "./testing-utils";
import * as util from "./util";
setupTests(test);
test("download and populate codeql bundle cache", async (t) => {
await util.withTmpDir(async (tmpDir) => {
const versions = ["20200601", "20200610"];
const languages: Language[][] = [
[Language.cpp],
[Language.cpp, Language.python], // Multi-language requires the full bundle
];
const platform =
process.platform === "win32"
? "win64"
: process.platform === "linux"
? "linux64"
: process.platform === "darwin"
? "osx64"
: undefined;
for (let i = 0; i < versions.length; i++) {
for (let j = 0; j < languages.length; j++) {
const version = versions[i];
const plVersion =
languages[j].length === 1
? `${platform}-${languages[j][0]}`
: undefined;
nock("https://example.com")
.get(`/download/codeql-bundle-${version}/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
`https://example.com/download/codeql-bundle-${version}/codeql-bundle.tar.gz`,
languages[j],
"token",
"https://github.example.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);
const toolcacheVersion = plVersion
? `0.0.0-${version}-${plVersion}`
: `0.0.0-${version}`;
t.assert(
toolcache.find("CodeQL", toolcacheVersion),
`Looking for ${toolcacheVersion}`
);
}
}
const cachedVersions = toolcache.findAllVersions("CodeQL");
// We should now have 4 cached versions: e.g.,
// 20200601, 20200601-linux64-cpp, 20200610, 20200610-linux64-cpp
t.is(cachedVersions.length, 4);
});
});
test("download small codeql bundle if analyzing only one language", async (t) => {
// Note: We do not specify a codeqlURL in this test, thus testing that
// the logic for constructing the URL takes into account the
// language being analyzed
await util.withTmpDir(async (tmpDir) => {
const languages: Language[][] = [
[Language.cpp],
[Language.cpp, Language.python], // Multi-language requires the full bundle
];
const platform =
process.platform === "win32"
? "win64"
: process.platform === "linux"
? "linux64"
: process.platform === "darwin"
? "osx64"
: undefined;
for (let i = 0; i < languages.length; i++) {
const plVersion =
languages[i].length === 1
? `${platform}-${languages[i][0]}`
: undefined;
const pkg = plVersion
? `codeql-bundle-${plVersion}.tar.gz`
: "codeql-bundle.tar.gz";
// Mock the API client
const client = new github.GitHub("123");
const response = {
data: {
assets: [
{
name: `codeql-bundle-${platform}-cpp.tar.gz`,
url: `https://github.example.com/url/codeql-bundle-${platform}-cpp.tar.gz`,
},
{
name: "codeql-bundle.tar.gz",
url: "https://github.example.com/url/codeql-bundle.tar.gz",
},
],
},
};
sinon.stub(client.repos, "getReleaseByTag").resolves(response as any);
sinon.stub(api, "getApiClient").value(() => client);
nock("https://github.example.com")
.get(`/url/${pkg}`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
undefined,
languages[i],
"token",
"https://github.example.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);
const parsedVersion = codeql.getCodeQLURLVersion(
`/${defaults.bundleVersion}/`,
getRunnerLogger(true)
);
const toolcacheVersion = plVersion
? `${parsedVersion}-${plVersion}`
: parsedVersion;
t.assert(
toolcache.find("CodeQL", toolcacheVersion),
`Looking for ${toolcacheVersion} - ${plVersion}`
);
}
const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 2);
});
});
test("use full codeql bundle cache if smaller bundle is not available", async (t) => {
// If we look for a platform-language version but find the full bundle in the cache,
// we use the full bundle
await util.withTmpDir(async (tmpDir) => {
const version = "20200601";
nock("https://example.com")
.get(`/download/codeql-bundle-${version}/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
`https://example.com/download/codeql-bundle-${version}/codeql-bundle.tar.gz`,
[],
"token",
"https://github.example.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);
t.assert(toolcache.find("CodeQL", `0.0.0-${version}`));
t.is(toolcache.findAllVersions("CodeQL").length, 1);
// Now try to request the cpp version, and see that we do not change the cache
await codeql.setupCodeQL(
`https://example.com/download/codeql-bundle-${version}/codeql-bundle.tar.gz`,
[Language.cpp],
"token",
"https://github.example.com",
tmpDir,
tmpDir,
"runner",
getRunnerLogger(true)
);
t.assert(toolcache.find("CodeQL", `0.0.0-${version}`));
t.is(toolcache.findAllVersions("CodeQL").length, 1);
});
});
test("use larger bundles if smaller ones are not released", async (t) => {
// Mock the API client
const client = new github.GitHub("123");
const response = {
data: {
assets: [{ name: "full-bundle", url: "url/file.gz" }],
},
};
const getReleaseByTagMock = sinon
.stub(client.repos, "getReleaseByTag")
.resolves(response as any);
sinon.stub(api, "getApiClient").value(() => client);
// Setting this env is required by a dependency of getCodeQLBundleDownloadURL
process.env["RUNNER_TEMP"] = "abc";
const codeqlURL = await codeql.getCodeQLBundleDownloadURL(
["small-bundle", "full-bundle"],
"",
"",
"actions",
getRunnerLogger(true)
);
t.deepEqual(codeqlURL, "url/file.gz");
t.assert(getReleaseByTagMock.called);
});
test("parse codeql bundle url version", (t) => {
const tests = {
"20200601": "0.0.0-20200601",
"20200601.0": "0.0.0-20200601.0",
"20200601.0.0": "20200601.0.0",
"1.2.3": "1.2.3",
"1.2.3-alpha": "1.2.3-alpha",
"1.2.3-beta.1": "1.2.3-beta.1",
"20200601-linux64-python": "0.0.0-20200601-linux64-python",
};
for (const [version, expectedVersion] of Object.entries(tests)) {
const url = `https://github.com/.../codeql-bundle-${version}/...`;
try {
const parsedVersion = codeql.getCodeQLURLVersion(
url,
getRunnerLogger(true)
);
t.deepEqual(parsedVersion, expectedVersion);
} catch (e) {
t.fail(e.message);
}
}
});
test("getExtraOptions works for explicit paths", (t) => {
t.deepEqual(codeql.getExtraOptions({}, ["foo"], []), []);
t.deepEqual(codeql.getExtraOptions({ foo: [42] }, ["foo"], []), ["42"]);
t.deepEqual(
codeql.getExtraOptions({ foo: { bar: [42] } }, ["foo", "bar"], []),
["42"]
);
});
test("getExtraOptions works for wildcards", (t) => {
t.deepEqual(codeql.getExtraOptions({ "*": [42] }, ["foo"], []), ["42"]);
});
test("getExtraOptions works for wildcards and explicit paths", (t) => {
const o1 = { "*": [42], foo: [87] };
t.deepEqual(codeql.getExtraOptions(o1, ["foo"], []), ["42", "87"]);
const o2 = { "*": [42], foo: [87] };
t.deepEqual(codeql.getExtraOptions(o2, ["foo", "bar"], []), ["42"]);
const o3 = { "*": [42], foo: { "*": [87], bar: [99] } };
const p = ["foo", "bar"];
t.deepEqual(codeql.getExtraOptions(o3, p, []), ["42", "87", "99"]);
});
test("getExtraOptions throws for bad content", (t) => {
t.throws(() => codeql.getExtraOptions({ "*": 42 }, ["foo"], []));
t.throws(() => codeql.getExtraOptions({ foo: 87 }, ["foo"], []));
t.throws(() =>
codeql.getExtraOptions(
{ "*": [42], foo: { "*": 87, bar: [99] } },
["foo", "bar"],
[]
)
);
});