mock API request in test

This commit is contained in:
Robert 2020-08-07 17:15:46 +01:00
parent 657540584e
commit d5c453c995
6 changed files with 34 additions and 6 deletions

View file

@ -36,6 +36,19 @@ function mockGetContents(content: GetContentsResponse): sinon.SinonStub<any, any
return spyGetContents;
}
function mockListLanguages(languages: string[]) {
// Passing an auth token is required, so we just use a dummy value
let client = new github.GitHub('123');
const response = {
data: {},
};
for (const language of languages) {
response.data[language] = 123;
}
sinon.stub(client.repos, "listLanguages").resolves(response as any);
sinon.stub(api, "getApiClient").value(() => client);
}
test("load empty config", async t => {
return await util.withTmpDir(async tmpDir => {
process.env['RUNNER_TEMP'] = tmpDir;
@ -348,6 +361,8 @@ test("No detected languages", async t => {
process.env['RUNNER_TEMP'] = tmpDir;
process.env['GITHUB_WORKSPACE'] = tmpDir;
mockListLanguages([]);
try {
await configUtils.initConfig();
throw new Error('initConfig did not throw error');

View file

@ -442,10 +442,10 @@ async function getLanguagesInRepo(): Promise<Language[]> {
let repo = repo_nwo[1];
core.debug(`GitHub repo ${owner} ${repo}`);
const response = await api.getApiClient(true).request("GET /repos/:owner/:repo/languages", ({
const response = await api.getApiClient(true).repos.listLanguages({
owner,
repo
}));
});
core.debug("Languages API response: " + JSON.stringify(response));