Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add coverage for custom loader hooks with permission model #46977

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions test/es-module/test-esm-loader-hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,56 @@ describe('Loader hooks', { concurrency: true }, () => {
assert.strictEqual(signal, null);
});
});

it('should work without worker permission', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-permission',
'--allow-fs-read',
'*',
'--experimental-loader',
fixtures.fileURL('empty.js'),
fixtures.path('es-modules/esm-top-level-await.mjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^1\r?\n2\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should allow loader hooks to spawn workers when allowed by the CLI flags', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-permission',
'--allow-worker',
'--allow-fs-read',
'*',
'--experimental-loader',
`data:text/javascript,import{Worker}from"worker_threads";new Worker(${encodeURIComponent(JSON.stringify(fixtures.path('empty.js')))}).unref()`,
fixtures.path('es-modules/esm-top-level-await.mjs'),
]);

assert.strictEqual(stderr, '');
assert.match(stdout, /^1\r?\n2\r?\n$/);
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});

it('should not allow loader hooks to spawn workers if restricted by the CLI flags', async () => {
const { code, signal, stdout, stderr } = await spawnPromisified(execPath, [
'--no-warnings',
'--experimental-permission',
'--allow-fs-read',
'*',
'--experimental-loader',
`data:text/javascript,import{Worker}from"worker_threads";new Worker(${encodeURIComponent(JSON.stringify(fixtures.path('empty.js')))}).unref()`,
fixtures.path('es-modules/esm-top-level-await.mjs'),
]);

assert.match(stderr, /code: 'ERR_ACCESS_DENIED'/);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
});
});