Skip to content

Commit 36d56a2

Browse files
committed
move test write into test runner, test runner accepts string
1 parent 9f2e788 commit 36d56a2

25 files changed

+330
-230
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.7.0] -
6+
- improved console logger
7+
58
## [0.6.0] - 2016-04-01
69
- loaders are now internal to Atom-Coderoad
710

lib/.tmp.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/// coderoad-test-file
2+
3+
4+
5+
6+
var expect = require('chai').expect;
7+
8+
global.something = {
9+
some: 'data'
10+
};
11+
12+
// addOne
13+
function addOne(x) {
14+
return x + 1;
15+
}
16+
17+
// subtractOne
18+
function subtractOne(x) {
19+
return x - 1
20+
}
21+
22+
23+
describe('01 addOne', function() {
24+
25+
it('doesn\'t exist', function() {
26+
expect(addOne).to.be.defined;
27+
});
28+
29+
it('should take a parameter', function() {
30+
expect(addOne).to.have.length(1);
31+
});
32+
33+
it('doesn\'t return anything', function() {
34+
expect(addOne(1)).to.exist;
35+
});
36+
37+
it('should output a number', function() {
38+
expect(addOne(1)).to.be.a('number');
39+
});
40+
41+
it('doesn\'t add 1 + 1', function() {
42+
expect(addOne(1)).to.equal(2);
43+
expect(addOne(10)).to.equal(11);
44+
});
45+
46+
});
47+
48+
describe('02 subtractOne', function() {
49+
50+
it('doesn\'t exist', function () {
51+
expect(subtractOne).to.be.defined;
52+
});
53+
54+
it('should take a parameter', function() {
55+
expect(subtractOne).to.have.length(1);
56+
});
57+
58+
it('should output a number', function () {
59+
expect(subtractOne(1)).to.be.a('number');
60+
});
61+
62+
it('doesn\'t subtract 1', function() {
63+
expect(subtractOne(1)).to.equal(0);
64+
expect(subtractOne(10)).to.equal(9);
65+
});
66+
67+
});
68+

lib/constants.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
exports.signal = '@@@CodeRoad Results@@@';
4+
exports.testPath = path_1.join(__dirname, '.tmp.js');

lib/create-runner.js

-43
This file was deleted.

lib/exists.js

-17
This file was deleted.

lib/runner.js renamed to lib/index.js

