Skip to content

Commit 97a5d9e

Browse files
ozyxmjbvz
authored andcommitted
modifications to mocha.js and some TestExecutor logic (microsoft#1334)
* have run_tests return structured result used by TestExecutor * have run_tests accept test data from stdin * run_tests receives test data over stdin * expose StandardOutput and disable async readline * TestExecutor receives test results over process StandardOutput * include testing framework in testInfo object sent to run_tests.js * comments * close readline interface after running test * revert ProcessOutput * use JsonConvert.DeserializeObject<TestResult>() instead * create GetTestResultFromProcess() method * use Process, copy over helper methods from ProcessOutput * close StandardInput after sending test info * include dependency for copied ProcessOutput method * launch node inside of RunTestCases instead * remove some debugging stuff * handle case if result is null * pass in streams as parameters to allow continual reading/writing * move setup logic to RunTests * only remove double quotes from test case info * move Process launch logic into function * mocha uses runner events to determine test pass/fail * get TestExecutor in working state, still launching one process per one test * get TextExecutor code into working state * fixes from PR feedback
1 parent a7f4433 commit 97a5d9e

4 files changed

Lines changed: 250 additions & 66 deletions

File tree

Common/Product/SharedProject/ProcessOutput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public static ProcessOutput RunElevated(
304304
return result;
305305
}
306306

307-
private static string GetArguments(IEnumerable<string> arguments, bool quoteArgs) {
307+
public static string GetArguments(IEnumerable<string> arguments, bool quoteArgs) {
308308
if (quoteArgs) {
309309
return string.Join(" ", arguments.Where(a => a != null).Select(QuoteSingleArgument));
310310
} else {
@@ -335,7 +335,7 @@ internal static IEnumerable<string> SplitLines(string source) {
335335
}
336336
}
337337

338-
internal static string QuoteSingleArgument(string arg) {
338+
public static string QuoteSingleArgument(string arg) {
339339
if (string.IsNullOrEmpty(arg)) {
340340
return "\"\"";
341341
}

Nodejs/Product/Nodejs/TestFrameworks/mocha/mocha.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22
var EOL = require('os').EOL;
33
var fs = require('fs');
44
var path = require('path');
5-
5+
var result = {
6+
"title": "",
7+
"passed": false,
8+
"stdOut": "",
9+
"stdErr": ""
10+
};
611
// Choose 'tap' rather than 'min' or 'xunit'. The reason is that
712
// 'min' produces undisplayable text to stdout and stderr under piped/redirect,
813
// and 'xunit' does not print the stack trace from the test.
914
var defaultMochaOptions = { ui: 'tdd', reporter: 'tap', timeout: 2000 };
1015

16+
process.stdout.write = function (string, encoding, fd) {
17+
result.stdOut += string;
18+
}
19+
20+
process.stderr.write = function (string, encoding, fd) {
21+
result.stdErr += string;
22+
}
23+
1124
var find_tests = function (testFileList, discoverResultFile, projectFolder) {
1225
var Mocha = detectMocha(projectFolder);
1326
if (!Mocha) {
@@ -56,7 +69,8 @@ var find_tests = function (testFileList, discoverResultFile, projectFolder) {
5669
};
5770
module.exports.find_tests = find_tests;
5871

59-
var run_tests = function (testName, testFile, workingFolder, projectFolder) {
72+
var run_tests = function (testName, testFile, workingFolder, projectFolder, callback) {
73+
//var testResults = [];
6074
var Mocha = detectMocha(projectFolder);
6175
if (!Mocha) {
6276
return;
@@ -70,10 +84,27 @@ var run_tests = function (testName, testFile, workingFolder, projectFolder) {
7084
else
7185
mocha.grep(testName); // prior Mocha 3.0.0
7286
}
87+
7388
mocha.addFile(testFile);
7489

75-
mocha.run(function (code) {
76-
process.exit(code);
90+
// run tests
91+
var runner = mocha.run(function (code) { });
92+
93+
runner.on('start', function () {
94+
});
95+
runner.on('test', function (test) {
96+
result.title = test.title;
97+
});
98+
runner.on('end', function () {
99+
callback(result);
100+
});
101+
runner.on('pass', function (test) {
102+
result.passed = true;
103+
//testResults.push(result);
104+
});
105+
runner.on('fail', function (test, err) {
106+
result.passed = false;
107+
//testResults.push(result);
77108
});
78109
};
79110

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
var framework;
2-
try {
3-
framework = require('./' + process.argv[2] + '/' + process.argv[2] + '.js');
4-
} catch (exception) {
5-
console.log("NTVS_ERROR:Failed to load TestFramework (" + process.argv[2] + "), " + exception);
6-
process.exit(1);
7-
}
2+
var readline = require('readline');
3+
var old_stdout = process.stdout.write;
4+
var old_stderr = process.stderr.write;
5+
var rl = readline.createInterface({
6+
input: process.stdin,
7+
output: process.stdout
8+
});
89

9-
framework.run_tests(process.argv[3], process.argv[4], process.argv[5], process.argv[6]);
10+
rl.on('line', (line) => {
11+
var testInfo = JSON.parse(line);
12+
// get rid of leftover quotations from C# (necessary?)
13+
for(var s in testInfo) {
14+
testInfo[s] = testInfo[s].replace(/["]+/g, '');
15+
}
1016

17+
try {
18+
framework = require('./' + testInfo.framework + '/' + testInfo.framework + '.js');
19+
} catch (exception) {
20+
console.log("NTVS_ERROR:Failed to load TestFramework (" + process.argv[2] + "), " + exception);
21+
process.exit(1);
22+
}
23+
24+
function sendResult(result) {
25+
process.stdout.write = old_stdout;
26+
process.stderr.write = old_stderr;
27+
console.log(JSON.stringify(result));
28+
process.exit(0);
29+
}
30+
// run the test
31+
framework.run_tests(testInfo.testName, testInfo.testFile, testInfo.workingFolder, testInfo.projectFolder, sendResult);
32+
33+
// close readline interface
34+
rl.close();
35+
});

0 commit comments

Comments
 (0)