Skip to content

Commit ac1a97e

Browse files
committed
Refactor databaseFetcher tests to not use proxyquire
1 parent 8d5067f commit ac1a97e

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

extensions/ql-vscode/src/vscode-tests/no-workspace/databaseFetcher.test.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ import { expect } from 'chai';
66
import { window } from 'vscode';
77

88
import {
9+
convertGithubNwoToDatabaseUrl,
910
convertLgtmUrlToDatabaseUrl,
1011
looksLikeLgtmUrl,
1112
findDirWithFile,
1213
looksLikeGithubRepo,
1314
} from '../../databaseFetcher';
1415
import { ProgressCallback } from '../../commandRunner';
15-
import * as pq from 'proxyquire';
16-
17-
const proxyquire = pq.noPreserveCache();
16+
import * as Octokit from '@octokit/rest';
1817

1918
describe('databaseFetcher', function() {
2019
// These tests make API calls and may need extra time to complete.
@@ -25,20 +24,16 @@ describe('databaseFetcher', function() {
2524
let quickPickSpy: sinon.SinonStub;
2625
let progressSpy: ProgressCallback;
2726
let mockRequest: sinon.SinonStub;
28-
let mod: any;
29-
30-
const credentials = getMockCredentials(0);
27+
let octokit: Octokit.Octokit;
3128

3229
beforeEach(() => {
3330
sandbox = sinon.createSandbox();
3431
quickPickSpy = sandbox.stub(window, 'showQuickPick');
3532
progressSpy = sandbox.spy();
3633
mockRequest = sandbox.stub();
37-
mod = proxyquire('../../databaseFetcher', {
38-
'./authentication': {
39-
Credentials: credentials,
40-
},
41-
});
34+
octokit = ({
35+
request: mockRequest,
36+
}) as unknown as Octokit.Octokit;
4237
});
4338

4439
afterEach(() => {
@@ -90,11 +85,17 @@ describe('databaseFetcher', function() {
9085
mockRequest.resolves(mockApiResponse);
9186
quickPickSpy.resolves('javascript');
9287
const githubRepo = 'github/codeql';
93-
const { databaseUrl, name, owner } = await mod.convertGithubNwoToDatabaseUrl(
88+
const result = await convertGithubNwoToDatabaseUrl(
9489
githubRepo,
95-
credentials,
90+
octokit,
9691
progressSpy
9792
);
93+
expect(result).not.to.be.undefined;
94+
if (result === undefined) {
95+
return;
96+
}
97+
98+
const { databaseUrl, name, owner } = result;
9899

99100
expect(databaseUrl).to.equal(
100101
'https://api.github.com/repos/github/codeql/code-scanning/codeql/databases/javascript'
@@ -119,7 +120,7 @@ describe('databaseFetcher', function() {
119120
mockRequest.resolves(mockApiResponse);
120121
const githubRepo = 'foo/bar-not-real';
121122
await expect(
122-
mod.convertGithubNwoToDatabaseUrl(githubRepo, credentials, progressSpy)
123+
convertGithubNwoToDatabaseUrl(githubRepo, octokit, progressSpy)
123124
).to.be.rejectedWith(/Unable to get database/);
124125
expect(progressSpy).to.have.callCount(0);
125126
});
@@ -133,19 +134,10 @@ describe('databaseFetcher', function() {
133134
mockRequest.resolves(mockApiResponse);
134135
const githubRepo = 'foo/bar-with-no-dbs';
135136
await expect(
136-
mod.convertGithubNwoToDatabaseUrl(githubRepo, credentials, progressSpy)
137+
convertGithubNwoToDatabaseUrl(githubRepo, octokit, progressSpy)
137138
).to.be.rejectedWith(/Unable to get database/);
138139
expect(progressSpy).to.have.been.calledOnce;
139140
});
140-
141-
function getMockCredentials(response: any) {
142-
mockRequest = sinon.stub().resolves(response);
143-
return {
144-
getOctokit: () => ({
145-
request: mockRequest,
146-
}),
147-
};
148-
}
149141
});
150142

151143
describe('convertLgtmUrlToDatabaseUrl', () => {

0 commit comments

Comments
 (0)