Improve readability of CodeQL bundle tests

This commit is contained in:
Henry Mercer
2022-08-16 15:38:55 +01:00
parent 96a8424f0c
commit d45b0eba23
3 changed files with 122 additions and 199 deletions

View File

@@ -9,17 +9,14 @@ import * as yaml from "js-yaml";
import nock from "nock";
import * as sinon from "sinon";
import { GitHubApiDetails } from "./api-client";
import * as codeql from "./codeql";
import { AugmentationProperties, Config } from "./config-utils";
import * as defaults from "./defaults.json";
import { createFeatureFlags, FeatureFlag } from "./feature-flags";
import { Language } from "./languages";
import { getRunnerLogger } from "./logging";
import {
setupTests,
setupActionsVars,
mockFeatureFlagApiEndpoint,
} from "./testing-utils";
import { setupTests, setupActionsVars } from "./testing-utils";
import * as util from "./util";
import { Mode, initializeEnvironment } from "./util";
@@ -68,6 +65,52 @@ test.beforeEach(() => {
};
});
async function mockApiAndSetupCodeQL({
version,
isPinned,
tmpDir,
toolsInput,
apiDetails,
}: {
version: string;
isPinned?: boolean;
tmpDir: string;
toolsInput?: { input?: string };
apiDetails?: GitHubApiDetails;
}) {
const platform =
process.platform === "win32"
? "win64"
: process.platform === "linux"
? "linux64"
: "osx64";
const baseUrl = apiDetails?.url ?? "https://example.com";
const relativeUrl = apiDetails
? `/github/codeql-action/releases/download/${version}/codeql-bundle-${platform}.tar.gz`
: `/download/codeql-bundle-${version}/codeql-bundle.tar.gz`;
nock(baseUrl)
.get(relativeUrl)
.replyWithFile(
200,
path.join(
__dirname,
`/../src/testdata/codeql-bundle${isPinned ? "-pinned" : ""}.tar.gz`
)
);
await codeql.setupCodeQL(
toolsInput ? toolsInput.input : `${baseUrl}${relativeUrl}`,
apiDetails ?? sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
}
test("download codeql bundle cache", async (t) => {
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
@@ -77,31 +120,11 @@ test("download codeql bundle cache", async (t) => {
for (let i = 0; i < versions.length; i++) {
const version = versions[i];
nock("https://example.com")
.get(`/download/codeql-bundle-${version}/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
mockFeatureFlagApiEndpoint(200, {});
await codeql.setupCodeQL(
`https://example.com/download/codeql-bundle-${version}/codeql-bundle.tar.gz`,
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
await mockApiAndSetupCodeQL({ version, tmpDir });
t.assert(toolcache.find("CodeQL", `0.0.0-${version}`));
}
const cachedVersions = toolcache.findAllVersions("CodeQL");
t.is(cachedVersions.length, 2);
t.is(toolcache.findAllVersions("CodeQL").length, 2);
});
});
@@ -109,42 +132,14 @@ test("download codeql bundle cache explicitly requested with pinned different ve
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle-pinned.tar.gz`)
);
await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
sampleApiDetails,
await mockApiAndSetupCodeQL({
version: "20200601",
isPinned: true,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
});
t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));
nock("https://example.com")
.get(`/download/codeql-bundle-20200610/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200610/codeql-bundle.tar.gz",
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
await mockApiAndSetupCodeQL({ version: "20200610", tmpDir });
t.assert(toolcache.find("CodeQL", "0.0.0-20200610"));
});
});
@@ -153,22 +148,11 @@ test("don't download codeql bundle cache with pinned different version cached",
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle-pinned.tar.gz`)
);
await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
sampleApiDetails,
await mockApiAndSetupCodeQL({
version: "20200601",
isPinned: true,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
});
t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));
@@ -192,49 +176,16 @@ test("download codeql bundle cache with different version cached (not pinned)",
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
sampleApiDetails,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
await mockApiAndSetupCodeQL({ version: "20200601", tmpDir });
t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));
const platform =
process.platform === "win32"
? "win64"
: process.platform === "linux"
? "linux64"
: "osx64";
nock("https://github.com")
.get(
`/github/codeql-action/releases/download/${defaults.bundleVersion}/codeql-bundle-${platform}.tar.gz`
)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
undefined,
sampleApiDetails,
await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
apiDetails: sampleApiDetails,
toolsInput: { input: undefined },
});
const cachedVersions = toolcache.findAllVersions("CodeQL");
@@ -246,50 +197,20 @@ test('download codeql bundle cache with pinned different version cached if "late
await util.withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
nock("https://example.com")
.get(`/download/codeql-bundle-20200601/codeql-bundle.tar.gz`)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle-pinned.tar.gz`)
);
await codeql.setupCodeQL(
"https://example.com/download/codeql-bundle-20200601/codeql-bundle.tar.gz",
sampleApiDetails,
await mockApiAndSetupCodeQL({
version: "20200601",
isPinned: true,
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
});
t.assert(toolcache.find("CodeQL", "0.0.0-20200601"));
const platform =
process.platform === "win32"
? "win64"
: process.platform === "linux"
? "linux64"
: "osx64";
nock("https://github.com")
.get(
`/github/codeql-action/releases/download/${defaults.bundleVersion}/codeql-bundle-${platform}.tar.gz`
)
.replyWithFile(
200,
path.join(__dirname, `/../src/testdata/codeql-bundle.tar.gz`)
);
await codeql.setupCodeQL(
"latest",
sampleApiDetails,
await mockApiAndSetupCodeQL({
version: defaults.bundleVersion,
apiDetails: sampleApiDetails,
toolsInput: { input: "latest" },
tmpDir,
util.GitHubVariant.DOTCOM,
createFeatureFlags([]),
getRunnerLogger(true),
false
);
});
const cachedVersions = toolcache.findAllVersions("CodeQL");