Skip to content

Commit e40252c

Browse files
committed
prompt to configure test framework #459
1 parent f5a8ab8 commit e40252c

8 files changed

Lines changed: 93 additions & 25 deletions

File tree

‎package.json‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,11 @@
715715
"default": [],
716716
"description": "List of paths to libraries and the like that need to be imported by auto complete engine. E.g. when using Google App SDK, the paths are not in system path, hence need to be added into this list."
717717
},
718+
"python.unitTest.promptToConfigure": {
719+
"type": "boolean",
720+
"default": true,
721+
"description": "Where to prompt to configure a test framework if potential tests directories are discovered."
722+
},
718723
"python.unitTest.nosetestsEnabled": {
719724
"type": "boolean",
720725
"default": false,
@@ -853,4 +858,4 @@
853858
"vscode": "^1.0.0",
854859
"webpack": "^1.13.2"
855860
}
856-
}
861+
}

‎src/client/common/configSettings.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface ISortImportSettings {
2222
}
2323

2424
export interface IUnitTestSettings {
25+
promptToConfigure: boolean;
2526
nosetestsEnabled: boolean;
2627
nosetestPath: string;
2728
nosetestArgs: string[];
@@ -197,6 +198,7 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
197198

198199
// Support for travis
199200
this.unitTest = this.unitTest ? this.unitTest : {
201+
promptToConfigure:true,
200202
nosetestArgs: [], nosetestPath: 'nosetest', nosetestsEnabled: false,
201203
outputWindow: 'python',
202204
pyTestArgs: [], pyTestEnabled: false, pyTestPath: 'pytest',

‎src/client/unittests/common/testConfigurationManager.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { getSubDirectories } from '../../common/utils';
44
import * as path from 'path';
55

66
export abstract class TestConfigurationManager {
7-
public abstract enable();
8-
public abstract disable();
7+
public abstract enable(): Thenable<any>;
8+
public abstract disable(): Thenable<any>;
99

1010
public abstract configure(rootDir: string): Promise<any>;
1111

‎src/client/unittests/configuration.ts‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { TestConfigurationManager } from './common/testConfigurationManager';
66
import * as nose from './nosetest/testConfigurationManager';
77
import * as pytest from './pytest/testConfigurationManager';
88
import * as unittest from './unittest/testConfigurationManager';
9+
import { getSubDirectories } from '../common/utils';
10+
import * as path from 'path';
911

1012
const settings = PythonSettings.getInstance();
1113

@@ -57,7 +59,6 @@ function promptToEnableAndConfigureTestFramework(messageToDisplay: string = 'Sel
5759
}
5860

5961
if (enableOnly) {
60-
configMgr.enable();
6162
// Ensure others are disabled
6263
if (item.product !== Product.unittest) {
6364
(new unittest.ConfigurationManager()).disable();
@@ -68,17 +69,27 @@ function promptToEnableAndConfigureTestFramework(messageToDisplay: string = 'Sel
6869
if (item.product !== Product.nosetest) {
6970
(new nose.ConfigurationManager()).disable();
7071
}
71-
return Promise.resolve();
72+
return configMgr.enable();
7273
}
7374

7475
// Configure everything before enabling
7576
// Cuz we don't want the test engine (in main.ts file - tests get discovered when config changes are detected)
7677
// to start discovering tests when tests haven't been configured properly
78+
function enableTest(): Thenable<any> {
79+
const pythonConfig = vscode.workspace.getConfiguration('python');
80+
if (settings.unitTest.promptToConfigure) {
81+
return configMgr.enable();
82+
}
83+
return pythonConfig.update('unitTest.promptToConfigure', undefined).then(() => {
84+
return configMgr.enable();
85+
}, reason => {
86+
return configMgr.enable().then(() => Promise.reject(reason));
87+
});
88+
}
7789
return configMgr.configure(vscode.workspace.rootPath).then(() => {
78-
configMgr.enable();
90+
return enableTest();
7991
}).catch(reason => {
80-
configMgr.enable();
81-
return Promise.reject(reason);
92+
return enableTest().then(() => Promise.reject(reason));
8293
});
8394
});
8495
}
@@ -98,4 +109,42 @@ export function displayTestFrameworkError(): Thenable<any> {
98109
return Promise.reject(null);
99110
});
100111
}
112+
}
113+
export function displayPromptToEnableTests(rootDir: string): Thenable<any> {
114+
if (settings.unitTest.pyTestEnabled ||
115+
settings.unitTest.nosetestsEnabled ||
116+
settings.unitTest.unittestEnabled) {
117+
return Promise.reject(null);
118+
}
119+
120+
if (!settings.unitTest.promptToConfigure) {
121+
return Promise.reject(null);
122+
}
123+
124+
const yes = 'Yes';
125+
const no = `No`;
126+
const noNotAgain = `No, don't ask again`;
127+
128+
checkIfHasTestDirs(rootDir).then(hasTests => {
129+
if (!yes) {
130+
return Promise.reject(null);
131+
}
132+
return vscode.window.showInformationMessage('You seem to have tests, would you like to enable a test framework?', yes, no, noNotAgain).then(item => {
133+
if (!item || item === no) {
134+
return Promise.reject(null);
135+
}
136+
if (item === yes) {
137+
return promptToEnableAndConfigureTestFramework();
138+
}
139+
else {
140+
const pythonConfig = vscode.workspace.getConfiguration('python');
141+
return pythonConfig.update('unitTest.promptToConfigure', false);
142+
}
143+
});
144+
});
145+
}
146+
function checkIfHasTestDirs(rootDir: string): Promise<boolean> {
147+
return getSubDirectories(rootDir).then(subDirs => {
148+
return subDirs.map(dir => path.relative(rootDir, dir)).filter(dir => dir.match(/test/i)).length > 0;
149+
});
101150
}

