Skip to content

Commit 20b8879

Browse files
committed
Tests for the warning message display
1 parent 81a92ba commit 20b8879

File tree

8 files changed

+60
-57
lines changed

8 files changed

+60
-57
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
"runtimeDependencies": [
145145
{
146146
"description": "OmniSharp for Windows (.NET 4.6 / x86)",
147-
"url": "https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback",
147+
"url": "https://download.visualstudio.microsoft.com/download/pr/11655913/afaf4c0f0bee0304c8b1c55dfc190a9f/omnisharp-win-x86-1.29.1.zip",
148148
"fallbackUrl": "https://omnisharpdownload.blob.core.windows.net/ext/omnisharp-win-x86-1.29.1.zip",
149149
"installPath": ".omnisharp/1.29.1",
150150
"platforms": [
@@ -158,7 +158,7 @@
158158
},
159159
{
160160
"description": "OmniSharp for Windows (.NET 4.6 / x64)",
161-
"url": "https://nodejs.org/api/stream.html#stream_writable_write_chunk_encoding_callback",
161+
"url": "https://download.visualstudio.microsoft.com/download/pr/11655914/fc5dedbd95030e4015ffc24e48177255/omnisharp-win-x64-1.29.1.zip",
162162
"fallbackUrl": "https://omnisharpdownload.blob.core.windows.net/ext/omnisharp-win-x64-1.29.1.zip",
163163
"installPath": ".omnisharp/1.29.1",
164164
"platforms": [

src/observers/WarningMessageObserver.ts

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import * as fs from 'async-file';
76
import { MessageItem, vscode } from '../vscodeAdapter';
8-
import { BaseEvent, OmnisharpServerOnError, OmnisharpServerMsBuildProjectDiagnostics, ZipFileError } from "../omnisharp/loggingEvents";
7+
import { BaseEvent, OmnisharpServerOnError, OmnisharpServerMsBuildProjectDiagnostics, ArchiveError } from "../omnisharp/loggingEvents";
98
import { Scheduler } from 'rxjs/Scheduler';
109
import { Subject } from 'rxjs/Subject';
1110
import 'rxjs/add/operator/debounceTime';
12-
import { CreateTmpFile } from '../CreateTmpAsset';
13-
import * as vscode1 from 'vscode';
1411

1512
export interface MessageItemWithCommand extends MessageItem {
1613
command: string;
@@ -23,16 +20,7 @@ export class WarningMessageObserver {
2320
this.warningMessageDebouncer = new Subject<BaseEvent>();
2421
this.warningMessageDebouncer.debounceTime(1500, scheduler).subscribe(async event => {
2522
let message = "Some projects have trouble loading. Please review the output for more details.";
26-
let value: MessageItemWithCommand;
27-
try {
28-
value = await this.vscode.window.showWarningMessage<MessageItemWithCommand>(message, { title: "Show Output", command: 'o.showOutput' });
29-
if (value) {
30-
await this.vscode.commands.executeCommand<string>(value.command);
31-
}
32-
}
33-
catch (err) {
34-
console.log(err);
35-
}
23+
await showWarningMessage(this.vscode, message, { title: "Show Output", command: 'o.showOutput' });
3624
});
3725
}
3826

@@ -44,8 +32,8 @@ export class WarningMessageObserver {
4432
case OmnisharpServerMsBuildProjectDiagnostics.name:
4533
this.handleOmnisharpServerMsBuildProjectDiagnostics(<OmnisharpServerMsBuildProjectDiagnostics>event);
4634
break;
47-
case ZipFileError.name:
48-
this.handleZipFileError(<ZipFileError>event);
35+
case ArchiveError.name:
36+
this.handleArchiveError(<ArchiveError>event);
4937
break;
5038
}
5139
}
@@ -56,26 +44,19 @@ export class WarningMessageObserver {
5644
}
5745
}
5846

59-
private async handleZipFileError(event: ZipFileError) {
60-
let tmpFile = await CreateTmpFile();
61-
let writestream = fs.createWriteStream(tmpFile.name);
62-
writestream.write(event.content);
63-
//once we have written to the file show a warning message to open it
64-
let value: MessageItemWithCommand;
65-
let message = " The downloaded file is not a zip. Please review your proxy settings or view the downloaded file below.";
66-
try {
67-
//let file = "C:\\Users\\akagarw\\AppData\\Local\\Temp\\package-32592bbSNWBM3SWGP";
68-
let file = vscode1.Uri.file(tmpFile.name);
69-
value = await this.vscode.window.showWarningMessage<MessageItemWithCommand>(message, { title: "View downloaded file", command: 'vscode.open' });
70-
if (value) {
71-
await this.vscode.commands.executeCommand<string>(value.command,file);
72-
}
73-
}
74-
catch (err) {
75-
console.log(err);
76-
}
77-
finally {
78-
tmpFile.dispose();
47+
private async handleArchiveError(event: ArchiveError) {
48+
await showWarningMessage(this.vscode, event.message);
49+
}
50+
}
51+
52+
async function showWarningMessage(vscode: vscode, message: string, ...items: MessageItemWithCommand[]) {
53+
try {
54+
let value = await vscode.window.showWarningMessage<MessageItemWithCommand>(message, ...items);
55+
if (value && value.command) {
56+
await vscode.commands.executeCommand<string>(value.command);
7957
}
8058
}
59+
catch (err) {
60+
console.log(err);
61+
}
8162
}

src/omnisharp/loggingEvents.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ export class DownloadSizeObtained implements BaseEvent {
118118
constructor(public packageSize: number) { }
119119
}
120120

121-
export class ZipFileError implements BaseEvent {
122-
constructor(public content: string) { }
121+
export class ArchiveError implements BaseEvent {
122+
constructor(public message: string) { }
123123
}
124124

125125
export class DebuggerPrerequisiteFailure extends EventWithMessage { }

src/packageManager/ZipInstaller.ts renamed to src/packageManager/ArchiveInstaller.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import * as mkdirp from 'mkdirp';
88
import * as path from 'path';
99
import * as yauzl from 'yauzl';
1010
import { EventStream } from "../EventStream";
11-
import { InstallationStart, ZipFileError } from "../omnisharp/loggingEvents";
11+
import { InstallationStart, ArchiveError } from "../omnisharp/loggingEvents";
1212
import { NestedError } from '../NestedError';
1313

14-
export async function InstallZip(buffer: Buffer, description: string, destinationInstallPath: string, binaries: string[], eventStream: EventStream): Promise<void> {
14+
export async function InstallArchive(buffer: Buffer, description: string, destinationInstallPath: string, binaries: string[], eventStream: EventStream): Promise<void> {
1515
eventStream.post(new InstallationStart(description));
1616

1717
return new Promise<void>((resolve, reject) => {
18-
yauzl.fromBuffer(buffer, { lazyEntries: true }, async (err, zipFile) => {
18+
yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipFile) => {
1919
if (err) {
20-
eventStream.post(new ZipFileError(buffer.toString()));
21-
return reject(new NestedError('Immediate zip file error', err));
20+
eventStream.post(new ArchiveError("C# Extension was unable to download its dependencies. Please check your internet connection. If you use a proxy server, please visit https://aka.ms/VsCodeCsharpNetworking"));
21+
return reject(new NestedError('Corrupted archive error'));
2222
}
2323

2424
zipFile.readEntry();

src/packageManager/PackageManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Package } from './Package';
88
import { PackageError } from './PackageError';
99
import { NestedError } from "../NestedError";
1010
import { DownloadFile } from './FileDownloader';
11-
import { InstallZip } from './ZipInstaller';
11+
import { InstallArchive } from './ArchiveInstaller';
1212
import { EventStream } from '../EventStream';
1313
import { NetworkSettingsProvider } from "../NetworkSettings";
1414
import { filterPackages } from "./PackageFilterer";
@@ -21,7 +21,7 @@ export async function DownloadAndInstallPackages(packages: Package[], provider:
2121
for (let pkg of filteredPackages) {
2222
try {
2323
let buffer = await DownloadFile(pkg.description, eventStream, provider, pkg.url, pkg.fallbackUrl);
24-
await InstallZip(buffer, pkg.description, pkg.installPath, pkg.binaries, eventStream);
24+
await InstallArchive(buffer, pkg.description, pkg.installPath, pkg.binaries, eventStream);
2525
}
2626
catch (error) {
2727
if (error instanceof NestedError) {

src/vscodeAdapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ export interface vscode {
899899
window: {
900900
activeTextEditor: TextEditor | undefined;
901901
showInformationMessage: (message: string, ...items: string[]) => Thenable<string | undefined>;
902-
showWarningMessage: <T extends MessageItem>(message: string, ...items: T[]) => Thenable<T | undefined>;
902+
showWarningMessage: <T extends MessageItem>(message: string, ...items: T[]) => Thenable<T | undefined>;
903903
};
904904
workspace: {
905905
getConfiguration: (section?: string, resource?: Uri) => WorkspaceConfiguration;

test/unitTests/Packages/ZipInstaller.test.ts renamed to test/unitTests/Packages/ArchiveInstaller.test.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ import * as path from 'path';
88
import * as chai from 'chai';
99
import * as util from '../../../src/common';
1010
import { CreateTmpDir, TmpAsset } from '../../../src/CreateTmpAsset';
11-
import { InstallZip } from '../../../src/packageManager/ZipInstaller';
11+
import { InstallArchive } from '../../../src/packageManager/ArchiveInstaller';
1212
import { EventStream } from '../../../src/EventStream';
1313
import { PlatformInformation } from '../../../src/platform';
14-
import { BaseEvent, InstallationStart } from '../../../src/omnisharp/loggingEvents';
14+
import { BaseEvent, InstallationStart, ArchiveError } from '../../../src/omnisharp/loggingEvents';
1515
import { Files, Binaries, createTestZipAsync } from '../testAssets/CreateTestZip';
1616

1717
chai.use(require("chai-as-promised"));
1818
let expect = chai.expect;
1919

20-
suite('ZipInstaller', () => {
20+
suite('ArchiveInstaller', () => {
2121
let tmpInstallDir: TmpAsset;
2222
let installationPath: string;
2323
let testBuffer: Buffer;
@@ -38,15 +38,15 @@ suite('ZipInstaller', () => {
3838
});
3939

4040
test('The folder is unzipped and all the files are present at the expected paths', async () => {
41-
await InstallZip(testBuffer, fileDescription, installationPath, [], eventStream);
41+
await InstallArchive(testBuffer, fileDescription, installationPath, [], eventStream);
4242
for (let elem of allFiles) {
4343
let filePath = path.join(installationPath, elem.path);
4444
expect(await util.fileExists(filePath)).to.be.true;
4545
}
4646
});
4747

4848
test('The folder is unzipped and all the expected events are created', async () => {
49-
await InstallZip(testBuffer, fileDescription, installationPath, [], eventStream);
49+
await InstallArchive(testBuffer, fileDescription, installationPath, [], eventStream);
5050
let eventSequence: BaseEvent[] = [
5151
new InstallationStart(fileDescription)
5252
];
@@ -56,7 +56,7 @@ suite('ZipInstaller', () => {
5656
test('The folder is unzipped and the binaries have the expected permissions(except on Windows)', async () => {
5757
if (!((await PlatformInformation.GetCurrent()).isWindows())) {
5858
let resolvedBinaryPaths = Binaries.map(binary => path.join(installationPath, binary.path));
59-
await InstallZip(testBuffer, fileDescription, installationPath, resolvedBinaryPaths, eventStream);
59+
await InstallArchive(testBuffer, fileDescription, installationPath, resolvedBinaryPaths, eventStream);
6060
for (let binaryPath of resolvedBinaryPaths) {
6161
expect(await util.fileExists(binaryPath)).to.be.true;
6262
let mode = (await fs.stat(binaryPath)).mode;
@@ -65,8 +65,21 @@ suite('ZipInstaller', () => {
6565
}
6666
});
6767

68-
test('Error is thrown when the file is not a zip', async () => {
69-
expect(InstallZip(new Buffer("My file", "utf8"), "Text File", installationPath, [], eventStream)).to.be.rejected;
68+
test('Error is thrown when the buffer contains an invalid archive', async () => {
69+
expect(InstallArchive(new Buffer("My file", "utf8"), "Text File", installationPath, [], eventStream)).to.be.rejected;
70+
});
71+
72+
test('Error event is created when the buffer contains an invalid archive', async () => {
73+
try {
74+
await InstallArchive(new Buffer("some content", "utf8"), "Text File", installationPath, [], eventStream);
75+
}
76+
catch{
77+
let eventSequence: BaseEvent[] = [
78+
new InstallationStart("Text File"),
79+
new ArchiveError("C# Extension was unable to download its dependencies. Please check your internet connection. If you use a proxy server, please visit https://aka.ms/VsCodeCsharpNetworking")
80+
];
81+
expect(eventBus).to.be.deep.equal(eventSequence);
82+
}
7083
});
7184

7285
teardown(async () => {

test/unitTests/logging/WarningMessageObserver.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { WarningMessageObserver } from '../../../src/observers/WarningMessageObserver';
77
import { assert, use as chaiUse, expect, should } from 'chai';
88
import { getFakeVsCode, getMSBuildDiagnosticsMessage, getOmnisharpMSBuildProjectDiagnosticsEvent, getOmnisharpServerOnErrorEvent } from '../testAssets/Fakes';
9-
import { BaseEvent } from '../../../src/omnisharp/loggingEvents';
9+
import { BaseEvent, ArchiveError } from '../../../src/omnisharp/loggingEvents';
1010
import { vscode } from '../../../src/vscodeAdapter';
1111
import { TestScheduler } from 'rxjs/testing/TestScheduler';
1212
import { Observable } from 'rxjs/Observable';
@@ -172,6 +172,15 @@ suite('WarningMessageObserver', () => {
172172
});
173173
});
174174
});
175+
176+
suite('ArchiveError', () => {
177+
test('When the event is fired then a warning message is displayed', () => {
178+
let event = new ArchiveError("This is an error");
179+
observer.post(event);
180+
expect(warningMessages.length).to.be.equal(1);
181+
expect(warningMessages[0]).to.be.equal("This is an error");
182+
});
183+
});
175184
});
176185

177186
function timeToMarble(timeinMilliseconds: number): string {

0 commit comments

Comments
 (0)