【发布时间】:2019-09-18 13:36:28
【问题描述】:
我正在尝试使用 Jest 运行示例测试以验证我的 Google Cloud 功能是否正常工作,但我不断收到以下错误。
错误:命令失败:gcloud beta 函数调用 cf-1 --region europe-west1 --data '{"data":"eyJkYXRhIjoiMSJ9"}'
错误:(gcloud.beta.functions.call)[--data] 的值无效:不是有效的 JSON:无法解码任何 JSON 对象
我知道在 Windows 终端中运行命令时可以使用反斜杠转义双引号,但如何在 JavaScript 中执行。
test.js
const childProcess = require('child_process');
describe('Test CF', () => {
it('print outs the error message when received JSON is blank', done => {
const msg = { data: '1' };
const encodedMsg = Buffer.from(JSON.stringify(msg)).toString('base64');
const data = JSON.stringify({ data: encodedMsg });
const executeResultOutput = childProcess.execSync(`gcloud beta functions call cf-1 --region europe-west1 --data '${data}'`).toString();
const logs = childProcess
.execSync(
`gcloud functions logs read cf-1 --region europe-west1 --execution-id ${executionIdObj}`,
)
.toString();
expect(logs).toEqual(expect.stringContaining('Error..'));
});
});
【问题讨论】:
-
仅供参考,在 Windows 中,除了可执行路径的初始引用,这对
CreateProcessW很重要,双引号或单引号的含义以及如何转义它们取决于应用程序。大多数关注VC++ rules。这与 Unix 形成鲜明对比,在 Unix 中,父级控制如何将命令行解析为exec*系统调用的参数。
标签: javascript windows child-process