From c06b7f4b578b74fcecc1e0a20c2b701e0f725429 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Mon, 29 Jul 2019 17:15:12 -0700 Subject: [PATCH 1/6] Fix code lens bug --- news/2 Fixes/6303.md | 1 + src/client/testing/codeLenses/testFiles.ts | 5 ++++- src/client/testing/common/services/discoveredTestParser.ts | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 news/2 Fixes/6303.md diff --git a/news/2 Fixes/6303.md b/news/2 Fixes/6303.md new file mode 100644 index 000000000000..1a6942035063 --- /dev/null +++ b/news/2 Fixes/6303.md @@ -0,0 +1 @@ +No code lenses shown for pytest diff --git a/src/client/testing/codeLenses/testFiles.ts b/src/client/testing/codeLenses/testFiles.ts index 140d1bddb702..747d0ea23c18 100644 --- a/src/client/testing/codeLenses/testFiles.ts +++ b/src/client/testing/codeLenses/testFiles.ts @@ -3,6 +3,8 @@ // tslint:disable:no-object-literal-type-assertion import { CancellationToken, CancellationTokenSource, CodeLens, CodeLensProvider, DocumentSymbolProvider, Event, EventEmitter, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, workspace } from 'vscode'; +import { FileSystem } from '../../../client/common/platform/fileSystem'; +import { PlatformService } from '../../../client/common/platform/platformService'; import * as constants from '../../common/constants'; import { CommandSource } from '../common/constants'; import { ITestCollectionStorageService, TestFile, TestFunction, TestStatus, TestsToRun, TestSuite } from '../common/types'; @@ -61,7 +63,8 @@ export class TestFileCodeLensProvider implements CodeLensProvider { if (!tests) { return []; } - const file = tests.testFiles.find(item => item.fullPath === document.uri.fsPath); + const fileSystem = new FileSystem(new PlatformService()); + const file = tests.testFiles.find(item => fileSystem.arePathsSame(item.fullPath, document.uri.fsPath)); if (!file) { return []; } diff --git a/src/client/testing/common/services/discoveredTestParser.ts b/src/client/testing/common/services/discoveredTestParser.ts index ed123270b12e..3aa061af5788 100644 --- a/src/client/testing/common/services/discoveredTestParser.ts +++ b/src/client/testing/common/services/discoveredTestParser.ts @@ -35,7 +35,7 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { // If the root is the workspace folder, then ignore that. for (const data of discoveredTests) { const rootFolder = { - name: data.root, folders: [], time: 0, + name: workspace.uri.fsPath, folders: [], time: 0, testFiles: [], resource: resource, nameToRun: data.rootid }; tests.rootTestFolders.push(rootFolder); From b5f6b7ca6ca21607f61c086472f018e356969856 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 30 Jul 2019 11:51:24 -0700 Subject: [PATCH 2/6] Added tests for code lenses test file --- src/client/testing/codeLenses/main.ts | 6 +- src/client/testing/codeLenses/testFiles.ts | 31 ++-- src/client/testing/main.ts | 2 +- .../testing/codeLenses/testFiles.unit.test.ts | 158 ++++++++++++++++++ 4 files changed, 183 insertions(+), 14 deletions(-) create mode 100644 src/test/testing/codeLenses/testFiles.unit.test.ts diff --git a/src/client/testing/codeLenses/main.ts b/src/client/testing/codeLenses/main.ts index 9f955d485f34..7b54d0ffe00c 100644 --- a/src/client/testing/codeLenses/main.ts +++ b/src/client/testing/codeLenses/main.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import { IServiceContainer } from '../../../client/ioc/types'; import { PYTHON } from '../../common/constants'; import { ITestCollectionStorageService } from '../common/types'; import { TestFileCodeLensProvider } from './testFiles'; @@ -6,10 +7,11 @@ import { TestFileCodeLensProvider } from './testFiles'; export function activateCodeLenses( onDidChange: vscode.EventEmitter, symbolProvider: vscode.DocumentSymbolProvider, - testCollectionStorage: ITestCollectionStorageService + testCollectionStorage: ITestCollectionStorageService, + serviceContainer: IServiceContainer ): vscode.Disposable { const disposables: vscode.Disposable[] = []; - const codeLensProvider = new TestFileCodeLensProvider(onDidChange, symbolProvider, testCollectionStorage); + const codeLensProvider = new TestFileCodeLensProvider(onDidChange, symbolProvider, testCollectionStorage, serviceContainer); disposables.push(vscode.languages.registerCodeLensProvider(PYTHON, codeLensProvider)); return { diff --git a/src/client/testing/codeLenses/testFiles.ts b/src/client/testing/codeLenses/testFiles.ts index 747d0ea23c18..19cf9a5c1783 100644 --- a/src/client/testing/codeLenses/testFiles.ts +++ b/src/client/testing/codeLenses/testFiles.ts @@ -2,9 +2,10 @@ // tslint:disable:no-object-literal-type-assertion -import { CancellationToken, CancellationTokenSource, CodeLens, CodeLensProvider, DocumentSymbolProvider, Event, EventEmitter, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri, workspace } from 'vscode'; -import { FileSystem } from '../../../client/common/platform/fileSystem'; -import { PlatformService } from '../../../client/common/platform/platformService'; +import { CancellationToken, CancellationTokenSource, CodeLens, CodeLensProvider, DocumentSymbolProvider, Event, EventEmitter, Position, Range, SymbolInformation, SymbolKind, TextDocument, Uri } from 'vscode'; +import { IWorkspaceService } from '../../../client/common/application/types'; +import { IFileSystem } from '../../../client/common/platform/types'; +import { IServiceContainer } from '../../../client/ioc/types'; import * as constants from '../../common/constants'; import { CommandSource } from '../common/constants'; import { ITestCollectionStorageService, TestFile, TestFunction, TestStatus, TestsToRun, TestSuite } from '../common/types'; @@ -15,10 +16,15 @@ type FunctionsAndSuites = { }; export class TestFileCodeLensProvider implements CodeLensProvider { + private workspaceService: IWorkspaceService; + private fileSystem: IFileSystem; // tslint:disable-next-line:variable-name constructor(private _onDidChange: EventEmitter, private symbolProvider: DocumentSymbolProvider, - private testCollectionStorage: ITestCollectionStorageService) { + private testCollectionStorage: ITestCollectionStorageService, + serviceContainer: IServiceContainer) { + this.workspaceService = serviceContainer.get(IWorkspaceService); + this.fileSystem = serviceContainer.get(IFileSystem); } get onDidChangeCodeLenses(): Event { @@ -26,7 +32,7 @@ export class TestFileCodeLensProvider implements CodeLensProvider { } public async provideCodeLenses(document: TextDocument, token: CancellationToken) { - const wkspace = workspace.getWorkspaceFolder(document.uri); + const wkspace = this.workspaceService.getWorkspaceFolder(document.uri); if (!wkspace) { return []; } @@ -54,17 +60,20 @@ export class TestFileCodeLensProvider implements CodeLensProvider { return Promise.resolve(codeLens); } - private async getCodeLenses(document: TextDocument, token: CancellationToken, symbolProvider: DocumentSymbolProvider) { - const wkspace = workspace.getWorkspaceFolder(document.uri); + public getTestFileWhichNeedsCodeLens(document: TextDocument): TestFile | undefined { + const wkspace = this.workspaceService.getWorkspaceFolder(document.uri); if (!wkspace) { - return []; + return; } const tests = this.testCollectionStorage.getTests(wkspace.uri); if (!tests) { - return []; + return; } - const fileSystem = new FileSystem(new PlatformService()); - const file = tests.testFiles.find(item => fileSystem.arePathsSame(item.fullPath, document.uri.fsPath)); + return tests.testFiles.find(item => this.fileSystem.arePathsSame(item.fullPath, document.uri.fsPath)); + } + + private async getCodeLenses(document: TextDocument, token: CancellationToken, symbolProvider: DocumentSymbolProvider) { + const file = this.getTestFileWhichNeedsCodeLens(document); if (!file) { return []; } diff --git a/src/client/testing/main.ts b/src/client/testing/main.ts index fc71ecaa22a1..b9b697391b80 100644 --- a/src/client/testing/main.ts +++ b/src/client/testing/main.ts @@ -324,7 +324,7 @@ export class UnitTestManagementService implements ITestManagementService, Dispos } }); this.disposableRegistry.push(handler); - this.disposableRegistry.push(activateCodeLenses(event, symbolProvider, testCollectionStorage)); + this.disposableRegistry.push(activateCodeLenses(event, symbolProvider, testCollectionStorage, this.serviceContainer)); } @captureTelemetry(EventName.UNITTEST_CONFIGURE, undefined, false) diff --git a/src/test/testing/codeLenses/testFiles.unit.test.ts b/src/test/testing/codeLenses/testFiles.unit.test.ts new file mode 100644 index 000000000000..9d2ac5763602 --- /dev/null +++ b/src/test/testing/codeLenses/testFiles.unit.test.ts @@ -0,0 +1,158 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +'use strict'; + +// tslint:disable:no-any + +import { assert, expect } from 'chai'; +import { mock } from 'ts-mockito'; +import * as typemoq from 'typemoq'; +import { DocumentSymbolProvider, EventEmitter, Uri } from 'vscode'; +import { IWorkspaceService } from '../../../client/common/application/types'; +import { IFileSystem } from '../../../client/common/platform/types'; +import { IServiceContainer } from '../../../client/ioc/types'; +import { LanguageServerSymbolProvider } from '../../../client/providers/symbolProvider'; +import { TestFileCodeLensProvider } from '../../../client/testing/codeLenses/testFiles'; +import { ITestCollectionStorageService } from '../../../client/testing/common/types'; + +// tslint:disable-next-line: max-func-body-length +suite('xCode lenses - Test files', () => { + let testCollectionStorage: typemoq.IMock; + let workspaceService: typemoq.IMock; + let fileSystem: typemoq.IMock; + let serviceContainer: typemoq.IMock; + let symbolProvider: DocumentSymbolProvider; + let onDidChange: EventEmitter; + let codeLensProvider: TestFileCodeLensProvider; + setup(() => { + workspaceService = typemoq.Mock.ofType(); + fileSystem = typemoq.Mock.ofType(); + testCollectionStorage = typemoq.Mock.ofType(); + serviceContainer = typemoq.Mock.ofType(); + symbolProvider = mock(LanguageServerSymbolProvider); + onDidChange = new EventEmitter(); + serviceContainer + .setup(c => c.get(typemoq.It.isValue(IWorkspaceService))) + .returns(() => workspaceService.object); + serviceContainer + .setup(c => c.get(typemoq.It.isValue(IFileSystem))) + .returns(() => fileSystem.object); + codeLensProvider = new TestFileCodeLensProvider(onDidChange, symbolProvider, testCollectionStorage.object, serviceContainer.object); + }); + + teardown(() => { + onDidChange.dispose(); + }); + + test('Function getTestFileWhichNeedsCodeLens() returns `undefined` if there are no workspace corresponding to document', async () => { + const document = { + uri: Uri.file('path/to/document') + }; + workspaceService + .setup(w => w.getWorkspaceFolder(document.uri)) + .returns(() => undefined) + .verifiable(typemoq.Times.once()); + testCollectionStorage + .setup(w => w.getTests(typemoq.It.isAny())) + .returns(() => undefined) + .verifiable(typemoq.Times.never()); + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); + expect(files).to.equal(undefined, 'No files should be returned'); + workspaceService.verifyAll(); + testCollectionStorage.verifyAll(); + }); + + test('Function getTestFileWhichNeedsCodeLens() returns `undefined` if test storage is empty', async () => { + const document = { + uri: Uri.file('path/to/document') + }; + const workspaceUri = Uri.file('path/to/workspace'); + const workspace = { uri: workspaceUri }; + workspaceService + .setup(w => w.getWorkspaceFolder(document.uri)) + .returns(() => workspace as any) + .verifiable(typemoq.Times.once()); + testCollectionStorage + .setup(w => w.getTests(workspaceUri)) + .returns(() => undefined) + .verifiable(typemoq.Times.once()); + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); + expect(files).to.equal(undefined, 'No files should be returned'); + workspaceService.verifyAll(); + testCollectionStorage.verifyAll(); + }); + + test('Function getTestFileWhichNeedsCodeLens() returns `undefined` if tests returned from storage does not contain document', async () => { + const document = { + uri: Uri.file('path/to/document5') + }; + const workspaceUri = Uri.file('path/to/workspace'); + const workspace = { uri: workspaceUri }; + const tests = { + testFiles: [ + { + fullPath: 'path/to/document1' + }, + { + fullPath: 'path/to/document2' + } + ] + }; + workspaceService + .setup(w => w.getWorkspaceFolder(document.uri)) + .returns(() => workspace as any) + .verifiable(typemoq.Times.once()); + testCollectionStorage + .setup(w => w.getTests(workspaceUri)) + .returns(() => tests as any) + .verifiable(typemoq.Times.once()); + fileSystem + .setup(f => f.arePathsSame('path/to/document1', 'path/to/document5')) + .returns(() => false); + fileSystem + .setup(f => f.arePathsSame('path/to/document2', 'path/to/document5')) + .returns(() => false); + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); + expect(files).to.equal(undefined, 'No files should be returned'); + workspaceService.verifyAll(); + testCollectionStorage.verifyAll(); + }); + + test('Function getTestFileWhichNeedsCodeLens() returns test file if tests returned from storage contains document', async () => { + const document = { + uri: Uri.file('path/to/document2') + }; + const workspaceUri = Uri.file('path/to/workspace'); + const workspace = { uri: workspaceUri }; + const testFile2 = { + fullPath: 'path/to/document2' + }; + const tests = { + testFiles: [ + { + fullPath: 'path/to/document1' + }, + testFile2 + ] + }; + workspaceService + .setup(w => w.getWorkspaceFolder(document.uri)) + .returns(() => workspace as any) + .verifiable(typemoq.Times.once()); + testCollectionStorage + .setup(w => w.getTests(workspaceUri)) + .returns(() => tests as any) + .verifiable(typemoq.Times.once()); + fileSystem + .setup(f => f.arePathsSame('path/to/document1', 'path/to/document2')) + .returns(() => false); + fileSystem + .setup(f => f.arePathsSame('path/to/document2', 'path/to/document2')) + .returns(() => true); + const files = codeLensProvider.getTestFileWhichNeedsCodeLens(document as any); + assert.deepEqual(files, testFile2 as any); + workspaceService.verifyAll(); + testCollectionStorage.verifyAll(); + }); +}); From dbb6dd7e70a632f4538d994305af16f8a89835e9 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 30 Jul 2019 11:55:29 -0700 Subject: [PATCH 3/6] Corrected functionality --- .../common/services/discoveredTestParser.ts | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/client/testing/common/services/discoveredTestParser.ts b/src/client/testing/common/services/discoveredTestParser.ts index 3aa061af5788..b845dd1d16e7 100644 --- a/src/client/testing/common/services/discoveredTestParser.ts +++ b/src/client/testing/common/services/discoveredTestParser.ts @@ -8,6 +8,7 @@ import * as path from 'path'; import { Uri } from 'vscode'; import { IWorkspaceService } from '../../../common/application/types'; import { traceError } from '../../../common/logger'; +import { IFileSystem } from '../../../common/platform/types'; import { TestDataItem } from '../../types'; import { getParentFile, getParentSuite, getTestType } from '../testUtils'; import { FlattenedTestFunction, FlattenedTestSuite, SubtestParent, TestFile, TestFolder, TestFunction, Tests, TestSuite, TestType } from '../types'; @@ -15,7 +16,10 @@ import { DiscoveredTests, ITestDiscoveredTestParser, TestContainer, TestItem } f @injectable() export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { - constructor(@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService) { } + constructor( + @inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, + @inject(IFileSystem) private readonly fileSystem: IFileSystem + ) { } public parse(resource: Uri, discoveredTests: DiscoveredTests[]): Tests { const tests: Tests = { rootTestFolders: [], @@ -34,6 +38,12 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { // If the root is the workspace folder, then ignore that. for (const data of discoveredTests) { + // For now we only check the current workspace. + const root = this.findRoot(data.root, [workspace.uri.fsPath]); + if (!root) { + // For now we only support tests from the workspace. + continue; + } const rootFolder = { name: workspace.uri.fsPath, folders: [], time: 0, testFiles: [], resource: resource, nameToRun: data.rootid @@ -45,6 +55,15 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { return tests; } + + public findRoot(raw: string, roots: string[]): string | undefined { + for (const root of roots) { + if (this.fileSystem.arePathsSame(raw, root)) { + return root; + } + } + return undefined; + } /** * Not the best solution to use `case statements`, but it keeps the code simple and easy to read in one place. * Could go with separate classes for each type and use stratergies, but that just ends up a class for From 4fc5000f94ede82504fdc019f879cd601ec7fd7d Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 30 Jul 2019 12:37:05 -0700 Subject: [PATCH 4/6] Added tests for test parser --- .../common/services/discoveredTestParser.ts | 22 +-- .../testing/codeLenses/testFiles.unit.test.ts | 2 +- .../discoveredTestParser.unit.test.ts | 139 ++++++++++++++++++ .../pytest/pytest.testMessageService.test.ts | 4 +- 4 files changed, 155 insertions(+), 12 deletions(-) create mode 100644 src/test/testing/common/services/discoveredTestParser.unit.test.ts diff --git a/src/client/testing/common/services/discoveredTestParser.ts b/src/client/testing/common/services/discoveredTestParser.ts index b845dd1d16e7..cadf3db67da1 100644 --- a/src/client/testing/common/services/discoveredTestParser.ts +++ b/src/client/testing/common/services/discoveredTestParser.ts @@ -55,15 +55,6 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { return tests; } - - public findRoot(raw: string, roots: string[]): string | undefined { - for (const root of roots) { - if (this.fileSystem.arePathsSame(raw, root)) { - return root; - } - } - return undefined; - } /** * Not the best solution to use `case statements`, but it keeps the code simple and easy to read in one place. * Could go with separate classes for each type and use stratergies, but that just ends up a class for @@ -77,7 +68,7 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { * @param {Tests} tests * @memberof TestsDiscovery */ - protected buildChildren(rootFolder: TestFolder, parent: TestDataItem, discoveredTests: DiscoveredTests, tests: Tests) { + public buildChildren(rootFolder: TestFolder, parent: TestDataItem, discoveredTests: DiscoveredTests, tests: Tests) { const parentType = getTestType(parent); switch (parentType) { case TestType.testFolder: { @@ -212,6 +203,17 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { parent.functions.push(...functions); tests.testFunctions.push(...functions.map(func => createFlattenedParameterizedFunction(tests, func, parent))); } + + /** + * Returns the vscode recognized file paths which matches the pytest data test root + */ + private findRoot(raw: string, roots: string[]): string | undefined { + for (const root of roots) { + if (this.fileSystem.arePathsSame(raw, root)) { + return root; + } + } + } } function createTestFolder(root: TestFolder, item: TestContainer): TestFolder { diff --git a/src/test/testing/codeLenses/testFiles.unit.test.ts b/src/test/testing/codeLenses/testFiles.unit.test.ts index 9d2ac5763602..ce262c9d3d4b 100644 --- a/src/test/testing/codeLenses/testFiles.unit.test.ts +++ b/src/test/testing/codeLenses/testFiles.unit.test.ts @@ -17,7 +17,7 @@ import { TestFileCodeLensProvider } from '../../../client/testing/codeLenses/tes import { ITestCollectionStorageService } from '../../../client/testing/common/types'; // tslint:disable-next-line: max-func-body-length -suite('xCode lenses - Test files', () => { +suite('Code lenses - Test files', () => { let testCollectionStorage: typemoq.IMock; let workspaceService: typemoq.IMock; let fileSystem: typemoq.IMock; diff --git a/src/test/testing/common/services/discoveredTestParser.unit.test.ts b/src/test/testing/common/services/discoveredTestParser.unit.test.ts new file mode 100644 index 000000000000..5be70fc35d01 --- /dev/null +++ b/src/test/testing/common/services/discoveredTestParser.unit.test.ts @@ -0,0 +1,139 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +'use strict'; + +import * as assert from 'assert'; +import * as sinon from 'sinon'; +import * as typemoq from 'typemoq'; +import { Uri } from 'vscode'; +import { IWorkspaceService } from '../../../../client/common/application/types'; +import { IFileSystem } from '../../../../client/common/platform/types'; +import { TestDiscoveredTestParser } from '../../../../client/testing/common/services/discoveredTestParser'; +import { Tests } from '../../../../client/testing/common/types'; + +// tslint:disable:no-any max-func-body-length +suite('Services - Discovered test parser', () => { + let workspaceService: typemoq.IMock; + let fileSystem: typemoq.IMock; + let parser: TestDiscoveredTestParser; + setup(() => { + workspaceService = typemoq.Mock.ofType(); + fileSystem = typemoq.Mock.ofType(); + }); + + teardown(() => { + sinon.restore(); + }); + + test('Parse returns empty tests if resource does not belong to workspace', () => { + const expectedTests: Tests = { + rootTestFolders: [], + summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }, + testFiles: [], + testFolders: [], + testFunctions: [], + testSuites: [] + }; + const discoveredTests = [{ + root: 'path/to/testDataRoot' + }]; + const buildChildren = sinon.stub(TestDiscoveredTestParser.prototype, 'buildChildren'); + buildChildren.callsFake(() => undefined); + workspaceService + .setup(w => w.getWorkspaceFolder(typemoq.It.isAny())) + .returns(() => undefined) + .verifiable(typemoq.Times.once()); + fileSystem + .setup(f => f.arePathsSame('path/to/testDataRoot', 'path/to/workspace')) + .returns(() => false) + .verifiable(typemoq.Times.never()); + parser = new TestDiscoveredTestParser(workspaceService.object, fileSystem.object); + const result = parser.parse(Uri.file('path/to/resource'), discoveredTests as any); + assert.ok(buildChildren.notCalled); + assert.deepEqual(expectedTests, result); + workspaceService.verifyAll(); + fileSystem.verifyAll(); + }); + + test('Parse returns empty tests if data root does not matches with workspace root', () => { + const expectedTests: Tests = { + rootTestFolders: [], + summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }, + testFiles: [], + testFolders: [], + testFunctions: [], + testSuites: [] + }; + const discoveredTests = [{ + root: 'path/to/testDataRoot' + }]; + const workspaceUri = Uri.file('path/to/workspace'); + const workspace = { uri: workspaceUri }; + const buildChildren = sinon.stub(TestDiscoveredTestParser.prototype, 'buildChildren'); + buildChildren.callsFake(() => undefined); + workspaceService + .setup(w => w.getWorkspaceFolder(typemoq.It.isAny())) + .returns(() => workspace as any) + .verifiable(typemoq.Times.once()); + fileSystem + .setup(f => f.arePathsSame('path/to/testDataRoot', 'path/to/workspace')) + .returns(() => false) + .verifiable(typemoq.Times.atLeastOnce()); + parser = new TestDiscoveredTestParser(workspaceService.object, fileSystem.object); + const result = parser.parse(workspaceUri, discoveredTests as any); + assert.ok(buildChildren.notCalled); + assert.deepEqual(expectedTests, result); + fileSystem.verifyAll(); + }); + + test('Parse returns expected tests if some of data roots matches with workspace root', () => { + const expectedTests: Tests = { + rootTestFolders: [], + summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }, + testFiles: [], + testFolders: [], + testFunctions: [], + testSuites: [] + }; + const discoveredTests = [ + { + root: 'path/to/testDataRoot1', + rootid: 'rootId1' + }, + { + root: 'path/to/testDataRoot2', + rootid: 'rootId2' + } + ]; + const workspaceUri = Uri.file('path/to/workspace'); + const workspace = { uri: workspaceUri }; + const rootFolder1 = { + name: workspace.uri.fsPath, folders: [], time: 0, + testFiles: [], resource: workspaceUri, nameToRun: 'rootId1' + }; + expectedTests.rootTestFolders.push(rootFolder1); + expectedTests.testFolders.push(rootFolder1); + const buildChildren = sinon.stub(TestDiscoveredTestParser.prototype, 'buildChildren'); + buildChildren.callsFake(() => undefined); + workspaceService + .setup(w => w.getWorkspaceFolder(typemoq.It.isAny())) + .returns(() => workspace as any) + .verifiable(typemoq.Times.once()); + + // Only test data root 1 matches with path to workspace + fileSystem + .setup(f => f.arePathsSame('path/to/testDataRoot1', 'path/to/workspace')) + .returns(() => true) + .verifiable(typemoq.Times.atLeastOnce()); + fileSystem + .setup(f => f.arePathsSame('path/to/testDataRoot2', 'path/to/workspace')) + .returns(() => false) + .verifiable(typemoq.Times.atLeastOnce()); + parser = new TestDiscoveredTestParser(workspaceService.object, fileSystem.object); + const result = parser.parse(workspaceUri, discoveredTests as any); + assert.ok(buildChildren.calledOnce); + assert.deepEqual(expectedTests, result); + fileSystem.verifyAll(); + }); +}); diff --git a/src/test/testing/pytest/pytest.testMessageService.test.ts b/src/test/testing/pytest/pytest.testMessageService.test.ts index 69be5d7d624d..b63f217d67cf 100644 --- a/src/test/testing/pytest/pytest.testMessageService.test.ts +++ b/src/test/testing/pytest/pytest.testMessageService.test.ts @@ -12,6 +12,7 @@ import * as vscode from 'vscode'; import { IWorkspaceService } from '../../../client/common/application/types'; import { EXTENSION_ROOT_DIR } from '../../../client/common/constants'; import { ProductNames } from '../../../client/common/installer/productNames'; +import { IFileSystem } from '../../../client/common/platform/types'; import { Product } from '../../../client/common/types'; import { ICondaService, IInterpreterService } from '../../../client/interpreter/contracts'; import { InterpreterService } from '../../../client/interpreter/interpreterService'; @@ -136,7 +137,8 @@ suite('Unit Tests - PyTest - TestMessageService', () => { }; // Setup the parser. const workspaceService = ioc.serviceContainer.get(IWorkspaceService); - const parser = new TestDiscoveredTestParser(workspaceService); + const fileSystem = ioc.serviceContainer.get(IFileSystem); + const parser = new TestDiscoveredTestParser(workspaceService, fileSystem); const discoveryOutput = fs.readFileSync(path.join(PYTEST_RESULTS_PATH, scenario.discoveryOutput), 'utf8').replace(/\/Users\/donjayamanne\/.vscode-insiders\/extensions\/pythonVSCode\/src\/test\/pythonFiles\/testFiles/g, path.dirname(UNITTEST_TEST_FILES_PATH)).replace(/\\/g, '/'); const discoveredTest: DiscoveredTests[] = JSON.parse(discoveryOutput); options.workspaceFolder = vscode.Uri.file(discoveredTest[0].root); From 0f22bf610149d71bba6ec00c65c5057b7c9be391 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 30 Jul 2019 13:41:38 -0700 Subject: [PATCH 5/6] Code reviews --- news/2 Fixes/6303.md | 2 +- src/client/testing/common/services/discoveredTestParser.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/news/2 Fixes/6303.md b/news/2 Fixes/6303.md index 1a6942035063..1926ad186b2c 100644 --- a/news/2 Fixes/6303.md +++ b/news/2 Fixes/6303.md @@ -1 +1 @@ -No code lenses shown for pytest +Fix code lenses shown for pytest diff --git a/src/client/testing/common/services/discoveredTestParser.ts b/src/client/testing/common/services/discoveredTestParser.ts index cadf3db67da1..7e51904ee8b4 100644 --- a/src/client/testing/common/services/discoveredTestParser.ts +++ b/src/client/testing/common/services/discoveredTestParser.ts @@ -45,7 +45,7 @@ export class TestDiscoveredTestParser implements ITestDiscoveredTestParser { continue; } const rootFolder = { - name: workspace.uri.fsPath, folders: [], time: 0, + name: root, folders: [], time: 0, testFiles: [], resource: resource, nameToRun: data.rootid }; tests.rootTestFolders.push(rootFolder); From acece9f3f8ac7a15b33e01ae3027810f764b92b1 Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Tue, 30 Jul 2019 14:10:41 -0700 Subject: [PATCH 6/6] Minor comments --- .../discoveredTestParser.unit.test.ts | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/test/testing/common/services/discoveredTestParser.unit.test.ts b/src/test/testing/common/services/discoveredTestParser.unit.test.ts index 5be70fc35d01..867461ee0a96 100644 --- a/src/test/testing/common/services/discoveredTestParser.unit.test.ts +++ b/src/test/testing/common/services/discoveredTestParser.unit.test.ts @@ -27,6 +27,7 @@ suite('Services - Discovered test parser', () => { }); test('Parse returns empty tests if resource does not belong to workspace', () => { + // That is, getWorkspaceFolder() returns undefined. const expectedTests: Tests = { rootTestFolders: [], summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }, @@ -88,14 +89,6 @@ suite('Services - Discovered test parser', () => { }); test('Parse returns expected tests if some of data roots matches with workspace root', () => { - const expectedTests: Tests = { - rootTestFolders: [], - summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }, - testFiles: [], - testFolders: [], - testFunctions: [], - testSuites: [] - }; const discoveredTests = [ { root: 'path/to/testDataRoot1', @@ -108,12 +101,24 @@ suite('Services - Discovered test parser', () => { ]; const workspaceUri = Uri.file('path/to/workspace'); const workspace = { uri: workspaceUri }; - const rootFolder1 = { - name: workspace.uri.fsPath, folders: [], time: 0, - testFiles: [], resource: workspaceUri, nameToRun: 'rootId1' + const expectedTests: Tests = { + rootTestFolders: [ + { + name: workspace.uri.fsPath, folders: [], time: 0, + testFiles: [], resource: workspaceUri, nameToRun: 'rootId1' + } + ], + summary: { errors: 0, failures: 0, passed: 0, skipped: 0 }, + testFiles: [], + testFolders: [ + { + name: workspace.uri.fsPath, folders: [], time: 0, + testFiles: [], resource: workspaceUri, nameToRun: 'rootId1' + } + ], + testFunctions: [], + testSuites: [] }; - expectedTests.rootTestFolders.push(rootFolder1); - expectedTests.testFolders.push(rootFolder1); const buildChildren = sinon.stub(TestDiscoveredTestParser.prototype, 'buildChildren'); buildChildren.callsFake(() => undefined); workspaceService