﻿var fs = require('fs');

var find_tests = function (testFile, discoverResultFile, projectFolder) {
  var Mocha = require(projectFolder + '\\node_modules\\mocha');
  var mocha = new Mocha();
  var testList;
  function getTestList(suite) {
    if (suite) {
      if (suite.tests && suite.tests.length !== 0) {
        testList = testList ? testList + '\r\n' + suite.title : suite.title;
      }
      if (suite.suites) {
        suite.suites.forEach(function (s, i, ss) {
          getTestList(s);
        });
      }
    }
  }
  mocha.addFile(testFile);
  mocha.loadFiles();
  getTestList(mocha.suite);
  if (testList) {
    var fd = fs.openSync(discoverResultFile, 'w');
    fs.writeSync(fd, testList);
    fs.closeSync(fd);
  }
}
module.exports.find_tests = find_tests;

var run_tests = function (testName, testFile, projectFolder) {
  var Mocha = new require(projectFolder + '\\node_modules\\mocha');
  var mocha = new Mocha();
  mocha.suite.timeout(30000); //TODO: config
  if (testName) {
    mocha.grep(testName);
  }
  mocha.addFile(testFile);
  mocha.run(function (code) {
    console.log('Done with Code:' + code);
    process.exit(code);
  });
}
module.exports.run_tests = run_tests;
@@ -924,16 +924,84 @@
 ocha();%0A
+  //default at 2 sec might be too short (TODO: make it configuable)%0A
   mocha.
@@ -1026,22 +1026,8 @@
 0); 
-//TODO: config
 %0A  i
@@ -1104,82 +1104,278 @@
 ;%0A  
-mocha.run(function (code) %7B%0A    console.log('Done with Code:' + code);%0A   
+//Choose 'xunit' rather 'min'. The reason is when under piped/redirect,%0A  //mocha produces undisplayable text to stdout and stderr. Using xunit works fine %0A  mocha.reporter('xunit');%0A  mocha.run(exitLater);%0A%7D%0Afunction exitLater(code) %7B%0A  process.on('exit', function () %7B
  pro
@@ -1394,14 +1394,11 @@
 de);
-%0A 
  %7D)
-;
 %0A%7D%0Am

modify runner script to report test result correctly