如何将nodejs / nodeunit结果传递回TO命令行

时间:2016-02-12 14:59:50

标签: node.js git batch-file

我正在编写TortoiseGIT提交挂钩,它应该运行nodejs / nodeunit测试并在发生故障时阻止提交。

以下是 precommit.bat 挂钩文件(伪代码,不工作)

  SET RES =  node C:\Work\tests\precommit\precommit.js
  if(RES === false){
    MSG * Some tests failed!
    exit 1
 }

下面是 precommit.js ,它将回调传递给nodeunit的 customout 模块(WORKS FINE除了process.exit部分)

var reporter = require('./customout');

process.chdir(__dirname);

var run_tests = [
    '../unit/HelperTests.js',
    '../unit/coreTests.Logic.js'

];


// Tell the reporter to run the tests

if(run_tests.length > 0) {
    if(arguments[0]){
        console.log("TESTS OK");
        process.exit(0);
    }else{
        console.log("TESTS FAILED");
        process.exit(1);
    }
}

以下是 customout.js (WORKS FINE)

exports.run = function(files,options,callback){

nodeunit.runFiles(paths, {
    testspec: options.testspec,
    testFullSpec: options.testFullSpec,
    moduleStart: function (name) {},
    testDone: function (name, assertions) {
        tracker.remove(name);
        }
    },
    done: function (assertions, end) {
        //passes Boolean to process.exit
        if (callback) callback(
                assertions.failures() ?
                false :
                true)
        );
    },
    testStart: function(name) {
        tracker.put(name);
    }
});

};

将runFiles回调中的true / false传递给原始命令行进程的正确方法是什么,以防止提交并通知用户?

2 个答案:

答案 0 :(得分:1)

成功时process.exit(0),失败时process.exit(1)

答案 1 :(得分:0)

工作BAT

node C:\Work\perkinelmer\tests\precommit\precommit.js
if  %ERRORLEVEL% NEQ  0 (
    MSG * Some Unittests failed with errorlevel %errorlevel%
    exit 1
)