Skip to content
Permalink
Browse files

process, fix: make requried envs' kv fallback to the ones of parent p…

…rocess. (#516)
  • Loading branch information...
richardo2016 authored and xicilion committed Sep 2, 2019
1 parent efa57be commit 6c8a8d093103b6bae3ed01789c41198291783e1c
@@ -10,6 +10,7 @@
#include "config.h"
#include "object.h"
#include "ifs/process.h"
#include "ifs/util.h"
#include "SubProcess.h"
#include <spawn.h>
#include <vector>
@@ -19,6 +20,11 @@

namespace fibjs {

static const char* DEFT_ENV_KEYS[] = {
"HOME",
"TMPDIR"
};

static exlib::spinlock s_lock;
static std::map<pid_t, obj_ptr<SubProcess>> s_ids;
void init_signal();
@@ -151,20 +157,35 @@ result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8
std::vector<exlib::string> envstr;
std::vector<char*> envp;

v8::Local<v8::Object> envs;
v8::Local<v8::Object> cur_envs;
hr = process_base::get_env(cur_envs);
if (hr < 0)
return hr;

hr = GetConfigValue(isolate->m_isolate, opts, "env", envs, true);
v8::Local<v8::Value> opt_envs_v;
hr = GetConfigValue(isolate->m_isolate, opts, "env", opt_envs_v, true);
if (hr == CALL_E_PARAMNOTOPTIONAL)
hr = process_base::get_env(envs);
if (hr < 0)
util_base::clone(cur_envs, opt_envs_v);
else if (hr < 0)
return hr;

v8::Local<v8::Object> opt_envs = opt_envs_v->ToObject();
v8::Local<v8::Value> dflt_k;
bool has_k;
for (int32_t i = 0; i < (int32_t)ARRAYSIZE(DEFT_ENV_KEYS); i++) {
util_base::has(opt_envs, DEFT_ENV_KEYS[i], has_k);
if (!has_k) {
dflt_k = isolate->NewString(DEFT_ENV_KEYS[i]);
opt_envs->Set(dflt_k, cur_envs->Get(dflt_k));
}
}

v8::Local<v8::Array> keys = envs->GetPropertyNames();
v8::Local<v8::Array> keys = opt_envs->GetPropertyNames();
len = (int32_t)keys->Length();

for (i = 0; i < len; i++) {
v8::Local<v8::Value> k = keys->Get(i);
v8::Local<v8::Value> v = envs->Get(k);
v8::Local<v8::Value> v = opt_envs->Get(k);
exlib::string ks, vs;

hr = GetArgumentValue(k, ks);
@@ -9,6 +9,7 @@

#include "object.h"
#include "ifs/process.h"
#include "ifs/util.h"
#include "SubProcess.h"
#include <vector>
#include <psapi.h>
@@ -17,6 +18,20 @@

namespace fibjs {

static const char* DEFT_ENV_KEYS[] = {
"SystemRoot",
"TEMP",
"TMP",
// for registry :start
"CommonProgramFiles",
"CommonProgramFiles(x86)",
"CommonProgramW6432",
"ProgramFiles",
"ProgramFiles(x86)",
"ProgramW6432"
// for registry :end
};

void init_signal();

void init_process()
@@ -134,20 +149,35 @@ result_t SubProcess::create(exlib::string command, v8::Local<v8::Array> args, v8

exlib::wstring envstr;

v8::Local<v8::Object> envs;
v8::Local<v8::Object> cur_envs;
hr = process_base::get_env(cur_envs);
if (hr < 0)
return hr;

hr = GetConfigValue(isolate->m_isolate, opts, "env", envs, true);
v8::Local<v8::Value> opt_envs_v;
hr = GetConfigValue(isolate->m_isolate, opts, "env", opt_envs_v, true);
if (hr == CALL_E_PARAMNOTOPTIONAL)
hr = process_base::get_env(envs);
if (hr < 0)
util_base::clone(cur_envs, opt_envs_v);
else if (hr < 0)
return hr;

v8::Local<v8::Object> opt_envs = opt_envs_v->ToObject();
v8::Local<v8::Value> dflt_k;
bool has_k;
for (int32_t i = 0; i < (int32_t)ARRAYSIZE(DEFT_ENV_KEYS); i++) {
util_base::has(opt_envs, DEFT_ENV_KEYS[i], has_k);
if (!has_k) {
dflt_k = isolate->NewString(DEFT_ENV_KEYS[i]);
opt_envs->Set(dflt_k, cur_envs->Get(dflt_k));
}
}

v8::Local<v8::Array> keys = envs->GetPropertyNames();
v8::Local<v8::Array> keys = opt_envs->GetPropertyNames();
len = (int32_t)keys->Length();

for (i = 0; i < len; i++) {
v8::Local<v8::Value> k = keys->Get(i);
v8::Local<v8::Value> v = envs->Get(k);
v8::Local<v8::Value> v = opt_envs->Get(k);
exlib::string ks, vs;

hr = GetArgumentValue(k, ks);
@@ -96,7 +96,7 @@ module process
@code
{
"timeout": 100, // unit ms
"envs": [] // Enviroment variable of child process.
"env": {} // Enviroment variable of child process.
}
@endcode
@return Child process object containing result of command.
@@ -109,7 +109,7 @@ module process
@code
{
"timeout": 100, // unit ms
"envs": [] // Environment variable of child process.
"env": {} // Environment variable of child process.
}
@endcode
@return Child process object containing result of command.
@@ -123,7 +123,7 @@ module process
@code
{
"timeout": 100, // unit ms
"envs": [] // Environment variable of child process.
"env": {} // Environment variable of child process.
}
@endcode
@return Child process object containing result of the command.
@@ -136,7 +136,7 @@ module process
@code
{
"timeout": 100, // unit ms
"envs": [] // Environment variable of child process.
"env": {} // Environment variable of child process.
}
@endcode
@return Child process object containing result of the command.
@@ -150,7 +150,7 @@ module process
@code
{
"timeout": 100, // unit ms
"envs": [] // Environment variable of child process.
"env": {} // Environment variable of child process.
}
@endcode
@return Result code of bash command.
@@ -163,7 +163,7 @@ module process
@code
{
"timeout": 100, // unit ms
"envs": [] // Environment variable of child process.
"env": {} // Environment variable of child process.
}
@endcode
@return Result code of bash command.
@@ -157,7 +157,7 @@ module process : EventEmitter
```JavaScript
{
"timeout": 100, // 单位为 ms
"envs": [] // 进程环境变量
"env": {} // 进程环境变量
}
```
@param command 指定运行的命令行
@@ -173,7 +173,7 @@ module process : EventEmitter
```JavaScript
{
"timeout": 100, // 单位为 ms
"envs": [] // 进程环境变量
"env": {} // 进程环境变量
}
```
@param command 指定运行的命令行
@@ -188,7 +188,7 @@ module process : EventEmitter
```JavaScript
{
"timeout": 100, // 单位为 ms
"envs": [] // 进程环境变量
"env": {} // 进程环境变量
}
```
@param command 指定运行的命令行
@@ -204,7 +204,7 @@ module process : EventEmitter
```JavaScript
{
"timeout": 100, // 单位为 ms
"envs": [] // 进程环境变量
"env": {} // 进程环境变量
}
```
@param command 指定运行的命令行
@@ -219,7 +219,7 @@ module process : EventEmitter
```JavaScript
{
"timeout": 100, // 单位为 ms
"envs": [] // 进程环境变量
"env": {} // 进程环境变量
}
```
@param command 指定运行的命令行
@@ -235,7 +235,7 @@ module process : EventEmitter
```JavaScript
{
"timeout": 100, // 单位为 ms
"envs": [] // 进程环境变量
"env": {} // 进程环境变量
}
```
@param command 指定运行的命令行
@@ -0,0 +1 @@
process_env_keys.*.json
@@ -0,0 +1,12 @@
/**
* @description run this script to generate env keys for your device.
*/
const fs = require('fs')
const path = require('path')

const keys = Object.keys(process.env)

fs.writeTextFile(
path.resolve(__dirname, `process_env_keys.${process.platform}.json`),
JSON.stringify(keys, null, '\t')
)
@@ -0,0 +1,61 @@
/**
* @description run this script to check if any module's test case invalid in SubProcess
*/

const test = require("test");
test.setup();

const path = require("path");
const util = require("util");
const cmd = process.execPath;

describe("Run other Test Case In SubProcess", () => {
it(`run: ${process.platform}`, () => {
if (process.platform === 'win32') {
const retcode = process.run(cmd, [path.join(__dirname, '../main.js')], {
env1: {
...util.pick(process.env, [
"SystemRoot",
"TEMP",
"TMP",
"CommonProgramFiles",
"CommonProgramFiles(x86)",
"CommonProgramW6432",
"ProgramFiles",
"ProgramFiles(x86)",
"ProgramW6432",
]),
CI_SUBPROCESS_CHECK: 1
}
});

assert.equal(retcode, 0);
}

if (process.platform === 'darwin') {
const retcode = process.run(cmd, [path.join(__dirname, '../main.js')], {
env: {
HOME: process.env.HOME,
TMPDIR: process.env.TMPDIR,
CI_SUBPROCESS_CHECK: 1
}
});

assert.equal(retcode, 0);
}

if (process.platform === 'linux') {
const retcode = process.run(cmd, [path.join(__dirname, '../main.js')], {
env: {
HOME: process.env.HOME,
TMPDIR: process.env.TMPDIR,
CI_SUBPROCESS_CHECK: 1
}
});

assert.equal(retcode, 0);
}
});
});

require.main === module && test.run(console.DEBUG);
@@ -3,10 +3,9 @@ test.setup();

var test_util = require('./test_util');

var os = require("os");
var path = require("path");

var win = process.platform == 'win32';
var win = process.platform === 'win32';

var html =
`<html>
@@ -3,10 +3,10 @@
var test = require("test");
test.setup();

var coroutine = require('coroutine');

global.full_test = process.argv.indexOf('full') >= 0;

const CI_SUBPROCESS_CHECK = !!process.env.CI_SUBPROCESS_CHECK;

run("./assert_test.js");
run("./test_test.js");
run("./console_test.js");
@@ -29,7 +29,7 @@ run("./fs_test.js");
run("./ms_test.js");
run("./io_test.js");
run("./os_test.js");
run("./process_test.js");
!CI_SUBPROCESS_CHECK && run("./process_test.js");
run("./encoding_test.js");
run("./json_test.js");
run("./module_test.js");
@@ -71,6 +71,6 @@ run("./v8_test.js");
run("./getter_throw.js")

run("./internal_test/helpers.js")
run("./opt_tools_test/index.js")
!CI_SUBPROCESS_CHECK && run("./opt_tools_test/index.js")

process.exitCode = test.run();
@@ -18,7 +18,7 @@ chdirAndDo(

const processRunOptions = {
env: {
...process.env,
// all required environment would fallback to the parent's one
FIBJS_SILENT_INSALL: 1
}
}
@@ -0,0 +1,8 @@
const assert = require('assert')
const dns = require('dns')

dns.resolve('fibjs.org');
console.log('resolve domain success!');

dns.lookup('fibjs.org');
console.log('lookup domain success!');

0 comments on commit 6c8a8d0

Please sign in to comment.
You can’t perform that action at this time.