AssertionError [ERR_ASSERTION]:子进程配置错误
AssertionError [ERR_ASSERTION]: child process misconfigured
我有一个 test.js
文件,它接收 child_process 并检查环境变量 MY_ENV_VAR,这是在我的index.js
文件练习函数。当我 运行 node test.js
时,测试失败并显示 AssertionError [ERR_ASSERTION]: child process misconfigured 的错误。我试过调试,但没有得出任何可靠的结论。我相信子进程没有收到正确的 output.Below 是本练习所需的文件:
附加信息
我在 spawn 方法中添加了额外的命令,这些命令反映在下面的 index.js
文件中。现在这会导致另一个断言错误,因为我的环境变量的长度不等于 child.js
文件中的 assertion.strictEqual 指定的长度。子进程正在接收系统环境变量,而不是与此进程相关的环境变量。因此,如果我可以限制环境变量的范围,那么程序将按预期运行。
index.js
'use strict'
const { spawn } = require('child_process')
function exercise (myEnvVar) {
// TODO return a child process with
// a single environment variable set
// named MY_ENV_VAR. The MY_ENV_VAR
// environment variable's value should
// be the value of the myEnvVar parameter
// passed to this exercise function
process.env.MY_ENV_VAR = myEnvVar
return spawn(process.execPath,
['child.js', '-e','process.env',
'-e', 'process.stdout.pipe(process.stdout)', '-e', 'process.exit(0)']);
}
module.exports = exercise
child.js
'use strict'
const assert = require('assert')
const clean = (env) => Object.fromEntries(
Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k))
)
const env = clean(process.env)
assert.strictEqual(env.MY_ENV_VAR, 'is set')
assert.strictEqual(
Object.keys(env).length,
1,
'child process should have only one env var'
)
console.log('passed!')
test.js
'use strict'
const assert = require('assert')
const { equal } = assert.strict
const exercise = require('.')
let sp = null
try {
sp = exercise('is set')
assert(sp, 'exercise function should return a child process instance')
if (Buffer.isBuffer(sp)) {
equal(sp.toString().trim(), 'passed!', 'child process misconfigured')
process.stdout.write(sp)
return
}
} catch (err) {
const { status} = err
if (status == null) throw err
equal(status, 0, 'exit code should be 0')
return
}
if (!sp.on) {
const { stdout, stderr } = sp
if (stderr.length > 0) process.stderr.write(stderr)
if (stdout.length > 0) process.stdout.write(stdout)
equal(sp.status, 0, 'exit code should be 0')
equal(stdout.toString().trim(), 'passed!', 'child process misconfigured')
return
}
let out = ''
if (sp.stderr) sp.stderr.pipe(process.stderr)
if (sp.stdout) {
sp.stdout.once('data', (data) => { out = data })
sp.stdout.pipe(process.stdout)
} else {
// stdio may be misconfigured, or fork method may be used,
// allow benefit of the doubt since in either case
// exit code check will still fail:
out = 'passed!'
}
const timeout = setTimeout(() => {
equal(out.toString().trim(), 'passed!', 'child process misconfigured')
}, 1000)
sp.once('exit', (status) => {
equal(status, 0, 'exit code should be 0')
equal(out.toString().trim(), 'passed!', 'child process misconfigured')
clearTimeout(timeout)
})
欢迎并感谢所有帮助
我已经更新了 index.js
中的函数,并且代码在有人参与的情况下执行。正确的解决方案如下:
index.js
'use strict'
const { spawn } = require('child_process')
function exercise (myEnvVar) {
// TODO return a child process with
// a single environment variable set
// named MY_ENV_VAR. The MY_ENV_VAR
// environment variable's value should
// be the value of the myEnvVar parameter
// passed to this exercise function
return spawn(process.execPath, ['child.js'], {
env: {MY_ENV_VAR: myEnvVar}
});
}
module.exports = exercise
我相信我和你在做同一个项目。请注意,从 Node.js 版本 16 开始,我们无法使用 Windows 上的以下代码清理子进程的环境变量。 (虽然它在 Linux 上运行良好)
function exercise (myEnvVar) {
// TODO return a child process with
// a single environment variable set
// named MY_ENV_VAR. The MY_ENV_VAR
// environment variable's value should
// be the value of the myEnvVar parameter
// passed to this exercise function
return spawn(process.execPath, ['child.js'], {
env: {MY_ENV_VAR: myEnvVar}
});
}
我有一个 test.js
文件,它接收 child_process 并检查环境变量 MY_ENV_VAR,这是在我的index.js
文件练习函数。当我 运行 node test.js
时,测试失败并显示 AssertionError [ERR_ASSERTION]: child process misconfigured 的错误。我试过调试,但没有得出任何可靠的结论。我相信子进程没有收到正确的 output.Below 是本练习所需的文件:
附加信息
我在 spawn 方法中添加了额外的命令,这些命令反映在下面的index.js
文件中。现在这会导致另一个断言错误,因为我的环境变量的长度不等于 child.js
文件中的 assertion.strictEqual 指定的长度。子进程正在接收系统环境变量,而不是与此进程相关的环境变量。因此,如果我可以限制环境变量的范围,那么程序将按预期运行。
index.js
'use strict'
const { spawn } = require('child_process')
function exercise (myEnvVar) {
// TODO return a child process with
// a single environment variable set
// named MY_ENV_VAR. The MY_ENV_VAR
// environment variable's value should
// be the value of the myEnvVar parameter
// passed to this exercise function
process.env.MY_ENV_VAR = myEnvVar
return spawn(process.execPath,
['child.js', '-e','process.env',
'-e', 'process.stdout.pipe(process.stdout)', '-e', 'process.exit(0)']);
}
module.exports = exercise
child.js
'use strict'
const assert = require('assert')
const clean = (env) => Object.fromEntries(
Object.entries(env).filter(([k]) => !/^(_.*|pwd|shlvl)/i.test(k))
)
const env = clean(process.env)
assert.strictEqual(env.MY_ENV_VAR, 'is set')
assert.strictEqual(
Object.keys(env).length,
1,
'child process should have only one env var'
)
console.log('passed!')
test.js
'use strict'
const assert = require('assert')
const { equal } = assert.strict
const exercise = require('.')
let sp = null
try {
sp = exercise('is set')
assert(sp, 'exercise function should return a child process instance')
if (Buffer.isBuffer(sp)) {
equal(sp.toString().trim(), 'passed!', 'child process misconfigured')
process.stdout.write(sp)
return
}
} catch (err) {
const { status} = err
if (status == null) throw err
equal(status, 0, 'exit code should be 0')
return
}
if (!sp.on) {
const { stdout, stderr } = sp
if (stderr.length > 0) process.stderr.write(stderr)
if (stdout.length > 0) process.stdout.write(stdout)
equal(sp.status, 0, 'exit code should be 0')
equal(stdout.toString().trim(), 'passed!', 'child process misconfigured')
return
}
let out = ''
if (sp.stderr) sp.stderr.pipe(process.stderr)
if (sp.stdout) {
sp.stdout.once('data', (data) => { out = data })
sp.stdout.pipe(process.stdout)
} else {
// stdio may be misconfigured, or fork method may be used,
// allow benefit of the doubt since in either case
// exit code check will still fail:
out = 'passed!'
}
const timeout = setTimeout(() => {
equal(out.toString().trim(), 'passed!', 'child process misconfigured')
}, 1000)
sp.once('exit', (status) => {
equal(status, 0, 'exit code should be 0')
equal(out.toString().trim(), 'passed!', 'child process misconfigured')
clearTimeout(timeout)
})
欢迎并感谢所有帮助
我已经更新了 index.js
中的函数,并且代码在有人参与的情况下执行。正确的解决方案如下:
index.js
'use strict'
const { spawn } = require('child_process')
function exercise (myEnvVar) {
// TODO return a child process with
// a single environment variable set
// named MY_ENV_VAR. The MY_ENV_VAR
// environment variable's value should
// be the value of the myEnvVar parameter
// passed to this exercise function
return spawn(process.execPath, ['child.js'], {
env: {MY_ENV_VAR: myEnvVar}
});
}
module.exports = exercise
我相信我和你在做同一个项目。请注意,从 Node.js 版本 16 开始,我们无法使用 Windows 上的以下代码清理子进程的环境变量。 (虽然它在 Linux 上运行良好)
function exercise (myEnvVar) {
// TODO return a child process with
// a single environment variable set
// named MY_ENV_VAR. The MY_ENV_VAR
// environment variable's value should
// be the value of the myEnvVar parameter
// passed to this exercise function
return spawn(process.execPath, ['child.js'], {
env: {MY_ENV_VAR: myEnvVar}
});
}