azdata improvements (#11516)

* azdata improvements

* Don't error on sudo command stderr either

* Improve output channel logging for commands

* Fix childprocess stuff

* pr comments

* Fix compile errors

* more pr comments
This commit is contained in:
Charles Gagnon
2020-07-28 08:43:10 -07:00
committed by GitHub
parent 3c7f2df156
commit cf6d02d2b4
12 changed files with 255 additions and 105 deletions

View File

@@ -27,7 +27,7 @@ describe('azdata', function () {
describe('findAzdata', function () {
// Mock call to --version to simulate azdata being installed
sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve('v1.0.0'));
sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: 'v1.0.0', stderr: '' }));
it('successful', async function (): Promise<void> {
sinon.stub(utils, 'searchForCmd').returns(Promise.resolve('C:\\path\\to\\azdata.cmd'));
await should(azdata.findAzdata(outputChannelMock.object)).not.be.rejected();

View File

@@ -9,21 +9,22 @@ import * as TypeMoq from 'typemoq';
import { executeCommand } from '../../common/childProcess';
describe('ChildProcess', function () {
[undefined, [], ['test']].forEach(args => {
const outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
[[], ['test']].forEach(args => {
it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise<void> {
const outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
await executeCommand('echo', args, outputChannelMock.object);
outputChannelMock.verify(x => x.appendLine(TypeMoq.It.isAny()), TypeMoq.Times.once());
outputChannelMock.verify(x => x.appendLine(TypeMoq.It.isAny()), TypeMoq.Times.atLeastOnce());
});
});
it('Gets expected output', async function (): Promise<void> {
const echoOutput = 'test';
const output = await executeCommand('echo', [echoOutput]);
should(output).equal(echoOutput);
const output = await executeCommand('echo', [echoOutput], outputChannelMock.object);
should(output.stdout).equal(echoOutput);
});
it('Invalid command errors', async function (): Promise<void> {
await should(executeCommand('sdfkslkf')).be.rejected();
await should(executeCommand('invalid_command', [], outputChannelMock.object)).be.rejected();
});
});

View File

@@ -7,15 +7,14 @@ import * as vscode from 'vscode';
import * as should from 'should';
import * as TypeMoq from 'typemoq';
import { HttpClient } from '../../common/httpClient';
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import * as uuid from 'uuid';
import * as nock from 'nock';
import * as sinon from 'sinon';
import { PassThrough } from 'stream';
import { Deferred } from '../../common/promise';
describe('HttpClient', function () {
describe('HttpClient', function (): void {
let outputChannelMock: TypeMoq.IMock<vscode.OutputChannel>;
@@ -28,36 +27,88 @@ describe('HttpClient', function () {
nock.enableNetConnect();
});
it('downloads file successfully', async function (): Promise<void> {
const downloadPath = path.join(os.tmpdir(), `azdata-httpClientTest-${uuid.v4()}.txt`);
await HttpClient.download('https://raw.githubusercontent.com/microsoft/azuredatastudio/main/README.md', downloadPath, outputChannelMock.object);
// Verify file was downloaded correctly
await fs.promises.stat(downloadPath);
describe('download', function(): void {
it('downloads file successfully', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/README.md')
.replyWithFile(200, __filename);
const downloadFolder = os.tmpdir();
const downloadPath = await HttpClient.download('https://127.0.0.1/README.md', downloadFolder, outputChannelMock.object);
// Verify file was downloaded correctly
await fs.promises.stat(downloadPath);
});
it('errors on response stream error', async function (): Promise<void> {
const downloadFolder = os.tmpdir();
nock('https://127.0.0.1')
.get('/')
.replyWithError('Unexpected Error');
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
await should(downloadPromise).be.rejected();
});
it('rejects on non-OK status code', async function (): Promise<void> {
const downloadFolder = os.tmpdir();
nock('https://127.0.0.1')
.get('/')
.reply(404, '');
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
await should(downloadPromise).be.rejected();
});
it('errors on write stream error', async function (): Promise<void> {
const downloadFolder = os.tmpdir();
const mockWriteStream = new PassThrough();
const deferredPromise = new Deferred();
sinon.stub(fs, 'createWriteStream').callsFake(() => {
deferredPromise.resolve();
return <any>mockWriteStream;
});
nock('https://127.0.0.1')
.get('/')
.reply(200, '');
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
// Wait for the stream to be created before throwing the error or HttpClient will miss the event
await deferredPromise;
try {
// Passthrough streams will throw the error we emit so just no-op and
// let the HttpClient handler handle the error
mockWriteStream.emit('error', 'Unexpected write error');
} catch (err) { }
await should(downloadPromise).be.rejected();
});
});
it('errors on response stream error', async function (): Promise<void> {
const downloadPath = path.join(os.tmpdir(), `azdata-httpClientTest-error-${uuid.v4()}.txt`);
nock('https://127.0.0.1')
.get('/')
.replyWithError('Unexpected Error');
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadPath, outputChannelMock.object);
describe('getFilename', function(): void {
it('Gets filename correctly', async function (): Promise<void> {
const filename = 'azdata-cli-20.0.0.msi';
nock('https://127.0.0.1')
.get(`/${filename}`)
.reply(200);
const receivedFilename = await HttpClient.getFilename(`https://127.0.0.1/${filename}`, outputChannelMock.object);
await should(downloadPromise).be.rejected();
should(receivedFilename).equal(filename);
});
it('errors on response error', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/')
.replyWithError('Unexpected Error');
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1', outputChannelMock.object);
await should(getFilenamePromise).be.rejected();
});
it('rejects on non-OK status code', async function (): Promise<void> {
nock('https://127.0.0.1')
.get('/')
.reply(404, '');
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1', outputChannelMock.object);
await should(getFilenamePromise).be.rejected();
});
});
it('errors on write stream error', async function (): Promise<void> {
const downloadPath = path.join(os.tmpdir(), `azdata-httpClientTest-error-${uuid.v4()}.txt`);
const mockWriteStream = new PassThrough();
sinon.stub(fs, 'createWriteStream').returns(<any>mockWriteStream);
nock('https://127.0.0.1')
.get('/')
.reply(200, '');
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadPath, outputChannelMock.object);
try {
// Passthrough streams will throw the error we emit so just no-op and
// let the HttpClient handler handle the error
mockWriteStream.emit('error', 'Unexpected write error');
} catch (err) { }
await should(downloadPromise).be.rejected();
});
});

View File

@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as should from 'should';
import { Deferred } from '../../common/promise';
describe('DeferredPromise', function (): void {
it('Resolves correctly', async function(): Promise<void> {
const deferred = new Deferred();
deferred.resolve();
await should(deferred.promise).be.resolved();
});
it('Rejects correctly', async function(): Promise<void> {
const deferred = new Deferred();
deferred.reject();
await should(deferred.promise).be.rejected();
});
it('Chains then correctly', function(done): void {
const deferred = new Deferred();
deferred.then( () => {
done();
});
deferred.resolve();
});
});