deno.land / std@0.166.0 / node / _tools / setup.ts

نووسراو ببینە
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env -S deno run --no-check=remote --allow-read=. --allow-write=.// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { Foras, gunzip } from "https://deno.land/x/denoflate@2.0.2/deno/mod.ts";import { Untar } from "../../archive/tar.ts";import { walk } from "../../fs/walk.ts";import { basename, dirname, fromFileUrl, join, resolve, sep,} from "../../path/mod.ts";import { ensureFile } from "../../fs/ensure_file.ts";import { config, ignoreList } from "./common.ts";import { Buffer } from "../../io/buffer.ts";import { copy, readAll, writeAll } from "../../streams/conversion.ts";import { downloadFile } from "../../_util/download_file.ts";import { updateToDo } from "./list_remaining_tests.ts";
Foras.initSyncBundledOnce();
/** * This script will download and extract the test files specified in the * configuration file * * It will delete any previous tests unless they are specified on the `ignore` * section of the configuration file * * Usage: `deno run --allow-read --allow-net --allow-write setup.ts` * * You can additionally pass a flag to indicate if cache should be used for generating * the tests, or to generate the tests from scratch (-y/-n) */
Foras.initSyncBundledOnce();
const USE_CACHE = Deno.args.includes("-y");const DONT_USE_CACHE = Deno.args.includes("-n");
if (USE_CACHE && DONT_USE_CACHE) { throw new Error( '"-y" and "-n" options for cache can\'t be used at the same time', );}
let CACHE_MODE: "cache" | "prompt" | "no_cache";if (USE_CACHE) { CACHE_MODE = "cache";} else if (DONT_USE_CACHE) { CACHE_MODE = "no_cache";} else { CACHE_MODE = "prompt";}
const NODE_URL = "https://nodejs.org/dist/vNODE_VERSION";const NODE_FILE = "node-vNODE_VERSION";const NODE_ARCHIVE_FILE = `${NODE_FILE}.tar.gz`;const NATIVE_NODE_TESTS_FOLDER = "/test";
/** URL for the download */const url = `${NODE_URL}/${NODE_ARCHIVE_FILE}`.replaceAll( "NODE_VERSION", config.nodeVersion,);/** Local archive's url location */const archivePath = join( config.versionsFolder, NODE_ARCHIVE_FILE.replaceAll("NODE_VERSION", config.nodeVersion),);/** Local decompressed source's location */const decompressedSourcePath = join( config.versionsFolder, NODE_FILE.replaceAll("NODE_VERSION", config.nodeVersion),);
function checkConfigTestFilesOrder(testFileLists: Array<string[]>) { for (const testFileList of testFileLists) { const sortedTestList = JSON.parse(JSON.stringify(testFileList)); sortedTestList.sort(); if (JSON.stringify(testFileList) !== JSON.stringify(sortedTestList)) { throw new Error(`File names in \`config.json\` are not correct order.`); } }}
checkConfigTestFilesOrder([ ...Object.keys(config.ignore).map((suite) => config.ignore[suite]), ...Object.keys(config.tests).map((suite) => config.tests[suite]),]);
async function clearTests() { console.log("Cleaning up previous tests");
const files = walk( fromFileUrl(new URL(config.suitesFolder, import.meta.url)), { includeDirs: false, skip: ignoreList, }, );
for await (const file of files) { await Deno.remove(file.path); }}
async function decompressTests(archivePath: string) { console.log(`Decompressing ${basename(archivePath)}...`);
const compressedFile = await Deno.open( new URL(archivePath, import.meta.url), { read: true }, );
const buffer = new Buffer(gunzip(await readAll(compressedFile))); compressedFile.close();
const tar = new Untar(buffer); const outFolder = dirname(fromFileUrl(new URL(archivePath, import.meta.url))); const testsFolder = `${NODE_FILE}${NATIVE_NODE_TESTS_FOLDER}`.replace( "NODE_VERSION", config.nodeVersion, );
for await (const entry of tar) { if (entry.type !== "file") continue; if (!entry.fileName.startsWith(testsFolder)) continue; const path = join(outFolder, entry.fileName); await ensureFile(path); const file = await Deno.open(path, { create: true, truncate: true, write: true, }); await copy(entry, file); file.close(); }}
/** * This will iterate over test list defined in the configuration file and test the * passed file against it. If a match were to be found, it will return the test * suite specified for that file */function getRequestedFileSuite( file: string, expectedSuite: string,): string | undefined { if (!Array.isArray(config.tests[expectedSuite])) { return; }
for (const regex of config.tests[expectedSuite]) { if (new RegExp("^" + regex).test(file)) { return expectedSuite; } }}
async function copyTests(filePath: string) { console.log("Copying test files..."); const path = join( fromFileUrl(new URL(filePath, import.meta.url)), NATIVE_NODE_TESTS_FOLDER, ); const suitesFolder = fromFileUrl( new URL(config.suitesFolder, import.meta.url), );
for await (const entry of walk(path, { skip: ignoreList })) { const expectedSuite = dirname(entry.path).split(sep).at(-1)!; const suite = getRequestedFileSuite(entry.name, expectedSuite); if (!suite) continue;
const destPath = resolve(suitesFolder, suite, basename(entry.name)); await ensureFile(destPath); const destFile = await Deno.open(destPath, { create: true, truncate: true, write: true, }); const srcFile = await Deno.open(join(path, suite, entry.name), { read: true, }); // This will allow CI to pass without checking linting and formatting // on the test suite files, removing the need to maintain that as well await writeAll( destFile, new TextEncoder().encode( "// deno-fmt-ignore-file\n// deno-lint-ignore-file\n" + "\n// Copyright Joyent and Node contributors. All rights reserved. MIT license.\n" + `// Taken from Node ${config.nodeVersion}\n` + '// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually\n\n', ), ); await copy(srcFile, destFile); srcFile.close(); destFile.close(); }}
// mainlet shouldDownload = false;if (CACHE_MODE === "prompt") { let testArchiveExists = false;
try { Deno.lstatSync(new URL(archivePath, import.meta.url)); testArchiveExists = true; } catch (e) { if (!(e instanceof Deno.errors.NotFound)) { throw e; } shouldDownload = true; }
if (testArchiveExists) { while (true) { const r = (prompt(`File "${archivePath}" found, use file? Y/N:`) ?? "") .trim() .toUpperCase(); if (r === "Y") { break; } else if (r === "N") { shouldDownload = true; break; } else { console.log(`Unexpected: "${r}"`); } } }} else if (CACHE_MODE === "no_cache") { shouldDownload = true;}
if (shouldDownload) { console.log(`Downloading ${url} in path "${archivePath}" ...`); await downloadFile(url, new URL(archivePath, import.meta.url)); console.log(`Downloaded: ${url} into ${archivePath}`);}
let shouldDecompress = false;if (CACHE_MODE === "prompt") { let testFolderExists = false; try { Deno.lstatSync(new URL(decompressedSourcePath, import.meta.url)); testFolderExists = true; } catch (e) { if (!(e instanceof Deno.errors.NotFound)) { throw e; } shouldDecompress = true; }
if (testFolderExists) { while (true) { const r = ( prompt( `Decompressed file "${decompressedSourcePath}" found, use file? Y/N:`, ) ?? "" ) .trim() .toUpperCase(); if (r === "Y") { break; } else if (r === "N") { shouldDecompress = true; break; } else { console.log(`Unexpected: "${r}"`); } } }} else if (CACHE_MODE === "no_cache") { shouldDecompress = true;}
if (shouldDecompress) { await decompressTests(archivePath);}
await clearTests();await copyTests(decompressedSourcePath);await updateToDo();
std

Version Info

Tagged at
a year ago