Skip to content

Commit

Permalink
prepare run_tests to run multiple tests (#1355)
Browse files Browse the repository at this point in the history
* prepare run_tests to run multiple tests

* have tape.js run_tests use callback to return result

* launch node if test run from 'run selected tests'
  • Loading branch information
ozyx authored and mjbvz committed Oct 19, 2016
1 parent 97a5d9e commit 3321602
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 31 deletions.
22 changes: 20 additions & 2 deletions Nodejs/Product/Nodejs/TestFrameworks/Tape/tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
var EOL = require('os').EOL;
var fs = require('fs');
var path = require('path');
var result = {
"title": "",
"passed": false,
"stdOut": "",
"stdErr": ""
};

process.stdout.write = function (string, encoding, fd) {
result.stdOut += string;
}

process.stderr.write = function (string, encoding, fd) {
result.stdErr += string;
}

function find_tests(testFileList, discoverResultFile, projectFolder) {
var test = findTape(projectFolder);
Expand Down Expand Up @@ -37,8 +51,9 @@ function find_tests(testFileList, discoverResultFile, projectFolder) {
};
module.exports.find_tests = find_tests;

function run_tests(testName, testFile, workingFolder, projectFolder) {
function run_tests(testName, testFile, workingFolder, projectFolder, callback) {
var testCases = loadTestCases(testFile);
result.title = testName;
if (testCases === null) {
return;
}
Expand All @@ -51,10 +66,13 @@ function run_tests(testName, testFile, workingFolder, projectFolder) {
try {
var harness = test.getHarness();
harness.only(testName);
result.passed = true;
} catch (e) {
logError("Error running test:", testName, "in", testFile, e);
return;
result.passed = false;
}

callback(result);
}
module.exports.run_tests = run_tests;

Expand Down
41 changes: 27 additions & 14 deletions Nodejs/Product/Nodejs/TestFrameworks/mocha/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ var result = {
// 'min' produces undisplayable text to stdout and stderr under piped/redirect,
// and 'xunit' does not print the stack trace from the test.
var defaultMochaOptions = { ui: 'tdd', reporter: 'tap', timeout: 2000 };

process.stdout.write = function (string, encoding, fd) {
function append_stdout(string, encoding, fd) {
result.stdOut += string;
}

process.stderr.write = function (string, encoding, fd) {
function append_stderr(string, encoding, fd) {
result.stdErr += string;
}
process.stdout.write = append_stdout;
process.stderr.write = append_stderr;

var find_tests = function (testFileList, discoverResultFile, projectFolder) {
var Mocha = detectMocha(projectFolder);
Expand Down Expand Up @@ -78,33 +78,46 @@ var run_tests = function (testName, testFile, workingFolder, projectFolder, call

var mocha = initializeMocha(Mocha, projectFolder);

if (testName) {
if (typeof mocha.fgrep === 'function')
mocha.fgrep(testName); // since Mocha 3.0.0
else
mocha.grep(testName); // prior Mocha 3.0.0
}
//if (testName) {
// if (typeof mocha.fgrep === 'function')
// mocha.fgrep(testName); // since Mocha 3.0.0
// else
// mocha.grep(testName); // prior Mocha 3.0.0
//}

mocha.addFile(testFile);

// run tests
var runner = mocha.run(function (code) { });
var runner = mocha.run(function (code) { process.exit(code); });

runner.on('start', function () {
});
runner.on('test', function (test) {
result.title = test.title;
process.stdout.write = append_stdout;
process.stderr.write = append_stderr;
});
runner.on('end', function () {
callback(result);
});
runner.on('pass', function (test) {
result.passed = true;
//testResults.push(result);
callback(result);
result = {
'title': '',
'passed': false,
'stdOut': '',
'stdErr': ''
}
});
runner.on('fail', function (test, err) {
result.passed = false;
//testResults.push(result);
callback(result);
result = {
'title': '',
'passed': false,
'stdOut': '',
'stdErr': ''
}
});
};

Expand Down
4 changes: 2 additions & 2 deletions Nodejs/Product/Nodejs/TestFrameworks/run_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ rl.on('line', (line) => {
process.stdout.write = old_stdout;
process.stderr.write = old_stderr;
console.log(JSON.stringify(result));
process.exit(0);
//process.exit(0);
}
// run the test
framework.run_tests(testInfo.testName, testInfo.testFile, testInfo.workingFolder, testInfo.projectFolder, sendResult);

// close readline interface
rl.close();
//rl.close();
});
35 changes: 22 additions & 13 deletions Nodejs/Product/TestAdapter/TestExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrame
args.AddRange(GetDebugArgs(settings, out port));
}

//args.AddRange(GetInterpreterArgs(firstTest, entry.Key, settings.ProjectRootDir));

// eventually launch node process here
args.AddRange(GetInterpreterArgs(firstTest, entry.Key, settings.ProjectRootDir));

// launch node process
LaunchNodeProcess(settings.WorkingDir, settings.NodeExePath, args);
// Run all test cases in a given project
RunTestCases(entry.Value, runContext, frameworkHandle);

// dispose node process
_nodeProcess.Dispose();
}
}
}
Expand All @@ -141,10 +141,26 @@ public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrame
ValidateArg.NotNull(tests, "tests");
ValidateArg.NotNull(runContext, "runContext");
ValidateArg.NotNull(frameworkHandle, "frameworkHandle");

_cancelRequested.Reset();
bool hasExited = false;
bool isNull = _nodeProcess == null;
if (!isNull) {
hasExited = _nodeProcess.HasExited;
}
frameworkHandle.SendMessage(TestMessageLevel.Informational, isNull.ToString());
frameworkHandle.SendMessage(TestMessageLevel.Informational, hasExited.ToString());
if ( _nodeProcess == null || _nodeProcess.HasExited ) {
frameworkHandle.SendMessage(TestMessageLevel.Informational, "inside RunTests if statement");
TestCase firstTest = tests.First();
NodejsProjectSettings settings = LoadProjectSettings(firstTest.Source);
List<string> args = new List<string>();
args.AddRange(GetInterpreterArgs(firstTest, settings.WorkingDir, settings.ProjectRootDir));
LaunchNodeProcess(settings.WorkingDir, settings.NodeExePath, args);
}

RunTestCases(tests, runContext, frameworkHandle);

_nodeProcess.Dispose();
}

private void RunTestCases(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle) {
Expand Down Expand Up @@ -238,8 +254,6 @@ private void RunTestCase(VisualStudioApp app, IFrameworkHandle frameworkHandle,
}

lock (_syncObject) {
// launch node process
LaunchNodeProcess(settings.WorkingDir, settings.NodeExePath, args);
#if DEBUG
frameworkHandle.SendMessage(TestMessageLevel.Informational, "cd " + workingDir);
//frameworkHandle.SendMessage(TestMessageLevel.Informational, _nodeProcess.Arguments);
Expand All @@ -250,8 +264,7 @@ private void RunTestCase(VisualStudioApp app, IFrameworkHandle frameworkHandle,
_nodeProcess.StandardInput.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(testObject));
_nodeProcess.StandardInput.Close();
_nodeProcess.WaitForExit(5000);
}
//standardInput.Close();
}
if (runContext.IsBeingDebugged && app != null) {
try {
//the '#ping=0' is a special flag to tell VS node debugger not to connect to the port,
Expand Down Expand Up @@ -288,9 +301,6 @@ private void RunTestCase(VisualStudioApp app, IFrameworkHandle frameworkHandle,
} else {
frameworkHandle.SendMessage(TestMessageLevel.Error, "Failed to obtain result for " + test.DisplayName + " from TestRunner");
}

// dispose node process
_nodeProcess.Dispose();
}

private ResultObject ParseTestResult(string line) {
Expand All @@ -310,7 +320,6 @@ private ResultObject GetTestResultFromProcess(StreamReader sr) {
}
break;
}
sr.DiscardBufferedData();
return result;
}

Expand Down

0 comments on commit 3321602

Please sign in to comment.