11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ciconst child_process = require('child_process');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { Worker, workerData } = require('worker_threads');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Test for https://github.com/nodejs/node/issues/24947.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ciif (!workerData && process.argv[2] !== 'child') {
101cb0ef41Sopenharmony_ci  process.env.SET_IN_PARENT = 'set';
111cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_PARENT, 'set');
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  new Worker(__filename, { workerData: 'runInWorker' })
141cb0ef41Sopenharmony_ci    .on('exit', common.mustCall(() => {
151cb0ef41Sopenharmony_ci      // Env vars from the child thread are not set globally.
161cb0ef41Sopenharmony_ci      assert.strictEqual(process.env.SET_IN_WORKER, undefined);
171cb0ef41Sopenharmony_ci    }));
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci  process.env.SET_IN_PARENT_AFTER_CREATION = 'set';
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci  new Worker(__filename, {
221cb0ef41Sopenharmony_ci    workerData: 'resetEnv',
231cb0ef41Sopenharmony_ci    env: { 'MANUALLY_SET': true }
241cb0ef41Sopenharmony_ci  });
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  assert.throws(() => {
271cb0ef41Sopenharmony_ci    new Worker(__filename, { env: 42 });
281cb0ef41Sopenharmony_ci  }, {
291cb0ef41Sopenharmony_ci    name: 'TypeError',
301cb0ef41Sopenharmony_ci    code: 'ERR_INVALID_ARG_TYPE',
311cb0ef41Sopenharmony_ci    message: 'The "options.env" property must be of type object or ' +
321cb0ef41Sopenharmony_ci      'one of undefined, null, or worker_threads.SHARE_ENV. Received type ' +
331cb0ef41Sopenharmony_ci      'number (42)'
341cb0ef41Sopenharmony_ci  });
351cb0ef41Sopenharmony_ci} else if (workerData === 'runInWorker') {
361cb0ef41Sopenharmony_ci  // Env vars from the parent thread are inherited.
371cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_PARENT, 'set');
381cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_PARENT_AFTER_CREATION, undefined);
391cb0ef41Sopenharmony_ci  process.env.SET_IN_WORKER = 'set';
401cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_WORKER, 'set');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  assert.throws(
431cb0ef41Sopenharmony_ci    () => {
441cb0ef41Sopenharmony_ci      Object.defineProperty(process.env, 'DEFINED_IN_WORKER', {
451cb0ef41Sopenharmony_ci        value: 42
461cb0ef41Sopenharmony_ci      });
471cb0ef41Sopenharmony_ci    },
481cb0ef41Sopenharmony_ci    {
491cb0ef41Sopenharmony_ci      code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY',
501cb0ef41Sopenharmony_ci      name: 'TypeError',
511cb0ef41Sopenharmony_ci      message: '\'process.env\' only accepts a configurable, ' +
521cb0ef41Sopenharmony_ci          'writable, and enumerable data descriptor'
531cb0ef41Sopenharmony_ci    }
541cb0ef41Sopenharmony_ci  );
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  const { stderr } =
581cb0ef41Sopenharmony_ci    child_process.spawnSync(process.execPath, [__filename, 'child']);
591cb0ef41Sopenharmony_ci  assert.strictEqual(stderr.toString(), '', stderr.toString());
601cb0ef41Sopenharmony_ci} else if (workerData === 'resetEnv') {
611cb0ef41Sopenharmony_ci  assert.deepStrictEqual(Object.keys(process.env), ['MANUALLY_SET']);
621cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.MANUALLY_SET, 'true');
631cb0ef41Sopenharmony_ci} else {
641cb0ef41Sopenharmony_ci  // Child processes inherit the parent's env, even from Workers.
651cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_PARENT, 'set');
661cb0ef41Sopenharmony_ci  assert.strictEqual(process.env.SET_IN_WORKER, 'set');
671cb0ef41Sopenharmony_ci}
68