Skip to content

Commit bc7d554

Browse files
committed
Add checkPacksForOverlayCompatibility()
1 parent f700215 commit bc7d554

File tree

2 files changed

+426
-3
lines changed

2 files changed

+426
-3
lines changed

src/init.test.ts

Lines changed: 282 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import * as fs from "fs";
22
import path from "path";
33

4-
import test from "ava";
4+
import test, { ExecutionContext } from "ava";
55

6-
import { cleanupDatabaseClusterDirectory } from "./init";
6+
import { ResolvePacksStep, setCodeQL } from "./codeql";
7+
import {
8+
checkPacksForOverlayCompatibility,
9+
cleanupDatabaseClusterDirectory,
10+
} from "./init";
11+
import { Language } from "./languages";
712
import {
813
LoggedMessage,
914
createTestConfig,
@@ -106,3 +111,278 @@ for (const { runnerEnv, ErrorConstructor, message } of [
106111
});
107112
});
108113
}
114+
115+
type PackInfo = {
116+
kind: "query" | "library";
117+
packinfoContents?: string;
118+
};
119+
120+
const testCheckPacksForOverlayCompatibility = test.macro({
121+
exec: async (
122+
t: ExecutionContext,
123+
_title: string,
124+
{
125+
cliOverlayVersion,
126+
languages,
127+
packs,
128+
expectedResult,
129+
}: {
130+
cliOverlayVersion: number | undefined;
131+
languages: Language[];
132+
packs: Record<string, PackInfo>;
133+
expectedResult: boolean;
134+
},
135+
) => {
136+
await withTmpDir(async (tmpDir) => {
137+
const byNameFound = {};
138+
const byNameAndVersionFound = {};
139+
const steps: ResolvePacksStep[] = [
140+
{
141+
type: "by-name",
142+
scans: [
143+
{
144+
paths: [],
145+
found: byNameFound,
146+
},
147+
],
148+
},
149+
{
150+
type: "by-name-and-version",
151+
found: byNameAndVersionFound,
152+
},
153+
];
154+
155+
for (const [packName, packInfo] of Object.entries(packs)) {
156+
const packPath = path.join(tmpDir, packName);
157+
fs.mkdirSync(packPath, { recursive: true });
158+
if (packInfo.packinfoContents) {
159+
fs.writeFileSync(
160+
path.join(packPath, ".packinfo"),
161+
packInfo.packinfoContents,
162+
);
163+
}
164+
if (packName.startsWith("codeql/")) {
165+
byNameFound[packName] = {
166+
kind: packInfo.kind,
167+
path: path.join(packPath, "qlpack.yml"),
168+
version: "1.0.0",
169+
};
170+
} else {
171+
byNameAndVersionFound[packName] = {
172+
"1.0.0": {
173+
kind: packInfo.kind,
174+
path: path.join(packPath, "qlpack.yml"),
175+
},
176+
};
177+
}
178+
}
179+
180+
const codeql = setCodeQL({
181+
getVersion: async () => ({
182+
version: "2.22.2",
183+
overlayVersion: cliOverlayVersion,
184+
}),
185+
resolvePacks: async () => ({
186+
steps,
187+
}),
188+
});
189+
190+
const messages: LoggedMessage[] = [];
191+
const result = await checkPacksForOverlayCompatibility(
192+
codeql,
193+
createTestConfig({ dbLocation: tmpDir, languages }),
194+
getRecordingLogger(messages),
195+
);
196+
t.is(result, expectedResult);
197+
t.deepEqual(
198+
messages.length,
199+
expectedResult ? 0 : 1,
200+
"Expected log messages",
201+
);
202+
});
203+
},
204+
title: (_, title) => `checkPacksForOverlayCompatibility: ${title}`,
205+
});
206+
207+
test(
208+
testCheckPacksForOverlayCompatibility,
209+
"returns false when CLI does not support overlay",
210+
{
211+
cliOverlayVersion: undefined,
212+
languages: [Language.java],
213+
packs: {
214+
"codeql/java-queries": {
215+
kind: "query",
216+
packinfoContents: '{"overlayVersion":2}',
217+
},
218+
},
219+
expectedResult: false,
220+
},
221+
);
222+
223+
test(
224+
testCheckPacksForOverlayCompatibility,
225+
"returns true when there are no bundled query packs",
226+
{
227+
cliOverlayVersion: 2,
228+
languages: [Language.java],
229+
packs: {
230+
"custom/queries": {
231+
kind: "query",
232+
packinfoContents: '{"overlayVersion":2}',
233+
},
234+
},
235+
expectedResult: true,
236+
},
237+
);
238+
239+
test(
240+
testCheckPacksForOverlayCompatibility,
241+
"returns true when bundled query pack has expected overlay version",
242+
{
243+
cliOverlayVersion: 2,
244+
languages: [Language.java],
245+
packs: {
246+
"codeql/java-queries": {
247+
kind: "query",
248+
packinfoContents: '{"overlayVersion":2}',
249+
},
250+
},
251+
expectedResult: true,
252+
},
253+
);
254+
255+
test(
256+
testCheckPacksForOverlayCompatibility,
257+
"returns false when bundled query pack has different overlay version",
258+
{
259+
cliOverlayVersion: 2,
260+
languages: [Language.java],
261+
packs: {
262+
"codeql/java-queries": {
263+
kind: "query",
264+
packinfoContents: '{"overlayVersion":1}',
265+
},
266+
},
267+
expectedResult: false,
268+
},
269+
);
270+
271+
test(
272+
testCheckPacksForOverlayCompatibility,
273+
"returns true when there are local library packs",
274+
{
275+
cliOverlayVersion: 2,
276+
languages: [Language.java],
277+
packs: {
278+
"codeql/java-queries": {
279+
kind: "query",
280+
packinfoContents: '{"overlayVersion":2}',
281+
},
282+
"custom/library": {
283+
kind: "library",
284+
},
285+
},
286+
expectedResult: true,
287+
},
288+
);
289+
290+
test(
291+
testCheckPacksForOverlayCompatibility,
292+
"returns true when local query pack has expected overlay version",
293+
{
294+
cliOverlayVersion: 2,
295+
languages: [Language.java],
296+
packs: {
297+
"codeql/java-queries": {
298+
kind: "query",
299+
packinfoContents: '{"overlayVersion":2}',
300+
},
301+
"custom/queries": {
302+
kind: "query",
303+
packinfoContents: '{"overlayVersion":2}',
304+
},
305+
},
306+
expectedResult: true,
307+
},
308+
);
309+
310+
test(
311+
testCheckPacksForOverlayCompatibility,
312+
"returns false when local query pack is missing .packinfo",
313+
{
314+
cliOverlayVersion: 2,
315+
languages: [Language.java],
316+
packs: {
317+
"codeql/java-queries": {
318+
kind: "query",
319+
packinfoContents: '{"overlayVersion":2}',
320+
},
321+
"custom/queries": {
322+
kind: "query",
323+
packinfoContents: undefined,
324+
},
325+
},
326+
expectedResult: false,
327+
},
328+
);
329+
330+
test(
331+
testCheckPacksForOverlayCompatibility,
332+
"returns false when local query pack has different overlay version",
333+
{
334+
cliOverlayVersion: 2,
335+
languages: [Language.java],
336+
packs: {
337+
"codeql/java-queries": {
338+
kind: "query",
339+
packinfoContents: '{"overlayVersion":2}',
340+
},
341+
"custom/queries": {
342+
kind: "query",
343+
packinfoContents: '{"overlayVersion":1}',
344+
},
345+
},
346+
expectedResult: false,
347+
},
348+
);
349+
350+
test(
351+
testCheckPacksForOverlayCompatibility,
352+
"returns false when local query pack is missing overlayVersion in .packinfo",
353+
{
354+
cliOverlayVersion: 2,
355+
languages: [Language.java],
356+
packs: {
357+
"codeql/java-queries": {
358+
kind: "query",
359+
packinfoContents: '{"overlayVersion":2}',
360+
},
361+
"custom/queries": {
362+
kind: "query",
363+
packinfoContents: '{"name":"some-pack"}',
364+
},
365+
},
366+
expectedResult: false,
367+
},
368+
);
369+
370+
test(
371+
testCheckPacksForOverlayCompatibility,
372+
"returns false when .packinfo is not valid JSON",
373+
{
374+
cliOverlayVersion: 2,
375+
languages: [Language.java],
376+
packs: {
377+
"codeql/java-queries": {
378+
kind: "query",
379+
packinfoContents: '{"overlayVersion":2}',
380+
},
381+
"custom/queries": {
382+
kind: "query",
383+
packinfoContents: "this_is_not_valid_json",
384+
},
385+
},
386+
expectedResult: false,
387+
},
388+
);

0 commit comments

Comments
 (0)