Skip to content

Commit ba92e6c

Browse files
committed
Fix flaky active-solution-tracker.test.ts
1 parent 177ef4e commit ba92e6c

4 files changed

Lines changed: 31 additions & 27 deletions

File tree

src/solutions/active-solution-tracker.test.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ describe('ActiveSolutionTracker', () => {
6363
},
6464
};
6565

66-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([
67-
SOLUTION_URI_FOO,
68-
SOLUTION_URI_BAR,
69-
SOLUTION_URI_DEFAULT,
70-
]);
71-
7266
changeSolutionsListener = jest.fn();
7367
changeActiveListener = jest.fn();
7468

@@ -78,6 +72,12 @@ describe('ActiveSolutionTracker', () => {
7872
configurationProvider = configurationProviderFactory();
7973
workspaceFsProvider = workspaceFsProviderFactory();
8074

75+
workspaceFoldersProvider.findFiles.mockResolvedValue([
76+
SOLUTION_URI_FOO,
77+
SOLUTION_URI_BAR,
78+
SOLUTION_URI_DEFAULT,
79+
]);
80+
8181
activeSolutionTracker = new ActiveSolutionTrackerImpl(
8282
messageProviderFactory(),
8383
commandsProvider,
@@ -101,13 +101,13 @@ describe('ActiveSolutionTracker', () => {
101101
it('searches for solution files on activation', async () => {
102102
await activeSolutionTracker.activate(context as unknown as vscode.ExtensionContext);
103103
await waitForCondition(
104-
async () => (vscode.workspace.findFiles as jest.Mock).mock.calls.length > 0,
104+
async () => workspaceFoldersProvider.findFiles.mock.calls.length > 0,
105105
'solution file search to be triggered after activation',
106106
200,
107107
);
108108

109-
expect(vscode.workspace.findFiles).toHaveBeenCalledTimes(1);
110-
expect(vscode.workspace.findFiles).toHaveBeenCalledWith(ActiveSolutionTrackerImpl.GLOB_PATTERN, undefined);
109+
expect(workspaceFoldersProvider.findFiles).toHaveBeenCalledTimes(1);
110+
expect(workspaceFoldersProvider.findFiles).toHaveBeenCalledWith(ActiveSolutionTrackerImpl.GLOB_PATTERN, undefined);
111111
});
112112

113113
it('uses the configured glob pattern for searches', async () => {
@@ -116,24 +116,24 @@ describe('ActiveSolutionTracker', () => {
116116

117117
await activeSolutionTracker.activate(context as unknown as vscode.ExtensionContext);
118118
await waitForCondition(
119-
async () => (vscode.workspace.findFiles as jest.Mock).mock.calls.length > 0,
119+
async () => workspaceFoldersProvider.findFiles.mock.calls.length > 0,
120120
'solution file search to be triggered with configured exclude glob',
121121
200,
122122
);
123123

124-
expect(vscode.workspace.findFiles).toHaveBeenCalledTimes(1);
125-
expect(vscode.workspace.findFiles).toHaveBeenCalledWith(ActiveSolutionTrackerImpl.GLOB_PATTERN, testGlobPattern);
124+
expect(workspaceFoldersProvider.findFiles).toHaveBeenCalledTimes(1);
125+
expect(workspaceFoldersProvider.findFiles).toHaveBeenCalledWith(ActiveSolutionTrackerImpl.GLOB_PATTERN, testGlobPattern);
126126
});
127127

128128
it('updates when the configured glob pattern changes', async () => {
129129
await activeSolutionTracker.activate(context as unknown as vscode.ExtensionContext);
130130
await waitForCondition(
131-
async () => (vscode.workspace.findFiles as jest.Mock).mock.calls.length > 0,
131+
async () => workspaceFoldersProvider.findFiles.mock.calls.length > 0,
132132
'initial solution file search to complete',
133133
200,
134134
);
135135

136-
(vscode.workspace.findFiles as jest.Mock).mockClear();
136+
workspaceFoldersProvider.findFiles.mockClear();
137137

138138
expect(configurationProvider.onChangeConfiguration).toHaveBeenCalledTimes(1);
139139
expect(configurationProvider.onChangeConfiguration).toHaveBeenCalledWith(expect.any(Function), manifest.CONFIG_EXCLUDE);
@@ -142,22 +142,22 @@ describe('ActiveSolutionTracker', () => {
142142
configurationProvider.getConfigVariable.mockImplementation((name: string) => name === manifest.CONFIG_EXCLUDE ? testGlobPattern : undefined);
143143
configurationProvider.onChangeConfiguration.mock.calls[0][0]();
144144
await waitForCondition(
145-
async () => (vscode.workspace.findFiles as jest.Mock).mock.calls.length > 0,
145+
async () => workspaceFoldersProvider.findFiles.mock.calls.length > 0,
146146
'solution file search to run after configuration change',
147147
200,
148148
);
149149

150-
expect(vscode.workspace.findFiles).toHaveBeenCalledTimes(1);
151-
expect(vscode.workspace.findFiles).toHaveBeenCalledWith(ActiveSolutionTrackerImpl.GLOB_PATTERN, testGlobPattern);
150+
expect(workspaceFoldersProvider.findFiles).toHaveBeenCalledTimes(1);
151+
expect(workspaceFoldersProvider.findFiles).toHaveBeenCalledWith(ActiveSolutionTrackerImpl.GLOB_PATTERN, testGlobPattern);
152152
});
153153

154154
describe('activated with no solutions in the workspace', () => {
155155
beforeEach(async () => {
156-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([]);
156+
workspaceFoldersProvider.findFiles.mockResolvedValue([]);
157157

158158
await activeSolutionTracker.activate(context as unknown as vscode.ExtensionContext);
159159
await waitForCondition(
160-
async () => (vscode.workspace.findFiles as jest.Mock).mock.calls.length > 0,
160+
async () => workspaceFoldersProvider.findFiles.mock.calls.length > 0,
161161
'solution file search to complete in empty workspace',
162162
200,
163163
);
@@ -240,7 +240,7 @@ describe('ActiveSolutionTracker', () => {
240240

241241
describe('activated with solutions in workspace subfolders only', () => {
242242
beforeEach(async () => {
243-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([
243+
workspaceFoldersProvider.findFiles.mockResolvedValue([
244244
SOLUTION_URI_FOO,
245245
SOLUTION_URI_BAR,
246246
]);
@@ -301,7 +301,7 @@ describe('ActiveSolutionTracker', () => {
301301
});
302302

303303
it('updates the list of solutions when a new solution file is added', async () => {
304-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([
304+
workspaceFoldersProvider.findFiles.mockResolvedValue([
305305
SOLUTION_URI_NEW,
306306
SOLUTION_URI_FOO,
307307
SOLUTION_URI_BAR
@@ -321,7 +321,7 @@ describe('ActiveSolutionTracker', () => {
321321
});
322322

323323
it('updates the list of solutions when a solution file is deleted', async () => {
324-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([
324+
workspaceFoldersProvider.findFiles.mockResolvedValue([
325325
SOLUTION_URI_FOO,
326326
]);
327327

@@ -337,7 +337,7 @@ describe('ActiveSolutionTracker', () => {
337337
});
338338

339339
it('updates the list of solutions when a folder containing a solution file is deleted', async () => {
340-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([
340+
workspaceFoldersProvider.findFiles.mockResolvedValue([
341341
SOLUTION_URI_FOO,
342342
]);
343343

@@ -353,7 +353,7 @@ describe('ActiveSolutionTracker', () => {
353353
});
354354

355355
it('updates the list of solutions when a new workspace folder is added', async () => {
356-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([
356+
workspaceFoldersProvider.findFiles.mockResolvedValue([
357357
SOLUTION_URI_NEW,
358358
SOLUTION_URI_FOO,
359359
SOLUTION_URI_BAR
@@ -490,14 +490,14 @@ describe('ActiveSolutionTracker solution file watching', () => {
490490
},
491491
};
492492

493-
(vscode.workspace.findFiles as jest.Mock).mockResolvedValue([URI.file(activeSolution)]);
494-
495493
fileWatcherProvider = fileWatcherProviderFactory();
496494
commandsProvider = commandsProviderFactory();
497495
workspaceFoldersProvider = workspaceFoldersProviderFactory([{ uri: URI.file(solutionRoot), name: 'workspace', index: 0 }]);
498496
workspaceFsProvider = workspaceFsProviderFactory();
499497
configurationProvider = configurationProviderFactory();
500498

499+
workspaceFoldersProvider.findFiles.mockResolvedValue([URI.file(activeSolution)]);
500+
501501
tracker = new ActiveSolutionTrackerImpl(
502502
messageProviderFactory(),
503503
commandsProvider,

src/solutions/active-solution-tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class ActiveSolutionTrackerImpl implements ActiveSolutionTracker {
174174
}
175175

176176
private async getSolutionPaths(): Promise<string[]> {
177-
const uris = await vscode.workspace.findFiles(ActiveSolutionTrackerImpl.GLOB_PATTERN, this.getExcludeGlob());
177+
const uris = await this.workspaceFoldersProvider.findFiles(ActiveSolutionTrackerImpl.GLOB_PATTERN, this.getExcludeGlob());
178178
return uris.map(uri => uri.fsPath).sort();
179179
}
180180

src/vscode-api/workspace-folders-provider.factories.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type MockWorkspaceFoldersProvider = {
2323
onDidChangeWorkspaceFolders: WorkspaceFoldersProvider['onDidChangeWorkspaceFolders'];
2424
asRelativePath: jest.Mock;
2525
getWorkspaceFolder: jest.Mock;
26+
findFiles: jest.Mock;
2627
workspaceFolders: WorkspaceFolder[] | undefined;
2728
updateWorkspaceFolders: (workspaceFolders: WorkspaceFolder[] | undefined) => void;
2829
};
@@ -38,6 +39,7 @@ export const workspaceFoldersProviderFactory = (workspaceFolders?: WorkspaceFold
3839
},
3940
workspaceFolders: workspaceFolders,
4041
getWorkspaceFolder: jest.fn((fsPath: string): WorkspaceFolder | undefined => provider.workspaceFolders?.find(folder => fsPath.startsWith(folder.uri.fsPath))),
42+
findFiles: jest.fn(),
4143
asRelativePath: jest.fn((input: string): string => {
4244
const workspace = provider.getWorkspaceFolder(input)?.uri.fsPath;
4345
if (workspace) {

src/vscode-api/workspace-folders-provider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ export type WorkspaceFoldersProvider = {
2424
onDidChangeWorkspaceFolders: Event<unknown>;
2525
asRelativePath: (fsPath: string) => string;
2626
getWorkspaceFolder: (fsPath: string) => WorkspaceFolder | undefined;
27+
findFiles: typeof vscode.workspace.findFiles;
2728
readonly workspaceFolders: readonly WorkspaceFolder[] | undefined
2829
}
2930

3031
export const workspaceFoldersProvider: WorkspaceFoldersProvider = {
3132
onDidChangeWorkspaceFolders: vscode.workspace.onDidChangeWorkspaceFolders,
3233
asRelativePath: vscode.workspace.asRelativePath,
3334
getWorkspaceFolder: (fsPath: string) => vscode.workspace.getWorkspaceFolder(Uri.file(fsPath)),
35+
findFiles: vscode.workspace.findFiles,
3436

3537
get workspaceFolders(): readonly WorkspaceFolder[] | undefined {
3638
return vscode.workspace.workspaceFolders;

0 commit comments

Comments
 (0)