Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2 Fixes/6303.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix code lenses shown for pytest
6 changes: 4 additions & 2 deletions src/client/testing/codeLenses/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
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';

export function activateCodeLenses(
onDidChange: vscode.EventEmitter<void>,
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 {
Expand Down
28 changes: 20 additions & 8 deletions src/client/testing/codeLenses/testFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 { 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';
Expand All @@ -13,18 +16,23 @@ type FunctionsAndSuites = {
};

export class TestFileCodeLensProvider implements CodeLensProvider {
private workspaceService: IWorkspaceService;
private fileSystem: IFileSystem;
// tslint:disable-next-line:variable-name
constructor(private _onDidChange: EventEmitter<void>,
private symbolProvider: DocumentSymbolProvider,
private testCollectionStorage: ITestCollectionStorageService) {
private testCollectionStorage: ITestCollectionStorageService,
serviceContainer: IServiceContainer) {
this.workspaceService = serviceContainer.get<IWorkspaceService>(IWorkspaceService);
this.fileSystem = serviceContainer.get<IFileSystem>(IFileSystem);
}

get onDidChangeCodeLenses(): Event<void> {
return this._onDidChange.event;
}

public async provideCodeLenses(document: TextDocument, token: CancellationToken) {
const wkspace = workspace.getWorkspaceFolder(document.uri);
const wkspace = this.workspaceService.getWorkspaceFolder(document.uri);
Comment thread
karrtikr marked this conversation as resolved.
Resolved
if (!wkspace) {
return [];
}
Expand Down Expand Up @@ -52,16 +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 {
Comment thread
karrtikr marked this conversation as resolved.
Resolved
const wkspace = this.workspaceService.getWorkspaceFolder(document.uri);
if (!wkspace) {
return [];
return;
}
const tests = this.testCollectionStorage.getTests(wkspace.uri);
if (!tests) {
return [];
return;
}
const file = tests.testFiles.find(item => item.fullPath === document.uri.fsPath);
return tests.testFiles.find(item => this.fileSystem.arePathsSame(item.fullPath, document.uri.fsPath));
Comment thread
ericsnowcurrently marked this conversation as resolved.
Resolved
}

private async getCodeLenses(document: TextDocument, token: CancellationToken, symbolProvider: DocumentSymbolProvider) {
const file = this.getTestFileWhichNeedsCodeLens(document);
if (!file) {
return [];
}
Expand Down
27 changes: 24 additions & 3 deletions src/client/testing/common/services/discoveredTestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ 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';
import { DiscoveredTests, ITestDiscoveredTestParser, TestContainer, TestItem } from './types';

@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: [],
Expand All @@ -34,8 +38,14 @@ 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: data.root, folders: [], time: 0,
name: root, folders: [], time: 0,
testFiles: [], resource: resource, nameToRun: data.rootid
};
tests.rootTestFolders.push(rootFolder);
Expand All @@ -58,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: {
Expand Down Expand Up @@ -193,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 {
Expand Down
2 changes: 1 addition & 1 deletion src/client/testing/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
158 changes: 158 additions & 0 deletions src/test/testing/codeLenses/testFiles.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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('Code lenses - Test files', () => {
let testCollectionStorage: typemoq.IMock<ITestCollectionStorageService>;
let workspaceService: typemoq.IMock<IWorkspaceService>;
let fileSystem: typemoq.IMock<IFileSystem>;
let serviceContainer: typemoq.IMock<IServiceContainer>;
let symbolProvider: DocumentSymbolProvider;
let onDidChange: EventEmitter<void>;
let codeLensProvider: TestFileCodeLensProvider;
setup(() => {
workspaceService = typemoq.Mock.ofType<IWorkspaceService>();
fileSystem = typemoq.Mock.ofType<IFileSystem>();
testCollectionStorage = typemoq.Mock.ofType<ITestCollectionStorageService>();
serviceContainer = typemoq.Mock.ofType<IServiceContainer>();
symbolProvider = mock(LanguageServerSymbolProvider);
onDidChange = new EventEmitter<void>();
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();
});
});
Loading