11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciconst common = require('../common'); 251cb0ef41Sopenharmony_ciconst assert = require('assert'); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci// Changes in environment should be visible to child processes 281cb0ef41Sopenharmony_ciif (process.argv[2] === 'you-are-the-child') { 291cb0ef41Sopenharmony_ci assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, false); 301cb0ef41Sopenharmony_ci assert.strictEqual(process.env.NODE_PROCESS_ENV, '42'); 311cb0ef41Sopenharmony_ci assert.strictEqual(process.env.hasOwnProperty, 'asdf'); 321cb0ef41Sopenharmony_ci const has = Object.hasOwn(process.env, 'hasOwnProperty'); 331cb0ef41Sopenharmony_ci assert.strictEqual(has, true); 341cb0ef41Sopenharmony_ci process.exit(0); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci{ 381cb0ef41Sopenharmony_ci const spawn = require('child_process').spawn; 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci assert.strictEqual(Object.prototype.hasOwnProperty, 411cb0ef41Sopenharmony_ci process.env.hasOwnProperty); 421cb0ef41Sopenharmony_ci const has = Object.hasOwn(process.env, 'hasOwnProperty'); 431cb0ef41Sopenharmony_ci assert.strictEqual(has, false); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci process.env.hasOwnProperty = 'asdf'; 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci process.env.NODE_PROCESS_ENV = 42; 481cb0ef41Sopenharmony_ci assert.strictEqual(process.env.NODE_PROCESS_ENV, '42'); 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_ci process.env.NODE_PROCESS_ENV_DELETED = 42; 511cb0ef41Sopenharmony_ci assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, true); 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_ci delete process.env.NODE_PROCESS_ENV_DELETED; 541cb0ef41Sopenharmony_ci assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, false); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_ci const child = spawn(process.argv[0], [process.argv[1], 'you-are-the-child']); 571cb0ef41Sopenharmony_ci child.stdout.on('data', function(data) { console.log(data.toString()); }); 581cb0ef41Sopenharmony_ci child.stderr.on('data', function(data) { console.log(data.toString()); }); 591cb0ef41Sopenharmony_ci child.on('exit', function(statusCode) { 601cb0ef41Sopenharmony_ci if (statusCode !== 0) { 611cb0ef41Sopenharmony_ci process.exit(statusCode); // Failed assertion in child process 621cb0ef41Sopenharmony_ci } 631cb0ef41Sopenharmony_ci }); 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ci// Delete should return true except for non-configurable properties 681cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/7960 691cb0ef41Sopenharmony_cidelete process.env.NON_EXISTING_VARIABLE; 701cb0ef41Sopenharmony_ciassert(delete process.env.NON_EXISTING_VARIABLE); 711cb0ef41Sopenharmony_ci 721cb0ef41Sopenharmony_ci// For the moment we are not going to support setting the timezone via the 731cb0ef41Sopenharmony_ci// environment variables. The problem is that various V8 platform backends 741cb0ef41Sopenharmony_ci// deal with timezone in different ways. The Windows platform backend caches 751cb0ef41Sopenharmony_ci// the timezone value while the Linux one hits libc for every query. 761cb0ef41Sopenharmony_ci// 771cb0ef41Sopenharmony_ci// https://github.com/joyent/node/blob/08782931205bc4f6d28102ebc29fd806e8ccdf1f/deps/v8/src/platform-linux.cc#L339-345 781cb0ef41Sopenharmony_ci// https://github.com/joyent/node/blob/08782931205bc4f6d28102ebc29fd806e8ccdf1f/deps/v8/src/platform-win32.cc#L590-596 791cb0ef41Sopenharmony_ci// 801cb0ef41Sopenharmony_ci// // set the timezone; see tzset(3) 811cb0ef41Sopenharmony_ci// process.env.TZ = 'Europe/Amsterdam'; 821cb0ef41Sopenharmony_ci// 831cb0ef41Sopenharmony_ci// // time difference between Greenwich and Amsterdam is +2 hours in the summer 841cb0ef41Sopenharmony_ci// date = new Date('Fri, 10 Sep 1982 03:15:00 GMT'); 851cb0ef41Sopenharmony_ci// assert.strictEqual(3, date.getUTCHours()); 861cb0ef41Sopenharmony_ci// assert.strictEqual(5, date.getHours()); 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci// Environment variables should be case-insensitive on Windows, and 891cb0ef41Sopenharmony_ci// case-sensitive on other platforms. 901cb0ef41Sopenharmony_ciprocess.env.TEST = 'test'; 911cb0ef41Sopenharmony_ciassert.strictEqual(process.env.TEST, 'test'); 921cb0ef41Sopenharmony_ci 931cb0ef41Sopenharmony_ci// Check both mixed case and lower case, to avoid any regressions that might 941cb0ef41Sopenharmony_ci// simply convert input to lower case. 951cb0ef41Sopenharmony_ciif (common.isWindows) { 961cb0ef41Sopenharmony_ci assert.strictEqual(process.env.test, 'test'); 971cb0ef41Sopenharmony_ci assert.strictEqual(process.env.teST, 'test'); 981cb0ef41Sopenharmony_ci} else { 991cb0ef41Sopenharmony_ci assert.strictEqual(process.env.test, undefined); 1001cb0ef41Sopenharmony_ci assert.strictEqual(process.env.teST, undefined); 1011cb0ef41Sopenharmony_ci} 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci{ 1041cb0ef41Sopenharmony_ci const keys = Object.keys(process.env); 1051cb0ef41Sopenharmony_ci assert.ok(keys.length > 0); 1061cb0ef41Sopenharmony_ci} 1071cb0ef41Sopenharmony_ci 1081cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/45380 1091cb0ef41Sopenharmony_ci{ 1101cb0ef41Sopenharmony_ci const env = structuredClone(process.env); 1111cb0ef41Sopenharmony_ci // deepEqual(), not deepStrictEqual(), because of different prototypes. 1121cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-properties 1131cb0ef41Sopenharmony_ci assert.deepEqual(env, process.env); 1141cb0ef41Sopenharmony_ci} 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci// Setting environment variables on Windows with empty names should not cause 1171cb0ef41Sopenharmony_ci// an assertion failure. 1181cb0ef41Sopenharmony_ci// https://github.com/nodejs/node/issues/32920 1191cb0ef41Sopenharmony_ci{ 1201cb0ef41Sopenharmony_ci process.env[''] = ''; 1211cb0ef41Sopenharmony_ci assert.strictEqual(process.env[''], undefined); 1221cb0ef41Sopenharmony_ci} 123