‎src/client/unittests/main.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { TestResultDisplay } from './display/main';
1111
import { TestDisplay } from './display/picker';
1212
import * as constants from '../common/constants';
1313
import { activateCodeLenses } from './codeLenses/main';
14-
import { displayTestFrameworkError } from './configuration';
14+
import { displayTestFrameworkError, displayPromptToEnableTests } from './configuration';
1515

1616
const settings = PythonSettings.getInstance();
1717
let testManager: BaseTestManager;
@@ -22,7 +22,7 @@ let testResultDisplay: TestResultDisplay;
2222
let testDisplay: TestDisplay;
2323
let outChannel: vscode.OutputChannel;
2424

25-
export function activate(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel) {
25+
export function activate(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel) {
2626
context.subscriptions.push({ dispose: dispose });
2727
outChannel = outputChannel;
2828
let disposables = registerCommands();
@@ -35,6 +35,8 @@ export function activate(context: vscode.ExtensionContext, outputChannel: vscode
3535
});
3636
settings.addListener('change', onConfigChanged);
3737
context.subscriptions.push(activateCodeLenses());
38+
39+
displayPromptToEnableTests(vscode.workspace.rootPath);
3840
}
3941
function dispose() {
4042
if (pyTestManager) {
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import * as vscode from 'vscode';
2-
import {TestConfigurationManager} from '../common/testConfigurationManager';
2+
import { TestConfigurationManager } from '../common/testConfigurationManager';
33

44
export class ConfigurationManager extends TestConfigurationManager {
5-
public enable() {
5+
public enable(): Thenable<any> {
66
const pythonConfig = vscode.workspace.getConfiguration('python');
7-
pythonConfig.update('unitTest.nosetestsEnabled', true);
7+
return pythonConfig.update('unitTest.nosetestsEnabled', true);
88
}
9-
public disable() {
9+
public disable(): Thenable<any> {
1010
const pythonConfig = vscode.workspace.getConfiguration('python');
11-
pythonConfig.update('unitTest.nosetestsEnabled', false);
11+
return pythonConfig.update('unitTest.nosetestsEnabled', false);
1212
}
1313

1414
public configure(rootDir: string): Promise<any> {
15+
// TODO:
16+
// 1. Ask if pytest configuration exists
17+
// 2. Ask to create a py test config or use arguments
18+
// 3. Finally check if pytest is installed, if not prompt to install it
19+
// Do we have issues if nose is installed in a separate place (will nose be able to import the files??)
1520
return Promise.resolve();
1621
}
1722
}
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import * as vscode from 'vscode';
2-
import {TestConfigurationManager} from '../common/testConfigurationManager';
2+
import { TestConfigurationManager } from '../common/testConfigurationManager';
33

44
export class ConfigurationManager extends TestConfigurationManager {
5-
public enable() {
5+
public enable(): Thenable<any> {
66
const pythonConfig = vscode.workspace.getConfiguration('python');
7-
pythonConfig.update('unitTest.pyTestEnabled', true);
7+
return pythonConfig.update('unitTest.pyTestEnabled', true);
88
}
9-
public disable() {
9+
public disable(): Thenable<any> {
1010
const pythonConfig = vscode.workspace.getConfiguration('python');
11-
pythonConfig.update('unitTest.pyTestEnabled', false);
11+
return pythonConfig.update('unitTest.pyTestEnabled', false);
1212
}
1313

1414
public configure(rootDir: string): Promise<any> {
15+
// TODO:
16+
// 1. Ask if pytest configuration exists
17+
// 2. Ask to create a py test config or use arguments
18+
// 3. Finally check if pytest is installed, if not prompt to install it
19+
// Do we have issues if pytest is installed in a separate place (will pytest be able to import the files??)
1520
return Promise.resolve();
1621
}
1722
}

‎src/client/unittests/unittest/testConfigurationManager.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import * as path from 'path';
33
import { TestConfigurationManager } from '../common/testConfigurationManager';
44

55
export class ConfigurationManager extends TestConfigurationManager {
6-
public enable() {
6+
public enable(): Thenable<any> {
77
const pythonConfig = vscode.workspace.getConfiguration('python');
8-
pythonConfig.update('unitTest.unittestEnabled', true);
8+
return pythonConfig.update('unitTest.unittestEnabled', true);
99
}
10-
public disable() {
10+
public disable(): Thenable<any> {
1111
const pythonConfig = vscode.workspace.getConfiguration('python');
12-
pythonConfig.update('unitTest.unittestEnabled', false);
12+
return pythonConfig.update('unitTest.unittestEnabled', false);
1313
}
1414

1515
public configure(rootDir: string): Promise<any> {
@@ -36,7 +36,7 @@ export class ConfigurationManager extends TestConfigurationManager {
3636
}
3737
}).then(() => {
3838
const pythonConfig = vscode.workspace.getConfiguration('python');
39-
pythonConfig.update('unitTest.unittestArgs', args);
39+
return pythonConfig.update('unitTest.unittestArgs', args);
4040
});
4141
}
4242
}

0 commit comments

Comments
 (0)