0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-dotenv-node-options.js
Yagiz Nizipli 4dbb017301
test: improve UV_THREADPOOL_SIZE tests on .env
PR-URL: https://github.com/nodejs/node/pull/49213
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
2023-11-03 17:25:54 +00:00

66 lines
2.3 KiB
JavaScript

'use strict';
const common = require('../common');
const assert = require('node:assert');
const { describe, it } = require('node:test');
if (process.config.variables.node_without_node_options) {
common.skip('missing NODE_OPTIONS support');
}
const relativePath = '../fixtures/dotenv/node-options.env';
describe('.env supports NODE_OPTIONS', () => {
it('should have access to permission scope', async () => {
const code = `
require('assert')(process.permission.has('fs.read'));
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${relativePath}`, '--eval', code ],
{ cwd: __dirname },
);
// NODE_NO_WARNINGS is set, so `stderr` should not contain
// "ExperimentalWarning: Permission is an experimental feature" message
// and thus be empty
assert.strictEqual(child.stdout, '');
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
it('validate only read permissions are enabled', async () => {
const code = `
require('fs').writeFileSync(require('path').join(__dirname, 'should-not-write.txt'), 'hello', 'utf-8')
`.trim();
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${relativePath}`, '--eval', code ],
{ cwd: __dirname },
);
assert.match(child.stderr, /Error: Access to this API has been restricted/);
assert.match(child.stderr, /code: 'ERR_ACCESS_DENIED'/);
assert.match(child.stderr, /permission: 'FileSystemWrite'/);
assert.strictEqual(child.code, 1);
});
it('TZ environment variable', { skip: !common.hasIntl || process.config.variables.icu_small }, async () => {
const code = `
require('assert')(new Date().toString().includes('GMT-1000'))
`.trim();
// Some CI environments set TZ. Since an env file doesn't override existing
// environment variables, we need to delete it and then pass the env object
// as the environment to spawnPromisified.
const env = { ...process.env };
delete env.TZ;
const child = await common.spawnPromisified(
process.execPath,
[ `--env-file=${relativePath}`, '--eval', code ],
{ cwd: __dirname, env },
);
assert.strictEqual(child.stderr, '');
assert.strictEqual(child.code, 0);
});
});