Improve CodeQL setup test structure and naming

This commit is contained in:
Henry Mercer 2023-01-09 18:18:52 +00:00
parent 166d98c19e
commit 9203e314a3
3 changed files with 153 additions and 132 deletions

102
lib/codeql.test.js generated
View file

@ -83,7 +83,7 @@ ava_1.default.beforeEach(() => {
trapCacheDownloadTime: 0, trapCacheDownloadTime: 0,
}; };
}); });
async function mockApiAndSetupCodeQL({ apiDetails, bypassToolcache, isPinned, tmpDir, toolsInput, version, }) { async function mockDownloadApi({ apiDetails = sampleApiDetails, isPinned, tagName, }) {
var _a; var _a;
const platform = process.platform === "win32" const platform = process.platform === "win32"
? "win64" ? "win64"
@ -92,97 +92,94 @@ async function mockApiAndSetupCodeQL({ apiDetails, bypassToolcache, isPinned, tm
: "osx64"; : "osx64";
const baseUrl = (_a = apiDetails === null || apiDetails === void 0 ? void 0 : apiDetails.url) !== null && _a !== void 0 ? _a : "https://example.com"; const baseUrl = (_a = apiDetails === null || apiDetails === void 0 ? void 0 : apiDetails.url) !== null && _a !== void 0 ? _a : "https://example.com";
const relativeUrl = apiDetails const relativeUrl = apiDetails
? `/github/codeql-action/releases/download/${version}/codeql-bundle-${platform}.tar.gz` ? `/github/codeql-action/releases/download/${tagName}/codeql-bundle-${platform}.tar.gz`
: `/download/codeql-bundle-${version}/codeql-bundle.tar.gz`; : `/download/${tagName}/codeql-bundle.tar.gz`;
(0, nock_1.default)(baseUrl) (0, nock_1.default)(baseUrl)
.get(relativeUrl) .get(relativeUrl)
.replyWithFile(200, path.join(__dirname, `/../src/testdata/codeql-bundle${isPinned ? "-pinned" : ""}.tar.gz`)); .replyWithFile(200, path.join(__dirname, `/../src/testdata/codeql-bundle${isPinned ? "-pinned" : ""}.tar.gz`));
return await codeql.setupCodeQL(toolsInput ? toolsInput.input : `${baseUrl}${relativeUrl}`, apiDetails !== null && apiDetails !== void 0 ? apiDetails : sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, !!bypassToolcache, (0, logging_1.getRunnerLogger)(true), false); return `${baseUrl}${relativeUrl}`;
} }
(0, ava_1.default)("download codeql bundle cache", async (t) => { async function installIntoToolcache({ apiDetails = sampleApiDetails, isPinned, tagName, tmpDir, }) {
const url = await mockDownloadApi({ apiDetails, isPinned, tagName });
await codeql.setupCodeQL(url, apiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
}
(0, ava_1.default)("downloads and caches explicitly requested bundles that aren't in the toolcache", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const versions = ["20200601", "20200610"]; const versions = ["20200601", "20200610"];
for (let i = 0; i < versions.length; i++) { for (let i = 0; i < versions.length; i++) {
const version = versions[i]; const version = versions[i];
const codeQLConfig = await mockApiAndSetupCodeQL({ version, tmpDir }); const url = await mockDownloadApi({
tagName: `codeql-bundle-${version}`,
isPinned: false,
});
const result = await codeql.setupCodeQL(url, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
t.assert(toolcache.find("CodeQL", `0.0.0-${version}`)); t.assert(toolcache.find("CodeQL", `0.0.0-${version}`));
t.deepEqual(codeQLConfig.toolsVersion, version); t.is(result.toolsVersion, version);
} }
t.is(toolcache.findAllVersions("CodeQL").length, 2); t.is(toolcache.findAllVersions("CodeQL").length, 2);
}); });
}); });
(0, ava_1.default)("download codeql bundle cache explicitly requested with pinned different version cached", async (t) => { (0, ava_1.default)("downloads an explicitly requested bundle even if a different version is cached", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const pinnedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); const url = await mockDownloadApi({
t.deepEqual(pinnedCodeQLConfig.toolsVersion, "20200601"); tagName: "codeql-bundle-20200610",
const unpinnedCodeQLConfig = await mockApiAndSetupCodeQL({
version: "20200610",
tmpDir,
}); });
const result = await codeql.setupCodeQL(url, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
t.assert(toolcache.find("CodeQL", "0.0.0-20200610")); t.assert(toolcache.find("CodeQL", "0.0.0-20200610"));
t.deepEqual(unpinnedCodeQLConfig.toolsVersion, "20200610"); t.deepEqual(result.toolsVersion, "20200610");
}); });
}); });
(0, ava_1.default)("don't download codeql bundle cache with pinned different version cached", async (t) => { (0, ava_1.default)("uses a cached bundle when no tools input is given", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const pinnedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); const result = await codeql.setupCodeQL(undefined, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
t.deepEqual(pinnedCodeQLConfig.toolsVersion, "20200601"); t.deepEqual(result.toolsVersion, "0.0.0-20200601");
const codeQLConfig = await codeql.setupCodeQL(undefined, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
t.deepEqual(codeQLConfig.toolsVersion, "0.0.0-20200601");
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 1); t.is(cachedVersions.length, 1);
}); });
}); });
(0, ava_1.default)("download codeql bundle cache with different version cached (not pinned)", async (t) => { (0, ava_1.default)("downloads bundle if only an unpinned version is cached", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const cachedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: false,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); await mockDownloadApi({
t.deepEqual(cachedCodeQLConfig.toolsVersion, "20200601"); tagName: defaults.bundleVersion,
const codeQLConfig = await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
tmpDir,
apiDetails: sampleApiDetails,
toolsInput: { input: undefined },
}); });
t.deepEqual(codeQLConfig.toolsVersion, defaults.bundleVersion.replace("codeql-bundle-", "")); const result = await codeql.setupCodeQL(undefined, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
t.deepEqual(result.toolsVersion, defaults.bundleVersion.replace("codeql-bundle-", ""));
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 2); t.is(cachedVersions.length, 2);
}); });
}); });
(0, ava_1.default)('download codeql bundle cache with pinned different version cached if "latest" tools specified', async (t) => { (0, ava_1.default)('downloads bundle if "latest" tools specified but not cached', async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
const pinnedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); await mockDownloadApi({
t.deepEqual(pinnedCodeQLConfig.toolsVersion, "20200601"); tagName: defaults.bundleVersion,
const latestCodeQLConfig = await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
apiDetails: sampleApiDetails,
toolsInput: { input: "latest" },
tmpDir,
}); });
t.deepEqual(latestCodeQLConfig.toolsVersion, defaults.bundleVersion.replace("codeql-bundle-", "")); const result = await codeql.setupCodeQL("latest", sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, false, (0, logging_1.getRunnerLogger)(true), false);
t.deepEqual(result.toolsVersion, defaults.bundleVersion.replace("codeql-bundle-", ""));
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 2); t.is(cachedVersions.length, 2);
}); });
@ -200,20 +197,15 @@ for (const [isFeatureEnabled, toolsInput, shouldToolcacheBeBypassed,] of TOOLCAC
(0, ava_1.default)(`download codeql bundle ${shouldToolcacheBeBypassed ? "bypasses" : "does not bypass"} toolcache when feature ${isFeatureEnabled ? "enabled" : "disabled"} and tools: ${toolsInput} passed`, async (t) => { (0, ava_1.default)(`download codeql bundle ${shouldToolcacheBeBypassed ? "bypasses" : "does not bypass"} toolcache when feature ${isFeatureEnabled ? "enabled" : "disabled"} and tools: ${toolsInput} passed`, async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
(0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir); (0, testing_utils_1.setupActionsVars)(tmpDir, tmpDir);
await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "codeql-bundle-20200601", tagName: "codeql-bundle-20200601",
apiDetails: sampleApiDetails,
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); await mockDownloadApi({
await mockApiAndSetupCodeQL({ tagName: defaults.bundleVersion,
version: defaults.bundleVersion,
apiDetails: sampleApiDetails,
bypassToolcache: isFeatureEnabled,
toolsInput: { input: toolsInput },
tmpDir,
}); });
await codeql.setupCodeQL(toolsInput, sampleApiDetails, tmpDir, util.GitHubVariant.DOTCOM, isFeatureEnabled, (0, logging_1.getRunnerLogger)(true), false);
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, shouldToolcacheBeBypassed ? 2 : 1); t.is(cachedVersions.length, shouldToolcacheBeBypassed ? 2 : 1);
}); });

