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
80 lines (71 loc) · 3.04 KB
/
testFileWatcher.ts
File metadata and controls
80 lines (71 loc) · 3.04 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import * as _ from 'lodash';
import { Disposable, FileSystemWatcher, RelativePattern, Uri, workspace } from 'vscode';
import { testSourceProvider } from '../extension.bundle';
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';
class TestFileWatcher implements Disposable {
private disposables: Disposable[] = [];
private registerListenersDebounce: _.DebouncedFunc<() => Promise<void>> = _.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 = [];
}
protected async registerListenersInternal(): Promise<void> {
if (!isStandardServerReady()) {
return;
}
this.dispose();
try {
const patterns: RelativePattern[] = await testSourceProvider.getTestSourcePattern();
for (const pattern of patterns) {
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);
}
}
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();