pull out mockGetContents method

This commit is contained in:
Robert Brignull
2020-07-07 18:32:29 +01:00
parent f77ab09bf4
commit 07caa0f5cf
3 changed files with 36 additions and 40 deletions

View File

@@ -31,6 +31,16 @@ function setInput(name, value) {
delete process.env[envVar];
}
}
function mockGetContents(content) {
// Passing an auth token is required, so we just use a dummy value
let client = new github.GitHub('123');
const response = {
data: content
};
const spyGetContents = sinon_1.default.stub(client.repos, "getContents").resolves(response);
sinon_1.default.stub(api, "getApiClient").value(() => client);
return spyGetContents;
}
ava_1.default("load empty config", async (t) => {
return await util.withTmpDir(async (tmpDir) => {
process.env['RUNNER_TEMP'] = tmpDir;
@@ -147,13 +157,9 @@ ava_1.default("API client used when reading remote config", async (t) => {
paths:
- c/d`;
const dummyResponse = {
data: {
content: Buffer.from(inputFileContents).toString("base64"),
}
content: Buffer.from(inputFileContents).toString("base64"),
};
let client = new github.GitHub('123');
const spyGetContents = sinon_1.default.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
sinon_1.default.stub(api, "getApiClient").value(() => client);
const spyGetContents = mockGetContents(dummyResponse);
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
await configUtils.loadConfig();
t.assert(spyGetContents.called);
@@ -163,12 +169,8 @@ ava_1.default("Remote config handles the case where a directory is provided", as
return await util.withTmpDir(async (tmpDir) => {
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;
const dummyResponse = {
data: [],
};
let client = new github.GitHub('123');
sinon_1.default.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
sinon_1.default.stub(api, "getApiClient").value(() => client);
const dummyResponse = []; // directories are returned as arrays
mockGetContents(dummyResponse);
const repoReference = 'octo-org/codeql-config/config.yaml@main';
setInput('config-file', repoReference);
try {
@@ -185,13 +187,9 @@ ava_1.default("Invalid format of remote config handled correctly", async (t) =>
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;
const dummyResponse = {
data: {
// note no "content" property here
}
// note no "content" property here
};
let client = new github.GitHub('123');
sinon_1.default.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
sinon_1.default.stub(api, "getApiClient").value(() => client);
mockGetContents(dummyResponse);
const repoReference = 'octo-org/codeql-config/config.yaml@main';
setInput('config-file', repoReference);
try {