Skip to content

Commit aeb79d5

Browse files
committed
test: add controller recreation for integration tests
1 parent 9c2446e commit aeb79d5

File tree

4 files changed

+53
-39
lines changed

4 files changed

+53
-39
lines changed

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const defaultTestSymbols: IExtensionSettings = {
2323

2424
export const showConfigErrorCommand = 'mocha-vscode.showConfigError';
2525
export const getControllersForTestCommand = 'mocha-vscode.getControllersForTest';
26+
export const recreateControllersForTestCommand = 'mocha-vscode.recreateControllersForTestCommand';
2627

2728
function equalsIgnoreCase(a: string, b: string) {
2829
return a.localeCompare(b, undefined, { sensitivity: 'accent' }) === 0;

src/controller.ts

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -134,29 +134,33 @@ export class Controller {
134134

135135
this.disposables.add(
136136
this.configFile.onActivate(() => {
137-
const ctrl = (this.ctrl = vscode.tests.createTestController(
138-
configFileUri.toString(),
139-
configFileUri.fsPath,
140-
));
141-
this.disposables.add(ctrl);
142-
143-
this.recreateDiscoverer();
144-
const rescan = async (reason: string) => {
145-
try {
146-
logChannel.info(`Rescan of tests triggered (${reason}) - ${this.configFile.uri}}`);
147-
this.recreateDiscoverer();
148-
await this.scanFiles();
149-
} catch (e) {
150-
this.logChannel.error(e as Error, 'Failed to rescan tests');
151-
}
152-
};
153-
this.disposables.add(this.configFile.onDidChange(() => rescan('mocharc changed')));
154-
this.disposables.add(this.settings.onDidChange(() => rescan('settings changed')));
155-
ctrl.refreshHandler = () => {
156-
this.configFile.forget();
157-
rescan('user');
158-
};
159-
this.scanFiles();
137+
try {
138+
const ctrl = (this.ctrl = vscode.tests.createTestController(
139+
configFileUri.toString(),
140+
configFileUri.fsPath,
141+
));
142+
this.disposables.add(ctrl);
143+
144+
this.recreateDiscoverer();
145+
const rescan = async (reason: string) => {
146+
try {
147+
logChannel.info(`Rescan of tests triggered (${reason}) - ${this.configFile.uri}}`);
148+
this.recreateDiscoverer();
149+
await this.scanFiles();
150+
} catch (e) {
151+
this.logChannel.error(e as Error, 'Failed to rescan tests');
152+
}
153+
};
154+
this.disposables.add(this.configFile.onDidChange(() => rescan('mocharc changed')));
155+
this.disposables.add(this.settings.onDidChange(() => rescan('settings changed')));
156+
ctrl.refreshHandler = () => {
157+
this.configFile.forget();
158+
rescan('user');
159+
};
160+
this.scanFiles();
161+
} catch (e) {
162+
this.logChannel.error(e as Error);
163+
}
160164
}),
161165
);
162166

src/extension.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as timers from 'timers/promises';
1111
import * as vscode from 'vscode';
1212
import { ConfigValue } from './configValue';
1313
import { ConsoleOuputChannel } from './consoleLogChannel';
14-
import { getControllersForTestCommand } from './constants';
14+
import { getControllersForTestCommand, recreateControllersForTestCommand } from './constants';
1515
import { initESBuild } from './esbuild';
1616
import { TestRunner } from './runner';
1717
import { SourceMapStore } from './source-map-store';
@@ -103,9 +103,7 @@ export function activate(context: vscode.ExtensionContext) {
103103
}
104104
};
105105

106-
const initialSync = (async () => {
107-
await initESBuild(context, logChannel);
108-
106+
async function syncWorkspaceFoldersWithRetry() {
109107
// Workaround for vscode#179203 where findFiles doesn't work on startup.
110108
// This extension is only activated on workspaceContains, so we have pretty
111109
// high confidence that we should find something.
@@ -118,15 +116,31 @@ export function activate(context: vscode.ExtensionContext) {
118116

119117
await timers.setTimeout(1000);
120118
}
119+
}
120+
121+
const initialSync = (async () => {
122+
await initESBuild(context, logChannel);
123+
await syncWorkspaceFoldersWithRetry();
121124
})();
122125

123126
context.subscriptions.push(
124127
vscode.workspace.onDidChangeWorkspaceFolders(syncWorkspaceFolders),
125-
vscode.commands.registerCommand(getControllersForTestCommand, () =>
126-
initialSync.then(() =>
127-
Array.from(watchers.values()).flatMap((w) => Array.from(w.controllers.values())),
128-
),
129-
),
128+
vscode.commands.registerCommand(getControllersForTestCommand, async () => {
129+
await initialSync;
130+
return Array.from(watchers.values()).flatMap((w) => Array.from(w.controllers.values()));
131+
}),
132+
vscode.commands.registerCommand(recreateControllersForTestCommand, async () => {
133+
logChannel.debug('Destroying all watchers and test controllers');
134+
for (const [, watcher] of watchers) {
135+
watcher.dispose();
136+
}
137+
watchers.clear();
138+
resyncState = FolderSyncState.Idle;
139+
140+
logChannel.debug('Destroyed controllers, recreating');
141+
await syncWorkspaceFoldersWithRetry();
142+
return Array.from(watchers.values()).flatMap((w) => Array.from(w.controllers.values()));
143+
}),
130144
new vscode.Disposable(() => watchers.forEach((c) => c.dispose())),
131145
logChannel,
132146
);

src/test/util.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as path from 'path';
1515
import * as sinon from 'sinon';
1616
import { setTimeout } from 'timers/promises';
1717
import * as vscode from 'vscode';
18-
import { getControllersForTestCommand } from '../constants';
18+
import { getControllersForTestCommand, recreateControllersForTestCommand } from '../constants';
1919
import type { Controller } from '../controller';
2020
import { IParsedNode, NodeKind } from '../discoverer/types';
2121

@@ -64,12 +64,7 @@ export function integrationTestPrepare(name: string) {
6464

6565
afterEach(async () => {
6666
await restoreWorkspace(workspaceFolder, workspaceBackup);
67-
try {
68-
const c = await getController(false);
69-
await c.scanFiles();
70-
} catch (e) {
71-
// ignore
72-
}
67+
await vscode.commands.executeCommand(recreateControllersForTestCommand);
7368
});
7469

7570
return workspaceFolder;

0 commit comments

Comments
 (0)