diff --git a/extensions/azdata/src/test/azdata.test.ts b/extensions/azdata/src/test/azdata.test.ts index bc4f7e590b..e213dd2278 100644 --- a/extensions/azdata/src/test/azdata.test.ts +++ b/extensions/azdata/src/test/azdata.test.ts @@ -48,8 +48,12 @@ describe('azdata', function () { }); // TODO: Install not implemented on linux yet - describe.skip('downloadAndInstallAzdata', function (): void { + describe('downloadAndInstallAzdata', function (): void { it('successful download & install', async function (): Promise { + sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: '', stderr: '' })); + if (process.platform === 'linux') { + sinon.stub(childProcess, 'executeSudoCommand').returns(Promise.resolve({ stdout: '', stderr: '' })); + } nock(azdata.azdataHostname) .get(`/${azdata.azdataUri}`) .replyWithFile(200, __filename); diff --git a/extensions/azdata/src/test/common/childProcess.test.ts b/extensions/azdata/src/test/common/childProcess.test.ts index fc03995288..de31964ce2 100644 --- a/extensions/azdata/src/test/common/childProcess.test.ts +++ b/extensions/azdata/src/test/common/childProcess.test.ts @@ -6,25 +6,64 @@ import * as vscode from 'vscode'; import * as should from 'should'; import * as TypeMoq from 'typemoq'; -import { executeCommand } from '../../common/childProcess'; +import * as sudo from 'sudo-prompt'; +import * as sinon from 'sinon'; -describe('ChildProcess', function () { +import { executeCommand, executeSudoCommand } from '../../common/childProcess'; + +describe('ChildProcess', function (): void { const outputChannelMock = TypeMoq.Mock.ofType(); - [[], ['test']].forEach(args => { - it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise { - await executeCommand('echo', args, outputChannelMock.object); - outputChannelMock.verify(x => x.appendLine(TypeMoq.It.isAny()), TypeMoq.Times.atLeastOnce()); + afterEach(function(): void { + sinon.restore(); + }); + + describe('executeCommand', function(): void { + [[], ['test']].forEach(args => { + it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise { + await executeCommand('echo', args, outputChannelMock.object); + outputChannelMock.verify(x => x.appendLine(TypeMoq.It.isAny()), TypeMoq.Times.atLeastOnce()); + }); + }); + + it('Gets expected output', async function (): Promise { + const echoOutput = 'test'; + const output = await executeCommand('echo', [echoOutput], outputChannelMock.object); + should(output.stdout).equal(echoOutput); + }); + + it('Invalid command errors', async function (): Promise { + await should(executeCommand('invalid_command', [], outputChannelMock.object)).be.rejected(); }); }); - it('Gets expected output', async function (): Promise { - const echoOutput = 'test'; - const output = await executeCommand('echo', [echoOutput], outputChannelMock.object); - should(output.stdout).equal(echoOutput); + describe('executeSudoCommand', function(): void { + it('Gets expected stdout output', async function (): Promise { + const stdout = 'stdout output'; + sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => { + callback!(undefined, stdout); + }); + const result = await executeSudoCommand('echo', outputChannelMock.object); + should(result.stdout).equal(stdout); + should(result.stderr).equal(''); + }); + + it('Gets expected stderr output', async function (): Promise { + const stderr = 'stderr output'; + sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => { + callback!(undefined, undefined, stderr); + }); + const result = await executeSudoCommand('echo', outputChannelMock.object); + should(result.stdout).equal(''); + should(result.stderr).equal(stderr); + }); + + it('Error rejects', async function (): Promise { + sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => { + callback!(new Error('error')); + }); + await should(executeSudoCommand('invalid_command', outputChannelMock.object)).be.rejected(); + }); }); - it('Invalid command errors', async function (): Promise { - await should(executeCommand('invalid_command', [], outputChannelMock.object)).be.rejected(); - }); });