1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23 24const common = require('../common'); 25const assert = require('assert'); 26 27// Changes in environment should be visible to child processes 28if (process.argv[2] === 'you-are-the-child') { 29 assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, false); 30 assert.strictEqual(process.env.NODE_PROCESS_ENV, '42'); 31 assert.strictEqual(process.env.hasOwnProperty, 'asdf'); 32 const has = Object.hasOwn(process.env, 'hasOwnProperty'); 33 assert.strictEqual(has, true); 34 process.exit(0); 35} 36 37{ 38 const spawn = require('child_process').spawn; 39 40 assert.strictEqual(Object.prototype.hasOwnProperty, 41 process.env.hasOwnProperty); 42 const has = Object.hasOwn(process.env, 'hasOwnProperty'); 43 assert.strictEqual(has, false); 44 45 process.env.hasOwnProperty = 'asdf'; 46 47 process.env.NODE_PROCESS_ENV = 42; 48 assert.strictEqual(process.env.NODE_PROCESS_ENV, '42'); 49 50 process.env.NODE_PROCESS_ENV_DELETED = 42; 51 assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, true); 52 53 delete process.env.NODE_PROCESS_ENV_DELETED; 54 assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, false); 55 56 const child = spawn(process.argv[0], [process.argv[1], 'you-are-the-child']); 57 child.stdout.on('data', function(data) { console.log(data.toString()); }); 58 child.stderr.on('data', function(data) { console.log(data.toString()); }); 59 child.on('exit', function(statusCode) { 60 if (statusCode !== 0) { 61 process.exit(statusCode); // Failed assertion in child process 62 } 63 }); 64} 65 66 67// Delete should return true except for non-configurable properties 68// https://github.com/nodejs/node/issues/7960 69delete process.env.NON_EXISTING_VARIABLE; 70assert(delete process.env.NON_EXISTING_VARIABLE); 71 72// For the moment we are not going to support setting the timezone via the 73// environment variables. The problem is that various V8 platform backends 74// deal with timezone in different ways. The Windows platform backend caches 75// the timezone value while the Linux one hits libc for every query. 76// 77// https://github.com/joyent/node/blob/08782931205bc4f6d28102ebc29fd806e8ccdf1f/deps/v8/src/platform-linux.cc#L339-345 78// https://github.com/joyent/node/blob/08782931205bc4f6d28102ebc29fd806e8ccdf1f/deps/v8/src/platform-win32.cc#L590-596 79// 80// // set the timezone; see tzset(3) 81// process.env.TZ = 'Europe/Amsterdam'; 82// 83// // time difference between Greenwich and Amsterdam is +2 hours in the summer 84// date = new Date('Fri, 10 Sep 1982 03:15:00 GMT'); 85// assert.strictEqual(3, date.getUTCHours()); 86// assert.strictEqual(5, date.getHours()); 87 88// Environment variables should be case-insensitive on Windows, and 89// case-sensitive on other platforms. 90process.env.TEST = 'test'; 91assert.strictEqual(process.env.TEST, 'test'); 92 93// Check both mixed case and lower case, to avoid any regressions that might 94// simply convert input to lower case. 95if (common.isWindows) { 96 assert.strictEqual(process.env.test, 'test'); 97 assert.strictEqual(process.env.teST, 'test'); 98} else { 99 assert.strictEqual(process.env.test, undefined); 100 assert.strictEqual(process.env.teST, undefined); 101} 102 103{ 104 const keys = Object.keys(process.env); 105 assert.ok(keys.length > 0); 106} 107 108// https://github.com/nodejs/node/issues/45380 109{ 110 const env = structuredClone(process.env); 111 // deepEqual(), not deepStrictEqual(), because of different prototypes. 112 // eslint-disable-next-line no-restricted-properties 113 assert.deepEqual(env, process.env); 114} 115 116// Setting environment variables on Windows with empty names should not cause 117// an assertion failure. 118// https://github.com/nodejs/node/issues/32920 119{ 120 process.env[''] = ''; 121 assert.strictEqual(process.env[''], undefined); 122} 123