Skip to content

Commit 1edef30

Browse files
committed
init
0 parents  commit 1edef30

10 files changed

+273
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
tslint.json
3+
src/*.js
4+
test/**/*.ts
5+
tsconfig.json
6+
typings
7+
tsd.json

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Mocha CodeRoad
2+
3+
[Atom-CodeRoad](https://github.com/coderoad/atom-coderoad) test runner & reporter.
4+
5+
npm install mocha-coderoad

lib/create-runner.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
var path = require('path');
3+
var spawn = require('child_process').spawn;
4+
function createRunner(config, testFile) {
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+
options.env.TASK_POSITION = config.taskPosition;
15+
var node = null;
16+
if (process.platform === 'darwin' && process.resourcesPath) {
17+
node = path.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
18+
}
19+
else {
20+
node = process.execPath;
21+
}
22+
var ava = path.join(__dirname, '..', '..', 'ava', 'cli');
23+
var tapJson = path.join('..', '..', 'tap-json', 'bin', 'tap-json');
24+
return spawn(node, [
25+
ava,
26+
'--fail-fast',
27+
'--tap | ' + tapJson,
28+
testFile
29+
], options);
30+
}
31+
exports.createRunner = createRunner;

lib/runner.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"use strict";
2+
var utils_1 = require('./utils');
3+
var create_runner_1 = require('./create-runner');
4+
;
5+
function runner(testFile, config, handleResult, handleLog) {
6+
var runner = create_runner_1.createRunner(config, testFile);
7+
var final = null;
8+
var signalMatch = new RegExp(utils_1.signal);
9+
return new Promise(function (resolve, reject) {
10+
runner.stdout.on('data', function (data) {
11+
data = data.toString();
12+
var result = JSON.parse(JSON.stringify(data));
13+
if (typeof result === 'string') {
14+
result = JSON.parse(result);
15+
}
16+
var finish = result.asserts[result.asserts.length - 1];
17+
var obj = getIndexAndTitle(finish.comment);
18+
if (result.stats.failures > 0) {
19+
final = {
20+
msg: obj.msg,
21+
taskPosition: obj.index - 1
22+
};
23+
}
24+
else {
25+
final = {
26+
msg: "Task " + obj.index + " Complete",
27+
taskPosition: obj.index
28+
};
29+
}
30+
final.change = final.taskPosition - config.taskPosition;
31+
final.pass = final.change > 0;
32+
handleResult(final);
33+
});
34+
runner.stderr.on('data', function (data) {
35+
console.log('test error', data.toString());
36+
});
37+
runner.on('close', function (code) {
38+
if (code === 0) {
39+
resolve(final);
40+
}
41+
else {
42+
resolve(final);
43+
}
44+
});
45+
});
46+
}
47+
Object.defineProperty(exports, "__esModule", { value: true });
48+
exports.default = runner;
49+
function getIndexAndTitle(title) {
50+
var indexString = title.match(/^[0-9]+/);
51+
if (!indexString) {
52+
throw 'Tests should begin with a number, indicating the task number';
53+
}
54+
return {
55+
index: parseInt(indexString[0]),
56+
msg: title.slice(indexString[0].length + 1)
57+
};
58+
}

lib/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.signal = '@@@CodeRoad Results@@@';

package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "ava-coderoad",
3+
"version": "0.1.0",
4+
"description": "ava test runner & reporter for atom-coderoad",
5+
"main": "lib/runner.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/coderoad/ava-coderoad.git"
12+
},
13+
"files": ["lib"],
14+
"keywords": [
15+
"coderoad",
16+
"ava"
17+
],
18+
"author": "Shawn McKay <[email protected]>",
19+
"license": "ISC",
20+
"bugs": {
21+
"url": "https://github.com/coderoad/ava-coderoad/issues"
22+
},
23+
"homepage": "https://github.com/coderoad/ava-coderoad#readme",
24+
"dependencies": {
25+
"ava": "0.12.0",
26+
"tap-json": "0.1.1"
27+
},
28+
"devDependencies": {}
29+
}

src/create-runner.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
let ava = path.join(__dirname, '..', '..', 'ava', 'cli');
27+
let tapJson = path.join('..', '..', 'tap-json', 'bin', 'tap-json');
28+
29+
// 3. spawn child process calling ava test runner
30+
return spawn(node, [
31+
// into shared node_modules directory
32+
ava,
33+
'--fail-fast',
34+
'--tap | ' + tapJson,
35+
testFile
36+
], options);
37+
38+
}

src/runner.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
timedOut?: boolean;
10+
};
11+
12+
interface Config {
13+
dir: string;
14+
taskPosition: number;
15+
}
16+
17+
export default function runner(testFile, config: Config, handleResult, handleLog) {
18+
19+
let runner = createRunner(config, testFile);
20+
var final = null;
21+
let signalMatch = new RegExp(signal);
22+
23+
return new Promise((resolve, reject) => {
24+
runner.stdout.on('data', function(data): void {
25+
26+
data = data.toString();
27+
28+
// parse only final output data
29+
// let match = signalMatch.exec(data); // 0
30+
//
31+
// if (!match) {
32+
// handleLog(data);
33+
// return;
34+
// }
35+
//
36+
// /* Result */
37+
// // transform string result into object
38+
// let resultString = data.substring(match.index + signal.length);
39+
let result = JSON.parse(JSON.stringify(data));
40+
// // why parse twice? I don't know, but it works
41+
if (typeof result === 'string') {
42+
result = JSON.parse(result);
43+
}
44+
45+
let finish = result.asserts[result.asserts.length - 1];
46+
let obj = getIndexAndTitle(finish.comment);
47+
48+
if (result.stats.failures > 0) {
49+
// fail: return first failure
50+
final = {
51+
msg: obj.msg,
52+
taskPosition: obj.index - 1
53+
};
54+
} else {
55+
// pass
56+
final = {
57+
msg: `Task ${obj.index} Complete`,
58+
taskPosition: obj.index
59+
};
60+
}
61+
final.change = final.taskPosition - config.taskPosition;
62+
final.pass = final.change > 0;
63+
// // return result to atom-coderoad
64+
handleResult(final);
65+
});
66+
67+
runner.stderr.on('data', function(data) {
68+
console.log('test error', data.toString());
69+
});
70+
71+
runner.on('close', function(code) {
72+
if (code === 0) {
73+
resolve(final);
74+
} else {
75+
resolve(final);
76+
}
77+
});
78+
});
79+
}
80+
81+
function getIndexAndTitle(title: string): { index: number, msg: string } {
82+
// tests prefixed with task number: "01 title"
83+
let indexString = title.match(/^[0-9]+/);
84+
if (!indexString) {
85+
throw 'Tests should begin with a number, indicating the task number';
86+
}
87+
return {
88+
index: parseInt(indexString[0]),
89+
msg: title.slice(indexString[0].length + 1)
90+
};
91+
}

src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var signal = '@@@CodeRoad Results@@@';

test/runner.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import test from 'ava';
2+
3+
test('01 Pass', t => {
4+
t.same(1, 1);
5+
});
6+
7+
test('01 Test Pass', t => {
8+
t.same([1, 2], [1, 2]);
9+
});
10+
11+
console.log('something');

0 commit comments

Comments
 (0)