|
| 1 | +import * as path from 'path'; |
| 2 | + |
| 3 | +import * as sinon from 'sinon'; |
| 4 | + |
| 5 | +import { commands, extensions, TextDocument, window, workspace } from 'vscode'; |
| 6 | +import * as Octokit from '@octokit/rest'; |
| 7 | +import { retry } from '@octokit/plugin-retry'; |
| 8 | + |
| 9 | +import { CodeQLExtensionInterface } from '../../../extension'; |
| 10 | +import * as config from '../../../config'; |
| 11 | +import { Credentials } from '../../../authentication'; |
| 12 | +import { MockGitHubApiServer } from '../../../mocks/mock-gh-api-server'; |
| 13 | + |
| 14 | +const mockServer = new MockGitHubApiServer(); |
| 15 | +before(() => mockServer.startServer()); |
| 16 | +afterEach(() => mockServer.unloadScenario()); |
| 17 | +after(() => mockServer.stopServer()); |
| 18 | + |
| 19 | +async function showQlDocument(name: string): Promise<TextDocument> { |
| 20 | + const folderPath = workspace.workspaceFolders![0].uri.fsPath; |
| 21 | + const documentPath = path.resolve(folderPath, name); |
| 22 | + const document = await workspace.openTextDocument(documentPath); |
| 23 | + await window.showTextDocument(document!); |
| 24 | + return document; |
| 25 | +} |
| 26 | + |
| 27 | +describe('Variant Analysis Submission Integration', function() { |
| 28 | + this.timeout(10_000); |
| 29 | + |
| 30 | + let sandbox: sinon.SinonSandbox; |
| 31 | + let quickPickSpy: sinon.SinonStub; |
| 32 | + let inputBoxSpy: sinon.SinonStub; |
| 33 | + let executeCommandSpy: sinon.SinonStub; |
| 34 | + let showErrorMessageSpy: sinon.SinonStub; |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + sandbox = sinon.createSandbox(); |
| 38 | + |
| 39 | + sandbox.stub(config, 'isCanary').returns(true); |
| 40 | + sandbox.stub(config, 'isVariantAnalysisLiveResultsEnabled').returns(true); |
| 41 | + |
| 42 | + const mockCredentials = { |
| 43 | + getOctokit: () => Promise.resolve(new Octokit.Octokit({ retry })), |
| 44 | + } as unknown as Credentials; |
| 45 | + sandbox.stub(Credentials, 'initialize').resolves(mockCredentials); |
| 46 | + |
| 47 | + await config.setRemoteControllerRepo('github/vscode-codeql'); |
| 48 | + |
| 49 | + quickPickSpy = sandbox.stub(window, 'showQuickPick').resolves(undefined); |
| 50 | + inputBoxSpy = sandbox.stub(window, 'showInputBox').resolves(undefined); |
| 51 | + |
| 52 | + executeCommandSpy = sandbox.stub(commands, 'executeCommand').callThrough(); |
| 53 | + showErrorMessageSpy = sandbox.stub(window, 'showErrorMessage').resolves(undefined); |
| 54 | + |
| 55 | + try { |
| 56 | + await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate(); |
| 57 | + } catch (e) { |
| 58 | + fail(e as Error); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + afterEach(() => { |
| 63 | + sandbox.restore(); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('Successful scenario', () => { |
| 67 | + beforeEach(async () => { |
| 68 | + await mockServer.loadScenario('problem-query-success'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('opens the variant analysis view', async () => { |
| 72 | + await showQlDocument('query.ql'); |
| 73 | + |
| 74 | + // Select a repository list |
| 75 | + quickPickSpy.onFirstCall().resolves({ |
| 76 | + useCustomRepo: true, |
| 77 | + }); |
| 78 | + // Enter a GitHub repository |
| 79 | + inputBoxSpy.onFirstCall().resolves('github/codeql'); |
| 80 | + // Select target language for your query |
| 81 | + quickPickSpy.onSecondCall().resolves('javascript'); |
| 82 | + |
| 83 | + await commands.executeCommand('codeQL.runVariantAnalysis'); |
| 84 | + |
| 85 | + sinon.assert.calledWith(executeCommandSpy, 'codeQL.openVariantAnalysisView', 146); |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('Missing controller repo', () => { |
| 90 | + beforeEach(async () => { |
| 91 | + await mockServer.loadScenario('missing-controller-repo'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('shows the error message', async () => { |
| 95 | + await showQlDocument('query.ql'); |
| 96 | + |
| 97 | + // Select a repository list |
| 98 | + quickPickSpy.onFirstCall().resolves({ |
| 99 | + useCustomRepo: true, |
| 100 | + }); |
| 101 | + // Enter a GitHub repository |
| 102 | + inputBoxSpy.onFirstCall().resolves('github/codeql'); |
| 103 | + |
| 104 | + await commands.executeCommand('codeQL.runVariantAnalysis'); |
| 105 | + |
| 106 | + sinon.assert.calledWith(showErrorMessageSpy, sinon.match('Controller repository "github/vscode-codeql" not found'), sinon.match.string); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('Submission failure', () => { |
| 111 | + beforeEach(async () => { |
| 112 | + await mockServer.loadScenario('submission-failure'); |
| 113 | + }); |
| 114 | + |
| 115 | + it('shows the error message', async () => { |
| 116 | + await showQlDocument('query.ql'); |
| 117 | + |
| 118 | + // Select a repository list |
| 119 | + quickPickSpy.onFirstCall().resolves({ |
| 120 | + useCustomRepo: true, |
| 121 | + }); |
| 122 | + // Enter a GitHub repository |
| 123 | + inputBoxSpy.onFirstCall().resolves('github/codeql'); |
| 124 | + // Select target language for your query |
| 125 | + quickPickSpy.onSecondCall().resolves('javascript'); |
| 126 | + |
| 127 | + await commands.executeCommand('codeQL.runVariantAnalysis'); |
| 128 | + |
| 129 | + sinon.assert.calledWith(showErrorMessageSpy, sinon.match('No repositories could be queried.'), sinon.match.string); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments