Skip to content

Commit

Permalink
more corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Feb 15, 2019
1 parent 095dcbb commit 64126f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/client/unittests/providers/commandHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import { TestTreeItem } from './testTreeViewItem';
const testNavigationCommandMapping = {
[TestType.testFile]: Commands.navigateToTestFile,
[TestType.testFunction]: Commands.navigateToTestFunction,
[TestType.testSuite]: Commands.navigateToTestSuite
[TestType.testSuite]: Commands.navigateToTestSuite,
[TestType.testFolder]: undefined
};

@injectable()
Expand Down
50 changes: 26 additions & 24 deletions src/client/unittests/pytest/services/testMessageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class TestMessageService implements ITestMessageService {
*/
public async getFilteredTestMessages(rootDirectory: string, testResults: Tests): Promise<IPythonUnitTestMessage[]> {
const testFuncs: FlattenedTestFunction[] = testResults.testFunctions.reduce((filtered, test) => {
if (test.testFunction.passed !== undefined || test.testFunction.status === TestStatus.Skipped){
if (test.testFunction.passed !== undefined || test.testFunction.status === TestStatus.Skipped) {
filtered.push(test as never);
}
return filtered;
Expand Down Expand Up @@ -85,7 +85,7 @@ export class TestMessageService implements ITestMessageService {
const locationStack: ILocationStackFrameDetails[] = [];
if (testFunction.testFunction.traceback) {
const fileMatches = testFunction.testFunction.traceback.match(/^((\.\.[\\\/])*.+\.py)\:(\d+)\:.*$/gim);
for (const fileDetailsMatch of fileMatches) {
for (const fileDetailsMatch of fileMatches!) {
const fileDetails = fileDetailsMatch.split(':');
let filePath = fileDetails[0];
filePath = path.isAbsolute(filePath) ? filePath : path.resolve(rootDirectory, filePath);
Expand All @@ -97,41 +97,43 @@ export class TestMessageService implements ITestMessageService {
new Position((fileLineNum - 1), line.firstNonWhitespaceCharacterIndex),
new Position((fileLineNum - 1), line.text.length)
));
const stackFrame: ILocationStackFrameDetails = {location: location, lineText: file.getText(location.range)};
const stackFrame: ILocationStackFrameDetails = { location: location, lineText: file.getText(location.range) };
locationStack.push(stackFrame);
}
}
// Find where the file the test was defined.
let testSourceFilePath = testFunction.testFunction.file;
testSourceFilePath = path.isAbsolute(testSourceFilePath) ? testSourceFilePath : path.resolve(rootDirectory, testSourceFilePath);
const testSourceFileUri = Uri.file(testSourceFilePath);
testSourceFilePath = path.isAbsolute(testSourceFilePath!) ? testSourceFilePath : path.resolve(rootDirectory, testSourceFilePath!);
const testSourceFileUri = Uri.file(testSourceFilePath!);
const testSourceFile = await workspace.openTextDocument(testSourceFileUri);
let testDefLine: TextLine;
let lineNum = testFunction.testFunction.line;
let testDefLineSet: Boolean = false;
let lineNum = testFunction.testFunction.line!;
let lineText: string;
let trimmedLineText: string;
const testDefPrefix = 'def ';

while (testDefLine === undefined) {
while (testDefLineSet === false) {
const possibleTestDefLine = testSourceFile.lineAt(lineNum);
lineText = possibleTestDefLine.text;
trimmedLineText = lineText.trimLeft();
if (trimmedLineText.toLowerCase().startsWith(testDefPrefix)) {
testDefLine = possibleTestDefLine;
testDefLineSet = true;
} else {
// The test definition may have been decorated, and there may be multiple
// decorations, so move to the next line and check it.
lineNum += 1;
}
}
const testSimpleName = trimmedLineText.slice(testDefPrefix.length).match(/[^ \(:]+/)[0];
const testDefStartCharNum = (lineText.length - trimmedLineText.length) + testDefPrefix.length;
const testSimpleName = trimmedLineText!.slice(testDefPrefix.length).match(/[^ \(:]+/)![0];
const testDefStartCharNum = (lineText!.length - trimmedLineText!.length) + testDefPrefix.length;
const testDefEndCharNum = testDefStartCharNum + testSimpleName.length;
const lineStart = new Position(testDefLine.lineNumber, testDefStartCharNum);
const lineEnd = new Position(testDefLine.lineNumber, testDefEndCharNum);
const lineStart = new Position(testDefLine!.lineNumber, testDefStartCharNum);
const lineEnd = new Position(testDefLine!.lineNumber, testDefEndCharNum);
const lineRange = new Range(lineStart, lineEnd);
const testDefLocation = new Location(testSourceFileUri, lineRange);
const testSourceLocationDetails = {location: testDefLocation, lineText: testSourceFile.getText(lineRange)};
const testSourceLocationDetails = { location: testDefLocation, lineText: testSourceFile.getText(lineRange) };
locationStack.unshift(testSourceLocationDetails);

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

const classPrefix = 'class ';
while (suiteStack.length > 0) {
let indentation: number;
let prevLowestIndentation: number;
let prevLowestIndentation: number = -1;
// Get the name of the suite on top of the stack so it can be located.
const suiteName = suiteStack.shift();
let suiteDefLineIndex: number;
let suiteDefLineIndex: number = -1;
for (let index = parentScopeEndIndex; index < parentScopeStartIndex; index += 1) {
const lineText = reversedTestFileLines[index];
if (lineText.trim().length === 0) {
Expand All @@ -193,10 +195,10 @@ export class TestMessageService implements ITestMessageService {
// line is not a class declaration
continue;
}
const lineClassName = trimmedLineText.slice(classPrefix.length).match(/[^ \(:]+/)[0];
const lineClassName = trimmedLineText.slice(classPrefix.length).match(/[^ \(:]+/)![0];

// Check if the indentation is proper.
if (parentIndentation === undefined) {
if (parentIndentation === -1) {
// The parentIndentation hasn't been set yet, so we are looking for a class that was
// defined in the global scope of the module.
if (trimmedLineText.length === lineText.length) {
Expand All @@ -219,7 +221,7 @@ export class TestMessageService implements ITestMessageService {
parentScopeEndIndex = index + 1;
continue;
}
if (prevLowestIndentation === undefined || indentation < prevLowestIndentation) {
if (prevLowestIndentation === -1 || indentation < prevLowestIndentation) {
if (lineClassName === suiteName) {
// This might be the line that we want.
suiteDefLineIndex = index;
Expand All @@ -231,22 +233,22 @@ export class TestMessageService implements ITestMessageService {
}
}
}
if (suiteDefLineIndex === undefined) {
if (suiteDefLineIndex === -1) {
// Could not find the suite declaration line, so give up and move on with the latest one that we found.
break;
}
// Found the line to process.
parentScopeStartIndex = suiteDefLineIndex;
parentIndentation = indentation;
parentIndentation = indentation!;

// Invert the index to get the unreversed equivalent.
const realIndex = (reversedTestFileLines.length - 1) - suiteDefLineIndex;
const startChar = indentation + classPrefix.length;
const startChar = indentation! + classPrefix.length;
const suiteStartPos = new Position(realIndex, startChar);
const suiteEndPos = new Position(realIndex, (startChar + suiteName.length));
const suiteEndPos = new Position(realIndex, (startChar + suiteName!.length));
const suiteRange = new Range(suiteStartPos, suiteEndPos);
const suiteLocation = new Location(testFileUri, suiteRange);
suiteLocationStackFrameDetails.push({location: suiteLocation, lineText: testFile.getText(suiteRange)});
suiteLocationStackFrameDetails.push({ location: suiteLocation, lineText: testFile.getText(suiteRange) });
}
return suiteLocationStackFrameDetails[suiteLocationStackFrameDetails.length - 1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/client/unittests/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export interface IPythonUnitTestMessage {
provider: string | undefined;
traceback?: string;
testTime: number;
status: TestStatus;
status?: TestStatus;
locationStack?: ILocationStackFrameDetails[];
testFilePath: string;
}
Expand Down

0 comments on commit 64126f2

Please sign in to comment.