Skip to content

Commit 64126f2

Browse files
author
Kartik Raj
committed
more corrections
1 parent 095dcbb commit 64126f2

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

src/client/unittests/providers/commandHandlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import { TestTreeItem } from './testTreeViewItem';
1717
const testNavigationCommandMapping = {
1818
[TestType.testFile]: Commands.navigateToTestFile,
1919
[TestType.testFunction]: Commands.navigateToTestFunction,
20-
[TestType.testSuite]: Commands.navigateToTestSuite
20+
[TestType.testSuite]: Commands.navigateToTestSuite,
21+
[TestType.testFolder]: undefined
2122
};
2223

2324
@injectable()

src/client/unittests/pytest/services/testMessageService.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class TestMessageService implements ITestMessageService {
2525
*/
2626
public async getFilteredTestMessages(rootDirectory: string, testResults: Tests): Promise<IPythonUnitTestMessage[]> {
2727
const testFuncs: FlattenedTestFunction[] = testResults.testFunctions.reduce((filtered, test) => {
28-
if (test.testFunction.passed !== undefined || test.testFunction.status === TestStatus.Skipped){
28+
if (test.testFunction.passed !== undefined || test.testFunction.status === TestStatus.Skipped) {
2929
filtered.push(test as never);
3030
}
3131
return filtered;
@@ -85,7 +85,7 @@ export class TestMessageService implements ITestMessageService {
8585
const locationStack: ILocationStackFrameDetails[] = [];
8686
if (testFunction.testFunction.traceback) {
8787
const fileMatches = testFunction.testFunction.traceback.match(/^((\.\.[\\\/])*.+\.py)\:(\d+)\:.*$/gim);
88-
for (const fileDetailsMatch of fileMatches) {
88+
for (const fileDetailsMatch of fileMatches!) {
8989
const fileDetails = fileDetailsMatch.split(':');
9090
let filePath = fileDetails[0];
9191
filePath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDirectory, filePath);
@@ -97,41 +97,43 @@ export class TestMessageService implements ITestMessageService {
9797
new Position((fileLineNum - 1), line.firstNonWhitespaceCharacterIndex),
9898
new Position((fileLineNum - 1), line.text.length)
9999
));
100-
const stackFrame: ILocationStackFrameDetails = {location: location, lineText: file.getText(location.range)};
100+
const stackFrame: ILocationStackFrameDetails = { location: location, lineText: file.getText(location.range) };
101101
locationStack.push(stackFrame);
102102
}
103103
}
104104
// Find where the file the test was defined.
105105
let testSourceFilePath = testFunction.testFunction.file;
106-
testSourceFilePath = path.isAbsolute(testSourceFilePath) ? testSourceFilePath : path.resolve(rootDirectory, testSourceFilePath);
107-
const testSourceFileUri = Uri.file(testSourceFilePath);
106+
testSourceFilePath = path.isAbsolute(testSourceFilePath!) ? testSourceFilePath : path.resolve(rootDirectory, testSourceFilePath!);
107+
const testSourceFileUri = Uri.file(testSourceFilePath!);
108108
const testSourceFile = await workspace.openTextDocument(testSourceFileUri);
109109
let testDefLine: TextLine;
110-
let lineNum = testFunction.testFunction.line;
110+
let testDefLineSet: Boolean = false;
111+
let lineNum = testFunction.testFunction.line!;
111112
let lineText: string;
112113
let trimmedLineText: string;
113114
const testDefPrefix = 'def ';
114115

115-
while (testDefLine === undefined) {
116+
while (testDefLineSet === false) {
116117
const possibleTestDefLine = testSourceFile.lineAt(lineNum);
117118
lineText = possibleTestDefLine.text;
118119
trimmedLineText = lineText.trimLeft();
119120
if (trimmedLineText.toLowerCase().startsWith(testDefPrefix)) {
120121
testDefLine = possibleTestDefLine;
122+
testDefLineSet = true;
121123
} else {
122124
// The test definition may have been decorated, and there may be multiple
123125
// decorations, so move to the next line and check it.
124126
lineNum += 1;
125127
}
126128
}
127-
const testSimpleName = trimmedLineText.slice(testDefPrefix.length).match(/[^ \(:]+/)[0];
128-
const testDefStartCharNum = (lineText.length - trimmedLineText.length) + testDefPrefix.length;
129+
const testSimpleName = trimmedLineText!.slice(testDefPrefix.length).match(/[^ \(:]+/)![0];
130+
const testDefStartCharNum = (lineText!.length - trimmedLineText!.length) + testDefPrefix.length;
129131
const testDefEndCharNum = testDefStartCharNum + testSimpleName.length;
130-
const lineStart = new Position(testDefLine.lineNumber, testDefStartCharNum);
131-
const lineEnd = new Position(testDefLine.lineNumber, testDefEndCharNum);
132+
const lineStart = new Position(testDefLine!.lineNumber, testDefStartCharNum);
133+
const lineEnd = new Position(testDefLine!.lineNumber, testDefEndCharNum);
132134
const lineRange = new Range(lineStart, lineEnd);
133135
const testDefLocation = new Location(testSourceFileUri, lineRange);
134-
const testSourceLocationDetails = {location: testDefLocation, lineText: testSourceFile.getText(lineRange)};
136+
const testSourceLocationDetails = { location: testDefLocation, lineText: testSourceFile.getText(lineRange) };
135137
locationStack.unshift(testSourceLocationDetails);
136138

137139
// Put the class declaration at the top of the stack if the test was imported.
@@ -167,21 +169,21 @@ export class TestMessageService implements ITestMessageService {
167169
const suiteStack = suiteStackWithFileAndTest.slice(1, (suiteStackWithFileAndTest.length - 1));
168170
const testFileUri = Uri.file(testFunction.parentTestFile.fullPath);
169171
const testFile = await workspace.openTextDocument(testFileUri);
170-
const testFileLines = testFile.getText().splitLines({trim: false, removeEmptyEntries: false});
172+
const testFileLines = testFile.getText().splitLines({ trim: false, removeEmptyEntries: false });
171173
const reversedTestFileLines = testFileLines.slice().reverse();
172174
// Track the end of the parent scope.
173175
let parentScopeEndIndex = 0;
174176
let parentScopeStartIndex = testFileLines.length;
175-
let parentIndentation: number;
177+
let parentIndentation: number = -1;
176178
const suiteLocationStackFrameDetails: ILocationStackFrameDetails[] = [];
177179

178180
const classPrefix = 'class ';
179181
while (suiteStack.length > 0) {
180182
let indentation: number;
181-
let prevLowestIndentation: number;
183+
let prevLowestIndentation: number = -1;
182184
// Get the name of the suite on top of the stack so it can be located.
183185
const suiteName = suiteStack.shift();
184-
let suiteDefLineIndex: number;
186+
let suiteDefLineIndex: number = -1;
185187
for (let index = parentScopeEndIndex; index < parentScopeStartIndex; index += 1) {
186188
const lineText = reversedTestFileLines[index];
187189
if (lineText.trim().length === 0) {
@@ -193,10 +195,10 @@ export class TestMessageService implements ITestMessageService {
193195
// line is not a class declaration
194196
continue;
195197
}
196-
const lineClassName = trimmedLineText.slice(classPrefix.length).match(/[^ \(:]+/)[0];
198+
const lineClassName = trimmedLineText.slice(classPrefix.length).match(/[^ \(:]+/)![0];
197199

198200
// Check if the indentation is proper.
199-
if (parentIndentation === undefined) {
201+
if (parentIndentation === -1) {
200202
// The parentIndentation hasn't been set yet, so we are looking for a class that was
201203
// defined in the global scope of the module.
202204
if (trimmedLineText.length === lineText.length) {
@@ -219,7 +221,7 @@ export class TestMessageService implements ITestMessageService {
219221
parentScopeEndIndex = index + 1;
220222
continue;
221223
}
222-
if (prevLowestIndentation === undefined || indentation < prevLowestIndentation) {
224+
if (prevLowestIndentation === -1 || indentation < prevLowestIndentation) {
223225
if (lineClassName === suiteName) {
224226
// This might be the line that we want.
225227
suiteDefLineIndex = index;
@@ -231,22 +233,22 @@ export class TestMessageService implements ITestMessageService {
231233
}
232234
}
233235
}
234-
if (suiteDefLineIndex === undefined) {
236+
if (suiteDefLineIndex === -1) {
235237
// Could not find the suite declaration line, so give up and move on with the latest one that we found.
236238
break;
237239
}
238240
// Found the line to process.
239241
parentScopeStartIndex = suiteDefLineIndex;
240-
parentIndentation = indentation;
242+
parentIndentation = indentation!;
241243

242244
// Invert the index to get the unreversed equivalent.
243245
const realIndex = (reversedTestFileLines.length - 1) - suiteDefLineIndex;
244-
const startChar = indentation + classPrefix.length;
246+
const startChar = indentation! + classPrefix.length;
245247
const suiteStartPos = new Position(realIndex, startChar);
246-
const suiteEndPos = new Position(realIndex, (startChar + suiteName.length));
248+
const suiteEndPos = new Position(realIndex, (startChar + suiteName!.length));
247249
const suiteRange = new Range(suiteStartPos, suiteEndPos);
248250
const suiteLocation = new Location(testFileUri, suiteRange);
249-
suiteLocationStackFrameDetails.push({location: suiteLocation, lineText: testFile.getText(suiteRange)});
251+
suiteLocationStackFrameDetails.push({ location: suiteLocation, lineText: testFile.getText(suiteRange) });
250252
}
251253
return suiteLocationStackFrameDetails[suiteLocationStackFrameDetails.length - 1];
252254
}

src/client/unittests/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export interface IPythonUnitTestMessage {
123123
provider: string | undefined;
124124
traceback?: string;
125125
testTime: number;
126-
status: TestStatus;
126+
status?: TestStatus;
127127
locationStack?: ILocationStackFrameDetails[];
128128
testFilePath: string;
129129
}

0 commit comments

Comments
 (0)