File diff suppressed because one or more lines are too long

View file

@ -69,21 +69,15 @@ test.beforeEach(() => {
}; };
}); });
async function mockApiAndSetupCodeQL({ async function mockDownloadApi({
apiDetails, apiDetails = sampleApiDetails,
bypassToolcache,
isPinned, isPinned,
tmpDir, tagName,
toolsInput,
version,
}: { }: {
apiDetails?: GitHubApiDetails; apiDetails?: GitHubApiDetails;
bypassToolcache?: boolean;
isPinned?: boolean; isPinned?: boolean;
tmpDir: string; tagName: string;
toolsInput?: { input?: string }; }): Promise<string> {
version: string;
}): Promise<{ codeql: codeql.CodeQL; toolsVersion: string }> {
const platform = const platform =
process.platform === "win32" process.platform === "win32"
? "win64" ? "win64"
@ -93,8 +87,8 @@ async function mockApiAndSetupCodeQL({
const baseUrl = apiDetails?.url ?? "https://example.com"; const baseUrl = apiDetails?.url ?? "https://example.com";
const relativeUrl = apiDetails const relativeUrl = apiDetails
? `/github/codeql-action/releases/download/${version}/codeql-bundle-${platform}.tar.gz` ? `/github/codeql-action/releases/download/${tagName}/codeql-bundle-${platform}.tar.gz`
: `/download/codeql-bundle-${version}/codeql-bundle.tar.gz`; : `/download/${tagName}/codeql-bundle.tar.gz`;
nock(baseUrl) nock(baseUrl)
.get(relativeUrl) .get(relativeUrl)
@ -106,18 +100,33 @@ async function mockApiAndSetupCodeQL({
) )
); );
return await codeql.setupCodeQL( return `${baseUrl}${relativeUrl}`;
toolsInput ? toolsInput.input : `${baseUrl}${relativeUrl}`, }
apiDetails ?? sampleApiDetails,
async function installIntoToolcache({
apiDetails = sampleApiDetails,
isPinned,
tagName,
tmpDir,
}: {
apiDetails?: GitHubApiDetails;
isPinned: boolean;
tagName: string;
tmpDir: string;
}) {
const url = await mockDownloadApi({ apiDetails, isPinned, tagName });
await codeql.setupCodeQL(
url,
apiDetails,
tmpDir, tmpDir,
util.GitHubVariant.DOTCOM, util.GitHubVariant.DOTCOM,
!!bypassToolcache, false,
getRunnerLogger(true), getRunnerLogger(true),
false false
); );
} }
test("download codeql bundle cache", async (t) => { test("downloads and caches explicitly requested bundles that aren't in the toolcache", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir); setupActionsVars(tmpDir, tmpDir);
@ -126,50 +135,65 @@ test("download codeql bundle cache", async (t) => {
for (let i = 0; i < versions.length; i++) { for (let i = 0; i < versions.length; i++) {
const version = versions[i]; const version = versions[i];
const codeQLConfig = await mockApiAndSetupCodeQL({ version, tmpDir }); const url = await mockDownloadApi({
tagName: `codeql-bundle-${version}`,
isPinned: false,
});
const result = await codeql.setupCodeQL(
url,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
false,
getRunnerLogger(true),
false
);
t.assert(toolcache.find("CodeQL", `0.0.0-${version}`)); t.assert(toolcache.find("CodeQL", `0.0.0-${version}`));
t.deepEqual(codeQLConfig.toolsVersion, version); t.is(result.toolsVersion, version);
} }
t.is(toolcache.findAllVersions("CodeQL").length, 2); t.is(toolcache.findAllVersions("CodeQL").length, 2);
}); });
}); });
test("download codeql bundle cache explicitly requested with pinned different version cached", async (t) => { test("downloads an explicitly requested bundle even if a different version is cached", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir); setupActionsVars(tmpDir, tmpDir);
const pinnedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));
t.deepEqual(pinnedCodeQLConfig.toolsVersion, "20200601");
const unpinnedCodeQLConfig = await mockApiAndSetupCodeQL({ const url = await mockDownloadApi({
version: "20200610", tagName: "codeql-bundle-20200610",
tmpDir,
}); });
const result = await codeql.setupCodeQL(
url,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
false,
getRunnerLogger(true),
false
);
t.assert(toolcache.find("CodeQL", "0.0.0-20200610")); t.assert(toolcache.find("CodeQL", "0.0.0-20200610"));
t.deepEqual(unpinnedCodeQLConfig.toolsVersion, "20200610"); t.deepEqual(result.toolsVersion, "20200610");
}); });
}); });
test("don't download codeql bundle cache with pinned different version cached", async (t) => { test("uses a cached bundle when no tools input is given", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir); setupActionsVars(tmpDir, tmpDir);
const pinnedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); const result = await codeql.setupCodeQL(
t.deepEqual(pinnedCodeQLConfig.toolsVersion, "20200601");
const codeQLConfig = await codeql.setupCodeQL(
undefined, undefined,
sampleApiDetails, sampleApiDetails,
tmpDir, tmpDir,
@ -178,69 +202,73 @@ test("don't download codeql bundle cache with pinned different version cached",
getRunnerLogger(true), getRunnerLogger(true),
false false
); );
t.deepEqual(codeQLConfig.toolsVersion, "0.0.0-20200601"); t.deepEqual(result.toolsVersion, "0.0.0-20200601");
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 1); t.is(cachedVersions.length, 1);
}); });
}); });
test("download codeql bundle cache with different version cached (not pinned)", async (t) => { test("downloads bundle if only an unpinned version is cached", async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir); setupActionsVars(tmpDir, tmpDir);
const cachedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: false,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); await mockDownloadApi({
t.deepEqual(cachedCodeQLConfig.toolsVersion, "20200601"); tagName: defaults.bundleVersion,
const codeQLConfig = await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
tmpDir,
apiDetails: sampleApiDetails,
toolsInput: { input: undefined },
}); });
const result = await codeql.setupCodeQL(
undefined,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
false,
getRunnerLogger(true),
false
);
t.deepEqual( t.deepEqual(
codeQLConfig.toolsVersion, result.toolsVersion,
defaults.bundleVersion.replace("codeql-bundle-", "") defaults.bundleVersion.replace("codeql-bundle-", "")
); );
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 2); t.is(cachedVersions.length, 2);
}); });
}); });
test('download codeql bundle cache with pinned different version cached if "latest" tools specified', async (t) => { test('downloads bundle if "latest" tools specified but not cached', async (t) => {
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir); setupActionsVars(tmpDir, tmpDir);
const pinnedCodeQLConfig = await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "20200601", tagName: "codeql-bundle-20200601",
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); await mockDownloadApi({
t.deepEqual(pinnedCodeQLConfig.toolsVersion, "20200601"); tagName: defaults.bundleVersion,
const latestCodeQLConfig = await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
apiDetails: sampleApiDetails,
toolsInput: { input: "latest" },
tmpDir,
}); });
const result = await codeql.setupCodeQL(
"latest",
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
false,
getRunnerLogger(true),
false
);
t.deepEqual( t.deepEqual(
latestCodeQLConfig.toolsVersion, result.toolsVersion,
defaults.bundleVersion.replace("codeql-bundle-", "") defaults.bundleVersion.replace("codeql-bundle-", "")
); );
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 2); t.is(cachedVersions.length, 2);
}); });
}); });
@ -270,25 +298,26 @@ for (const [
await util.withTmpDir(async (tmpDir) => { await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir); setupActionsVars(tmpDir, tmpDir);
await mockApiAndSetupCodeQL({ await installIntoToolcache({
version: "codeql-bundle-20200601", tagName: "codeql-bundle-20200601",
apiDetails: sampleApiDetails,
isPinned: true, isPinned: true,
tmpDir, tmpDir,
}); });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601")); await mockDownloadApi({
tagName: defaults.bundleVersion,
await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
apiDetails: sampleApiDetails,
bypassToolcache: isFeatureEnabled,
toolsInput: { input: toolsInput },
tmpDir,
}); });
await codeql.setupCodeQL(
toolsInput,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
isFeatureEnabled,
getRunnerLogger(true),
false
);
const cachedVersions = toolcache.findAllVersions("CodeQL"); const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, shouldToolcacheBeBypassed ? 2 : 1); t.is(cachedVersions.length, shouldToolcacheBeBypassed ? 2 : 1);
}); });
}); });