summaryrefslogtreecommitdiff
path: root/tools/node_compat/setup.ts
blob: c8fd6a8e09ddd46770142714077c82ddd48b0720 (plain)
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
#!/usr/bin/env -S deno run --allow-read=. --allow-write=. --allow-net=nodejs.org
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

/** This script downloads Node.js source tarball, extracts it and copies the
 * test files according to the config file `cli/tests/node_compat/config.json`
 */

import { Foras, gunzip } from "https://deno.land/x/denoflate@2.0.2/deno/mod.ts";
import { Untar } from "../../test_util/std/archive/untar.ts";
import { walk } from "../../test_util/std/fs/walk.ts";
import {
  dirname,
  fromFileUrl,
  join,
  sep,
} from "../../test_util/std/path/mod.ts";
import { ensureFile } from "../../test_util/std/fs/ensure_file.ts";
import { Buffer } from "../../test_util/std/io/buffer.ts";
import { copy } from "../../test_util/std/streams/copy.ts";
import { readAll } from "../../test_util/std/streams/read_all.ts";
import { writeAll } from "../../test_util/std/streams/write_all.ts";
import { withoutAll } from "../../test_util/std/collections/without_all.ts";
import { relative } from "../../test_util/std/path/posix.ts";

import { config, ignoreList } from "../../cli/tests/node_compat/common.ts";

const encoder = new TextEncoder();

const NODE_VERSION = config.nodeVersion;
const NODE_NAME = "node-v" + NODE_VERSION;
const NODE_ARCHIVE_NAME = `${NODE_NAME}.tar.gz`;

const NODE_IGNORED_TEST_DIRS = [
  "addons",
  "async-hooks",
  "cctest",
  "common",
  "doctool",
  "embedding",
  "fixtures",
  "fuzzers",
  "js-native-api",
  "node-api",
  "overlapped-checker",
  "report",
  "testpy",
  "tick-processor",
  "tools",
  "v8-updates",
  "wasi",
  "wpt",
];

const NODE_TARBALL_URL =
  `https://nodejs.org/dist/v${NODE_VERSION}/${NODE_ARCHIVE_NAME}`;
const NODE_VERSIONS_ROOT = new URL("versions/", import.meta.url);
const NODE_TARBALL_LOCAL_URL = new URL(NODE_ARCHIVE_NAME, NODE_VERSIONS_ROOT);
// local dir url where we copy the node tests
const NODE_LOCAL_ROOT_URL = new URL(NODE_NAME, NODE_VERSIONS_ROOT);
const NODE_LOCAL_TEST_URL = new URL(NODE_NAME + "/test/", NODE_VERSIONS_ROOT);
const NODE_COMPAT_TEST_DEST_URL = new URL(
  "../../cli/tests/node_compat/test/",
  import.meta.url,
);

Foras.initSyncBundledOnce();

async function getNodeTests(): Promise<string[]> {
  const paths: string[] = [];
  const rootPath = NODE_LOCAL_TEST_URL.href.slice(7);
  for await (
    const item of walk(NODE_LOCAL_TEST_URL, { exts: [".js"] })
  ) {
    const path = relative(rootPath, item.path);
    if (NODE_IGNORED_TEST_DIRS.every((dir) => !path.startsWith(dir))) {
      paths.push(path);
    }
  }

  return paths.sort();
}

function getDenoTests() {
  return Object.entries(config.tests)
    .filter(([testDir]) => !NODE_IGNORED_TEST_DIRS.includes(testDir))
    .flatMap(([testDir, tests]) => tests.map((test) => testDir + "/" + test));
}

