Skip to content

Commit dca668e

Browse files
committed
show ts files
1 parent ab19c12 commit dca668e

10 files changed

+179
-2
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
node_modules
22
tslint.json
3-
src/*.ts
3+
src/*.js
44
test/**/*.ts
55
tsconfig.json
66
typings
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
"name": "mocha-coderoad",
33
"version": "0.4.1",
44
"description": "mocha test runner & reporter for atom-coderoad",
5-
"main": "src/runner.js",
5+
"main": "lib/runner.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
99
"repository": {
1010
"type": "git",
1111
"url": "git+https://github.com/coderoad/mocha-coderoad.git"
1212
},
13+
"files": ["lib"],
1314
"keywords": [
1415
"coderoad",
1516
"mocha"

src/create-runner.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as path from 'path';
2+
const spawn = require('child_process').spawn;
3+
4+
export function createRunner(config, testFile) {
5+
// 1. node electron setup
6+
let options: any = {
7+
cwd: config.dir
8+
};
9+
if (options.env == null) {
10+
options.env = Object.create(process.env);
11+
}
12+
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
13+
options.env.DIR = config.dir;
14+
options.env.TUTORIAL_DIR = config.tutorialDir;
15+
options.env.TASK_POSITION = config.taskPosition;
16+
17+
// 2. get absolute path to node exec
18+
let node = null;
19+
if (process.platform === 'darwin' && process.resourcesPath) {
20+
node = path.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
21+
} else {
22+
node = process.execPath;
23+
}
24+
25+
// let runnerOptions = []; // setRunnerOptions(config);
26+
27+
// 3. spawn child process calling mocha test runner
28+
return spawn(node, [
29+
'/usr/local/bin/mocha',
30+
'--bail',
31+
'--harmony',
32+
'--no-colors',
33+
`--reporter=${path.join(__dirname, 'reporter') }`,
34+
testFile
35+
], options);
36+
// .concat(runnerOptions)
37+
38+
}
39+
40+
// function setRunnerOptions(config) {
41+
// let runnerOptions = [];
42+
// if (!config.testRunnerOptions) {
43+
// return runnerOptions;
44+
// }
45+
// if (config.testRunnerOptions.babel) {
46+
// require('babel-core');
47+
// let babelOptions = [
48+
// '--require babel-polyfill',
49+
// '--compilers js:babel-core/register'
50+
// ];
51+
// runnerOptions.concat(babelOptions);
52+
// }
53+
// return runnerOptions;
54+
// }

src/reporter.ts

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"use strict";
2+
import {signal} from './utils';
3+
4+
exports = module.exports = reporter;
5+
function reporter(runner) {
6+
let result = {
7+
passes: [],
8+
failures: [],
9+
pass: true
10+
};
11+
12+
runner.on('pass', function(test) {
13+
let title = test.fullTitle();
14+
let obj = getIndexAndTitle(title);
15+
// add pass
16+
result.passes.push({
17+
msg: `Task ${obj.index} Complete`,
18+
taskPosition: obj.index
19+
});
20+
});
21+
22+
runner.on('fail', function(test, err) {
23+
let title = test.fullTitle();
24+
let obj = getIndexAndTitle(title);
25+
// add fail
26+
result.failures.push({
27+
msg: obj.msg,
28+
taskPosition: obj.index - 1,
29+
// body: test.body,
30+
timedOut: test.timedOut
31+
// duration: test.duration
32+
});
33+
result.pass = false;
34+
});
35+
36+
runner.on('end', function() {
37+
// anything before the signal will be captured as log
38+
process.stdout.write(signal + JSON.stringify(result, null, 2));
39+
});
40+
41+
function getIndexAndTitle(title: string): { index: number, msg: string } {
42+
// tests prefixed with task number: "01 title"
43+
let indexString = title.match(/^[0-9]+/);
44+
if (!indexString) {
45+
throw 'Tests should begin with a number, indicating the task number';
46+
}
47+
return {
48+
index: parseInt(indexString[0]),
49+
msg: title.slice(indexString[0].length + 1)
50+
};
51+
}
52+
53+
}

src/runner.ts

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {signal} from './utils';
2+
import {createRunner} from './create-runner';
3+
4+
interface Result {
5+
msg: string;
6+
taskPosition: number;
7+
pass: boolean;
8+
change: number;
9+
failedAtFile?: string;
10+
body?: string;
11+
timedOut?: boolean;
12+
duration?: number;
13+
};
14+
15+
export default function runner(testFile, config: Config, handleResult, handleLog) {
16+
17+
let runner = createRunner(config, testFile);
18+
var final = null;
19+
let signalMatch = new RegExp(signal);
20+
21+
return new Promise((resolve, reject) => {
22+
runner.stdout.on('data', function(data): void {
23+
24+
data = data.toString();
25+
// parse only final output data
26+
let match = signalMatch.exec(data); // 0
27+
28+
if (!match) {
29+
handleLog(data);
30+
return;
31+
}
32+
33+
/* Result */
34+
// transform string result into object
35+
let resultString = data.substring(match.index + signal.length);
36+
let result = JSON.parse(JSON.stringify(resultString));
37+
// why parse twice? I don't know, but it works
38+
if (typeof result === 'string') {
39+
result = JSON.parse(result);
40+
}
41+
42+
if (!result.pass) {
43+
// fail: return first failure
44+
final = result.failures[0];
45+
} else {
46+
// pass
47+
final = result.passes[result.passes.length - 1];
48+
}
49+
50+
final.change = final.taskPosition - config.taskPosition;
51+
final.pass = final.change > 0;
52+
// return result to atom-coderoad
53+
handleResult(final);
54+
});
55+
56+
runner.stderr.on('data', function(data) {
57+
console.log('test error', data.toString());
58+
});
59+
60+
runner.on('close', function(code) {
61+
if (code === 0) {
62+
resolve(final);
63+
} else {
64+
resolve(final);
65+
}
66+
});
67+
});
68+
}

src/utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var signal = '@@@CodeRoad Results@@@';

0 commit comments

Comments
 (0)