forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: ensure successful import returns the same result
PR-URL: nodejs#46662 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
- Loading branch information
Showing
7 changed files
with
255 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Tests the impact on eager operations required for policies affecting | ||
// general startup, does not test lazy operations | ||
'use strict'; | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
const common = require('../common.js'); | ||
|
||
const tmpdir = require('../../test/common/tmpdir.js'); | ||
const { pathToFileURL } = require('node:url'); | ||
|
||
const benchmarkDirectory = pathToFileURL(path.resolve(tmpdir.path, 'benchmark-import')); | ||
|
||
const configs = { | ||
n: [1e3], | ||
specifier: [ | ||
'data:text/javascript,{i}', | ||
'./relative-existing.js', | ||
'./relative-nonexistent.js', | ||
'node:prefixed-nonexistent', | ||
'node:os', | ||
], | ||
}; | ||
|
||
const options = { | ||
flags: ['--expose-internals'], | ||
}; | ||
|
||
const bench = common.createBenchmark(main, configs, options); | ||
|
||
async function main(conf) { | ||
tmpdir.refresh(); | ||
|
||
fs.mkdirSync(benchmarkDirectory, { recursive: true }); | ||
fs.writeFileSync(new URL('./relative-existing.js', benchmarkDirectory), '\n'); | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < conf.n; i++) { | ||
try { | ||
await import(new URL(conf.specifier.replace('{i}', i), benchmarkDirectory)); | ||
} catch { /* empty */ } | ||
} | ||
|
||
bench.end(conf.n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const tmpdir = require('../common/tmpdir'); | ||
|
||
const assert = require('node:assert'); | ||
const fs = require('node:fs/promises'); | ||
const { pathToFileURL } = require('node:url'); | ||
|
||
tmpdir.refresh(); | ||
const tmpDir = pathToFileURL(tmpdir.path); | ||
|
||
const target = new URL(`./${Math.random()}.mjs`, tmpDir); | ||
|
||
(async () => { | ||
|
||
await assert.rejects(import(target), { code: 'ERR_MODULE_NOT_FOUND' }); | ||
|
||
await fs.writeFile(target, 'export default "actual target"\n'); | ||
|
||
const moduleRecord = await import(target); | ||
|
||
await fs.rm(target); | ||
|
||
assert.strictEqual(await import(target), moduleRecord); | ||
})().then(common.mustCall()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { spawnPromisified } from '../common/index.mjs'; | ||
import tmpdir from '../common/tmpdir.js'; | ||
|
||
import assert from 'node:assert'; | ||
import fs from 'node:fs/promises'; | ||
import { execPath } from 'node:process'; | ||
import { pathToFileURL } from 'node:url'; | ||
|
||
tmpdir.refresh(); | ||
const tmpDir = pathToFileURL(tmpdir.path); | ||
|
||
const target = new URL(`./${Math.random()}.mjs`, tmpDir); | ||
|
||
await assert.rejects(import(target), { code: 'ERR_MODULE_NOT_FOUND' }); | ||
|
||
await fs.writeFile(target, 'export default "actual target"\n'); | ||
|
||
const moduleRecord = await import(target); | ||
|
||
await fs.rm(target); | ||
|
||
assert.strictEqual(await import(target), moduleRecord); | ||
|
||
// Add the file back, it should be deleted by the subprocess. | ||
await fs.writeFile(target, 'export default "actual target"\n'); | ||
|
||
assert.deepStrictEqual( | ||
await spawnPromisified(execPath, [ | ||
'--input-type=module', | ||
'--eval', | ||
[`import * as d from${JSON.stringify(target)};`, | ||
'import{rm}from"node:fs/promises";', | ||
`await rm(new URL(${JSON.stringify(target)}));`, | ||
'import{strictEqual}from"node:assert";', | ||
`strictEqual(JSON.stringify(await import(${JSON.stringify(target)})),JSON.stringify(d));`].join(''), | ||
]), | ||
{ | ||
code: 0, | ||
signal: null, | ||
stderr: '', | ||
stdout: '', | ||
}); |
Oops, something went wrong.