Upgrade Ava to v4

This commit is contained in:
Henry Mercer 2022-02-01 18:01:11 +00:00
parent 9a40cc5274
commit ce89f1b611
1153 changed files with 27264 additions and 95308 deletions

View file

@ -172,7 +172,10 @@ test("loading config saves config", async (t) => {
// And that same newly-initialised config should now be returned by getConfig
const config2 = await configUtils.getConfig(tmpDir, logger);
t.deepEqual(config1, config2);
t.not(config2, undefined);
if (config2 !== undefined) {
t.deepEqual(config1, config2);
}
});
});
@ -1428,62 +1431,67 @@ test("path sanitisation", (t) => {
/**
* Test macro for ensuring the packs block is valid
*/
function parsePacksMacro(
t: ExecutionContext<unknown>,
packsByLanguage: string[] | Record<string, string[]>,
languages: Language[],
expected
) {
t.deepEqual(
configUtils.parsePacksFromConfig(packsByLanguage, languages, "/a/b"),
expected
);
}
parsePacksMacro.title = (providedTitle: string) =>
`Parse Packs: ${providedTitle}`;
const parsePacksMacro = test.macro({
exec: (
t: ExecutionContext<unknown>,
packsByLanguage: string[] | Record<string, string[]>,
languages: Language[],
expected: Partial<Record<Language, configUtils.PackWithVersion[]>>
) =>
t.deepEqual(
configUtils.parsePacksFromConfig(packsByLanguage, languages, "/a/b"),
expected
),
title: (providedTitle = "") => `Parse Packs: ${providedTitle}`,
});
/**
* Test macro for testing when the packs block is invalid
*/
function parsePacksErrorMacro(
t: ExecutionContext<unknown>,
packsByLanguage,
languages: Language[],
expected: RegExp
) {
t.throws(
() => {
configUtils.parsePacksFromConfig(packsByLanguage, languages, "/a/b");
},
{
message: expected,
}
);
}
parsePacksErrorMacro.title = (providedTitle: string) =>
`Parse Packs Error: ${providedTitle}`;
const parsePacksErrorMacro = test.macro({
exec: (
t: ExecutionContext<unknown>,
packsByLanguage: unknown,
languages: Language[],
expected: RegExp
) =>
t.throws(
() =>
configUtils.parsePacksFromConfig(
packsByLanguage as string[] | Record<string, string[]>,
languages,
"/a/b"
),
{
message: expected,
}
),
title: (providedTitle = "") => `Parse Packs Error: ${providedTitle}`,
});
/**
* Test macro for testing when the packs block is invalid
*/
function invalidPackNameMacro(t: ExecutionContext<unknown>, name: string) {
parsePacksErrorMacro(
t,
{ [Language.cpp]: [name] },
[Language.cpp],
new RegExp(
`The configuration file "/a/b" is invalid: property "packs" "${name}" is not a valid pack`
)
);
}
invalidPackNameMacro.title = (_: string, arg: string) =>
`Invalid pack string: ${arg}`;
const invalidPackNameMacro = test.macro({
exec: (t: ExecutionContext, name: string) =>
parsePacksErrorMacro.exec(
t,
{ [Language.cpp]: [name] },
[Language.cpp],
new RegExp(
`The configuration file "/a/b" is invalid: property "packs" "${name}" is not a valid pack`
)
),
title: (_providedTitle: string | undefined, arg: string | undefined) =>
`Invalid pack string: ${arg}`,
});
test("no packs", parsePacksMacro, {}, [], {});
test("two packs", parsePacksMacro, ["a/b", "c/d@1.2.3"], [Language.cpp], {
[Language.cpp]: [
{ packName: "a/b", version: undefined },
{ packName: "c/d", version: clean("1.2.3") },
{ packName: "c/d", version: clean("1.2.3") as string },
],
});
test(
@ -1494,7 +1502,7 @@ test(
{
[Language.cpp]: [
{ packName: "a/b", version: undefined },
{ packName: "c/d", version: clean("1.2.3") },
{ packName: "c/d", version: clean("1.2.3") as string },
],
}
);
@ -1509,11 +1517,11 @@ test(
{
[Language.cpp]: [
{ packName: "a/b", version: undefined },
{ packName: "c/d", version: clean("1.2.3") },
{ packName: "c/d", version: clean("1.2.3") as string },
],
[Language.java]: [
{ packName: "d/e", version: undefined },
{ packName: "f/g", version: clean("1.2.3") },
{ packName: "f/g", version: clean("1.2.3") as string },
],
}
);
@ -1700,84 +1708,85 @@ test(
/"xxx" is not a valid pack/
);
async function mlPoweredQueriesMacro(
t: ExecutionContext,
codeQLVersion: string,
isMlPoweredQueriesFlagEnabled: boolean,
queriesInput: string | undefined,
shouldRunMlPoweredQueries: boolean
) {
return await util.withTmpDir(async (tmpDir) => {
const codeQL = setCodeQL({
async getVersion() {
return codeQLVersion;
},
async resolveQueries() {
return {
byLanguage: {
javascript: { "fake-query.ql": {} },
},
noDeclaredLanguage: {},
multipleDeclaredLanguages: {},
};
},
});
const { packs } = await configUtils.initConfig(
"javascript",
queriesInput,
undefined,
undefined,
undefined,
false,
"",
"",
{ owner: "github", repo: "example " },
tmpDir,
tmpDir,
codeQL,
tmpDir,
gitHubVersion,
sampleApiDetails,
createFeatureFlags(
isMlPoweredQueriesFlagEnabled
? [FeatureFlag.MlPoweredQueriesEnabled]
: []
),
getRunnerLogger(true)
);
if (shouldRunMlPoweredQueries) {
t.deepEqual(packs as unknown, {
[Language.javascript]: [
{
packName: "codeql/javascript-experimental-atm-queries",
version: "~0.0.2",
},
],
const mlPoweredQueriesMacro = test.macro({
exec: async (
t: ExecutionContext,
codeQLVersion: string,
isMlPoweredQueriesFlagEnabled: boolean,
queriesInput: string | undefined,
shouldRunMlPoweredQueries: boolean
) => {
return await util.withTmpDir(async (tmpDir) => {
const codeQL = setCodeQL({
async getVersion() {
return codeQLVersion;
},
async resolveQueries() {
return {
byLanguage: {
javascript: { "fake-query.ql": {} },
},
noDeclaredLanguage: {},
multipleDeclaredLanguages: {},
};
},
});
} else {
t.deepEqual(packs as unknown, {});
}
});
}
mlPoweredQueriesMacro.title = (
_providedTitle: string,
codeQLVersion: string,
isMlPoweredQueriesFlagEnabled: boolean,
queriesInput: string | undefined,
shouldRunMlPoweredQueries: boolean
) => {
const queriesInputDescription = queriesInput
? `'queries: ${queriesInput}'`
: "default config";
const { packs } = await configUtils.initConfig(
"javascript",
queriesInput,
undefined,
undefined,
undefined,
false,
"",
"",
{ owner: "github", repo: "example " },
tmpDir,
tmpDir,
codeQL,
tmpDir,
gitHubVersion,
sampleApiDetails,
createFeatureFlags(
isMlPoweredQueriesFlagEnabled
? [FeatureFlag.MlPoweredQueriesEnabled]
: []
),
getRunnerLogger(true)
);
if (shouldRunMlPoweredQueries) {
t.deepEqual(packs as unknown, {
[Language.javascript]: [
{
packName: "codeql/javascript-experimental-atm-queries",
version: "~0.0.2",
},
],
});
} else {
t.deepEqual(packs as unknown, {});
}
});
},
title: (
_providedTitle: string | undefined,
codeQLVersion: string,
isMlPoweredQueriesFlagEnabled: boolean,
queriesInput: string | undefined,
shouldRunMlPoweredQueries: boolean
) => {
const queriesInputDescription = queriesInput
? `'queries: ${queriesInput}'`
: "default config";
return `ML-powered queries ${
shouldRunMlPoweredQueries ? "are" : "aren't"
} loaded for ${queriesInputDescription} using CLI v${codeQLVersion} when feature flag is ${
isMlPoweredQueriesFlagEnabled ? "enabled" : "disabled"
}`;
};
return `ML-powered queries ${
shouldRunMlPoweredQueries ? "are" : "aren't"
} loaded for ${queriesInputDescription} using CLI v${codeQLVersion} when feature flag is ${
isMlPoweredQueriesFlagEnabled ? "enabled" : "disabled"
}`;
},
});
// macro, isMlPoweredQueriesFlagEnabled, queriesInput, shouldRunMlPoweredQueries
test(mlPoweredQueriesMacro, "2.7.4", true, "security-extended", false);

View file

@ -141,14 +141,14 @@ for (const featureFlag of FEATURE_FLAGS) {
getRunnerLogger(true)
);
const expectedFeatureFlags = {};
const expectedFeatureFlags: { [flag: string]: boolean } = {};
for (const f of FEATURE_FLAGS) {
expectedFeatureFlags[f] = false;
}
expectedFeatureFlags[featureFlag] = true;
mockFeatureFlagApiEndpoint(200, expectedFeatureFlags);
const actualFeatureFlags = {
const actualFeatureFlags: { [flag: string]: boolean } = {
database_uploads_enabled: await featureFlags.getValue(
FeatureFlag.DatabaseUploadsEnabled
),

View file

@ -1,5 +1,5 @@
import * as github from "@actions/github";
import { TestInterface } from "ava";
import { TestFn } from "ava";
import * as sinon from "sinon";
import * as apiClient from "./api-client";
@ -46,8 +46,8 @@ function wrapOutput(context: TestContext) {
};
}
export function setupTests(test: TestInterface<any>) {
const typedTest = test as TestInterface<TestContext>;
export function setupTests(test: TestFn<any>) {
const typedTest = test as TestFn<TestContext>;
typedTest.beforeEach((t) => {
// Set an empty CodeQL object so that all method calls will fail

View file

@ -187,10 +187,13 @@ test("concatTracerConfigs - conflicting env vars", async (t) => {
config
)
);
t.deepEqual(
e.message,
"Incompatible values in environment parameter b: b and c"
);
// If e is undefined, then the previous assertion will fail.
if (e !== undefined) {
t.deepEqual(
e.message,
"Incompatible values in environment parameter b: b and c"
);
}
});
});

View file

@ -26,7 +26,7 @@ test("getMemoryFlag() should return the correct --ram flag", (t) => {
const totalMem = Math.floor(os.totalmem() / (1024 * 1024));
const expectedThreshold = process.platform === "win32" ? 1536 : 1024;
const tests = [
const tests: Array<[string | undefined, string]> = [
[undefined, `--ram=${totalMem - expectedThreshold}`],
["", `--ram=${totalMem - expectedThreshold}`],
["512", "--ram=512"],
@ -57,7 +57,7 @@ test("getAddSnippetsFlag() should return the correct flag", (t) => {
test("getThreadsFlag() should return the correct --threads flag", (t) => {
const numCpus = os.cpus().length;
const tests = [
const tests: Array<[string | undefined, string]> = [
["0", "--threads=0"],
["1", "--threads=1"],
[undefined, `--threads=${numCpus}`],
@ -213,7 +213,10 @@ test("getGitHubVersion", async (t) => {
auth: "",
url: "https://ghe.example.com",
});
t.deepEqual({ type: util.GitHubVariant.GHES, version: "2.0" }, v2);
t.deepEqual(
{ type: util.GitHubVariant.GHES, version: "2.0" } as util.GitHubVersion,
v2
);
mockGetMetaVersionHeader("GitHub AE");
const ghae = await util.getGitHubVersion({