Skip to content

Commit 18f9699

Browse files
committed
runner setup
1 parent 59cce64 commit 18f9699

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "mocha-coderoad",
33
"version": "0.1.0",
44
"description": "mocha test runner & reporter for atom-coderoad",
5-
"main": "lib/runner.js",
5+
"main": "src/runner.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},

src/createRunner.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
var spawn = require('child_process').spawn;
3+
var path = require('path');
4+
function createRunner(config, tests) {
5+
var options = {
6+
cwd: config.dir
7+
};
8+
if (options.env == null) {
9+
options.env = Object.create(process.env);
10+
}
11+
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
12+
options.env.DIR = config.dir;
13+
options.env.TUTORIAL_DIR = config.tutorialDir;
14+
var node = (process.platform === 'darwin' ?
15+
path.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper') : process.execPath);
16+
return spawn(node, [
17+
'/usr/local/bin/mocha',
18+
'--bail',
19+
("--reporter=" + path.join(__dirname, 'reporter')),
20+
].concat(tests), options);
21+
}
22+
exports.createRunner = createRunner;

src/reporter.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict";
2+
var utils_1 = require('./utils');
3+
exports = module.exports = reporter;
4+
function reporter(runner) {
5+
var result = {
6+
passes: [],
7+
failures: [],
8+
failedAt: null
9+
};
10+
runner.on('pass', function (test) {
11+
result.passes.push({
12+
msg: test.fullTitle(),
13+
file: test.file
14+
});
15+
});
16+
runner.on('fail', function (test, err) {
17+
result.failures.push({
18+
msg: test.fullTitle(),
19+
file: test.file,
20+
body: test.body,
21+
timedOut: test.timedOut,
22+
duration: test.duration
23+
});
24+
result.failedAt = test.file;
25+
});
26+
runner.on('end', function () {
27+
process.stdout.write(utils_1.signal + JSON.stringify(result, null, 2));
28+
});
29+
}

src/runner.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"use strict";
2+
var utils_1 = require('./utils');
3+
var createRunner_1 = require('./createRunner');
4+
function runner(files, config, handleTestResult, handleLog) {
5+
var tests = utils_1.concatAll(files);
6+
var runner = createRunner_1.createRunner(config, tests);
7+
runner.stdout.on('data', function (data) {
8+
var result = {
9+
pass: false,
10+
position: 0,
11+
failedAt: null,
12+
failure: null
13+
};
14+
var signalMatch = new RegExp(utils_1.signal);
15+
var match = signalMatch.exec(data);
16+
if (!!match) {
17+
var printed = data.toString().substring(0, match.index);
18+
if (!!printed.length) {
19+
var start = printed.substring(0, printed.length / 2);
20+
var end = printed.substring(printed.length / 2, printed.length);
21+
var message = '';
22+
if (start === end) {
23+
message = start;
24+
}
25+
else {
26+
message = printed;
27+
}
28+
handleLog(message);
29+
}
30+
var testResultString = data.toString().substring(match.index + 22);
31+
var testResult = JSON.parse(testResultString);
32+
if (!testResult.failed) {
33+
result.pass = false;
34+
result.failure = testResult.failures[0];
35+
result.position = findFailureTestPosition(files, testResult.failedAt);
36+
}
37+
else {
38+
result.pass = true;
39+
result.position = files.length;
40+
}
41+
}
42+
else {
43+
console.log('Error with test', data);
44+
}
45+
handleTestResult(result);
46+
});
47+
runner.stderr.on('error', function (data) {
48+
console.log('test error', data);
49+
});
50+
runner.on('close', function (code) {
51+
if (code === 1) {
52+
console.error('Error running test');
53+
}
54+
});
55+
}
56+
Object.defineProperty(exports, "__esModule", { value: true });
57+
exports.default = runner;
58+
function findFailureTestPosition(files, file) {
59+
for (var i = 0; i < files.length; i++) {
60+
if (files[i].indexOf(file) > -1) {
61+
return i;
62+
}
63+
}
64+
return 0;
65+
}

src/utils.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
exports.signal = '@@@CodeRoad Results@@@';
3+
function concatAll(items) {
4+
var results = [];
5+
items.forEach(function (subArray) {
6+
subArray.forEach(function (item) {
7+
results.push(item);
8+
});
9+
});
10+
return results;
11+
}
12+
exports.concatAll = concatAll;

0 commit comments

Comments
 (0)