Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/simulation/workbench/stores/baselineJSONProvider.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 fs from 'fs';
7
import * as mobx from 'mobx';
8
import * as path from 'path';
9
import { Disposable, toDisposable } from '../../../../src/util/vs/base/common/lifecycle';
10
import { IBaselineTestSummary, OLD_BASELINE_FILENAME, PRODUCED_BASELINE_FILENAME, SIMULATION_FOLDER_NAME } from '../../shared/sharedTypes';
11
import { REPO_ROOT, genericEquals } from '../utils/utils';
12
import { SimulationRunner } from './simulationRunner';
13
14
export const BASELINE_PATH = path.join(REPO_ROOT, './test/simulation/baseline.json');
15
16
export class BaselineJSONProvider extends Disposable {
17
18
@mobx.observable
19
public workingTreeBaselineJSON: IBaselineTestSummary[] = [];
20
21
/**
22
* Baseline produced by current run
23
*/
24
@mobx.observable
25
public baselineJSONProducedByRun: IBaselineTestSummary[] = [];
26
27
/**
28
* Baseline snapshotted before current run
29
*/
30
@mobx.observable
31
public baselineJSONBeforeCurrentRun: IBaselineTestSummary[] = [];
32
33
constructor(
34
private readonly _runner: SimulationRunner
35
) {
36
super();
37
mobx.makeObservable(this);
38
39
// watch for working-tree baseline.json that's checked into git
40
const listener = () => this._updateWorkingTreeBaselineJSON();
41
fs.watchFile(BASELINE_PATH, listener);
42
this._register(toDisposable(() => fs.unwatchFile(BASELINE_PATH, listener)));
43
44
this._updateWorkingTreeBaselineJSON();
45
46
mobx.autorun(() => {
47
this._updateRunBaselines();
48
});
49
}
50
51
private get oldBaselineJSONPath() {
52
return path.join(REPO_ROOT, SIMULATION_FOLDER_NAME, this._runner.selectedRun, OLD_BASELINE_FILENAME);
53
}
54
55
private get baselineJSONProducedByRunPath() {
56
return path.join(REPO_ROOT, SIMULATION_FOLDER_NAME, this._runner.selectedRun, PRODUCED_BASELINE_FILENAME);
57
}
58
59
private async _updateWorkingTreeBaselineJSON(): Promise<void> {
60
const newBaseline = await this._readBaselineJSON(BASELINE_PATH);
61
if (genericEquals(this.workingTreeBaselineJSON, newBaseline)) {
62
return;
63
}
64
65
mobx.runInAction(() => {
66
this.workingTreeBaselineJSON = newBaseline;
67
});
68
}
69
70
async updateRootBaselineJSON() {
71
await fs.promises.copyFile(this.baselineJSONProducedByRunPath, BASELINE_PATH);
72
this._updateWorkingTreeBaselineJSON();
73
}
74
75
private async _updateRunBaselines(): Promise<void> {
76
if (this._runner.selectedRun === '') {
77
return;
78
}
79
80
// TODO@ulugbekna: a baseline.old.json.txt is written only if `casUseBaseline` is true, so we need to make sure it doesn't throw here
81
const [bjBeforeCurrentRunR, bjProducedByRunR] = await Promise.allSettled([
82
this._readBaselineJSON(this.oldBaselineJSONPath),
83
this._readBaselineJSON(this.baselineJSONProducedByRunPath),
84
]);
85
86
if (bjBeforeCurrentRunR.status === 'rejected') {
87
console.error(`Failed to read baseline.old.json.txt: ${bjBeforeCurrentRunR.reason}`);
88
return;
89
}
90
91
if (bjProducedByRunR.status === 'rejected') {
92
console.error(`Failed to read baseline.produced.json.txt: ${bjProducedByRunR.reason}`);
93
return;
94
}
95
96
const bjBeforeCurrentRun = bjBeforeCurrentRunR.value;
97
const bjProducedByRun = bjProducedByRunR.value;
98
99
if (genericEquals(this.baselineJSONBeforeCurrentRun, bjBeforeCurrentRun) && genericEquals(this.baselineJSONProducedByRun, bjProducedByRun)) {
100
return;
101
}
102
103
mobx.runInAction(() => {
104
this.baselineJSONBeforeCurrentRun = bjBeforeCurrentRun;
105
this.baselineJSONProducedByRun = bjProducedByRun;
106
});
107
}
108
109
private async _readBaselineJSON(baselineJSONPath: string): Promise<IBaselineTestSummary[]> {
110
const contents = await fs.promises.readFile(baselineJSONPath, 'utf8');
111
const res = JSON.parse(contents) as IBaselineTestSummary[];
112
res.sort((a, b) => a.name.localeCompare(b.name));
113
return res;
114
}
115
}
116
117