Skip to content

Commit

Permalink
Add test for CodeQL version appearing in log.
Browse files Browse the repository at this point in the history
  • Loading branch information
NlightNFotis committed May 9, 2024
1 parent 67b9169 commit 3a2da5f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 48 deletions.
8 changes: 4 additions & 4 deletions lib/setup-codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.js.map

Large diffs are not rendered by default.

45 changes: 28 additions & 17 deletions lib/setup-codeql.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/setup-codeql.test.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 41 additions & 22 deletions src/setup-codeql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,53 +98,72 @@ test("getCodeQLSource sets CLI version for a semver tagged bundle", async (t) =>
});

test("getCodeQLSource correctly returns bundled CLI version when tools == linked", async (t) => {
const loggedMessages: LoggedMessage[] = [];
const logger = getRecordingLogger(loggedMessages);

await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
const source = await setupCodeql.getCodeQLSource(
"linked",
SAMPLE_DEFAULT_CLI_VERSION,
SAMPLE_DOTCOM_API_DETAILS,
GitHubVariant.DOTCOM,
logger,
getRunnerLogger(true),
);

// Assert first that we got the right version of the CodeQL CLI,
// and that we're sourcing it using the correct method for that.
t.is(source.toolsVersion, LINKED_CLI_VERSION.cliVersion);
t.is(source.sourceType, "download");

// Ensure that we're adequately notifying the user of the version we're using.
const expected_message: LoggedMessage = {
type: "info",
message: `Using CodeQL CLI version: ${LINKED_CLI_VERSION.cliVersion} from download.`,
};

loggedMessages.forEach((msg) => {
console.log(msg.message);
});

t.assert(loggedMessages.includes(expected_message));
});
});

test("getCodeQLSource correctly returns bundled CLI version when tools == latest", async (t) => {
const loggedMessages = [];
const logger = getRecordingLogger(loggedMessages);

await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
const source = await setupCodeql.getCodeQLSource(
"latest",
SAMPLE_DEFAULT_CLI_VERSION,
SAMPLE_DOTCOM_API_DETAILS,
GitHubVariant.DOTCOM,
logger,
getRunnerLogger(true),
);

t.is(source.toolsVersion, LINKED_CLI_VERSION.cliVersion);
t.is(source.sourceType, "download");
});
});

test("setupCodeQLBundle logs the CodeQL CLI version being used", async (t) => {
const loggedMessages: LoggedMessage[] = [];
const logger = getRecordingLogger(loggedMessages);

// Stub the downloadCodeQL function to prevent downloading artefacts
// during testing from being called.
sinon.stub(setupCodeql, "downloadCodeQL").resolves({
toolsVersion: LINKED_CLI_VERSION.cliVersion,
codeqlFolder: "codeql",
toolsDownloadDurationMs: 200,
});

await withTmpDir(async (tmpDir) => {
setupActionsVars(tmpDir, tmpDir);
const result = await setupCodeql.setupCodeQLBundle(
"linked",
SAMPLE_DOTCOM_API_DETAILS,
"tmp/codeql_action_test/",
GitHubVariant.DOTCOM,
SAMPLE_DEFAULT_CLI_VERSION,
logger,
);

// Basic sanity check that the version we got back is indeed
// the linked (default) CLI version.
t.is(result.toolsVersion, LINKED_CLI_VERSION.cliVersion);

const expected_message: LoggedMessage = {
type: "info",
message: `Using CodeQL CLI version ${LINKED_CLI_VERSION.cliVersion} from download.`,
};

// Ensure message logging CodeQL CLI version was present in user logs.
t.assert(
loggedMessages.some((msg) => msg.message === expected_message.message),
);
});
});
8 changes: 5 additions & 3 deletions src/setup-codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export async function tryGetFallbackToolcacheVersion(
return fallbackVersion;
}

export async function downloadCodeQL(
export const downloadCodeQL = async function (
codeqlURL: string,
maybeBundleVersion: string | undefined,
maybeCliVersion: string | undefined,
Expand Down Expand Up @@ -614,7 +614,7 @@ export async function downloadCodeQL(
codeqlFolder: toolcachedBundlePath,
toolsDownloadDurationMs,
};
}
};

export function getCodeQLURLVersion(url: string): string {
const match = url.match(/\/codeql-bundle-(.*)\//);
Expand Down Expand Up @@ -692,7 +692,9 @@ export async function setupCodeQLBundle(
logger,
);

logger.info("Using CodeQL CLI version " + source.toolsVersion + " from " + source.sourceType + ".");
logger.info(
`Using CodeQL CLI version ${source.toolsVersion} from ${source.sourceType}.`,
);

let codeqlFolder: string;
let toolsVersion = source.toolsVersion;
Expand Down

0 comments on commit 3a2da5f

Please sign in to comment.