+14-17
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
"use strict";
2-
var utils_1 = require('./utils');
3-
var create_runner_1 = require('./create-runner');
4-
function runner(testFile, config, handleResult) {
5-
var runner = create_runner_1.createRunner(config, testFile);
2+
var constants_1 = require('./constants');
3+
var runner_process_1 = require('./runner-process');
4+
var write_test_1 = require('./write-test');
5+
function runner(testString, config, handleResult) {
6+
write_test_1.default(testString);
7+
var runner = runner_process_1.default(config);
68
var final = null;
7-
var signalMatch = new RegExp(utils_1.signal);
8-
return new Promise(function (resolve, reject) {
9-
runner.stdout.on('data', function (data) {
9+
var signalMatch = new RegExp(constants_1.signal);
10+
return new Promise(function run(resolve, reject) {
11+
runner.stdout.on('data', function onData(data) {
1012
data = data.toString();
1113
var match = signalMatch.exec(data);
1214
if (!match) {
1315
try {
14-
console.dir(JSON.parse(JSON.stringify(data)));
16+
console.log(data);
1517
}
1618
catch (e) {
1719
console.log(data);
1820
}
1921
return;
2022
}
21-
var resultString = data.substring(match.index + utils_1.signal.length);
23+
var resultString = data.substring(match.index + constants_1.signal.length);
2224
var result = JSON.parse(JSON.stringify(resultString));
2325
if (typeof result === 'string') {
2426
result = JSON.parse(result);
@@ -37,16 +39,11 @@ function runner(testFile, config, handleResult) {
3739
final.completed = result.pass;
3840
handleResult(final);
3941
});
40-
runner.stderr.on('data', function (data) {
42+
runner.stderr.on('data', function onError(data) {
4143
console.log('test error', data.toString());
4244
});
43-
runner.on('close', function (code) {
44-
if (code === 0) {
45-
resolve(final);
46-
}
47-
else {
48-
resolve(final);
49-
}
45+
runner.on('close', function onClose(code) {
46+
resolve(final);
5047
});
5148
});
5249
}

lib/logger.js

Whitespace-only changes.

lib/reporter.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use strict";
2-
var utils_1 = require('./utils');
2+
var constants_1 = require('./constants');
33
exports = module.exports = reporter;
44
function reporter(runner) {
55
var result = {
66
passes: [],
77
failures: [],
8-
pass: true
8+
pass: true,
99
};
1010
runner.on('pass', function (test) {
1111
var title = test.fullTitle();
1212
var obj = getIndexAndTitle(title);
1313
result.passes.push({
1414
msg: "Task " + obj.index + " Complete",
15-
taskPosition: obj.index
15+
taskPosition: obj.index,
1616
});
1717
});
1818
runner.on('fail', function (test, err) {
@@ -21,12 +21,12 @@ function reporter(runner) {
2121
result.failures.push({
2222
msg: obj.msg,
2323
taskPosition: obj.index - 1,
24-
timedOut: test.timedOut
24+
timedOut: test.timedOut,
2525
});
2626
result.pass = false;
2727
});
2828
runner.on('end', function () {
29-
process.stdout.write(utils_1.signal + JSON.stringify(result, null, 2));
29+
process.stdout.write(constants_1.signal + JSON.stringify(result, null, 2));
3030
});
3131
function getIndexAndTitle(title) {
3232
var indexString = title.match(/^[0-9]+/);
@@ -35,7 +35,7 @@ function reporter(runner) {
3535
}
3636
return {
3737
index: parseInt(indexString[0], 10),
38-
msg: title.slice(indexString[0].length + 1)
38+
msg: title.slice(indexString[0].length + 1),
3939
};
4040
}
4141
}

lib/runner-process.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var child_process_1 = require('child_process');
4+
var node_file_exists_1 = require('node-file-exists');
5+
var constants_1 = require('./constants');
6+
function getMocha() {
7+
var mocha = path_1.join(__dirname, '..', '..', 'mocha', 'bin', 'mocha');
8+
if (!node_file_exists_1.default(mocha)) {
9+
mocha = path_1.join(__dirname, '..', 'node_modules', 'mocha', 'bin', 'mocha');
10+
if (!node_file_exists_1.default(mocha)) {
11+
var error = 'Error finding mocha';
12+
throw (error);
13+
}
14+
}
15+
return mocha;
16+
}
17+
function getNode() {
18+
if (process.platform === 'darwin' && process.resourcesPath) {
19+
return path_1.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
20+
}
21+
else if (process.platform.match(/win/)) {
22+
return 'node';
23+
}
24+
return process.execPath;
25+
}
26+
var node = getNode();
27+
var mocha = getMocha();
28+
function runnerProcess(config) {
29+
var options = {
30+
cwd: config.dir
31+
};
32+
if (options.env == null) {
33+
options.env = Object.create(process.env);
34+
}
35+
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
36+
options.env.DIR = config.dir;
37+
options.env.TUTORIAL_DIR = config.dir;
38+
options.env.TASK_POSITION = config.taskPosition;
39+
return child_process_1.spawn(node, [
40+
mocha,
41+
'--bail',
42+
'--harmony',
43+
'--no-colors',
44+
("--reporter=" + path_1.join(__dirname, 'reporter.js')),
45+
constants_1.testPath
46+
], options);
47+
}
48+
Object.defineProperty(exports, "__esModule", { value: true });
49+
exports.default = runnerProcess;

lib/utils.js

-2
This file was deleted.

lib/write-test.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"use strict";
2+
var fs_1 = require('fs');
3+
var constants_1 = require('./constants');
4+
function writeTest(output) {
5+
fs_1.writeFileSync(constants_1.testPath, output, 'utf8');
6+
}
7+
Object.defineProperty(exports, "__esModule", { value: true });
8+
exports.default = writeTest;

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "mocha-coderoad",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"description": "mocha test runner & reporter for atom-coderoad",
5-
"main": "lib/runner.js",
5+
"main": "lib/index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
@@ -26,7 +26,9 @@
2626
},
2727
"homepage": "https://github.com/coderoad/mocha-coderoad#readme",
2828
"dependencies": {
29-
"mocha": "2.4.5"
29+
"mocha": "2.4.5",
30+
"process-console-log": "^0.2.0",
31+
"node-file-exists": "^0.1.0"
3032
},
3133
"devDependencies": {
3234
"chai": "^3.5.0",

src/constants.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {join} from 'path';
2+
3+
export const signal = '@@@CodeRoad Results@@@';
4+
export const testPath = join(__dirname, '.tmp.js');

0 commit comments

Comments
 (0)