Skip to content

Commit ea6719a

Browse files
committed
fix: refresh test discovery when source paths change
1 parent a8f5de6 commit ea6719a

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎src/controller/testController.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ export function createTestController(): void {
4141
testController.createRunProfile('Debug Tests', TestRunProfileKind.Debug, runHandler, true, runnableTag);
4242
testController.createRunProfile('Run Tests with Coverage', TestRunProfileKind.Coverage, runHandler, true, runnableTag);
4343

44-
testController.refreshHandler = () => {
45-
refreshExplorer();
46-
}
44+
testController.refreshHandler = () => refreshTestSourcePaths();
4745

4846
startWatchingWorkspace();
4947
}
@@ -52,6 +50,12 @@ export function creatTestProfile(name: string, kind: TestRunProfileKind): void {
5250
testController?.createRunProfile(name, kind, runHandler, false, runnableTag);
5351
}
5452

53+
export async function refreshTestSourcePaths(): Promise<void> {
54+
testSourceProvider.clear();
55+
await refreshExplorer();
56+
await startWatchingWorkspace();
57+
}
58+
5559
export const loadChildren: (item: TestItem, token?: CancellationToken, force?: boolean) => Promise<void> = instrumentOperation('java.test.explorer.loadChildren', async (_operationId: string, item: TestItem, token?: CancellationToken, force: boolean = false) => {
5660
if (!item) {
5761
await loadJavaProjects();
@@ -153,7 +157,7 @@ async function startWatchingWorkspace(): Promise<void> {
153157
return;
154158
}
155159

156-
for (const disposable of watchers) {
160+
for (const disposable of watchers.splice(0)) {
157161
disposable.dispose();
158162
}
159163

‎src/extension.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
// Licensed under the MIT license.
33

44
import * as path from 'path';
5-
import { commands, DebugConfiguration, Event, Extension, ExtensionContext, extensions, TestItem, TestRunProfileKind, TextDocument, TextDocumentChangeEvent, TextEditor, Uri, window, workspace, WorkspaceFoldersChangeEvent } from 'vscode';
5+
import { commands, ConfigurationChangeEvent, DebugConfiguration, Event, Extension, ExtensionContext, extensions, TestItem, TestRunProfileKind, TextDocument, TextDocumentChangeEvent, TextEditor, Uri, window, workspace, WorkspaceFoldersChangeEvent } from 'vscode';
66
import { dispose as disposeTelemetryWrapper, initializeFromJsonFile, instrumentOperation, instrumentOperationAsVsCodeCommand } from 'vscode-extension-telemetry-wrapper';
77
import { navigateToTestOrTarget } from './commands/navigation/navigationCommands';
88
import { generateTests } from './commands/generationCommands';
99
import { runTestsFromJavaProjectExplorer } from './commands/projectExplorerCommands';
1010
import { refreshExplorer, refreshProject, runTestsFromTestExplorer } from './commands/testExplorerCommands';
1111
import { openStackTrace } from './commands/testReportCommands';
1212
import { Context, ExtensionName, JavaTestRunnerCommands, VSCodeCommands } from './constants';
13-
import { createTestController, testController, watchers } from './controller/testController';
13+
import { createTestController, refreshTestSourcePaths, testController, watchers } from './controller/testController';
1414
import { updateItemForDocument, updateItemForDocumentWithDebounce } from './controller/utils';
1515
import { IProgressProvider } from './debugger.api';
1616
import { initExpService } from './experimentationService';
@@ -131,6 +131,11 @@ function registerComponents(context: ExtensionContext): void {
131131
await updateItemForDocumentWithDebounce(e.document.uri);
132132
}
133133
}),
134+
workspace.onDidChangeConfiguration(async (event: ConfigurationChangeEvent) => {
135+
if (event.affectsConfiguration('java.test.additionalTestSourcePaths')) {
136+
await refreshTestSourcePaths();
137+
}
138+
}),
134139
workspace.onDidChangeWorkspaceFolders(async (e: WorkspaceFoldersChangeEvent) => {
135140
for (const deletedFolder of e.removed) {
136141
testSourceProvider.delete(deletedFolder.uri);

‎test/suite/testController.loadChildren.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import * as assert from 'assert';
55
import * as sinon from 'sinon';
66
import { CancellationTokenSource, TestController, TestItem, tests, Uri } from 'vscode';
7-
import { loadChildren } from '../../src/controller/testController';
7+
import { loadChildren, refreshTestSourcePaths } from '../../src/controller/testController';
88
import { dataCache, invalidateResolutionVersion } from '../../src/controller/testItemDataCache';
99
import * as controllerUtils from '../../src/controller/utils';
10+
import * as testExplorerCommands from '../../src/commands/testExplorerCommands';
1011
import { TestKind, TestLevel } from '../../src/java-test-runner.api';
1112
import { IJavaTestItem } from '../../src/types';
13+
import { testSourceProvider } from '../../src/provider/testSourceProvider';
1214
import { setupTestEnv } from './utils';
1315

1416
function createTestItem(testController: TestController, id: string, testLevel: TestLevel,
@@ -43,6 +45,17 @@ suite('testController - loadChildren', () => {
4345
testController.dispose();
4446
});
4547

48+
test('refreshes test source paths after clearing cached discovery paths', async () => {
49+
const clearPathsStub = sinon.stub(testSourceProvider, 'clear');
50+
const refreshExplorerStub = sinon.stub(testExplorerCommands, 'refreshExplorer').resolves();
51+
const watcherPatternsStub = sinon.stub(testSourceProvider, 'getTestSourcePattern').resolves([]);
52+
const additionalPathsStub = sinon.stub(testSourceProvider, 'getAdditionalTestSourcePaths').resolves([]);
53+
54+
await refreshTestSourcePaths();
55+
56+
sinon.assert.callOrder(clearPathsStub, refreshExplorerStub, watcherPatternsStub, additionalPathsStub);
57+
});
58+
4659
test('should reuse resolved project children until a forced refresh', async () => {
4760
const project: TestItem = createTestItem(testController, 'project', TestLevel.Project);
4861
const findTestsStub = sinon.stub(controllerUtils, 'findTestPackagesAndTypes').resolves([]);

0 commit comments

Comments
 (0)