Skip to content

Commit

Permalink
Pull latest code from upstream (#7)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aduh95 authored May 1, 2022
1 parent 1c85b1b commit d6d706e
Show file tree
Hide file tree
Showing 50 changed files with 1,527 additions and 2,271 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ node core.
Differences from the core implementation:

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

## Docs

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

### `only` tests

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

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

```bash
node-core-test
node-core-test --test
# or use the shortcut version:
node--test
```

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

```bash
node-core-test test1.js test2.mjs custom_test_dir/
node-core-test --test test1.js test2.mjs custom_test_dir/
node--test test1.js test2.mjs custom_test_dir/
```

In this example, the test runner will execute the files `test1.js` and
Expand Down
7 changes: 7 additions & 0 deletions bin/node--test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env node

const { argv } = require('#internal/options')

argv.test = true

require('./node-core-test.js')
81 changes: 81 additions & 0 deletions bin/node-core-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env node
'use strict'

const Module = require('node:module')
const path = require('node:path')
const { pathToFileURL } = require('node:url')
const minimist = require('minimist')

const { argv } = require('#internal/options')

Object.assign(argv, minimist(process.argv.slice(2), {
boolean: ['test', 'test-only'],
default: Object.prototype.hasOwnProperty.call(argv, 'test') ? { test: argv.test } : undefined
}))

process.argv.splice(1, Infinity, ...argv._)
if (argv.test) {
require('#internal/main/test_runner')
} else {
const entryPointPath = path.resolve(argv._[0])
try {
loadMainModule(entryPointPath)
} catch (err) {
if (err.code !== 'ERR_REQUIRE_ESM') throw err

// Override process exit code logic to handle TLA:

let shouldOverwriteExitCode = true
const { exit: originalExitFunction } = process
process.exit = function exit (code) {
if (code === undefined && shouldOverwriteExitCode) {
process.exitCode = 0
}
Reflect.apply(originalExitFunction, process, arguments)
}
Object.defineProperty(process, 'exitCode', {
get: () => 13,
set (val) {
shouldOverwriteExitCode = false
delete process.exitCode
process.exitCode = val
},
configurable: true,
enumerable: true
})

// Import module

import(pathToFileURL(entryPointPath)).then(() => {
if (shouldOverwriteExitCode) process.exitCode = 0
}, (err) => {
console.error(err)
process.exit(1)
})
}
}

/**
* Loads a module as a main module, enabling the `require.main === module` pattern.
* https://github.com/nodejs/corepack/blob/5ff6e82028e58448ba5ba986854b61ecdc69885b/sources/nodeUtils.ts#L24
*/
function loadMainModule (id) {
const modulePath = Module._resolveFilename(id, null, true)

const module = new Module(modulePath, undefined)

module.filename = modulePath
module.paths = Module._nodeModulePaths(path.dirname(modulePath))

Module._cache[modulePath] = module

process.mainModule = module
module.id = '.'

try {
return module.load(modulePath)
} catch (error) {
delete Module._cache[modulePath]
throw error
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://github.com/nodejs/node/blob/b476b1b91ef8715f096f815db5a0c8722b613678/lib/test.js
// https://github.com/nodejs/node/blob/1aab13cad9c800f4121c1d35b554b78c1b17bdbd/lib/test.js

'use strict'

Expand Down
3 changes: 3 additions & 0 deletions lib/internal/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = require('assert')
5 changes: 5 additions & 0 deletions lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict'

module.exports = {
prepareMainThreadExecution () {}
}
3 changes: 3 additions & 0 deletions lib/internal/console/global.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = console
Loading

0 comments on commit d6d706e

Please sign in to comment.