Browse Source

Make writes to process.env update the real environment. Tests included.

v0.7.4-release
Ben Noordhuis 14 years ago
committed by Ryan Dahl
parent
commit
b4def4806c
  1. 80
      src/node.cc
  2. 36
      test/simple/test-process-env.js

80
src/node.cc

@ -1530,6 +1530,69 @@ static void ProcessTitleSetter(Local<String> property,
} }
static Handle<Value> EnvGetter(Local<String> property,
const AccessorInfo& info) {
String::Utf8Value key(property);
const char* val = getenv(*key);
if (val) {
HandleScope scope;
return scope.Close(String::New(val));
}
return Undefined();
}
static Handle<Value> EnvSetter(Local<String> property,
Local<Value> value,
const AccessorInfo& info) {
String::Utf8Value key(property);
String::Utf8Value val(value);
setenv(*key, *val, 1);
return value;
}
static Handle<Integer> EnvQuery(Local<String> property,
const AccessorInfo& info) {
String::Utf8Value key(property);
if (getenv(*key)) {
HandleScope scope;
return scope.Close(Integer::New(None));
}
return Handle<Integer>();
}
static Handle<Boolean> EnvDeleter(Local<String> property,
const AccessorInfo& info) {
String::Utf8Value key(property);
if (getenv(*key)) {
unsetenv(*key); // prototyped as `void unsetenv(const char*)` on some platforms
return True();
}
return False();
}
static Handle<Array> EnvEnumerator(const AccessorInfo& info) {
HandleScope scope;
int size = 0;
while (environ[size]) size++;
Local<Array> env = Array::New(size);
for (int i = 0; i < size; ++i) {
const char* var = environ[i];
const char* s = strchr(var, '=');
const int length = s ? s - var : strlen(var);
env->Set(i, String::New(var, length));
}
return scope.Close(env);
}
static void Load(int argc, char *argv[]) { static void Load(int argc, char *argv[]) {
HandleScope scope; HandleScope scope;
@ -1577,20 +1640,11 @@ static void Load(int argc, char *argv[]) {
process->Set(String::NewSymbol("argv"), arguments); process->Set(String::NewSymbol("argv"), arguments);
// create process.env // create process.env
Local<Object> env = Object::New(); Local<ObjectTemplate> envTemplate = ObjectTemplate::New();
for (i = 0; environ[i]; i++) { envTemplate->SetNamedPropertyHandler(EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, Undefined());
// skip entries without a '=' character
for (j = 0; environ[i][j] && environ[i][j] != '='; j++) { ; }
// create the v8 objects
Local<String> field = String::New(environ[i], j);
Local<String> value = Local<String>();
if (environ[i][j] == '=') {
value = String::New(environ[i]+j+1);
}
// assign them
env->Set(field, value);
}
// assign process.ENV // assign process.ENV
Local<Object> env = envTemplate->NewInstance();
process->Set(String::NewSymbol("ENV"), env); process->Set(String::NewSymbol("ENV"), env);
process->Set(String::NewSymbol("env"), env); process->Set(String::NewSymbol("env"), env);

36
test/simple/test-process-env.js

@ -0,0 +1,36 @@
// first things first, set the timezone; see tzset(3)
process.env.TZ = 'Europe/Amsterdam';
assert = require('assert');
spawn = require('child_process').spawn;
// time difference between Greenwich and Amsterdam is +2 hours in the summer
date = new Date('Fri, 10 Sep 1982 03:15:00 GMT');
assert.equal(3, date.getUTCHours());
assert.equal(5, date.getHours());
// changes in environment should be visible to child processes
if (process.argv[2] == 'you-are-the-child') {
// failed assertion results in process exiting with status code 1
assert.equal(false, 'NODE_PROCESS_ENV_DELETED' in process.env);
assert.equal(42, process.env.NODE_PROCESS_ENV);
process.exit(0);
} else {
process.env.NODE_PROCESS_ENV = 42;
assert.equal(42, process.env.NODE_PROCESS_ENV);
process.env.NODE_PROCESS_ENV_DELETED = 42;
assert.equal(true, 'NODE_PROCESS_ENV_DELETED' in process.env);
delete process.env.NODE_PROCESS_ENV_DELETED;
assert.equal(false, 'NODE_PROCESS_ENV_DELETED' in process.env);
child = spawn(process.argv[0], [process.argv[1], 'you-are-the-child']);
child.stdout.on('data', function(data) { console.log(data.toString()); });
child.stderr.on('data', function(data) { console.log(data.toString()); });
child.on('exit', function(statusCode) {
if (statusCode != 0) {
process.exit(statusCode); // failed assertion in child process
}
});
}
Loading…
Cancel
Save