Skip to content

Commit 958f328

Browse files
committed
Clean up test
1 parent 8161ce8 commit 958f328

File tree

4 files changed

+78
-116
lines changed

4 files changed

+78
-116
lines changed

test/featureTests/testAssets/testAssets.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,9 @@
77
import { EventStream } from "../../../src/EventStream";
88
import { PlatformInformation } from "../../../src/platform";
99
import { OmnisharpDownloader } from "../../../src/omnisharp/OmnisharpDownloader";
10-
import { getFakeVsCode, getNullWorkspaceConfiguration } from "../../unitTests/testAssets/Fakes";
11-
import { Uri } from "../../../src/vscodeAdapter";
1210
import NetworkSettings from "../../../src/NetworkSettings";
1311

14-
1512
export function GetTestOmnisharpDownloader(sink: EventStream, platformInfo: PlatformInformation): OmnisharpDownloader {
16-
let vscode = getFakeVsCode();
17-
vscode.workspace.getConfiguration = (section?: string, resource?: Uri) => {
18-
return {
19-
...getNullWorkspaceConfiguration(),
20-
};
21-
};
22-
2313
return new OmnisharpDownloader(() => new NetworkSettings(undefined, undefined), sink, testPackageJSON, platformInfo);
2414
}
2515

test/unitTests/logging/InformationMessageObserver.test.ts

Lines changed: 38 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55

