pull out mockGetContents method
This commit is contained in:
parent
f77ab09bf4
commit
07caa0f5cf
3 changed files with 36 additions and 40 deletions
34
lib/config-utils.test.js
generated
34
lib/config-utils.test.js
generated
|
|
@ -31,6 +31,16 @@ function setInput(name, value) {
|
||||||
delete process.env[envVar];
|
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) => {
|
ava_1.default("load empty config", async (t) => {
|
||||||
return await util.withTmpDir(async (tmpDir) => {
|
return await util.withTmpDir(async (tmpDir) => {
|
||||||
process.env['RUNNER_TEMP'] = tmpDir;
|
process.env['RUNNER_TEMP'] = tmpDir;
|
||||||
|
|
@ -147,13 +157,9 @@ ava_1.default("API client used when reading remote config", async (t) => {
|
||||||
paths:
|
paths:
|
||||||
- c/d`;
|
- c/d`;
|
||||||
const dummyResponse = {
|
const dummyResponse = {
|
||||||
data: {
|
content: Buffer.from(inputFileContents).toString("base64"),
|
||||||
content: Buffer.from(inputFileContents).toString("base64"),
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let client = new github.GitHub('123');
|
const spyGetContents = mockGetContents(dummyResponse);
|
||||||
const spyGetContents = sinon_1.default.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
|
|
||||||
sinon_1.default.stub(api, "getApiClient").value(() => client);
|
|
||||||
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
||||||
await configUtils.loadConfig();
|
await configUtils.loadConfig();
|
||||||
t.assert(spyGetContents.called);
|
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) => {
|
return await util.withTmpDir(async (tmpDir) => {
|
||||||
process.env['RUNNER_TEMP'] = tmpDir;
|
process.env['RUNNER_TEMP'] = tmpDir;
|
||||||
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
||||||
const dummyResponse = {
|
const dummyResponse = []; // directories are returned as arrays
|
||||||
data: [],
|
mockGetContents(dummyResponse);
|
||||||
};
|
|
||||||
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 repoReference = 'octo-org/codeql-config/config.yaml@main';
|
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
||||||
setInput('config-file', repoReference);
|
setInput('config-file', repoReference);
|
||||||
try {
|
try {
|
||||||
|
|
@ -185,13 +187,9 @@ ava_1.default("Invalid format of remote config handled correctly", async (t) =>
|
||||||
process.env['RUNNER_TEMP'] = tmpDir;
|
process.env['RUNNER_TEMP'] = tmpDir;
|
||||||
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
||||||
const dummyResponse = {
|
const dummyResponse = {
|
||||||
data: {
|
// note no "content" property here
|
||||||
// note no "content" property here
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let client = new github.GitHub('123');
|
mockGetContents(dummyResponse);
|
||||||
sinon_1.default.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
|
|
||||||
sinon_1.default.stub(api, "getApiClient").value(() => client);
|
|
||||||
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
||||||
setInput('config-file', repoReference);
|
setInput('config-file', repoReference);
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
File diff suppressed because one or more lines are too long
|
|
@ -22,6 +22,19 @@ function setInput(name: string, value: string | undefined) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetContentsResponse = { content?: string; } | {}[];
|
||||||
|
|
||||||
|
function mockGetContents(content: GetContentsResponse): sinon.SinonStub<any, any> {
|
||||||
|
// 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.stub(client.repos, "getContents").resolves(response as any);
|
||||||
|
sinon.stub(api, "getApiClient").value(() => client);
|
||||||
|
return spyGetContents;
|
||||||
|
}
|
||||||
|
|
||||||
test("load empty config", async t => {
|
test("load empty config", async t => {
|
||||||
return await util.withTmpDir(async tmpDir => {
|
return await util.withTmpDir(async tmpDir => {
|
||||||
process.env['RUNNER_TEMP'] = tmpDir;
|
process.env['RUNNER_TEMP'] = tmpDir;
|
||||||
|
|
@ -161,14 +174,9 @@ test("API client used when reading remote config", async t => {
|
||||||
paths:
|
paths:
|
||||||
- c/d`;
|
- c/d`;
|
||||||
const dummyResponse = {
|
const dummyResponse = {
|
||||||
data: {
|
content: Buffer.from(inputFileContents).toString("base64"),
|
||||||
content: Buffer.from(inputFileContents).toString("base64"),
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
const spyGetContents = mockGetContents(dummyResponse);
|
||||||
let client = new github.GitHub('123');
|
|
||||||
const spyGetContents = sinon.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
|
|
||||||
sinon.stub(api, "getApiClient").value(() => client);
|
|
||||||
|
|
||||||
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
setInput('config-file', 'octo-org/codeql-config/config.yaml@main');
|
||||||
await configUtils.loadConfig();
|
await configUtils.loadConfig();
|
||||||
|
|
@ -181,13 +189,8 @@ test("Remote config handles the case where a directory is provided", async t =>
|
||||||
process.env['RUNNER_TEMP'] = tmpDir;
|
process.env['RUNNER_TEMP'] = tmpDir;
|
||||||
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
||||||
|
|
||||||
const dummyResponse = {
|
const dummyResponse = []; // directories are returned as arrays
|
||||||
data: [], // directories are returned as arrays
|
mockGetContents(dummyResponse);
|
||||||
};
|
|
||||||
|
|
||||||
let client = new github.GitHub('123');
|
|
||||||
sinon.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
|
|
||||||
sinon.stub(api, "getApiClient").value(() => client);
|
|
||||||
|
|
||||||
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
||||||
setInput('config-file', repoReference);
|
setInput('config-file', repoReference);
|
||||||
|
|
@ -206,14 +209,9 @@ test("Invalid format of remote config handled correctly", async t => {
|
||||||
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
process.env['GITHUB_WORKSPACE'] = tmpDir;
|
||||||
|
|
||||||
const dummyResponse = {
|
const dummyResponse = {
|
||||||
data: {
|
// note no "content" property here
|
||||||
// note no "content" property here
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
mockGetContents(dummyResponse);
|
||||||
let client = new github.GitHub('123');
|
|
||||||
sinon.stub(client.repos, "getContents").resolves(Promise.resolve(dummyResponse));
|
|
||||||
sinon.stub(api, "getApiClient").value(() => client);
|
|
||||||
|
|
||||||
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
const repoReference = 'octo-org/codeql-config/config.yaml@main';
|
||||||
setInput('config-file', repoReference);
|
setInput('config-file', repoReference);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue