@@ -10,9 +10,9 @@ const path = require('path');
1010const { runFile } = require ( 'node:bench' ) ;
1111
1212const fixture = fixtures . path ( 'bench-runner/run-file.cjs' ) ;
13+ const relativeFixture = path . relative ( process . cwd ( ) , fixture ) ;
1314
1415assert . throws ( ( ) => runFile ( null ) , { code : 'ERR_INVALID_ARG_TYPE' } ) ;
15- assert . throws ( ( ) => runFile ( 'relative.cjs' ) , { code : 'ERR_INVALID_ARG_VALUE' } ) ;
1616assert . throws ( ( ) => runFile ( fixture , null ) , { code : 'ERR_INVALID_ARG_TYPE' } ) ;
1717assert . throws ( ( ) => runFile ( fixture , { execArgv : null } ) , {
1818 code : 'ERR_INVALID_ARG_TYPE' ,
@@ -50,6 +50,9 @@ assert.throws(() => runFile(fixture, {
5050assert . throws ( ( ) => runFile ( `${ fixture } \0` ) , {
5151 code : 'ERR_INVALID_ARG_VALUE' ,
5252} ) ;
53+ assert . throws ( ( ) => runFile ( Buffer . from ( `${ fixture } \0` ) ) , {
54+ code : 'ERR_INVALID_ARG_VALUE' ,
55+ } ) ;
5356assert . throws ( ( ) => runFile ( fixture , { env : null } ) , {
5457 code : 'ERR_INVALID_ARG_TYPE' ,
5558} ) ;
@@ -79,7 +82,7 @@ async function testRunFile() {
7982 NODE_CHANNEL_FD : '999' ,
8083 NODE_CHANNEL_SERIALIZATION_MODE : 'json' ,
8184 } ;
82- const stream = runFile ( fixture , { env, execArgv } ) ;
85+ const stream = runFile ( relativeFixture , { env, execArgv } ) ;
8386 execArgv . length = 0 ;
8487 env . NODE_BENCH_RUN_FILE = 'mutated' ;
8588 const records = await stream . toArray ( ) ;
@@ -108,8 +111,11 @@ async function testRunFile() {
108111
109112async function testConcurrentCalls ( ) {
110113 const [ cjsRecords , esmRecords ] = await Promise . all ( [
111- runFile ( fixtures . path ( 'bench-runner/a.cjs' ) ) . toArray ( ) ,
112- runFile ( fixtures . path ( 'bench-runner/b.mjs' ) ) . toArray ( ) ,
114+ runFile ( fixtures . fileURL ( 'bench-runner/a.cjs' ) ) . toArray ( ) ,
115+ runFile ( Buffer . from ( path . relative (
116+ process . cwd ( ) ,
117+ fixtures . path ( 'bench-runner/b.mjs' ) ,
118+ ) ) ) . toArray ( ) ,
113119 ] ) ;
114120 const cjsResult = cjsRecords . find (
115121 ( { type } ) => type === 'bench:complete' ) . data ;
@@ -119,6 +125,14 @@ async function testConcurrentCalls() {
119125 assert . strictEqual ( esmResult . name , 'beta' ) ;
120126 assert . notStrictEqual ( cjsResult . params . pid , esmResult . params . pid ) ;
121127 assert . notStrictEqual ( cjsResult . runId , esmResult . runId ) ;
128+ assert . strictEqual (
129+ cjsRecords . at ( - 1 ) . data . file ,
130+ fixtures . path ( 'bench-runner/a.cjs' ) ,
131+ ) ;
132+ assert . strictEqual (
133+ esmRecords . at ( - 1 ) . data . file ,
134+ fixtures . path ( 'bench-runner/b.mjs' ) ,
135+ ) ;
122136}
123137
124138async function testLoadFailure ( ) {
@@ -280,6 +294,74 @@ function testEvalParent() {
280294 assert . strictEqual ( result . stdout , 'true\n' ) ;
281295}
282296
297+ function testPermissions ( ) {
298+ const fsReadScript = `
299+ const assert = require('assert');
300+ const { runFile } = require('node:bench');
301+ const { pathToFileURL } = require('url');
302+ const target = ${ JSON . stringify ( fixture ) } ;
303+ assert.strictEqual(process.permission.has('child'), true);
304+ assert.strictEqual(process.permission.has('fs.read', target), false);
305+ Promise.all([
306+ target,
307+ Buffer.from(target),
308+ pathToFileURL(target),
309+ ].map(async (input) => {
310+ const records = await runFile(input).toArray();
311+ const diagnostic = records.find(({ type, data }) =>
312+ type === 'bench:diagnostic' &&
313+ data.error?.code === 'ERR_ACCESS_DENIED');
314+ assert.strictEqual(diagnostic.data.error.permission, 'FileSystemRead');
315+ assert.strictEqual(diagnostic.data.error.resource, target);
316+ assert.strictEqual(records.at(-1).type, 'bench:summary');
317+ assert.strictEqual(records.at(-1).data.success, false);
318+ })).catch((error) => {
319+ console.error(error);
320+ process.exitCode = 1;
321+ });
322+ ` ;
323+ const result = spawnSync ( process . execPath , [
324+ '--no-warnings' ,
325+ '--permission' ,
326+ '--allow-child-process' ,
327+ '-e' ,
328+ fsReadScript ,
329+ ] , { encoding : 'utf8' } ) ;
330+ assert . strictEqual ( result . status , 0 , result . stderr ) ;
331+
332+ const childProcessScript = `
333+ const assert = require('assert');
334+ const { runFile } = require('node:bench');
335+ const target = ${ JSON . stringify ( fixture ) } ;
336+ assert.strictEqual(process.permission.has('fs.read', target), true);
337+ assert.strictEqual(process.permission.has('child'), false);
338+ runFile(target).toArray().then((records) => {
339+ const diagnostic = records.find(({ type, data }) =>
340+ type === 'bench:diagnostic' &&
341+ data.error?.code === 'ERR_ACCESS_DENIED');
342+ assert.strictEqual(diagnostic.data.error.permission, 'ChildProcess');
343+ assert.strictEqual(diagnostic.data.error.resource, process.execPath);
344+ assert.strictEqual(records.at(-1).type, 'bench:summary');
345+ assert.strictEqual(records.at(-1).data.success, false);
346+ }).catch((error) => {
347+ console.error(error);
348+ process.exitCode = 1;
349+ });
350+ ` ;
351+ const childProcessResult = spawnSync ( process . execPath , [
352+ '--no-warnings' ,
353+ '--permission' ,
354+ `--allow-fs-read=${ fixture } ` ,
355+ '-e' ,
356+ childProcessScript ,
357+ ] , { encoding : 'utf8' } ) ;
358+ assert . strictEqual (
359+ childProcessResult . status ,
360+ 0 ,
361+ childProcessResult . stderr ,
362+ ) ;
363+ }
364+
283365async function testPreAborted ( ) {
284366 const records = await runFile ( fixture , {
285367 signal : AbortSignal . abort ( new Error ( 'already cancelled' ) ) ,
@@ -315,4 +397,5 @@ async function testDestroy() {
315397 await testPreAborted ( ) ;
316398 await testDestroy ( ) ;
317399 testEvalParent ( ) ;
400+ testPermissions ( ) ;
318401} ) ( ) . then ( common . mustCall ( ) ) ;
0 commit comments