Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/script/testGeneration/editFromPatchTests.ts
13388 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
import { promises as fs } from 'fs';
6
import minimist from 'minimist';
7
import * as path from 'path';
8
9
const fixturesRootsFolder = path.join(__dirname, '../../src/extension/test/node/fixtures/patch');
10
const simulationsRootFolder = path.join(__dirname, '../../.simulation');
11
12
async function main(simulationFolder: string | undefined, all: boolean | undefined, annotationFilter: string = 'invalid patch'): Promise<void> {
13
if (!simulationFolder || !await checkExists(simulationsRootFolder)) {
14
const lastRunName = await findLastRun(simulationsRootFolder);
15
if (!lastRunName) {
16
console.log(`No run found in ${simulationsRootFolder}`);
17
return;
18
}
19
simulationFolder = path.join(simulationsRootFolder, lastRunName);
20
}
21
const outputFixturesFolder = path.join(fixturesRootsFolder, path.basename(simulationFolder));
22
console.log(`Looking for stest results in ${simulationFolder}`);
23
const entries = await fs.readdir(simulationFolder);
24
for (const entry of entries) {
25
for (let testRun = 0; testRun < 10; testRun++) {
26
const simTextPath = path.join(simulationFolder, entry, `0${testRun}-inline-simulator.txt`);
27
if (!await checkExists(simTextPath)) {
28
break;
29
}
30
const simText = JSON.parse(await fs.readFile(simTextPath, 'utf8')) as any[];
31
if (!all) {
32
const isInvalidPatch = (() => {
33
for (let i = 0; i < simText.length; i++) {
34
const data = simText[i];
35
if (data.kind === 'interaction' && Array.isArray(data.annotations)) {
36
for (const annotation of data.annotations) {
37
if (annotation.label === annotationFilter) {
38
return true;
39
}
40
}
41
}
42
}
43
return undefined;
44
})();
45
if (!isInvalidPatch) {
46
continue;
47
}
48
}
49
const simRequest = JSON.parse(await fs.readFile(path.join(simulationFolder, entry, `0${testRun}-sim-requests.txt`), 'utf8')) as any[];
50
const originalFileEntry = (() => {
51
for (let i = 0; i < simText.length; i++) {
52
const data = simText[i];
53
if (data.kind === 'initial') {
54
return data.file;
55
}
56
}
57
return undefined;
58
})();
59
if (!originalFileEntry) {
60
console.log('No original file path found');
61
break;
62
}
63
const original = await fs.readFile(path.join(simulationFolder, originalFileEntry.relativeDiskPath), 'utf8');
64
const modifedFilePath = (() => {
65
for (let i = 0; i < simText.length; i++) {
66
const data = simText[i];
67
if (data.kind === 'interaction' && Array.isArray(data.changedFiles)) {
68
for (const changedFile of data.changedFiles) {
69
if (changedFile.workspacePath === originalFileEntry.workspacePath) {
70
return changedFile.relativeDiskPath;
71
}
72
}
73
}
74
}
75
return undefined;
76
})();
77
78
if (!modifedFilePath) {
79
console.log('No modified file path found');
80
break;
81
}
82
const modified = await fs.readFile(path.join(simulationFolder, modifedFilePath), 'utf8');
83
const response = simRequest[0].response.value.join('');
84
85
const name = `${entry}0${testRun}`;
86
console.log(`Writing fixtures for ${name} at ${outputFixturesFolder}`);
87
await fs.mkdir(outputFixturesFolder, { recursive: true });
88
await fs.writeFile(path.join(outputFixturesFolder, `${name}.original.txt`), original);
89
await fs.writeFile(path.join(outputFixturesFolder, `${name}.expected.txt`), modified);
90
await fs.writeFile(path.join(outputFixturesFolder, `${name}.patch.txt`), response);
91
}
92
93
}
94
}
95
96
async function findLastRun(simulationsRootFolder: string): Promise<string | undefined> {
97
const entries = await fs.readdir(simulationsRootFolder);
98
return entries.filter(entry => entry.match(/^out-\d{8}-\d{6}$/)).sort().pop();
99
}
100
101
async function checkExists(filePath: string): Promise<boolean> {
102
try {
103
await fs.stat(filePath);
104
return true;
105
} catch (error) {
106
return false;
107
}
108
}
109
110
111
if (require.main === module) {
112
const parsedArgs = minimist(process.argv);
113
if (parsedArgs.help) {
114
console.log('Usage: npx tsx editFromPatchTests.ts --simulation-folder <path-to-simulation-folder> --all --annotation <annotation>');
115
process.exit(0);
116
}
117
118
119
main(parsedArgs['simulation-folder'], parsedArgs['all'], parsedArgs['annotation']).catch(err => {
120
console.error(err);
121
process.exit(1);
122
});
123
124
}
125
126