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

@@ -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();
});
});