async function updateToDo() {
  const file = await Deno.open(new URL("./TODO.md", import.meta.url), {
    write: true,
    create: true,
    truncate: true,
  });

  const missingTests = withoutAll(await getNodeTests(), await getDenoTests());

  await file.write(encoder.encode(`<!-- deno-fmt-ignore-file -->
# Remaining Node Tests

NOTE: This file should not be manually edited. Please edit 'cli/tests/node_compat/config.json' and run 'tools/node_compat/setup.ts' instead.

Total: ${missingTests.length}

`));
  for (const test of missingTests) {
    await file.write(
      encoder.encode(
        `- [${test}](https://github.com/nodejs/node/tree/v${NODE_VERSION}/test/${test})\n`,
      ),
    );
  }
  file.close();
}

async function clearTests() {
  console.log("Cleaning up previous tests");
  for await (
    const file of walk(NODE_COMPAT_TEST_DEST_URL, {
      includeDirs: false,
      skip: ignoreList,
    })
  ) {
    await Deno.remove(file.path);
  }
}

async function decompressTests() {
  console.log(`Decompressing ${NODE_ARCHIVE_NAME}...`);

  const compressedFile = await Deno.open(NODE_TARBALL_LOCAL_URL);

  const buffer = new Buffer(gunzip(await readAll(compressedFile)));
  compressedFile.close();

  const tar = new Untar(buffer);
  const outFolder = dirname(fromFileUrl(NODE_TARBALL_LOCAL_URL));
  const testsFolder = `${NODE_NAME}/test`;

  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();
  }
}

/** Checks if file has entry in config.json */
function hasEntry(file: string, suite: string) {
  return Array.isArray(config.tests[suite]) &&
    config.tests[suite].includes(file);
}

async function copyTests() {
  console.log("Copying test files...");

  for await (const entry of walk(NODE_LOCAL_TEST_URL, { skip: ignoreList })) {
    const fragments = entry.path.split(sep);
    // suite is the directory name after test/. For example, if the file is
    // "node-v18.12.1/test/fixtures/policy/main.mjs"
    // then suite is "fixtures/policy"
    const suite = fragments.slice(fragments.indexOf(NODE_NAME) + 2, -1)
      .join("/");
    if (!hasEntry(entry.name, suite)) {
      continue;
    }

    const dest = new URL(`${suite}/${entry.name}`, NODE_COMPAT_TEST_DEST_URL);
    await ensureFile(dest);
    const destFile = await Deno.open(dest, {
      create: true,
      truncate: true,
      write: true,
    });
    const srcFile = await Deno.open(
      new URL(`${suite}/${entry.name}`, NODE_LOCAL_TEST_URL),
    );
    if (dest.pathname.endsWith("js")) {
      await writeAll(
        destFile,
        encoder.encode(`// deno-fmt-ignore-file
// deno-lint-ignore-file

// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node ${NODE_VERSION}
// This file is automatically generated by "node/_tools/setup.ts". Do not modify this file manually

`),
      );
    }
    await srcFile.readable.pipeTo(destFile.writable);
  }
}

/** Downloads Node tarball  */
async function downloadFile() {
  console.log(
    `Downloading ${NODE_TARBALL_URL} in "${NODE_TARBALL_LOCAL_URL}" ...`,
  );
  const response = await fetch(NODE_TARBALL_URL);
  if (!response.ok) {
    throw new Error(`Request failed with status ${response.status}`);
  }
  await ensureFile(NODE_TARBALL_LOCAL_URL);
  const file = await Deno.open(NODE_TARBALL_LOCAL_URL, {
    truncate: true,
    write: true,
    create: true,
  });
  await response.body.pipeTo(file.writable);
}

// main

try {
  Deno.lstatSync(NODE_TARBALL_LOCAL_URL);
} catch (e) {
  if (!(e instanceof Deno.errors.NotFound)) {
    throw e;
  }
  await downloadFile();
}

try {
  Deno.lstatSync(NODE_LOCAL_ROOT_URL);
} catch (e) {
  if (!(e instanceof Deno.errors.NotFound)) {
    throw e;
  }
  await decompressTests();
}

await clearTests();
await copyTests();
await updateToDo();