Skip to content

Commit d6d706e

Browse files
authored
Pull latest code from upstream (#7)
* Pull latest code from upstream * vendor in more of `internal/error` * tests are passing (!) * revert unnecessary primordials changes * Patch for Node.js v14.x * fix test * default to --test if no other flag is detected * Update nodejs/node HEAD sha * fixup! Update nodejs/node HEAD sha * add proper support for `--test-only`, separate entry point from Node.js code * remove unnecessary hack * add support for ESM entry point and TLA (if available) * read bin path from `package.json` * separate bin in two files: `node-core-test` and `node--test` * add noop files to match more closely the node implementation
1 parent 1c85b1b commit d6d706e

50 files changed

Lines changed: 1527 additions & 2271 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ node core.
1414
Differences from the core implementation:
1515

1616
- Doesn't hide its own stack frames
17-
- Internally uses `._private` property names instead of `#private` fields,
18-
for compatibility
19-
- Uses `String` instead of `Symbol`, for compatibility
2017

2118
## Docs
2219

@@ -163,14 +160,14 @@ test('skip() method with message', t => {
163160

164161
### `only` tests
165162

166-
If Node.js is started with the `--test-only` command-line option, it is
163+
If `node-core-test` is started with the `--test-only` command-line option, it is
167164
possible to skip all top level tests except for a selected subset by passing
168165
the `only` option to the tests that should be run. When a test with the `only`
169166
option set is run, all subtests are also run. The test context's `runOnly()`
170167
method can be used to implement the same behavior at the subtest level.
171168

172169
```js
173-
// Assume Node.js is run with the --test-only command-line option.
170+
// Assume node-core-test is run with the --test-only command-line option.
174171
// The 'only' option is set, so this test is run.
175172
test('this test is run', { only: true }, async t => {
176173
// Within this test, all subtests are run by default.
@@ -237,7 +234,9 @@ test('a test that creates asynchronous activity', t => {
237234
The Node.js test runner can be invoked from the command line:
238235

239236
```bash
240-
node-core-test
237+
node-core-test --test
238+
# or use the shortcut version:
239+
node--test
241240
```
242241

243242
By default, Node.js will recursively search the current directory for
@@ -250,7 +249,8 @@ Alternatively, one or more paths can be provided as the final argument(s) to
250249
the Node.js command, as shown below.
251250

252251
```bash
253-
node-core-test test1.js test2.mjs custom_test_dir/
252+
node-core-test --test test1.js test2.mjs custom_test_dir/
253+
node--test test1.js test2.mjs custom_test_dir/
254254
```
255255

256256
In this example, the test runner will execute the files `test1.js` and

bin/node--test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env node
2+
3+
const { argv } = require('#internal/options')
4+
5+
argv.test = true
6+
7+
require('./node-core-test.js')

bin/node-core-test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
'use strict'
3+
4+
const Module = require('node:module')
5+
const path = require('node:path')
6+
const { pathToFileURL } = require('node:url')
7+
const minimist = require('minimist')
8+
9+
const { argv } = require('#internal/options')
10+
11+
Object.assign(argv, minimist(process.argv.slice(2), {
12+
boolean: ['test', 'test-only'],
13+
default: Object.prototype.hasOwnProperty.call(argv, 'test') ? { test: argv.test } : undefined
14+
}))
15+
16+
process.argv.splice(1, Infinity, ...argv._)
17+
if (argv.test) {
18+
require('#internal/main/test_runner')
19+
} else {
20+
const entryPointPath = path.resolve(argv._[0])
21+
try {
22+
loadMainModule(entryPointPath)
23+
} catch (err) {
24+
if (err.code !== 'ERR_REQUIRE_ESM') throw err
25+
26+
// Override process exit code logic to handle TLA:
27+
28+
let shouldOverwriteExitCode = true
29+
const { exit: originalExitFunction } = process
30+
process.exit = function exit (code) {
31+
if (code === undefined && shouldOverwriteExitCode) {
32+
process.exitCode = 0
33+
}
34+
Reflect.apply(originalExitFunction, process, arguments)
35+
}
36+
Object.defineProperty(process, 'exitCode', {
37+
get: () => 13,
38+
set (val) {
39+
shouldOverwriteExitCode = false
40+
delete process.exitCode
41+
process.exitCode = val
42+
},
43+
configurable: true,
44+
enumerable: true
45+
})
46+
47+
// Import module
48+
49+
import(pathToFileURL(entryPointPath)).then(() => {
50+
if (shouldOverwriteExitCode) process.exitCode = 0
51+
}, (err) => {
52+
console.error(err)
53+
process.exit(1)
54+
})
55+
}
56+
}
57+
58+
/**
59+
* Loads a module as a main module, enabling the `require.main === module` pattern.
60+
* https://github.com/nodejs/corepack/blob/5ff6e82028e58448ba5ba986854b61ecdc69885b/sources/nodeUtils.ts#L24
61+
*/
62+
function loadMainModule (id) {
63+
const modulePath = Module._resolveFilename(id, null, true)
64+
65+
const module = new Module(modulePath, undefined)
66+
67+
module.filename = modulePath
68+
module.paths = Module._nodeModulePaths(path.dirname(modulePath))
69+
70+
Module._cache[modulePath] = module
71+
72+
process.mainModule = module
73+
module.id = '.'
74+
75+
try {
76+
return module.load(modulePath)
77+
} catch (error) {
78+
delete Module._cache[modulePath]
79+
throw error
80+
}
81+
}

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/nodejs/node/blob/b476b1b91ef8715f096f815db5a0c8722b613678/lib/test.js
1+
// https://github.com/nodejs/node/blob/1aab13cad9c800f4121c1d35b554b78c1b17bdbd/lib/test.js
22

33
'use strict'
44

lib/internal/assert.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = require('assert')
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
module.exports = {
4+
prepareMainThreadExecution () {}
5+
}

lib/internal/console/global.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = console

0 commit comments

Comments
 (0)