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 hasOwnProperty = Object.prototype.hasOwnProperty; 33 const has = hasOwnProperty.call(process.env, 'hasOwnProperty'); 34 assert.strictEqual(has, true); 35 process.exit(0); 36} 37 38{ 39 const spawn = require('child_process').spawn; 40 41 assert.strictEqual(Object.prototype.hasOwnProperty, 42 process.env.hasOwnProperty); 43 const has = process.env.hasOwnProperty('hasOwnProperty'); 44 assert.strictEqual(has, false); 45 46 process.env.hasOwnProperty = 'asdf'; 47 48 process.env.NODE_PROCESS_ENV = 42; 49 assert.strictEqual(process.env.NODE_PROCESS_ENV, '42'); 50 51 process.env.NODE_PROCESS_ENV_DELETED = 42; 52 assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, true); 53 54 delete process.env.NODE_PROCESS_ENV_DELETED; 55 assert.strictEqual('NODE_PROCESS_ENV_DELETED' in process.env, false); 56 57 const child = spawn(process.argv[0], [process.argv[1], 'you-are-the-child']); 58 child.stdout.on('data', function(data) { console.log(data.toString()); }); 59 child.stderr.on('data', function(data) { console.log(data.toString()); }); 60 child.on('exit', function(statusCode) { 61 if (statusCode !== 0) { 62 process.exit(statusCode); // Failed assertion in child process 63 } 64 }); 65} 66 67 68// Delete should return true except for non-configurable properties 69// https://github.com/nodejs/node/issues/7960 70delete process.env.NON_EXISTING_VARIABLE; 71assert(delete process.env.NON_EXISTING_VARIABLE); 72 73// For the moment we are not going to support setting the timezone via the 74// environment variables. The problem is that various V8 platform backends 75// deal with timezone in different ways. The Windows platform backend caches 76// the timezone value while the Linux one hits libc for every query. 77// 78// https://github.com/joyent/node/blob/08782931205bc4f6d28102ebc29fd806e8ccdf1f/deps/v8/src/platform-linux.cc#L339-345 79// https://github.com/joyent/node/blob/08782931205bc4f6d28102ebc29fd806e8ccdf1f/deps/v8/src/platform-win32.cc#L590-596 80// 81// // set the timezone; see tzset(3) 82// process.env.TZ = 'Europe/Amsterdam'; 83// 84// // time difference between Greenwich and Amsterdam is +2 hours in the summer 85// date = new Date('Fri, 10 Sep 1982 03:15:00 GMT'); 86// assert.strictEqual(3, date.getUTCHours()); 87// assert.strictEqual(5, date.getHours()); 88 89// Environment variables should be case-insensitive on Windows, and 90// case-sensitive on other platforms. 91process.env.TEST = 'test'; 92assert.strictEqual(process.env.TEST, 'test'); 93 94// Check both mixed case and lower case, to avoid any regressions that might 95// simply convert input to lower case. 96if (common.isWindows) { 97 assert.strictEqual(process.env.test, 'test'); 98 assert.strictEqual(process.env.teST, 'test'); 99} else { 100 assert.strictEqual(process.env.test, undefined); 101 assert.strictEqual(process.env.teST, undefined); 102} 103 104{ 105 const keys = Object.keys(process.env); 106 assert.ok(keys.length > 0); 107} 108 109// Setting environment variables on Windows with empty names should not cause 110// an assertion failure. 111// https://github.com/nodejs/node/issues/32920 112{ 113 process.env[''] = ''; 114 assert.strictEqual(process.env[''], undefined); 115} 116