66
import { InformationMessageObserver } from '../../../src/observers/InformationMessageObserver';
77
import { use as chaiUse, expect, should } from 'chai';
8-
import { vscode, Uri } from '../../../src/vscodeAdapter';
9-
import { getFakeVsCode, getNullWorkspaceConfiguration, getUnresolvedDependenices } from '../testAssets/Fakes';
8+
import { getUnresolvedDependenices, updateConfig, getVSCodeWithConfig } from '../testAssets/Fakes';
109
import { Observable } from 'rxjs/Observable';
1110
import 'rxjs/add/observable/fromPromise';
1211
import 'rxjs/add/operator/timeout';
@@ -23,36 +22,12 @@ suite("InformationMessageObserver", () => {
2322
let commandDone = new Promise<void>(resolve => {
2423
signalCommandDone = () => { resolve(); };
2524
});
26-
let vscode: vscode = getFakeVsCode();
25+
let vscode = getVsCode();
2726
let infoMessage: string;
2827
let relativePath: string;
2928
let invokedCommand: string;
3029
let observer: InformationMessageObserver = new InformationMessageObserver(vscode);
3130

32-
vscode.window.showInformationMessage = async (message: string, ...items: string[]) => {
33-
infoMessage = message;
34-
return new Promise<string>(resolve => {
35-
doClickCancel = () => {
36-
resolve(undefined);
37-
};
38-
39-
doClickOk = () => {
40-
resolve(message);
41-
};
42-
});
43-
};
44-
45-
vscode.commands.executeCommand = (command: string, ...rest: any[]) => {
46-
invokedCommand = command;
47-
signalCommandDone();
48-
return undefined;
49-
};
50-
51-
vscode.workspace.asRelativePath = (pathOrUri?: string, includeWorspaceFolder?: boolean) => {
52-
relativePath = pathOrUri;
53-
return relativePath;
54-
};
55-
5631
setup(() => {
5732
infoMessage = undefined;
5833
relativePath = undefined;
@@ -66,34 +41,16 @@ suite("InformationMessageObserver", () => {
6641
let event = getUnresolvedDependenices("someFile");
6742

6843
suite('Suppress Dotnet Restore Notification is true', () => {
69-
setup(() => {
70-
vscode.workspace.getConfiguration = (section?: string, resource?: Uri) => {
71-
return {
72-
...getNullWorkspaceConfiguration(),
73-
get: <T>(section: string) => {
74-
return true;// suppress the restore information
75-
}
76-
};
77-
};
78-
});
44+
setup(() => updateConfig(vscode, 'csharp', 'suppressDotnetRestoreNotification', true));
7945

80-
test('The information message is not shown', () => {
46+
test('The information message is not shown', () => {
8147
observer.post(event);
8248
expect(infoMessage).to.be.undefined;
8349
});
8450
});
85-
51+
8652
suite('Suppress Dotnet Restore Notification is false', () => {
87-
setup(() => {
88-
vscode.workspace.getConfiguration = (section?: string, resource?: Uri) => {
89-
return {
90-
...getNullWorkspaceConfiguration(),
91-
get: <T>(section: string) => {
92-
return false; // do not suppress the restore info
93-
}
94-
};
95-
};
96-
});
53+
setup(() => updateConfig(vscode, 'csharp', 'suppressDotnetRestoreNotification', false));
9754

9855
test('The information message is shown', async () => {
9956
observer.post(event);
@@ -103,20 +60,49 @@ suite("InformationMessageObserver", () => {
10360
await commandDone;
10461
expect(invokedCommand).to.be.equal('dotnet.restore');
10562
});
106-
63+
10764
test('Given an information message if the user clicks Restore, the command is executed', async () => {
10865
observer.post(event);
10966
doClickOk();
11067
await commandDone;
11168
expect(invokedCommand).to.be.equal('dotnet.restore');
11269
});
113-
70+
11471
test('Given an information message if the user clicks cancel, the command is not executed', async () => {
11572
observer.post(event);
11673
doClickCancel();
11774
await expect(Observable.fromPromise(commandDone).timeout(1).toPromise()).to.be.rejected;
11875
expect(invokedCommand).to.be.undefined;
11976
});
120-
});
77+
});
12178
});
79+
80+
function getVsCode() {
81+
let vscode = getVSCodeWithConfig();
82+
vscode.window.showInformationMessage = async (message: string, ...items: string[]) => {
83+
infoMessage = message;
84+
return new Promise<string>(resolve => {
85+
doClickCancel = () => {
86+
resolve(undefined);
87+
};
88+
89+
doClickOk = () => {
90+
resolve(message);
91+
};
92+
});
93+
};
94+
95+
vscode.commands.executeCommand = (command: string, ...rest: any[]) => {
96+
invokedCommand = command;
97+
signalCommandDone();
98+
return undefined;
99+
};
100+
101+
vscode.workspace.asRelativePath = (pathOrUri?: string, includeWorspaceFolder?: boolean) => {
102+
relativePath = pathOrUri;
103+
return relativePath;
104+
};
105+
106+
return vscode;
107+
}
122108
});

test/unitTests/options.test.ts

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,15 @@
55

66
import { should, expect } from 'chai';
77
import { Options } from '../../src/omnisharp/options';
8-
import { getFakeVsCode, getWorkspaceConfiguration } from './testAssets/Fakes';
9-
import { WorkspaceConfiguration } from '../../src/vscodeAdapter';
10-
11-
function getVSCode(omnisharpConfig?: WorkspaceConfiguration, csharpConfig?: WorkspaceConfiguration) {
12-
const vscode = getFakeVsCode();
13-
14-
const _omnisharpConfig = omnisharpConfig || getWorkspaceConfiguration();
15-
const _csharpConfig = csharpConfig || getWorkspaceConfiguration();
16-
17-
vscode.workspace.getConfiguration = (section?, resource?) =>
18-
{
19-
if (section === 'omnisharp')
20-
{
21-
return _omnisharpConfig;
22-
}
23-
24-
if (section === 'csharp')
25-
{
26-
return _csharpConfig;
27-
}
28-
29-
return undefined;
30-
};
31-
32-
return vscode;
33-
}
8+
import { getWorkspaceConfiguration, getVSCodeWithConfig } from './testAssets/Fakes';
349

3510
suite("Options tests", () => {
3611
suiteSetup(() => should());
3712

3813
test('Verify defaults', () =>
3914
{
40-
const vscode = getVSCode();
15+
const vscode = getVSCodeWithConfig();
4116
const options = Options.Read(vscode);
42-
4317
expect(options.path).to.be.null;
4418
options.useGlobalMono.should.equal("auto");
4519
options.waitForDebugger.should.equal(false);
@@ -59,7 +33,7 @@ suite("Options tests", () => {
5933
{
6034
const omnisharpConfig = getWorkspaceConfiguration();
6135
omnisharpConfig.update('loggingLevel', "verbose");
62-
const vscode = getVSCode(omnisharpConfig);
36+
const vscode = getVSCodeWithConfig(omnisharpConfig);
6337

6438
const options = Options.Read(vscode);
6539

@@ -70,7 +44,7 @@ suite("Options tests", () => {
7044
{
7145
const omnisharpConfig = getWorkspaceConfiguration();
7246
omnisharpConfig.update('useMono', true);
73-
const vscode = getVSCode(omnisharpConfig);
47+
const vscode = getVSCodeWithConfig(omnisharpConfig);
7448

7549
const options = Options.Read(vscode);
7650

@@ -81,7 +55,7 @@ suite("Options tests", () => {
8155
{
8256
const omnisharpConfig = getWorkspaceConfiguration();
8357
omnisharpConfig.update('useMono', false);
84-
const vscode = getVSCode(omnisharpConfig);
58+
const vscode = getVSCodeWithConfig(omnisharpConfig);
8559

8660
const options = Options.Read(vscode);
8761

@@ -92,7 +66,7 @@ suite("Options tests", () => {
9266
{
9367
const csharpConfig = getWorkspaceConfiguration();
9468
csharpConfig.update('omnisharpUsesMono', true);
95-
const vscode = getVSCode(undefined, csharpConfig);
69+
const vscode = getVSCodeWithConfig(undefined, csharpConfig);
9670

9771
const options = Options.Read(vscode);
9872

@@ -103,7 +77,7 @@ suite("Options tests", () => {
10377
{
10478
const csharpConfig = getWorkspaceConfiguration();
10579
csharpConfig.update('omnisharpUsesMono', false);
106-
const vscode = getVSCode(undefined, csharpConfig);
80+
const vscode = getVSCodeWithConfig(undefined, csharpConfig);
10781

10882
const options = Options.Read(vscode);
10983

@@ -114,7 +88,7 @@ suite("Options tests", () => {
11488
{
11589
const csharpConfig = getWorkspaceConfiguration();
11690
csharpConfig.update('omnisharp', 'OldPath');
117-
const vscode = getVSCode(undefined, csharpConfig);
91+
const vscode = getVSCodeWithConfig(undefined, csharpConfig);
11892

11993
const options = Options.Read(vscode);
12094

@@ -127,7 +101,7 @@ suite("Options tests", () => {
127101
omnisharpConfig.update('path', 'NewPath');
128102
const csharpConfig = getWorkspaceConfiguration();
129103
csharpConfig.update('omnisharp', 'OldPath');
130-
const vscode = getVSCode(omnisharpConfig, csharpConfig);
104+
const vscode = getVSCodeWithConfig(omnisharpConfig, csharpConfig);
131105

132106
const options = Options.Read(vscode);
133107

test/unitTests/testAssets/Fakes.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as vscode from '../../../src/vscodeAdapter';
77
import * as protocol from '../../../src/omnisharp/protocol';
8-
import { DocumentSelector, MessageItem, TextDocument, Uri, GlobPattern } from '../../../src/vscodeAdapter';
8+
import { DocumentSelector, MessageItem, TextDocument, Uri, GlobPattern, WorkspaceConfiguration } from '../../../src/vscodeAdapter';
99
import { ITelemetryReporter } from '../../../src/observers/TelemetryObserver';
1010
import { MSBuildDiagnosticsMessage } from '../../../src/omnisharp/protocol';
1111
import { OmnisharpServerMsBuildProjectDiagnostics, OmnisharpServerOnError, OmnisharpServerUnresolvedDependencies, WorkspaceInformationUpdated } from '../../../src/omnisharp/loggingEvents';
@@ -31,23 +31,6 @@ export const getNullTelemetryReporter = (): ITelemetryReporter => {
3131
return reporter;
3232
};
3333

34-
export const getNullWorkspaceConfiguration = (): vscode.WorkspaceConfiguration => {
35-
let configuration: vscode.WorkspaceConfiguration = {
36-
get: <T>(section: string): T | undefined => {
37-
return undefined;
38-
},
39-
has: (section: string) => { return undefined; },
40-
inspect: () => {
41-
return {
42-
key: undefined
43-
};
44-
},
45-
update: async () => { return Promise.resolve(); }
46-
};
47-
48-
return configuration;
49-
};
50-
5134
export const getWorkspaceConfiguration = (): vscode.WorkspaceConfiguration => {
5235
let values: { [key: string]: any } = {};
5336

@@ -164,4 +147,33 @@ export function getWorkspaceInformationUpdated(msbuild: protocol.MsBuildWorkspac
164147
};
165148

166149
return new WorkspaceInformationUpdated(a);
167-
}
150+
}
151+
152+
export function getVSCodeWithConfig(omnisharpConfig?: WorkspaceConfiguration, csharpConfig?: WorkspaceConfiguration) {
153+
const vscode = getFakeVsCode();
154+
155+
const _omnisharpConfig = omnisharpConfig || getWorkspaceConfiguration();
156+
const _csharpConfig = csharpConfig || getWorkspaceConfiguration();
157+
158+
vscode.workspace.getConfiguration = (section?, resource?) =>
159+
{
160+
if (section === 'omnisharp')
161+
{
162+
return _omnisharpConfig;
163+
}
164+
165+
if (section === 'csharp')
166+
{
167+
return _csharpConfig;
168+
}
169+
170+
return undefined;
171+
};
172+
173+
return vscode;
174+
}
175+
176+
export function updateConfig(vscode: vscode.vscode, section: string, config: string, value: any) {
177+
let workspaceConfig = vscode.workspace.getConfiguration(section);
178+
workspaceConfig.update(config, value);
179+
}

0 commit comments

Comments
 (0)