Skip to content

Commit a167131

Browse files
Don JayamanneDon Jayamanne
authored andcommitted
2 parents 88846d0 + 749792a commit a167131

4 files changed

Lines changed: 86 additions & 1 deletion

File tree

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
"onCommand:python.viewTestOutput",
5656
"onCommand:python.selectAndRunTestMethod",
5757
"onCommand:python.selectAndDebugTestMethod",
58+
"onCommand:python.selectAndRunTestFile",
59+
"onCommand:python.runCurrentTestFile",
5860
"onCommand:python.runFailedTests",
5961
"onCommand:python.execSelectionInTerminal",
6062
"onCommand:python.execSelectionInDjangoShell",
@@ -161,6 +163,16 @@
161163
"title": "Debug Unit Test Method ...",
162164
"category": "Python"
163165
},
166+
{
167+
"command": "python.selectAndRunTestFile",
168+
"title": "Run Unit Test File ...",
169+
"category": "Python"
170+
},
171+
{
172+
"command": "python.runCurrentTestFile",
173+
"title": "Run Current Unit Test File",
174+
"category": "Python"
175+
},
164176
{
165177
"command": "python.runFailedTests",
166178
"title": "Run Failed Unit Tests",

src/client/common/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ export namespace Commands {
2020
export const Tests_ViewOutput = 'python.viewTestOutput';
2121
export const Tests_Select_And_Run_Method = 'python.selectAndRunTestMethod';
2222
export const Tests_Select_And_Debug_Method = 'python.selectAndDebugTestMethod';
23+
export const Tests_Select_And_Run_File = 'python.selectAndRunTestFile';
24+
export const Tests_Run_Current_File = 'python.runCurrentTestFile';
2325
export const Refactor_Extract_Variable = 'python.refactorExtractVariable';
2426
export const Refaactor_Extract_Method = 'python.refactorExtractMethod';
2527
export const Update_SparkLibrary = 'python.updateSparkLibrary';

src/client/unittests/display/picker.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { QuickPickItem, window } from 'vscode';
22
import * as vscode from 'vscode';
3-
import { Tests, TestFunction, FlattenedTestFunction, TestStatus } from '../common/contracts';
3+
import { Tests, TestFile, TestFunction, FlattenedTestFunction, TestStatus } from '../common/contracts';
44
import { getDiscoveredTests } from '../common/testUtils';
55
import * as constants from '../../common/constants';
66
import * as path from 'path';
@@ -30,6 +30,17 @@ export class TestDisplay {
3030
}, reject);
3131
});
3232
}
33+
public selectTestFile(rootDirectory: string, tests: Tests): Promise<TestFile> {
34+
return new Promise<TestFile>((resolve, reject) => {
35+
window.showQuickPick(buildItemsForTestFiles(rootDirectory, tests.testFiles), { matchOnDescription: true, matchOnDetail: true })
36+
.then(item => {
37+
if (item && item.testFile) {
38+
return resolve(item.testFile);
39+
}
40+
return reject();
41+
}, reject);
42+
});
43+
}
3344
public displayFunctionTestPickerUI(rootDirectory: string, fileName: string, testFunctions: TestFunction[], debug?: boolean) {
3445
const tests = getDiscoveredTests();
3546
if (!tests) {
@@ -73,6 +84,10 @@ interface TestItem extends QuickPickItem {
7384
type: Type;
7485
fn?: FlattenedTestFunction;
7586
}
87+
interface TestFileItem extends QuickPickItem {
88+
type: Type;
89+
testFile?: TestFile;
90+
}
7691
function getSummary(tests?: Tests) {
7792
if (!tests || !tests.summary) {
7893
return '';
@@ -150,6 +165,27 @@ function buildItemsForFunctions(rootDirectory: string, tests: FlattenedTestFunct
150165
});
151166
return functionItems;
152167
}
168+
function buildItemsForTestFiles(rootDirectory: string, testFiles: TestFile[]): TestFileItem[] {
169+
let fileItems: TestFileItem[] = testFiles.map(testFile => {
170+
return {
171+
description: '',
172+
detail: path.relative(rootDirectory, testFile.fullPath),
173+
type: Type.RunFile,
174+
label: path.basename(testFile.fullPath),
175+
testFile: testFile
176+
}
177+
})
178+
fileItems.sort((a, b) => {
179+
if (a.detail < b.detail) {
180+
return -1;
181+
}
182+
if (a.detail > b.detail) {
183+
return 1;
184+
}
185+
return 0;
186+
})
187+
return fileItems;
188+
}
153189
function onItemSelected(selection: TestItem, debug?: boolean) {
154190
if (!selection || typeof selection.type !== 'number') {
155191
return;

src/client/unittests/main.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ function registerCommands(): vscode.Disposable[] {
7070
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Ask_To_Stop_Test, () => displayStopUI('Stop running tests')));
7171
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Select_And_Run_Method, () => selectAndRunTestMethod()));
7272
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Select_And_Debug_Method, () => selectAndRunTestMethod(true)));
73+
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Select_And_Run_File, () => selectAndRunTestFile()));
74+
disposables.push(vscode.commands.registerCommand(constants.Commands.Tests_Run_Current_File, () => runCurrentTestFile()));
7375

7476
return disposables;
7577
}
@@ -105,6 +107,39 @@ function selectAndRunTestMethod(debug?: boolean) {
105107
}).catch(() => { });
106108
});
107109
}
110+
function selectAndRunTestFile() {
111+
let testManager = getTestRunner();
112+
if (!testManager) {
113+
return displayTestFrameworkError(outChannel);
114+
}
115+
testManager.discoverTests(true, true).then(() => {
116+
const tests = getDiscoveredTests();
117+
testDisplay = testDisplay ? testDisplay : new TestDisplay();
118+
testDisplay.selectTestFile(vscode.workspace.rootPath, tests).then(testFile => {
119+
runTestsImpl({testFile: [testFile]});
120+
}).catch(() => { });
121+
});
122+
}
123+
function runCurrentTestFile() {
124+
if (!vscode.window.activeTextEditor) {
125+
return;
126+
}
127+
const currentFilePath = vscode.window.activeTextEditor.document.fileName;
128+
let testManager = getTestRunner();
129+
if (!testManager) {
130+
return displayTestFrameworkError(outChannel);
131+
}
132+
testManager.discoverTests(true, true).then(() => {
133+
const tests = getDiscoveredTests();
134+
const testFiles = tests.testFiles.filter(testFile => {
135+
return testFile.fullPath == currentFilePath;
136+
});
137+
if (testFiles.length < 1) {
138+
return;
139+
}
140+
runTestsImpl({testFile: [testFiles[0]]});
141+
});
142+
}
108143
function displayStopUI(message: string) {
109144
let testManager = getTestRunner();
110145
if (!testManager) {

0 commit comments

Comments
 (0)