Skip to content

Commit f834fd2

Browse files
committed
release 0.5.1
1 parent 5110f39 commit f834fd2

12 files changed

+216
-154
lines changed

CHANGELOG.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
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.4.4] - 2016-02-26
6-
### Added
7-
- `snippets.cson`, for quickly generating tests
5+
## [0.5.1] - 2016-03-11
6+
- optimizations
7+
- handle older NPM versions
8+
- fix for Windows
89

910
## [0.5.0] - 2016-02-29
10-
### Added
1111
- return `result.completed`: boolean, if all tests pass
12+
13+
## [0.4.4] - 2016-02-26
14+
- `snippets.cson`, for quickly generating tests

lib/create-runner.js

+43-32
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
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 mocha = path.join(__dirname, '..', '..', 'mocha', 'bin', 'mocha');
23-
return spawn(node, [
24-
mocha,
25-
'--bail',
26-
'--harmony',
27-
'--no-colors',
28-
("--reporter=" + path.join(__dirname, 'reporter')),
29-
testFile
30-
], options);
31-
}
32-
exports.createRunner = createRunner;
1+
"use strict";
2+
var path = require('path');
3+
var exists_1 = require('./exists');
4+
var spawn = require('child_process').spawn;
5+
var node = null;
6+
if (process.platform === 'darwin' && process.resourcesPath) {
7+
node = path.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
8+
}
9+
else if (process.platform.match(/win/)) {
10+
node = 'node';
11+
}
12+
else {
13+
node = process.execPath;
14+
}
15+
var mocha = path.join(__dirname, '..', '..', 'mocha', 'bin', 'mocha');
16+
if (!exists_1.fileExists(mocha)) {
17+
mocha = path.join(__dirname, '..', 'node_modules', 'mocha', 'bin', 'mocha');
18+
if (!exists_1.fileExists(mocha)) {
19+
var error = 'Error finding mocha';
20+
throw (error);
21+
}
22+
}
23+
function createRunner(config, testFile) {
24+
var options = {
25+
cwd: config.dir
26+
};
27+
if (options.env == null) {
28+
options.env = Object.create(process.env);
29+
}
30+
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
31+
options.env.DIR = config.dir;
32+
options.env.TUTORIAL_DIR = config.tutorialDir;
33+
options.env.TASK_POSITION = config.taskPosition;
34+
return spawn(node, [
35+
mocha,
36+
'--bail',
37+
'--harmony',
38+
'--no-colors',
39+
("--reporter=" + path.join(__dirname, 'reporter.js')),
40+
testFile
41+
], options);
42+
}
43+
exports.createRunner = createRunner;

lib/exists.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"use strict";
2+
var fs = require('fs');
3+
function fileExists(path) {
4+
try {
5+
fs.accessSync(path, fs.R_OK | fs.W_OK);
6+
}
7+
catch (e) {
8+
if (e) {
9+
if (e.code !== 'ENOENT') {
10+
console.log(e);
11+
}
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
exports.fileExists = fileExists;

lib/reporter.js

+41-41
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
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-
pass: true
9-
};
10-
runner.on('pass', function (test) {
11-
var title = test.fullTitle();
12-
var obj = getIndexAndTitle(title);
13-
result.passes.push({
14-
msg: "Task " + obj.index + " Complete",
15-
taskPosition: obj.index
16-
});
17-
});
18-
runner.on('fail', function (test, err) {
19-
var title = test.fullTitle();
20-
var obj = getIndexAndTitle(title);
21-
result.failures.push({
22-
msg: obj.msg,
23-
taskPosition: obj.index - 1,
24-
timedOut: test.timedOut
25-
});
26-
result.pass = false;
27-
});
28-
runner.on('end', function () {
29-
process.stdout.write(utils_1.signal + JSON.stringify(result, null, 2));
30-
});
31-
function getIndexAndTitle(title) {
32-
var indexString = title.match(/^[0-9]+/);
33-
if (!indexString) {
34-
throw 'Tests should begin with a number, indicating the task number';
35-
}
36-
return {
37-
index: parseInt(indexString[0]),
38-
msg: title.slice(indexString[0].length + 1)
39-
};
40-
}
41-
}
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+
pass: true
9+
};
10+
runner.on('pass', function (test) {
11+
var title = test.fullTitle();
12+
var obj = getIndexAndTitle(title);
13+
result.passes.push({
14+
msg: "Task " + obj.index + " Complete",
15+
taskPosition: obj.index
16+
});
17+
});
18+
runner.on('fail', function (test, err) {
19+
var title = test.fullTitle();
20+
var obj = getIndexAndTitle(title);
21+
result.failures.push({
22+
msg: obj.msg,
23+
taskPosition: obj.index - 1,
24+
timedOut: test.timedOut
25+
});
26+
result.pass = false;
27+
});
28+
runner.on('end', function () {
29+
process.stdout.write(utils_1.signal + JSON.stringify(result, null, 2));
30+
});
31+
function getIndexAndTitle(title) {
32+
var indexString = title.match(/^[0-9]+/);
33+
if (!indexString) {
34+
throw 'Tests should begin with a number, indicating the task number';
35+
}
36+
return {
37+
index: parseInt(indexString[0], 10),
38+
msg: title.slice(indexString[0].length + 1)
39+
};
40+
}
41+
}

lib/runner.js

