forked from microsoft/vscode-java-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestFileWatcher.ts
More file actions
89 lines (80 loc) · 3.69 KB
/
testFileWatcher.ts
File metadata and controls
89 lines (80 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as _ from 'lodash';
import { Disposable, FileSystemWatcher, RelativePattern, Uri, workspace, WorkspaceFolder } from 'vscode';
import { testCodeLensController } from './codelens/TestCodeLensController';
import { testExplorer } from './explorer/testExplorer';
import { isStandardServerReady } from './extension';
import { logger } from './logger/logger';
import { ITestItem, TestLevel } from './protocols';
import { testItemModel } from './testItemModel';
import { testResultManager } from './testResultManager';
import { getTestSourcePaths } from './utils/commandUtils';
class TestFileWatcher implements Disposable {
private patterns: RelativePattern[] = [];
private disposables: Disposable[] = [];
private registerListenersDebounce: (() => Promise<void>) & _.Cancelable = _.debounce(this.registerListenersInternal, 2 * 1000 /*ms*/);
public async registerListeners(debounce: boolean = false): Promise<void> {
if (debounce) {
await this.registerListenersDebounce();
} else {
await this.registerListenersInternal();
}
}
public dispose(): void {
for (const disposable of this.disposables) {
if (disposable) {
disposable.dispose();
}
}
this.disposables = [];
this.patterns = [];
}
protected async registerListenersInternal(): Promise<void> {
if (!isStandardServerReady()) {
return;
}
this.dispose();
if (workspace.workspaceFolders) {
try {
const sourcePaths: string[] = await getTestSourcePaths(workspace.workspaceFolders.map((workspaceFolder: WorkspaceFolder) => workspaceFolder.uri.toString()));
for (const sourcePath of sourcePaths) {
const normalizedPath: string = Uri.file(sourcePath).fsPath;
const pattern: RelativePattern = new RelativePattern(normalizedPath, '**/*.java');
this.patterns.push(pattern);
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher(pattern, true /* ignoreCreateEvents */);
this.registerWatcherListeners(watcher);
this.disposables.push(watcher);
}
} catch (error) {
logger.error('Failed to get the test paths', error);
const watcher: FileSystemWatcher = workspace.createFileSystemWatcher('**/*.java');
this.registerWatcherListeners(watcher);
this.disposables.push(watcher);
}
testCodeLensController.registerCodeLensProvider(this.patterns);
}
}
private registerWatcherListeners(watcher: FileSystemWatcher): void {
this.disposables.push(
watcher.onDidChange((uri: Uri) => {
const nodes: ITestItem[] = testItemModel.getItemsByFsPath(uri.fsPath);
for (const node of nodes) {
if (node.level === TestLevel.Class) {
testExplorer.refresh(node);
}
}
}),
watcher.onDidDelete((uri: Uri) => {
const nodes: ITestItem[] = testItemModel.getItemsByFsPath(uri.fsPath);
for (const node of nodes) {
testItemModel.removeTestItemById(node.id);
testResultManager.removeResultById(node.id);
}
testItemModel.removeIdMappingByFsPath(uri.fsPath);
testExplorer.refresh();
}),
);
}
}
export const testFileWatcher: TestFileWatcher = new TestFileWatcher();