mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-03 09:35:40 -05:00
Add static logger class for azdata extension (#11939)
* Add static logger class for arc extension * Fix compile errors * Fix test
This commit is contained in:
@@ -10,6 +10,7 @@ import * as loc from './localizedConstants';
|
||||
import { executeCommand, executeSudoCommand, ExitCodeError } from './common/childProcess';
|
||||
import { searchForCmd } from './common/utils';
|
||||
import * as azdataExt from 'azdata-ext';
|
||||
import Logger from './common/logger';
|
||||
|
||||
export const azdataHostname = 'https://aka.ms';
|
||||
export const azdataUri = 'azdata-msi';
|
||||
@@ -26,7 +27,7 @@ export interface IAzdataTool extends azdataExt.IAzdataApi {
|
||||
}
|
||||
|
||||
class AzdataTool implements IAzdataTool {
|
||||
constructor(public path: string, public toolVersion: string, private _outputChannel: vscode.OutputChannel) { }
|
||||
constructor(public path: string, public toolVersion: string) { }
|
||||
|
||||
public arc = {
|
||||
dc: {
|
||||
@@ -97,7 +98,7 @@ class AzdataTool implements IAzdataTool {
|
||||
|
||||
public async executeCommand<R>(args: string[], additionalEnvVars?: { [key: string]: string }): Promise<azdataExt.AzdataOutput<R>> {
|
||||
try {
|
||||
const output = JSON.parse((await executeCommand(`"${this.path}"`, args.concat(['--output', 'json']), this._outputChannel, additionalEnvVars)).stdout);
|
||||
const output = JSON.parse((await executeCommand(`"${this.path}"`, args.concat(['--output', 'json']), additionalEnvVars)).stdout);
|
||||
return {
|
||||
logs: <string[]>output.log,
|
||||
stdout: <string[]>output.stdout,
|
||||
@@ -119,45 +120,43 @@ class AzdataTool implements IAzdataTool {
|
||||
/**
|
||||
* Finds the existing installation of azdata, or throws an error if it couldn't find it
|
||||
* or encountered an unexpected error.
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
export async function findAzdata(outputChannel: vscode.OutputChannel): Promise<IAzdataTool> {
|
||||
outputChannel.appendLine(loc.searchingForAzdata);
|
||||
export async function findAzdata(): Promise<IAzdataTool> {
|
||||
Logger.log(loc.searchingForAzdata);
|
||||
try {
|
||||
let azdata: IAzdataTool | undefined = undefined;
|
||||
switch (process.platform) {
|
||||
case 'win32':
|
||||
azdata = await findAzdataWin32(outputChannel);
|
||||
azdata = await findAzdataWin32();
|
||||
break;
|
||||
default:
|
||||
azdata = await findSpecificAzdata('azdata', outputChannel);
|
||||
azdata = await findSpecificAzdata('azdata');
|
||||
}
|
||||
outputChannel.appendLine(loc.foundExistingAzdata(azdata.path, azdata.toolVersion));
|
||||
Logger.log(loc.foundExistingAzdata(azdata.path, azdata.toolVersion));
|
||||
return azdata;
|
||||
} catch (err) {
|
||||
outputChannel.appendLine(loc.couldNotFindAzdata(err));
|
||||
Logger.log(loc.couldNotFindAzdata(err));
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads the appropriate installer and/or runs the command to install azdata
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
export async function downloadAndInstallAzdata(outputChannel: vscode.OutputChannel): Promise<void> {
|
||||
export async function downloadAndInstallAzdata(): Promise<void> {
|
||||
const statusDisposable = vscode.window.setStatusBarMessage(loc.installingAzdata);
|
||||
outputChannel.show();
|
||||
outputChannel.appendLine(loc.installingAzdata);
|
||||
Logger.show();
|
||||
Logger.log(loc.installingAzdata);
|
||||
try {
|
||||
switch (process.platform) {
|
||||
case 'win32':
|
||||
await downloadAndInstallAzdataWin32(outputChannel);
|
||||
await downloadAndInstallAzdataWin32();
|
||||
break;
|
||||
case 'darwin':
|
||||
await installAzdataDarwin(outputChannel);
|
||||
await installAzdataDarwin();
|
||||
break;
|
||||
case 'linux':
|
||||
await installAzdataLinux(outputChannel);
|
||||
await installAzdataLinux();
|
||||
break;
|
||||
default:
|
||||
throw new Error(loc.platformUnsupported(process.platform));
|
||||
@@ -169,58 +168,55 @@ export async function downloadAndInstallAzdata(outputChannel: vscode.OutputChann
|
||||
|
||||
/**
|
||||
* Downloads the Windows installer and runs it
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
async function downloadAndInstallAzdataWin32(outputChannel: vscode.OutputChannel): Promise<void> {
|
||||
async function downloadAndInstallAzdataWin32(): Promise<void> {
|
||||
const downloadFolder = os.tmpdir();
|
||||
const downloadedFile = await HttpClient.download(`${azdataHostname}/${azdataUri}`, downloadFolder, outputChannel);
|
||||
await executeCommand('msiexec', ['/qn', '/i', downloadedFile], outputChannel);
|
||||
const downloadedFile = await HttpClient.download(`${azdataHostname}/${azdataUri}`, downloadFolder);
|
||||
await executeCommand('msiexec', ['/qn', '/i', downloadedFile]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs commands to install azdata on MacOS
|
||||
*/
|
||||
async function installAzdataDarwin(outputChannel: vscode.OutputChannel): Promise<void> {
|
||||
await executeCommand('brew', ['tap', 'microsoft/azdata-cli-release'], outputChannel);
|
||||
await executeCommand('brew', ['update'], outputChannel);
|
||||
await executeCommand('brew', ['install', 'azdata-cli'], outputChannel);
|
||||
async function installAzdataDarwin(): Promise<void> {
|
||||
await executeCommand('brew', ['tap', 'microsoft/azdata-cli-release']);
|
||||
await executeCommand('brew', ['update']);
|
||||
await executeCommand('brew', ['install', 'azdata-cli']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs commands to install azdata on Linux
|
||||
*/
|
||||
async function installAzdataLinux(outputChannel: vscode.OutputChannel): Promise<void> {
|
||||
async function installAzdataLinux(): Promise<void> {
|
||||
// https://docs.microsoft.com/en-us/sql/big-data-cluster/deploy-install-azdata-linux-package
|
||||
// Get packages needed for install process
|
||||
await executeSudoCommand('apt-get update', outputChannel);
|
||||
await executeSudoCommand('apt-get install gnupg ca-certificates curl wget software-properties-common apt-transport-https lsb-release -y', outputChannel);
|
||||
await executeSudoCommand('apt-get update');
|
||||
await executeSudoCommand('apt-get install gnupg ca-certificates curl wget software-properties-common apt-transport-https lsb-release -y');
|
||||
// Download and install the signing key
|
||||
await executeSudoCommand('curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null', outputChannel);
|
||||
await executeSudoCommand('curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null');
|
||||
// Add the azdata repository information
|
||||
const release = (await executeCommand('lsb_release', ['-rs'], outputChannel)).stdout.trim();
|
||||
await executeSudoCommand(`add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/${release}/mssql-server-2019.list)"`, outputChannel);
|
||||
const release = (await executeCommand('lsb_release', ['-rs'])).stdout.trim();
|
||||
await executeSudoCommand(`add-apt-repository "$(wget -qO- https://packages.microsoft.com/config/ubuntu/${release}/mssql-server-2019.list)"`);
|
||||
// Update repository information and install azdata
|
||||
await executeSudoCommand('apt-get update', outputChannel);
|
||||
await executeSudoCommand('apt-get install -y azdata-cli', outputChannel);
|
||||
await executeSudoCommand('apt-get update');
|
||||
await executeSudoCommand('apt-get install -y azdata-cli');
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds azdata specifically on Windows
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
async function findAzdataWin32(outputChannel: vscode.OutputChannel): Promise<IAzdataTool> {
|
||||
async function findAzdataWin32(): Promise<IAzdataTool> {
|
||||
const promise = searchForCmd('azdata.cmd');
|
||||
return findSpecificAzdata(await promise, outputChannel);
|
||||
return findSpecificAzdata(await promise);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the version using a known azdata path
|
||||
* @param path The path to the azdata executable
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
async function findSpecificAzdata(path: string, outputChannel: vscode.OutputChannel): Promise<IAzdataTool> {
|
||||
const versionOutput = await executeCommand(`"${path}"`, ['--version'], outputChannel);
|
||||
return new AzdataTool(path, parseVersion(versionOutput.stdout), outputChannel);
|
||||
async function findSpecificAzdata(path: string): Promise<IAzdataTool> {
|
||||
const versionOutput = await executeCommand(`"${path}"`, ['--version']);
|
||||
return new AzdataTool(path, parseVersion(versionOutput.stdout));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,6 +7,7 @@ import * as vscode from 'vscode';
|
||||
import * as cp from 'child_process';
|
||||
import * as sudo from 'sudo-prompt';
|
||||
import * as loc from '../localizedConstants';
|
||||
import Logger from './logger';
|
||||
|
||||
/**
|
||||
* Wrapper error for when an unexpected exit code was received
|
||||
@@ -46,11 +47,10 @@ export type ProcessOutput = { stdout: string, stderr: string };
|
||||
* @param command The command to execute
|
||||
* @param args Optional args to pass, every arg and arg value must be a separate item in the array
|
||||
* @param additionalEnvVars Additional environment variables to add to the process environment
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
export async function executeCommand(command: string, args: string[], outputChannel: vscode.OutputChannel, additionalEnvVars?: { [key: string]: string },): Promise<ProcessOutput> {
|
||||
export async function executeCommand(command: string, args: string[], additionalEnvVars?: { [key: string]: string },): Promise<ProcessOutput> {
|
||||
return new Promise((resolve, reject) => {
|
||||
outputChannel.appendLine(loc.executingCommand(command, args));
|
||||
Logger.log(loc.executingCommand(command, args));
|
||||
const stdoutBuffers: Buffer[] = [];
|
||||
const stderrBuffers: Buffer[] = [];
|
||||
const env = Object.assign({}, process.env, additionalEnvVars);
|
||||
@@ -62,14 +62,14 @@ export async function executeCommand(command: string, args: string[], outputChan
|
||||
const stdout = Buffer.concat(stdoutBuffers).toString('utf8').trim();
|
||||
const stderr = Buffer.concat(stderrBuffers).toString('utf8').trim();
|
||||
if (stdout) {
|
||||
outputChannel.appendLine(loc.stdoutOutput(stdout));
|
||||
Logger.log(loc.stdoutOutput(stdout));
|
||||
}
|
||||
if (stderr) {
|
||||
outputChannel.appendLine(loc.stdoutOutput(stderr));
|
||||
Logger.log(loc.stdoutOutput(stderr));
|
||||
}
|
||||
if (code) {
|
||||
const err = new ExitCodeError(code, stderr);
|
||||
outputChannel.appendLine(err.message);
|
||||
Logger.log(err.message);
|
||||
reject(err);
|
||||
} else {
|
||||
resolve({ stdout: stdout, stderr: stderr });
|
||||
@@ -83,22 +83,21 @@ export async function executeCommand(command: string, args: string[], outputChan
|
||||
* this function. The exact prompt is platform-dependent.
|
||||
* @param command The command to execute
|
||||
* @param args The additional args
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
*/
|
||||
export async function executeSudoCommand(command: string, outputChannel: vscode.OutputChannel): Promise<ProcessOutput> {
|
||||
export async function executeSudoCommand(command: string): Promise<ProcessOutput> {
|
||||
return new Promise((resolve, reject) => {
|
||||
outputChannel.appendLine(loc.executingCommand(`sudo ${command}`, []));
|
||||
Logger.log(loc.executingCommand(`sudo ${command}`, []));
|
||||
sudo.exec(command, { name: vscode.env.appName }, (error, stdout, stderr) => {
|
||||
stdout = stdout?.toString() ?? '';
|
||||
stderr = stderr?.toString() ?? '';
|
||||
if (stdout) {
|
||||
outputChannel.appendLine(loc.stdoutOutput(stdout));
|
||||
Logger.log(loc.stdoutOutput(stdout));
|
||||
}
|
||||
if (stderr) {
|
||||
outputChannel.appendLine(loc.stdoutOutput(stderr));
|
||||
Logger.log(loc.stdoutOutput(stderr));
|
||||
}
|
||||
if (error) {
|
||||
outputChannel.appendLine(loc.unexpectedCommandError(error.message));
|
||||
Logger.log(loc.unexpectedCommandError(error.message));
|
||||
reject(error);
|
||||
} else {
|
||||
resolve({ stdout: stdout, stderr: stderr });
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import * as request from 'request';
|
||||
import * as path from 'path';
|
||||
import * as loc from '../localizedConstants';
|
||||
import Logger from './logger';
|
||||
|
||||
const DownloadTimeout = 20000;
|
||||
|
||||
@@ -17,34 +17,33 @@ export namespace HttpClient {
|
||||
* Downloads a file from the given URL, resolving to the full path of the downloaded file when complete
|
||||
* @param downloadUrl The URL to download the file from
|
||||
* @param targetFolder The folder to download the file to
|
||||
* @param outputChannel Channel used to display diagnostic information
|
||||
* @returns Full path to the downloaded file
|
||||
*/
|
||||
export function download(downloadUrl: string, targetFolder: string, outputChannel: vscode.OutputChannel): Promise<string> {
|
||||
export function download(downloadUrl: string, targetFolder: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let totalMegaBytes: number | undefined = undefined;
|
||||
let receivedBytes = 0;
|
||||
let printThreshold = 0.1;
|
||||
let downloadRequest = request.get(downloadUrl, { timeout: DownloadTimeout })
|
||||
.on('error', downloadError => {
|
||||
outputChannel.appendLine(loc.downloadError);
|
||||
outputChannel.appendLine(downloadError?.message ?? downloadError);
|
||||
Logger.log(loc.downloadError);
|
||||
Logger.log(downloadError?.message ?? downloadError);
|
||||
reject(downloadError);
|
||||
})
|
||||
.on('response', (response) => {
|
||||
if (response.statusCode !== 200) {
|
||||
outputChannel.appendLine(loc.downloadError);
|
||||
outputChannel.appendLine(response.statusMessage);
|
||||
Logger.log(loc.downloadError);
|
||||
Logger.log(response.statusMessage);
|
||||
return reject(response.statusMessage);
|
||||
}
|
||||
const filename = path.basename(response.request.path);
|
||||
const targetPath = path.join(targetFolder, filename);
|
||||
outputChannel.appendLine(loc.downloadingTo(filename, targetPath));
|
||||
Logger.log(loc.downloadingTo(filename, targetPath));
|
||||
// Wait to create the WriteStream until here so we can use the actual
|
||||
// filename based off of the URI.
|
||||
downloadRequest.pipe(fs.createWriteStream(targetPath))
|
||||
.on('close', async () => {
|
||||
outputChannel.appendLine(loc.downloadFinished);
|
||||
Logger.log(loc.downloadFinished);
|
||||
resolve(targetPath);
|
||||
})
|
||||
.on('error', (downloadError) => {
|
||||
@@ -54,7 +53,7 @@ export namespace HttpClient {
|
||||
let contentLength = response.headers['content-length'];
|
||||
let totalBytes = parseInt(contentLength || '0');
|
||||
totalMegaBytes = totalBytes / (1024 * 1024);
|
||||
outputChannel.appendLine(loc.downloadingProgressMb('0', totalMegaBytes.toFixed(2)));
|
||||
Logger.log(loc.downloadingProgressMb('0', totalMegaBytes.toFixed(2)));
|
||||
})
|
||||
.on('data', (data) => {
|
||||
receivedBytes += data.length;
|
||||
@@ -62,7 +61,7 @@ export namespace HttpClient {
|
||||
let receivedMegaBytes = receivedBytes / (1024 * 1024);
|
||||
let percentage = receivedMegaBytes / totalMegaBytes;
|
||||
if (percentage >= printThreshold) {
|
||||
outputChannel.appendLine(loc.downloadingProgressMb(receivedMegaBytes.toFixed(2), totalMegaBytes.toFixed(2)));
|
||||
Logger.log(loc.downloadingProgressMb(receivedMegaBytes.toFixed(2), totalMegaBytes.toFixed(2)));
|
||||
printThreshold += 0.1;
|
||||
}
|
||||
}
|
||||
@@ -74,8 +73,8 @@ export namespace HttpClient {
|
||||
* Gets the filename for the specified URL - following redirects as needed
|
||||
* @param url The URL to get the filename of
|
||||
*/
|
||||
export async function getFilename(url: string, outputChannel: vscode.OutputChannel): Promise<string> {
|
||||
outputChannel.appendLine(loc.gettingFilenameOfUrl(url));
|
||||
export async function getFilename(url: string): Promise<string> {
|
||||
Logger.log(loc.gettingFilenameOfUrl(url));
|
||||
return new Promise((resolve, reject) => {
|
||||
let httpRequest = request.get(url, { timeout: DownloadTimeout })
|
||||
.on('error', downloadError => {
|
||||
@@ -88,7 +87,7 @@ export namespace HttpClient {
|
||||
// We don't want to actually download the file so abort the request now
|
||||
httpRequest.abort();
|
||||
const filename = path.basename(response.request.path);
|
||||
outputChannel.appendLine(loc.gotFilenameOfUrl(response.request.path, filename));
|
||||
Logger.log(loc.gotFilenameOfUrl(response.request.path, filename));
|
||||
resolve(filename);
|
||||
});
|
||||
});
|
||||
|
||||
24
extensions/azdata/src/common/logger.ts
Normal file
24
extensions/azdata/src/common/logger.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
|
||||
export class Log {
|
||||
private _output: vscode.OutputChannel;
|
||||
|
||||
constructor() {
|
||||
this._output = vscode.window.createOutputChannel('azdata');
|
||||
}
|
||||
|
||||
log(msg: string): void {
|
||||
this._output.appendLine(msg);
|
||||
}
|
||||
|
||||
show(): void {
|
||||
this._output.show(true);
|
||||
}
|
||||
}
|
||||
const Logger = new Log();
|
||||
export default Logger;
|
||||
@@ -4,15 +4,13 @@
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as azdataExt from 'azdata-ext';
|
||||
import * as vscode from 'vscode';
|
||||
import { findAzdata, IAzdataTool } from './azdata';
|
||||
import * as loc from './localizedConstants';
|
||||
|
||||
let localAzdata: IAzdataTool | undefined = undefined;
|
||||
|
||||
export async function activate(): Promise<azdataExt.IExtension> {
|
||||
const outputChannel = vscode.window.createOutputChannel('azdata');
|
||||
localAzdata = await checkForAzdata(outputChannel);
|
||||
localAzdata = await checkForAzdata();
|
||||
return {
|
||||
azdata: {
|
||||
arc: {
|
||||
@@ -85,24 +83,24 @@ function throwIfNoAzdata(): void {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkForAzdata(outputChannel: vscode.OutputChannel): Promise<IAzdataTool | undefined> {
|
||||
async function checkForAzdata(): Promise<IAzdataTool | undefined> {
|
||||
try {
|
||||
return await findAzdata(outputChannel);
|
||||
return await findAzdata();
|
||||
} catch (err) {
|
||||
// Don't block on this since we want the extension to finish activating without needing user input.
|
||||
// Calls will be made to handle azdata not being installed
|
||||
promptToInstallAzdata(outputChannel).catch(e => console.log(`Unexpected error prompting to install azdata ${e}`));
|
||||
promptToInstallAzdata().catch(e => console.log(`Unexpected error prompting to install azdata ${e}`));
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function promptToInstallAzdata(_outputChannel: vscode.OutputChannel): Promise<void> {
|
||||
async function promptToInstallAzdata(): Promise<void> {
|
||||
//TODO: Figure out better way to display/prompt
|
||||
/*
|
||||
const response = await vscode.window.showErrorMessage(loc.couldNotFindAzdataWithPrompt, loc.install, loc.cancel);
|
||||
if (response === loc.install) {
|
||||
try {
|
||||
await downloadAndInstallAzdata(outputChannel);
|
||||
await downloadAndInstallAzdata();
|
||||
vscode.window.showInformationMessage(loc.azdataInstalled);
|
||||
} catch (err) {
|
||||
// Windows: 1602 is User Cancelling installation - not unexpected so don't display
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import * as azdata from '../azdata';
|
||||
import * as sinon from 'sinon';
|
||||
import * as childProcess from '../common/childProcess';
|
||||
@@ -14,11 +12,6 @@ import * as nock from 'nock';
|
||||
|
||||
describe('azdata', function () {
|
||||
|
||||
let outputChannelMock: TypeMoq.IMock<vscode.OutputChannel>;
|
||||
beforeEach(function (): void {
|
||||
outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
|
||||
|
||||
});
|
||||
afterEach(function (): void {
|
||||
sinon.restore();
|
||||
nock.cleanAll();
|
||||
@@ -33,7 +26,7 @@ describe('azdata', function () {
|
||||
}
|
||||
// Mock call to --version to simulate azdata being installed
|
||||
sinon.stub(childProcess, 'executeCommand').returns(Promise.resolve({ stdout: 'v1.0.0', stderr: '' }));
|
||||
await should(azdata.findAzdata(outputChannelMock.object)).not.be.rejected();
|
||||
await should(azdata.findAzdata()).not.be.rejected();
|
||||
});
|
||||
it('unsuccessful', async function (): Promise<void> {
|
||||
if (process.platform === 'win32') {
|
||||
@@ -43,7 +36,7 @@ describe('azdata', function () {
|
||||
// Mock call to executeCommand to simulate azdata --version returning error
|
||||
sinon.stub(childProcess, 'executeCommand').returns(Promise.reject({ stdout: '', stderr: 'command not found: azdata' }));
|
||||
}
|
||||
await should(azdata.findAzdata(outputChannelMock.object)).be.rejected();
|
||||
await should(azdata.findAzdata()).be.rejected();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -57,7 +50,7 @@ describe('azdata', function () {
|
||||
nock(azdata.azdataHostname)
|
||||
.get(`/${azdata.azdataUri}`)
|
||||
.replyWithFile(200, __filename);
|
||||
const downloadPromise = azdata.downloadAndInstallAzdata(outputChannelMock.object);
|
||||
const downloadPromise = azdata.downloadAndInstallAzdata();
|
||||
await downloadPromise;
|
||||
});
|
||||
|
||||
@@ -65,7 +58,7 @@ describe('azdata', function () {
|
||||
nock('https://aka.ms')
|
||||
.get('/azdata-msi')
|
||||
.reply(404);
|
||||
const downloadPromise = azdata.downloadAndInstallAzdata(outputChannelMock.object);
|
||||
const downloadPromise = azdata.downloadAndInstallAzdata();
|
||||
await should(downloadPromise).be.rejected();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,16 +3,14 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as should from 'should';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import * as sudo from 'sudo-prompt';
|
||||
import * as sinon from 'sinon';
|
||||
import Logger from '../../common/logger';
|
||||
|
||||
import { executeCommand, executeSudoCommand } from '../../common/childProcess';
|
||||
|
||||
describe('ChildProcess', function (): void {
|
||||
const outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
|
||||
|
||||
afterEach(function(): void {
|
||||
sinon.restore();
|
||||
@@ -21,19 +19,20 @@ describe('ChildProcess', function (): void {
|
||||
describe('executeCommand', function(): void {
|
||||
[[], ['test']].forEach(args => {
|
||||
it(`Output channel is used with ${JSON.stringify(args)} args`, async function (): Promise<void> {
|
||||
await executeCommand('echo', args, outputChannelMock.object);
|
||||
outputChannelMock.verify(x => x.appendLine(TypeMoq.It.isAny()), TypeMoq.Times.atLeastOnce());
|
||||
const logStub = sinon.stub(Logger, 'log');
|
||||
await executeCommand('echo', args);
|
||||
should(logStub.called).be.true('Log should have been called');
|
||||
});
|
||||
});
|
||||
|
||||
it('Gets expected output', async function (): Promise<void> {
|
||||
const echoOutput = 'test';
|
||||
const output = await executeCommand('echo', [echoOutput], outputChannelMock.object);
|
||||
const output = await executeCommand('echo', [echoOutput]);
|
||||
should(output.stdout).equal(echoOutput);
|
||||
});
|
||||
|
||||
it('Invalid command errors', async function (): Promise<void> {
|
||||
await should(executeCommand('invalid_command', [], outputChannelMock.object)).be.rejected();
|
||||
await should(executeCommand('invalid_command', [])).be.rejected();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -43,7 +42,7 @@ describe('ChildProcess', function (): void {
|
||||
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
||||
callback!(undefined, stdout);
|
||||
});
|
||||
const result = await executeSudoCommand('echo', outputChannelMock.object);
|
||||
const result = await executeSudoCommand('echo');
|
||||
should(result.stdout).equal(stdout);
|
||||
should(result.stderr).equal('');
|
||||
});
|
||||
@@ -53,7 +52,7 @@ describe('ChildProcess', function (): void {
|
||||
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
||||
callback!(undefined, undefined, stderr);
|
||||
});
|
||||
const result = await executeSudoCommand('echo', outputChannelMock.object);
|
||||
const result = await executeSudoCommand('echo');
|
||||
should(result.stdout).equal('');
|
||||
should(result.stderr).equal(stderr);
|
||||
});
|
||||
@@ -62,7 +61,7 @@ describe('ChildProcess', function (): void {
|
||||
sinon.stub(sudo, 'exec').callsFake( (_cmd, _options, callback) => {
|
||||
callback!(new Error('error'));
|
||||
});
|
||||
await should(executeSudoCommand('invalid_command', outputChannelMock.object)).be.rejected();
|
||||
await should(executeSudoCommand('invalid_command')).be.rejected();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -3,9 +3,7 @@
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as vscode from 'vscode';
|
||||
import * as should from 'should';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
import { HttpClient } from '../../common/httpClient';
|
||||
import * as os from 'os';
|
||||
import * as fs from 'fs';
|
||||
@@ -16,12 +14,6 @@ import { Deferred } from '../../common/promise';
|
||||
|
||||
describe('HttpClient', function (): void {
|
||||
|
||||
let outputChannelMock: TypeMoq.IMock<vscode.OutputChannel>;
|
||||
|
||||
before(function (): void {
|
||||
outputChannelMock = TypeMoq.Mock.ofType<vscode.OutputChannel>();
|
||||
});
|
||||
|
||||
afterEach(function (): void {
|
||||
nock.cleanAll();
|
||||
nock.enableNetConnect();
|
||||
@@ -33,7 +25,7 @@ describe('HttpClient', function (): void {
|
||||
.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);
|
||||
const downloadPath = await HttpClient.download('https://127.0.0.1/README.md', downloadFolder);
|
||||
// Verify file was downloaded correctly
|
||||
await fs.promises.stat(downloadPath);
|
||||
});
|
||||
@@ -43,7 +35,7 @@ describe('HttpClient', function (): void {
|
||||
nock('https://127.0.0.1')
|
||||
.get('/')
|
||||
.replyWithError('Unexpected Error');
|
||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
|
||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder);
|
||||
|
||||
await should(downloadPromise).be.rejected();
|
||||
});
|
||||
@@ -53,7 +45,7 @@ describe('HttpClient', function (): void {
|
||||
nock('https://127.0.0.1')
|
||||
.get('/')
|
||||
.reply(404, '');
|
||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
|
||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder);
|
||||
|
||||
await should(downloadPromise).be.rejected();
|
||||
});
|
||||
@@ -69,7 +61,7 @@ describe('HttpClient', function (): void {
|
||||
nock('https://127.0.0.1')
|
||||
.get('/')
|
||||
.reply(200, '');
|
||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder, outputChannelMock.object);
|
||||
const downloadPromise = HttpClient.download('https://127.0.0.1', downloadFolder);
|
||||
// Wait for the stream to be created before throwing the error or HttpClient will miss the event
|
||||
await deferredPromise;
|
||||
try {
|
||||
@@ -87,7 +79,7 @@ describe('HttpClient', function (): void {
|
||||
nock('https://127.0.0.1')
|
||||
.get(`/${filename}`)
|
||||
.reply(200);
|
||||
const receivedFilename = await HttpClient.getFilename(`https://127.0.0.1/${filename}`, outputChannelMock.object);
|
||||
const receivedFilename = await HttpClient.getFilename(`https://127.0.0.1/${filename}`);
|
||||
|
||||
should(receivedFilename).equal(filename);
|
||||
});
|
||||
@@ -96,7 +88,7 @@ describe('HttpClient', function (): void {
|
||||
nock('https://127.0.0.1')
|
||||
.get('/')
|
||||
.replyWithError('Unexpected Error');
|
||||
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1', outputChannelMock.object);
|
||||
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1');
|
||||
|
||||
await should(getFilenamePromise).be.rejected();
|
||||
});
|
||||
@@ -105,7 +97,7 @@ describe('HttpClient', function (): void {
|
||||
nock('https://127.0.0.1')
|
||||
.get('/')
|
||||
.reply(404, '');
|
||||
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1', outputChannelMock.object);
|
||||
const getFilenamePromise = HttpClient.getFilename('https://127.0.0.1');
|
||||
|
||||
await should(getFilenamePromise).be.rejected();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user