Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70cee3b

Browse files
committed
edge cases of tests being enabled/disabled #239
1 parent 5687d15 commit 70cee3b

File tree

4 files changed

+87
-29
lines changed

4 files changed

+87
-29
lines changed

src/client/unittest/common/baseTestManager.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ export abstract class BaseTestManager {
8888
if (file.errorsWhenDiscovering && file.errorsWhenDiscovering.length > 0) {
8989
haveErrorsInDiscovering = true;
9090
this.outputChannel.appendLine('');
91-
this.outputChannel.append('#'.repeat(10));
92-
this.outputChannel.append(`There was an error in identifying Unit Tests in ${file.nameToRun}`);
93-
this.outputChannel.appendLine('#'.repeat(10));
91+
this.outputChannel.append('_'.repeat(10));
92+
this.outputChannel.append(`There was an error in identifying unit tests in ${file.nameToRun}`);
93+
this.outputChannel.appendLine('_'.repeat(10));
9494
this.outputChannel.appendLine(file.errorsWhenDiscovering);
9595
}
9696
});
@@ -139,7 +139,7 @@ export abstract class BaseTestManager {
139139
if (this.cancellationToken.isCancellationRequested) {
140140
return Promise.reject(reason);
141141
}
142-
displayTestErrorMessage('Errors in discovering Tests, continuing with tests');
142+
displayTestErrorMessage('Errors in discovering tests, continuing with tests');
143143
return <Tests>{
144144
rootTestFolders: [], testFiles: [], testFolders: [], testFunctions: [], testSuits: [],
145145
summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }

src/client/unittest/display/main.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export class TestResultDisplay {
1515
public dispose() {
1616
this.statusBar.dispose();
1717
}
18+
public set enabled(enable: boolean) {
19+
if (enable) {
20+
this.statusBar.show();
21+
}
22+
else {
23+
this.statusBar.hide();
24+
}
25+
}
1826
public DisplayProgressStatus(tests: Promise<Tests>) {
1927
this.displayProgress('Running Tests', `Running Tests (Click to Stop)`, constants.Command_Tests_Stop);
2028
tests
@@ -93,13 +101,13 @@ export class TestResultDisplay {
93101
this.discoverCounter = 0;
94102
}
95103

96-
public DisplayDiscoverStatus(tests: Promise<Tests>) {
104+
public DisplayDiscoverStatus(tests: Promise<Tests>, quietMode: boolean = false) {
97105
this.displayProgress('Discovering Tests', 'Discovering Tests (Click to Stop)', constants.Command_Tests_Stop);
98106
return tests.then(tests => {
99107
this.updateWithDiscoverSuccess(tests);
100108
return tests;
101109
}).catch(reason => {
102-
this.updateWithDiscoverFailure(reason);
110+
this.updateWithDiscoverFailure(reason, quietMode);
103111
return Promise.reject(reason);
104112
});
105113
}
@@ -113,13 +121,13 @@ export class TestResultDisplay {
113121
this.statusBar.show();
114122
}
115123

116-
private updateWithDiscoverFailure(reason: any) {
124+
private updateWithDiscoverFailure(reason: any, quietMode: boolean = false) {
117125
this.clearProgressTicker();
118126
this.statusBar.text = `$(triangle-right) Discover Tests`;
119127
this.statusBar.tooltip = 'Discover Tests';
120128
this.statusBar.command = constants.Command_Tests_Discover;
121129
this.statusBar.show();
122-
if (reason !== CANCELLATION_REASON) {
130+
if (reason !== CANCELLATION_REASON && !quietMode) {
123131
vscode.window.showErrorMessage('There was an error in discovering tests');
124132
}
125133
}

src/client/unittest/display/picker.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ function getSummary(tests?: Tests) {
3939
}
4040
const statusText = [];
4141
if (tests.summary.passed > 0) {
42-
statusText.push(`${constants.Octicon_Test_Pass} ${tests.summary.passed}`);
42+
statusText.push(`${constants.Octicon_Test_Pass} ${tests.summary.passed} Passed`);
4343
}
4444
if (tests.summary.failures > 0) {
45-
statusText.push(`${constants.Octicon_Test_Fail} ${tests.summary.failures}`);
45+
statusText.push(`${constants.Octicon_Test_Fail} ${tests.summary.failures} Failed`);
4646
}
4747
if (tests.summary.errors > 0) {
48-
statusText.push(`${constants.Octicon_Test_Error} ${tests.summary.errors}`);
48+
const plural = tests.summary.errors === 1 ? '' : 's';
49+
statusText.push(`${constants.Octicon_Test_Error} ${tests.summary.errors} Error` + plural);
4950
}
5051
if (tests.summary.skipped > 0) {
51-
statusText.push(`${constants.Octicon_Test_Skip} ${tests.summary.skipped}`);
52+
statusText.push(`${constants.Octicon_Test_Skip} ${tests.summary.skipped} Skipped`);
5253
}
5354
return statusText.join(', ').trim();
5455
}
@@ -69,11 +70,10 @@ function buildItems(tests?: Tests): TestItem[] {
6970
let functionItems: TestItem[] = [];
7071
tests.testFunctions.forEach(fn => {
7172
const classPrefix = fn.parentTestSuite ? fn.parentTestSuite.name + '.' : '';
72-
const icon = statusIconMapping.has(fn.testFunction.status) ? statusIconMapping.get(fn.testFunction.status) + ' ' : '';
7373
functionItems.push({
7474
description: '',
7575
detail: fn.parentTestFile.name,
76-
label: icon + fn.testFunction.name,
76+
label: fn.testFunction.name,
7777
type: Type.RunMethod,
7878
fn: fn
7979
});

src/client/unittest/main.ts

+65-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as nosetests from './nosetest/main';
55
import * as pytest from './pytest/main';
66
import {resolveValueAsTestToRun} from './common/testUtils';
77
import {BaseTestManager} from './common/baseTestManager';
8-
import {PythonSettings} from '../common/configSettings';
8+
import {PythonSettings, IUnitTestSettings} from '../common/configSettings';
99
import {TestResultDisplay} from './display/main';
1010
import {TestFileCodeLensProvider} from './testFileCodeLensProvider';
1111
import {TestDisplay} from './display/picker';
@@ -29,7 +29,10 @@ export function activate(context: vscode.ExtensionContext, outputChannel: vscode
2929
context.subscriptions.push(...disposables);
3030
// Ignore the exceptions returned
3131
// This function is invoked via a command which will be invoked else where in the extension
32-
discoverTests().catch(() => { });
32+
discoverTests(true, true).catch(() => {
33+
// Ignore the errors
34+
let x = '';
35+
});
3336
settings.addListener('change', onConfigChanged);
3437
}
3538
function dispose() {
@@ -57,22 +60,69 @@ function registerCommands(): vscode.Disposable[] {
5760
}
5861

5962
function displayUI() {
63+
let testManager = getTestRunner();
64+
if (!testManager) {
65+
return displayTestFrameworkError();
66+
}
67+
6068
testDisplay = testDisplay ? testDisplay : new TestDisplay();
6169
testDisplay.displayTestUI();
6270
}
6371

64-
let uniTestSettings = JSON.stringify(settings.unitTest);
72+
let uniTestSettingsString = JSON.stringify(settings.unitTest);
6573

6674
function onConfigChanged() {
6775
// Possible that a test framework has been enabled or some settings have changed
6876
// Meaning we need to re-load the discovered tests (as something could have changed)
6977
const newSettings = JSON.stringify(settings.unitTest);
70-
if (uniTestSettings !== newSettings) {
71-
uniTestSettings = newSettings;
72-
discoverTests();
78+
if (uniTestSettingsString === newSettings) {
79+
return;
80+
}
81+
82+
uniTestSettingsString = newSettings;
83+
if (!settings.unitTest.nosetestsEnabled && !settings.unitTest.pyTestEnabled) {
84+
if (testResultDisplay) {
85+
testResultDisplay.enabled = false;
86+
}
87+
88+
if (testManager) {
89+
testManager.stop();
90+
testManager = null;
91+
}
92+
if (pyTestManager) {
93+
pyTestManager.dispose();
94+
pyTestManager = null;
95+
}
96+
if (nosetestManager) {
97+
nosetestManager.dispose();
98+
nosetestManager = null;
99+
}
100+
return;
73101
}
102+
103+
if (testResultDisplay) {
104+
testResultDisplay.enabled = true;
105+
}
106+
107+
// No need to display errors
108+
discoverTests(true, true);
109+
}
110+
function displayTestFrameworkError() {
111+
if (settings.unitTest.pyTestEnabled && settings.unitTest.nosetestsEnabled) {
112+
vscode.window.showErrorMessage("Enable only one of the test frameworks (nosetest or pytest), not both.")
113+
}
114+
else {
115+
vscode.window.showInformationMessage('Please enable one of the test frameworks (pytest or nosetest)');
116+
}
117+
return null;
74118
}
75119
function getTestRunner() {
120+
if (settings.unitTest.pyTestEnabled && settings.unitTest.nosetestsEnabled) {
121+
return null;
122+
}
123+
else if (settings.unitTest.nosetestsEnabled) {
124+
return nosetestManager = nosetestManager ? nosetestManager : new nosetests.TestManager(vscode.workspace.rootPath, outChannel);
125+
}
76126
if (settings.unitTest.pyTestEnabled) {
77127
return pyTestManager = pyTestManager ? pyTestManager : new pytest.TestManager(vscode.workspace.rootPath, outChannel);
78128
}
@@ -89,15 +139,16 @@ function stopTests() {
89139
}
90140
function discoverTests(ignoreCache?: boolean, quietMode: boolean = false) {
91141
let testManager = getTestRunner();
142+
if (!testManager) {
143+
if (!quietMode) {
144+
displayTestFrameworkError();
145+
}
146+
return Promise.resolve(null);
147+
}
92148

93149
if (testManager && (testManager.status !== TestStatus.Discovering && testManager.status !== TestStatus.Running)) {
94-
if (quietMode === true) {
95-
return testManager.discoverTests(ignoreCache, quietMode);
96-
}
97-
else {
98-
testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay(outChannel);
99-
return testResultDisplay.DisplayDiscoverStatus(testManager.discoverTests(ignoreCache));
100-
}
150+
testResultDisplay = testResultDisplay ? testResultDisplay : new TestResultDisplay(outChannel);
151+
return testResultDisplay.DisplayDiscoverStatus(testManager.discoverTests(ignoreCache, quietMode), quietMode);
101152
}
102153
else {
103154
return Promise.resolve(null);
@@ -131,8 +182,7 @@ function identifyTestType(arg?: vscode.Uri | TestsToRun | boolean | FlattenedTes
131182
function runTestsImpl(arg?: vscode.Uri | TestsToRun | boolean | FlattenedTestFunction) {
132183
let testManager = getTestRunner();
133184
if (!testManager) {
134-
vscode.window.showInformationMessage('Please enable one of the test frameworks (pytest or nosetest)');
135-
return;
185+
return displayTestFrameworkError();
136186
}
137187

138188
// lastRanTests = testsToRun;

0 commit comments

Comments
 (0)