【问题标题】:execFile not being calledexecFile 没有被调用
【发布时间】:2015-02-14 05:07:33
【问题描述】:

我遇到了问题,甚至不确定从哪里开始故障排除。

我正在使用slightly modified mocha-casperjs。 CasperJS 是 PhantomJS 的包装器。我正在尝试在完成测试时集成 Growl 通知。

我可以在调用 mocha.run 之前成功执行通知,如下所示:

execFile("terminal-notifier", ["-message", "Tests Begin"], null, function (err, stdout, stderr) {
  console.log("execFileSTDOUT:", JSON.stringify(stdout))
  console.log("execFileSTDERR:", JSON.stringify(stderr))
})



// for convience, expose the current runner on the mocha global
    mocha.runner = mocha.run(function() {
    ...

但是,这失败了:

// for convience, expose the current runner on the mocha global
mocha.runner = mocha.run(function() {
    execFile("terminal-notifier", ["-message", "Tests Begin"], null, function (err, stdout, stderr) {
      console.log("execFileSTDOUT:", JSON.stringify(stdout))
      console.log("execFileSTDERR:", JSON.stringify(stderr))
    })
    ...

我不太了解 Mocha 或 PhantomJS 的内容。 Mocha 是否会吃掉标准输出或类似的东西,导致 execFile 调用失败?还有什么我没有得到的吗?

谢谢, 凯文

--- 更新 ---

情节变厚了。只包含 casper 对象就会杀死 execFile。

使用“casperjs test.js”运行以下代码成功输出 execFile。取消注释 casper 对象不会导致任何输出。

'use strict';
var process = require("child_process");
var spawn = process.spawn;
var execFile = process.execFile;

execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) {
  console.log("execFileSTDOUT:", JSON.stringify(stdout));
  console.log("execFileSTDERR:", JSON.stringify(stderr));
});

//var casper = require('casper').create();
//casper.exit();

【问题讨论】:

  • 它是如何失败的?您收到错误消息吗?例外?它只是不执行?
  • 据我所知,什么都没有。没有错误,也没有例外。回调永远不会被调用。

标签: phantomjs mocha.js execfile


【解决方案1】:

您可以尝试将日志行更改为这一行:

console.log("execFileSTDOUT:" + JSON.stringify(stdout));

然后,您应该看到,如果调用了 execFile: execFileSTDOUT:""

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。 execFile 是异步运行的,所以你需要给它一个执行的机会。通过立即退出 CasperJS,没有输出,因为 ls 没有完成,所以没有调用回调。一种解决方法是使用setTimeout。这是我在 PhantomJS 中所做的。我认为 CasperJS 也一样。

    'use strict';
    var process = require("child_process");
    var spawn = process.spawn;
    var execFile = process.execFile;
    
    execFile("ls", ["-lF", "/usr"], null, function (err, stdout, stderr) {
      console.log("execFileSTDOUT:", JSON.stringify(stdout));
      console.log("execFileSTDERR:", JSON.stringify(stderr));
    });
    
    setTimeout(function() { phantom.exit(0) },1000);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2019-03-31
      • 2012-12-16
      • 2015-03-28
      相关资源
      最近更新 更多