Fix disabled azdata tests (#12659)

* Fix successful install

* fix more tests

* Fix stubs

* Don't throw

* fix check
This commit is contained in:
Charles Gagnon
2020-09-29 14:23:27 -07:00
committed by GitHub
parent a91b965a33
commit 5de1c10dd1
3 changed files with 50 additions and 64 deletions

View File

@@ -73,6 +73,11 @@ export namespace HttpClient {
reject(downloadError); reject(downloadError);
downloadRequest.abort(); downloadRequest.abort();
}); });
} else {
response.on('end', () => {
Logger.log(loc.downloadFinished);
resolve(strings.join(''));
});
} }
let contentLength = response.headers['content-length']; let contentLength = response.headers['content-length'];
let totalBytes = parseInt(contentLength || '0'); let totalBytes = parseInt(contentLength || '0');
@@ -92,13 +97,6 @@ export namespace HttpClient {
printThreshold += 0.1; printThreshold += 0.1;
} }
} }
})
.on('close', async () => {
if (targetFolder === undefined) {
Logger.log(loc.downloadFinished);
resolve(strings.join(''));
}
}); });
}); });
} }

View File

@@ -25,7 +25,7 @@ const releaseJson = {
'version': '9999.999.999' 'version': '9999.999.999'
} }
}; };
let executeSudoCommandStub: sinon.SinonStub;
describe('azdata', function () { describe('azdata', function () {
afterEach(function (): void { afterEach(function (): void {
@@ -53,13 +53,14 @@ describe('azdata', function () {
}); });
describe('installAzdata', function (): void { describe('installAzdata', function (): void {
beforeEach(function (): void { beforeEach(function (): void {
sinon.stub(vscode.window, 'showErrorMessage').returns(Promise.resolve(<any>loc.yes)); sinon.stub(vscode.window, 'showErrorMessage').returns(Promise.resolve(<any>loc.yes));
sinon.stub(utils, 'searchForCmd').returns(Promise.resolve('/path/to/azdata')); sinon.stub(utils, 'searchForCmd').returns(Promise.resolve('/path/to/azdata'));
sinon.stub(childProcess, 'executeSudoCommand').returns(Promise.resolve({ stdout: '', stderr: '' })); executeSudoCommandStub = sinon.stub(childProcess, 'executeSudoCommand').returns(Promise.resolve({ stdout: '', stderr: '' }));
}); });
it.skip('successful install', async function (): Promise<void> { it('successful install', async function (): Promise<void> {
switch (process.platform) { switch (process.platform) {
case 'win32': case 'win32':
await testWin32SuccessfulInstall(); await testWin32SuccessfulInstall();
@@ -74,14 +75,18 @@ describe('azdata', function () {
}); });
if (process.platform === 'win32') { if (process.platform === 'win32') {
it.skip('unsuccessful download - win32', async function (): Promise<void> { it('unsuccessful download - win32', async function (): Promise<void> {
sinon.stub(HttpClient, 'downloadFile').rejects(); sinon.stub(HttpClient, 'downloadFile').rejects();
const downloadPromise = azdata.checkAndInstallAzdata(); sinon.stub(childProcess, 'executeCommand')
await should(downloadPromise).be.rejected(); .onFirstCall()
.rejects(new Error('not Found')) // First call mock the tool not being found
.resolves({ stdout: '1.0.0', stderr: '' });
const azdataTool = await azdata.checkAndInstallAzdata();
should(azdataTool).be.undefined();
}); });
} }
it.skip('unsuccessful install', async function (): Promise<void> { it('unsuccessful install', async function (): Promise<void> {
switch (process.platform) { switch (process.platform) {
case 'win32': case 'win32':
await testWin32UnsuccessfulInstall(); await testWin32UnsuccessfulInstall();
@@ -99,10 +104,10 @@ describe('azdata', function () {
describe('updateAzdata', function (): void { describe('updateAzdata', function (): void {
beforeEach(function (): void { beforeEach(function (): void {
sinon.stub(vscode.window, 'showInformationMessage').returns(Promise.resolve(<any>loc.yes)); sinon.stub(vscode.window, 'showInformationMessage').returns(Promise.resolve(<any>loc.yes));
sinon.stub(childProcess, 'executeSudoCommand').returns(Promise.resolve({ stdout: '', stderr: '' })); executeSudoCommandStub = sinon.stub(childProcess, 'executeSudoCommand').returns(Promise.resolve({ stdout: '', stderr: '' }));
}); });
it.skip('successful update', async function (): Promise<void> { it('successful update', async function (): Promise<void> {
switch (process.platform) { switch (process.platform) {
case 'win32': case 'win32':
await testWin32SuccessfulUpdate(); await testWin32SuccessfulUpdate();
@@ -117,7 +122,7 @@ describe('azdata', function () {
}); });
it.skip('unsuccessful update', async function (): Promise<void> { it('unsuccessful update', async function (): Promise<void> {
switch (process.platform) { switch (process.platform) {
case 'win32': case 'win32':
await testWin32UnsuccessfulUpdate(); await testWin32UnsuccessfulUpdate();
@@ -141,7 +146,7 @@ describe('azdata', function () {
}); });
async function testLinuxUnsuccessfulUpdate() { async function testLinuxUnsuccessfulUpdate() {
const executeSudoCommandStub = sinon.stub(childProcess, 'executeSudoCommand').rejects(); executeSudoCommandStub.rejects();
const updateDone = await azdata.checkAndUpdateAzdata(oldAzdataMock); const updateDone = await azdata.checkAndUpdateAzdata(oldAzdataMock);
should(updateDone).be.false(); should(updateDone).be.false();
should(executeSudoCommandStub.calledOnce).be.true(); should(executeSudoCommandStub.calledOnce).be.true();
@@ -171,7 +176,7 @@ async function testDarwinUnsuccessfulUpdate() {
return Promise.reject(new Error('not Found')); return Promise.reject(new Error('not Found'));
}) })
.callsFake(async (_command: string, _args: string[]) => { // by default return success .callsFake(async (_command: string, _args: string[]) => { // by default return success
return Promise.resolve({stderr: '', stdout: 'success'}); return Promise.resolve({ stderr: '', stdout: 'success' });
}); });
const updateDone = await azdata.checkAndUpdateAzdata(oldAzdataMock); const updateDone = await azdata.checkAndUpdateAzdata(oldAzdataMock);
should(updateDone).be.false(); should(updateDone).be.false();
@@ -180,16 +185,16 @@ async function testDarwinUnsuccessfulUpdate() {
async function testWin32UnsuccessfulUpdate() { async function testWin32UnsuccessfulUpdate() {
sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename)); sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename));
const executeCommandStub = sinon.stub(childProcess, 'executeCommand').rejects(); executeSudoCommandStub.rejects();
const updateDone = await azdata.checkAndUpdateAzdata(oldAzdataMock); const updateDone = await azdata.checkAndUpdateAzdata(oldAzdataMock);
should(updateDone).be.false(); should(updateDone).be.false('Update should not have been successful');
should(executeCommandStub.calledOnce).be.true(); should(executeSudoCommandStub.calledOnce).be.true();
} }
async function testLinuxSuccessfulUpdate() { async function testLinuxSuccessfulUpdate() {
sinon.stub(HttpClient, 'getTextContent').returns(Promise.resolve(JSON.stringify(releaseJson))); sinon.stub(HttpClient, 'getTextContent').returns(Promise.resolve(JSON.stringify(releaseJson)));
const executeCommandStub = sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: '0.0.0', stderr: '' })); const executeCommandStub = sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: '0.0.0', stderr: '' }));
const executeSudoCommandStub = sinon.stub(childProcess, 'executeSudoCommand').returns(Promise.resolve({ stdout: '0.0.0', stderr: '' })); executeSudoCommandStub.resolves({ stdout: '0.0.0', stderr: '' });
await azdata.checkAndUpdateAzdata(oldAzdataMock); await azdata.checkAndUpdateAzdata(oldAzdataMock);
should(executeSudoCommandStub.callCount).be.equal(6); should(executeSudoCommandStub.callCount).be.equal(6);
should(executeCommandStub.calledOnce).be.true(); should(executeCommandStub.calledOnce).be.true();
@@ -208,50 +213,39 @@ async function testDarwinSuccessfulUpdate() {
}]; }];
const executeCommandStub = sinon.stub(childProcess, 'executeCommand') const executeCommandStub = sinon.stub(childProcess, 'executeCommand')
.onThirdCall() //third call is brew info azdata-cli --json which needs to return json of new available azdata versions. .onThirdCall() //third call is brew info azdata-cli --json which needs to return json of new available azdata versions.
.callsFake(async (command: string, args: string[]) => { .resolves({
should(command).be.equal('brew'); stderr: '',
should(args).deepEqual(['info', 'azdata-cli', '--json']); stdout: JSON.stringify(brewInfoOutput)
return Promise.resolve({
stderr: '',
stdout: JSON.stringify(brewInfoOutput)
});
}) })
.callsFake(async (_command: string, _args: string[]) => { // return success on all other command executions .resolves({ stdout: '0.0.0', stderr: '' });
return Promise.resolve({ stdout: '0.0.0', stderr: '' });
});
await azdata.checkAndUpdateAzdata(oldAzdataMock); await azdata.checkAndUpdateAzdata(oldAzdataMock);
should(executeCommandStub.callCount).be.equal(6); should(executeCommandStub.callCount).be.equal(6);
should(executeCommandStub.getCall(2).args[0]).be.equal('brew', '3rd call should have been to brew');
should(executeCommandStub.getCall(2).args[1]).deepEqual(['info', 'azdata-cli', '--json'], '3rd call did not have expected arguments');
} }
async function testWin32SuccessfulUpdate() { async function testWin32SuccessfulUpdate() {
sinon.stub(HttpClient, 'getTextContent').returns(Promise.resolve(JSON.stringify(releaseJson))); sinon.stub(HttpClient, 'getTextContent').returns(Promise.resolve(JSON.stringify(releaseJson)));
sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename)); sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename));
const executeCommandStub = sinon.stub(childProcess, 'executeCommand').callsFake(async (command: string, args: string[]) => {
should(command).be.equal('msiexec');
should(args[0]).be.equal('/qn');
should(args[1]).be.equal('/i');
return { stdout: '0.0.0', stderr: '' };
});
await azdata.checkAndUpdateAzdata(oldAzdataMock); await azdata.checkAndUpdateAzdata(oldAzdataMock);
should(executeCommandStub.calledOnce).be.true(); should(executeSudoCommandStub.calledOnce).be.true('executeSudoCommand should have been called once');
should(executeSudoCommandStub.getCall(0).args[0]).startWith('msiexec /qn /i');
} }
async function testWin32SuccessfulInstall() { async function testWin32SuccessfulInstall() {
sinon.stub(HttpClient, 'getTextContent').returns(Promise.resolve(JSON.stringify(releaseJson)));
sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename)); sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename));
const executeCommandStub = sinon.stub(childProcess, 'executeCommand') const executeCommandStub = sinon.stub(childProcess, 'executeCommand')
.onFirstCall() .onFirstCall()
.callsFake(async (_command: string, _args: string[]) => { .rejects(new Error('not Found')) // First call mock the tool not being found
return Promise.reject(new Error('not Found')); .resolves({ stdout: '1.0.0', stderr: '' });
}) executeSudoCommandStub
.callsFake(async (command: string, args: string[]) => { .returns({ stdout: '', stderr: '' });
should(command).be.equal('msiexec');
should(args[0]).be.equal('/qn');
should(args[1]).be.equal('/i');
return { stdout: '0.0.0', stderr: '' };
});
await azdata.checkAndInstallAzdata(); await azdata.checkAndInstallAzdata();
should(executeCommandStub.calledTwice).be.true(`executeCommand should have been called twice. Actual ${executeCommandStub.getCalls().length}`); should(executeCommandStub.calledTwice).be.true(`executeCommand should have been called twice. Actual ${executeCommandStub.getCalls().length}`);
should(executeSudoCommandStub.calledOnce).be.true(`executeSudoCommand should have been called once. Actual ${executeSudoCommandStub.getCalls().length}`);
should(executeSudoCommandStub.getCall(0).args[0]).startWith('msiexec /qn /i');
} }
async function testDarwinSuccessfulInstall() { async function testDarwinSuccessfulInstall() {
@@ -270,23 +264,17 @@ async function testDarwinSuccessfulInstall() {
async function testLinuxSuccessfulInstall() { async function testLinuxSuccessfulInstall() {
const executeCommandStub = sinon.stub(childProcess, 'executeCommand') const executeCommandStub = sinon.stub(childProcess, 'executeCommand')
.onFirstCall() .onFirstCall()
.callsFake(async (_command: string, _args: string[]) => { .rejects(new Error('not Found'))
return Promise.reject(new Error('not Found')); .resolves({ stdout: '0.0.0', stderr: '' });
}) executeSudoCommandStub
.callsFake(async (_command: string, _args: string[]) => { .resolves({ stdout: 'success', stderr: '' });
return Promise.resolve({ stdout: '0.0.0', stderr: '' });
});
const executeSudoCommandStub = sinon.stub(childProcess, 'executeSudoCommand')
.callsFake(async (_command: string ) => {
return Promise.resolve({ stdout: 'success', stderr: '' });
});
await azdata.checkAndInstallAzdata(); await azdata.checkAndInstallAzdata();
should(executeSudoCommandStub.callCount).be.equal(6); should(executeSudoCommandStub.callCount).be.equal(6);
should(executeCommandStub.calledThrice).be.true(); should(executeCommandStub.calledThrice).be.true();
} }
async function testLinuxUnsuccessfulInstall() { async function testLinuxUnsuccessfulInstall() {
const executeSudoCommandStub = sinon.stub(childProcess, 'executeSudoCommand').rejects(); executeSudoCommandStub.rejects();
const downloadPromise = azdata.installAzdata(); const downloadPromise = azdata.installAzdata();
await should(downloadPromise).be.rejected(); await should(downloadPromise).be.rejected();
should(executeSudoCommandStub.calledOnce).be.true(); should(executeSudoCommandStub.calledOnce).be.true();
@@ -300,9 +288,9 @@ async function testDarwinUnsuccessfulInstall() {
} }
async function testWin32UnsuccessfulInstall() { async function testWin32UnsuccessfulInstall() {
const executeCommandStub = sinon.stub(childProcess, 'executeCommand').rejects(); executeSudoCommandStub.rejects();
sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename)); sinon.stub(HttpClient, 'downloadFile').returns(Promise.resolve(__filename));
const downloadPromise = azdata.installAzdata(); const downloadPromise = azdata.installAzdata();
await should(downloadPromise).be.rejected(); await should(downloadPromise).be.rejected();
should(executeCommandStub.calledOnce).be.true(); should(executeSudoCommandStub.calledOnce).be.true();
} }

View File

@@ -73,12 +73,12 @@ describe('HttpClient', function (): void {
}); });
describe('getTextContent', function (): void { describe('getTextContent', function (): void {
it.skip('Gets file contents correctly', async function (): Promise<void> { it('Gets file contents correctly', async function (): Promise<void> {
nock('https://127.0.0.1') nock('https://127.0.0.1')
.get('/arbitraryFile') .get('/arbitraryFile')
.replyWithFile(200, __filename); .replyWithFile(200, __filename);
const receivedContents = await HttpClient.getTextContent(`https://127.0.0.1/arbitraryFile`); const receivedContents = await HttpClient.getTextContent(`https://127.0.0.1/arbitraryFile`);
should(receivedContents).equal(await fs.promises.readFile(__filename)); should(receivedContents).equal((await fs.promises.readFile(__filename)).toString());
}); });
it('rejects on response error', async function (): Promise<void> { it('rejects on response error', async function (): Promise<void> {