-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-snapshots.js
137 lines (123 loc) · 3.84 KB
/
update-snapshots.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { NixEval, getStringifyResult } from "../src/nix-eval.js";
import child_process from 'child_process'
import { fileTests } from './file-tests.js';
const stringify = getStringifyResult({
maximumDepth: 10,
maximumBreadth: 100,
})
import * as fs from "fs"
import * as path from "path"
import { fileURLToPath } from 'url';
let caseDir = path.dirname(fileURLToPath(import.meta.url))
for (let file of fs.readdirSync(caseDir)) {
if (!/\.snapshots.txt$/.test(file)) continue
const fileBase = file.slice(0, -1*'.snapshots.txt'.length)
let filePath = path.join(caseDir, file)
let fileContent = fs.readFileSync(filePath, "utf8")
const newTests = []
for (let testData of fileTests(fileContent, file)) {
const { name, text: textJson, expected: oldExpected, configStr, strict } = testData;
//console.dir(testData); // debug
console.log(`update: textJson: ${textJson}`)
if (/SKIP/.test(name)) {
// no change
newTests.push(`#${name ? ' ' : ''}${name}${(configStr || '')}\n${textJson}\n==>\n${oldExpected}`);
continue;
}
const nix = new NixEval();
let result;
let resultString;
let error;
let newExpected;
const text = JSON.parse(textJson);
try {
if (fileBase == 'nix-eval') {
result = nix.eval(text)
}
else if (fileBase == 'nix-normal') {
//result = nix.normal(text)
fs.mkdirSync('/tmp/nix-eval-js/home/work', { recursive: true })
const nix_instantiate = child_process.spawnSync(
'nix-instantiate', [
'--parse',
'--expr',
text,
],
{
//stdio: 'inherit',
encoding: 'utf8',
cwd: '/tmp/nix-eval-js/home/work', // must exist
env: Object.assign({}, process.env, {
// $HOME must be owned by user
// https://github.com/NixOS/nix/issues/6834
HOME: '/tmp/nix-eval-js/home',
}),
}
)
result = nix_instantiate.output[1].trim()
if (result == '' && nix_instantiate.status != 0) {
result = nix_instantiate.output[2].split("\n")[0]
//console.dir({ result })
if (result.startsWith('error: ')) {
result = 'ERROR EvalError ' + result.slice('error: '.length)
}
}
//console.dir({ nix_instantiate, result }); throw new Error("todo")
}
else {
throw new Error(`internal test error: unknown fileBase: ${fileBase}`)
}
}
catch (_error) {
//if (fileBase == 'nix-normal') throw _error
if (
_error.message && (
_error.message.startsWith('internal test error: unknown fileBase') ||
_error.message == 'todo'
)
) {
throw _error
}
error = _error;
}
if (error) {
newExpected = `ERROR ${error.name} ${error.message}`;
}
else {
try {
// not done yet
// some errors are triggered by stringify, because lazy eval
// example: "{a=1;b=a;}"
if (fileBase == 'nix-eval') {
resultString = String(stringify(result));
}
else {
// dont stringify
resultString = String(result);
}
}
catch (_error) {
error = _error;
}
if (error) {
newExpected = `ERROR ${error.name} ${error.message}`;
}
else {
// done
newExpected = resultString;
}
}
//if (name == 'some test name') { console.dir(testData) } // debug
newTests.push(`#${name ? ' ' : ''}${name}${(configStr || '')}\n${textJson}\n==>\n${newExpected}`)
}
const newFileContent = newTests.join("\n\n") + "\n";
const dryRun = false;
if (dryRun) {
console.log(newFileContent);
}
else {
// TODO backup?
console.log(`writing ${filePath}`);
fs.writeFileSync(filePath, newFileContent, "utf8");
}
}