summaryrefslogtreecommitdiff
path: root/tests/node_compat/test.ts
blob: 2f690e94363aa4583c6f5ce69c136472fc13762e (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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/**
 * This script will run the test files specified in the configuration file.
 *
 * Each test file will be run independently (in a separate process as this is
 * what Node.js is doing) and we wait until it completes. If the process reports
 * an abnormal code, the test is reported and the test suite will fail
 * immediately.
 *
 * Some tests check for presence of certain `process.exitCode`.
 * Some tests depends on directories/files created by other tests - they must
 * all share the same working directory.
 */

import { magenta } from "@std/fmt/colors.ts";
import { pooledMap } from "@std/async/pool.ts";
import { dirname, fromFileUrl, join } from "@std/path/mod.ts";
import { fail } from "@std/assert/mod.ts";
import {
  config,
  getPathsFromTestSuites,
  partitionParallelTestPaths,
} from "./common.ts";

// If the test case is invoked like
// deno test -A tests/node_compat/test.ts -- <test-names>
// Use the <test-names> as filters
const filters = Deno.args;
const hasFilters = filters.length > 0;
const toolsPath = dirname(fromFileUrl(import.meta.url));
const testPaths = partitionParallelTestPaths(
  getPathsFromTestSuites(config.tests),
);
const cwd = new URL(".", import.meta.url);
const windowsIgnorePaths = new Set(
  getPathsFromTestSuites(config.windowsIgnore),
);
const darwinIgnorePaths = new Set(
  getPathsFromTestSuites(config.darwinIgnore),
);

const decoder = new TextDecoder();
let testSerialId = 0;

async function runTest(t: Deno.TestContext, path: string): Promise<void> {
  // If filter patterns are given and any pattern doesn't match
  // to the file path, then skip the case
  if (
    filters.length > 0 &&
    filters.every((pattern) => !path.includes(pattern))
  ) {
    return;
  }
  const ignore =
    (Deno.build.os === "windows" && windowsIgnorePaths.has(path)) ||
    (Deno.build.os === "darwin" && darwinIgnorePaths.has(path));
  await t.step({
    name: `Node.js compatibility "${path}"`,
    ignore,
    sanitizeOps: false,
    sanitizeResources: false,
    sanitizeExit: false,
    fn: async () => {
      const testCase = join(toolsPath, "test", path);

      const v8Flags = ["--stack-size=4000"];
      const testSource = await Deno.readTextFile(testCase);
      const envVars: Record<string, string> = {};
      // TODO(kt3k): Parse `Flags` directive correctly
      if (testSource.includes("Flags: --expose_externalize_string")) {
        v8Flags.push("--expose-externalize-string");
        // TODO(bartlomieju): disable verifying globals if that V8 flag is
        // present. Even though we should be able to pass a list of globals
        // that are allowed, it doesn't work, because the list is expected to
        // contain actual JS objects, not strings :)).
        envVars["NODE_TEST_KNOWN_GLOBALS"] = "0";
      }
      // TODO(nathanwhit): once we match node's behavior on executing
      // `node:test` tests when we run a file, we can remove this
      const usesNodeTest = testSource.includes("node:test");
      const args = [
        usesNodeTest ? "test" : "run",
        "-A",
        "--quiet",
        //"--unsafely-ignore-certificate-errors",
        "--unstable-unsafe-proto",
        "--unstable-bare-node-builtins",
        "--v8-flags=" + v8Flags.join(),
      ];
      if (usesNodeTest) {
        // deno test typechecks by default + we want to pass script args
        args.push("--no-check", "runner.ts", "--", testCase);
      } else {
        args.push("runner.ts", testCase);
      }

      // Pipe stdout in order to output each test result as Deno.test output
      // That way the tests will respect the `--quiet` option when provided
      const command = new Deno.Command(Deno.execPath(), {
        args,
        env: {
          TEST_SERIAL_ID: String(testSerialId++),
          ...envVars,
        },
        cwd,
      });
      const { code, stdout, stderr } = await command.output();

      if (code !== 0) {
        // If the test case failed, show the stdout, stderr, and instruction
        // for repeating the single test case.
        if (stdout.length) {
          console.log(decoder.decode(stdout));
        }
        const stderrOutput = decoder.decode(stderr);
        const repeatCmd = magenta(
          `./target/debug/deno test -A tests/node_compat/test.ts -- ${path}`,
        );
        const msg = `"${magenta(path)}" failed:

${stderrOutput}

You can repeat only this test with the command:

  ${repeatCmd}
`;
        console.log(msg);
        fail(msg);
      } else if (hasFilters) {
        // Even if the test case is successful, shows the stdout and stderr
        // when test case filtering is specified.
        if (stdout.length) console.log(decoder.decode(stdout));
        if (stderr.length) console.log(decoder.decode(stderr));
      }
    },
  });
}

Deno.test("Node.js compatibility", async (t) => {
  for (const path of testPaths.sequential) {
    await runTest(t, path);
  }
  const testPool = pooledMap(
    navigator.hardwareConcurrency,
    testPaths.parallel,
    (path) => runTest(t, path),
  );
  const testCases = [];
  for await (const testCase of testPool) {
    testCases.push(testCase);
  }
  await Promise.all(testCases);
});

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.`,
      );
    }
  }
}

if (!hasFilters) {
  Deno.test("checkConfigTestFilesOrder", function () {
    checkConfigTestFilesOrder([
      ...Object.keys(config.ignore).map((suite) => config.ignore[suite]),
      ...Object.keys(config.tests).map((suite) => config.tests[suite]),
    ]);
  });
}