2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-03-27 15:54:22 -04:00
|
|
|
|
2023-07-02 14:19:30 -04:00
|
|
|
import { createRequire, Module } from "node:module";
|
2024-02-12 21:05:10 -05:00
|
|
|
import { assert, assertEquals } from "@std/assert/mod.ts";
|
2023-05-13 01:49:11 -04:00
|
|
|
import process from "node:process";
|
fix(node): duplicate node_module suffixes (#19222)
Noticed that we're checking more module paths than necessary. In
particular the module path array contains a couple of entries with a
duplicated `node_modules/node_modules` suffix.
```js
[
// ... more entries before here, where some also contain duplicate suffixes
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/.deno/node_modules",
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/node_modules", // <-- duplicate suffix
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules",
"/Users/marvinhagemeister/dev/node_modules",
"/Users/marvinhagemeister/node_modules",
"/Users/node_modules",
"/node_modules",
"/node_modules" // <-- duplicate entry
]
```
This was caused by a misunderstanding in how Rust's
[`Path::ends_with()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.ends_with)
works. It's designed to match on whole path segments and the suffix
`/node_modules` is not that, except for the root entry. This meant that
our check for if the path already ended with `node_module` always
returned `false`. Removing the leading slash fixes that.
While we're at it, we can remove the last condition where we explicitly
added the root `/node_modules` entry since the while loop prior to that
takes care of it already.
2023-05-23 06:46:14 -04:00
|
|
|
import * as path from "node:path";
|
2023-03-27 15:54:22 -04:00
|
|
|
|
|
|
|
Deno.test("[node/module _preloadModules] has internal require hook", () => {
|
|
|
|
// Check if it's there
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
(Module as any)._preloadModules([
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"./tests/unit_node/testdata/add_global_property.js",
|
2023-03-27 15:54:22 -04:00
|
|
|
]);
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2023-05-13 01:49:11 -04:00
|
|
|
assertEquals((globalThis as any).foo, "Hello");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[node/module runMain] loads module using the current process.argv", () => {
|
|
|
|
process.argv = [
|
|
|
|
process.argv[0],
|
chore: move cli/tests/ -> tests/ (#22369)
This looks like a massive PR, but it's only a move from cli/tests ->
tests, and updates of relative paths for files.
This is the first step towards aggregate all of the integration test
files under tests/, which will lead to a set of integration tests that
can run without the CLI binary being built.
While we could leave these tests under `cli`, it would require us to
keep a more complex directory structure for the various test runners. In
addition, we have a lot of complexity to ignore various test files in
the `cli` project itself (cargo publish exclusion rules, autotests =
false, etc).
And finally, the `tests/` folder will eventually house the `test_ffi`,
`test_napi` and other testing code, reducing the size of the root repo
directory.
For easier review, the extremely large and noisy "move" is in the first
commit (with no changes -- just a move), while the remainder of the
changes to actual files is in the second commit.
2024-02-10 15:22:13 -05:00
|
|
|
"./tests/unit_node/testdata/add_global_property_run_main.js",
|
2023-05-13 01:49:11 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
(Module as any).runMain();
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
assertEquals((globalThis as any).calledViaRunMain, true);
|
2023-03-27 15:54:22 -04:00
|
|
|
});
|
fix(node): duplicate node_module suffixes (#19222)
Noticed that we're checking more module paths than necessary. In
particular the module path array contains a couple of entries with a
duplicated `node_modules/node_modules` suffix.
```js
[
// ... more entries before here, where some also contain duplicate suffixes
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/.deno/node_modules",
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules/node_modules", // <-- duplicate suffix
"/Users/marvinhagemeister/dev/preact-render-to-string/node_modules",
"/Users/marvinhagemeister/dev/node_modules",
"/Users/marvinhagemeister/node_modules",
"/Users/node_modules",
"/node_modules",
"/node_modules" // <-- duplicate entry
]
```
This was caused by a misunderstanding in how Rust's
[`Path::ends_with()`](https://doc.rust-lang.org/std/path/struct.Path.html#method.ends_with)
works. It's designed to match on whole path segments and the suffix
`/node_modules` is not that, except for the root entry. This meant that
our check for if the path already ended with `node_module` always
returned `false`. Removing the leading slash fixes that.
While we're at it, we can remove the last condition where we explicitly
added the root `/node_modules` entry since the while loop prior to that
takes care of it already.
2023-05-23 06:46:14 -04:00
|
|
|
|
|
|
|
Deno.test("[node/module _nodeModulePaths] prevents duplicate /node_modules/node_modules suffix", () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const actual: string[] = (Module as any)._nodeModulePaths(
|
|
|
|
path.join(process.cwd(), "testdata", "node_modules", "foo"),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
!actual.some((dir) => /node_modules[/\\]node_modules/g.test(dir)),
|
|
|
|
"Duplicate 'node_modules/node_modules' suffix found",
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("[node/module _nodeModulePaths] prevents duplicate root /node_modules", () => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const actual: string[] = (Module as any)._nodeModulePaths(
|
|
|
|
path.join(process.cwd(), "testdata", "node_modules", "foo"),
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(
|
|
|
|
new Set(actual).size === actual.length,
|
|
|
|
"Duplicate path entries found",
|
|
|
|
);
|
|
|
|
const root = path.parse(actual[0]).root;
|
|
|
|
assert(
|
|
|
|
actual.includes(path.join(root, "node_modules")),
|
|
|
|
"Missing root 'node_modules' directory",
|
|
|
|
);
|
|
|
|
});
|
2023-07-02 14:19:30 -04:00
|
|
|
|
|
|
|
Deno.test("Built-in Node modules have `node:` prefix", () => {
|
|
|
|
let thrown = false;
|
|
|
|
try {
|
|
|
|
// @ts-ignore We want to explicitly test wrong call signature
|
|
|
|
createRequire();
|
|
|
|
} catch (e) {
|
|
|
|
thrown = true;
|
|
|
|
const stackLines = e.stack.split("\n");
|
|
|
|
// Assert that built-in node modules have `node:<mod_name>` specifiers.
|
|
|
|
assert(stackLines.some((line: string) => line.includes("(node:module:")));
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(thrown);
|
|
|
|
});
|