+49-46
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
1-
"use strict";
2-
var utils_1 = require('./utils');
3-
var create_runner_1 = require('./create-runner');
4-
function runner(testFile, config, handleResult, handleLog) {
5-
var runner = create_runner_1.createRunner(config, testFile);
6-
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) {
10-
data = data.toString();
11-
var match = signalMatch.exec(data);
12-
if (!match) {
13-
handleLog(data);
14-
return;
15-
}
16-
var resultString = data.substring(match.index + utils_1.signal.length);
17-
var result = JSON.parse(JSON.stringify(resultString));
18-
if (typeof result === 'string') {
19-
result = JSON.parse(result);
20-
}
21-
if (!result.pass) {
22-
final = result.failures[0];
23-
}
24-
else {
25-
final = result.passes[result.passes.length - 1];
26-
}
27-
final.change = final.taskPosition - config.taskPosition;
28-
final.pass = final.change > 0;
29-
final.completed = result.pass;
30-
handleResult(final);
31-
});
32-
runner.stderr.on('data', function (data) {
33-
console.log('test error', data.toString());
34-
});
35-
runner.on('close', function (code) {
36-
if (code === 0) {
37-
resolve(final);
38-
}
39-
else {
40-
resolve(final);
41-
}
42-
});
43-
});
44-
}
45-
Object.defineProperty(exports, "__esModule", { value: true });
46-
exports.default = runner;
1+
"use strict";
2+
var utils_1 = require('./utils');
3+
var create_runner_1 = require('./create-runner');
4+
function runner(testFile, config, handleResult, handleLog) {
5+
var runner = create_runner_1.createRunner(config, testFile);
6+
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) {
10+
data = data.toString();
11+
var match = signalMatch.exec(data);
12+
if (!match) {
13+
handleLog(data);
14+
return;
15+
}
16+
var resultString = data.substring(match.index + utils_1.signal.length);
17+
var result = JSON.parse(JSON.stringify(resultString));
18+
if (typeof result === 'string') {
19+
result = JSON.parse(result);
20+
}
21+
if (result.pass) {
22+
final = result.passes[result.passes.length - 1];
23+
}
24+
else if (result.pass === false) {
25+
final = result.failures[0];
26+
}
27+
else {
28+
console.log('error processing result: ', result);
29+
}
30+
final.change = final.taskPosition - config.taskPosition;
31+
final.pass = final.change > 0;
32+
final.completed = result.pass;
33+
handleResult(final);
34+
});
35+
runner.stderr.on('data', function (data) {
36+
console.log('test error', data.toString());
37+
});
38+
runner.on('close', function (code) {
39+
if (code === 0) {
40+
resolve(final);
41+
}
42+
else {
43+
resolve(final);
44+
}
45+
});
46+
});
47+
}
48+
Object.defineProperty(exports, "__esModule", { value: true });
49+
exports.default = runner;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mocha-coderoad",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "mocha test runner & reporter for atom-coderoad",
55
"main": "lib/runner.js",
66
"scripts": {

src/create-runner.ts

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,53 @@
11
import * as path from 'path';
2+
import {fileExists} from './exists';
23
const spawn = require('child_process').spawn;
34

4-
export function createRunner(config, testFile) {
5-
// 1. node electron setup
5+
// get absolute path to node exec
6+
let node = null;
7+
if (process.platform === 'darwin' && process.resourcesPath) {
8+
node = path.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
9+
} else if (process.platform.match(/win/)) {
10+
node = 'node';
11+
} else {
12+
node = process.execPath;
13+
}
14+
15+
// mocha, location may differ based on NPM version
16+
let mocha = path.join(__dirname, '..', '..', 'mocha', 'bin', 'mocha');
17+
if (!fileExists(mocha)) {
18+
mocha = path.join(__dirname, '..', 'node_modules', 'mocha', 'bin', 'mocha');
19+
if (!fileExists(mocha)) {
20+
let error = 'Error finding mocha';
21+
throw (error);
22+
}
23+
}
24+
25+
export function createRunner(config: CR.Config, testFile: string) {
26+
// node electron setup
627
let options: any = {
728
cwd: config.dir
829
};
930
if (options.env == null) {
1031
options.env = Object.create(process.env);
1132
}
1233
options.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE = 1;
34+
// options.env.ELECTRON_RUN_AS_NODE = 1;
35+
1336
options.env.DIR = config.dir;
1437
options.env.TUTORIAL_DIR = config.tutorialDir;
1538
options.env.TASK_POSITION = config.taskPosition;
1639

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 mocha = path.join(__dirname, '..', '..', 'mocha', 'bin', 'mocha');
27-
// let mocha = path.join(__dirname, '..', 'node_modules', 'mocha', 'bin', 'mocha');
28-
29-
// 3. spawn child process calling mocha test runner
40+
// spawn child process calling mocha test runner
3041
return spawn(node, [
31-
// into shared node_modules directory
42+
// into shared node_modules directory
3243
mocha,
3344
'--bail',
3445
'--harmony',
3546
'--no-colors',
36-
`--reporter=${path.join(__dirname, 'reporter') }`,
47+
`--reporter=${path.join(__dirname, 'reporter.js') }`,
3748
testFile
3849
], options);
3950
// .concat(runnerOptions)
40-
4151
}
4252

4353
// function setRunnerOptions(config) {

src/exists.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as fs from 'fs';
2+
3+
export function fileExists(path: string): boolean {
4+
try {
5+
fs.accessSync(path, fs.R_OK | fs.W_OK);
6+
} catch (e) {
7+
if (e) {
8+
if (e.code !== 'ENOENT') {
9+
console.log(e);
10+
}
11+
return false;
12+
}
13+
}
14+
return true;
15+
}

0 commit comments

Comments
 (0)