Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/workbench/stores/detectedTests.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as mobx from 'mobx';
7
import { Disposable, toDisposable } from '../../../../src/util/vs/base/common/lifecycle';
8
import { IDetectedTestOutput } from '../../shared/sharedTypes';
9
import { spawnSimulation } from '../utils/simulationExec';
10
import { genericEquals } from '../utils/utils';
11
12
/**
13
* Detects tests that are available in the simulation.
14
*
15
* @param extraArgs - Additional CLI args to pass to `simulationMain` when listing tests.
16
* Used by NES External mode to pass `--nes=external --external-scenarios=<path>`.
17
*/
18
19
export class DetectedTests extends Disposable {
20
21
@mobx.observable
22
public tests: IDetectedTestOutput[] = [];
23
24
constructor(private readonly _extraArgs?: () => string[]) {
25
super();
26
mobx.makeObservable(this);
27
28
let timer: TimeoutHandle | undefined = undefined;
29
this._register(toDisposable(() => clearInterval(timer)));
30
const resume = () => {
31
this._updateTests();
32
timer = setInterval(() => this._updateTests(), 5000);
33
};
34
const suspend = () => {
35
clearInterval(timer);
36
timer = undefined;
37
};
38
39
mobx.onBecomeObserved(this, 'tests', resume);
40
mobx.onBecomeUnobserved(this, 'tests', suspend);
41
}
42
43
private async _updateTests(): Promise<void> {
44
const newTests = await this._fetchTests();
45
if (genericEquals(this.tests, newTests)) {
46
return;
47
}
48
49
mobx.runInAction(() => {
50
this.tests = newTests;
51
});
52
}
53
54
private async _fetchTests(): Promise<IDetectedTestOutput[]> {
55
const args = ['--list-tests', '--json', ...(this._extraArgs?.() ?? [])];
56
const result = await spawnSimulation<IDetectedTestOutput>({
57
args,
58
ignoreNonJSONLines: true
59
}).toPromise();
60
result.sort((a, b) => a.name.localeCompare(b.name));
61
return result;
62
}
63
}
64
65