Skip to content

Commit 5922761

Browse files
committed
restructure mocha test runner
1 parent 68fa2a7 commit 5922761

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+541
-966
lines changed

‎lib/constants.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎lib/import-paths.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎lib/index.js

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,27 @@
11
"use strict";
2-
var constants_1 = require('./constants');
3-
var runner_process_1 = require('./runner-process');
4-
var write_test_1 = require('./write-test');
5-
var process_console_log_1 = require('process-console-log');
6-
function runner(_a) {
7-
var testString = _a.testString, config = _a.config, handleResult = _a.handleResult;
8-
write_test_1.default(config, testString);
9-
var runner = runner_process_1.default(config);
10-
var final = null;
11-
var signalMatch = new RegExp(constants_1.signal);
12-
return new Promise(function run(resolve, reject) {
13-
runner.stdout.on('data', function onData(data) {
14-
data = data.toString();
15-
var match = signalMatch.exec(data);
16-
if (!match) {
17-
try {
18-
process_console_log_1.parseLog(data);
19-
}
20-
catch (e) {
21-
process_console_log_1.parseLog(data);
22-
}
23-
return;
24-
}
25-
var resultString = data.substring(match.index + constants_1.signal.length);
26-
var result = JSON.parse(JSON.stringify(resultString));
27-
if (typeof result === 'string') {
28-
result = JSON.parse(result);
29-
}
30-
switch (result.pass) {
31-
case true:
32-
final = result.passes[result.passes.length - 1];
33-
break;
34-
case false:
35-
final = result.failures[0];
36-
break;
37-
default:
38-
console.log('error processing result: ', result);
39-
}
40-
final.change = final.taskPosition - config.taskPosition;
41-
final.pass = final.change > 0;
42-
final.completed = result.pass;
43-
handleResult(final);
44-
});
45-
runner.stderr.on('data', function onError(data) {
46-
console.log('test error', data.toString());
47-
});
48-
runner.on('close', function onClose(code) {
49-
resolve(final);
50-
});
2+
var writeTests_1 = require('./writeTests');
3+
var runner_1 = require('./runner');
4+
var testPath_1 = require('./testPath');
5+
var settings = {
6+
dir: null,
7+
tutorial: null,
8+
tests: '',
9+
step: 0,
10+
testPath: '',
11+
};
12+
exports.combineTests = function (options) {
13+
options.testPath = testPath_1.default(options);
14+
settings = Object.assign(settings, options);
15+
writeTests_1.default(options);
16+
};
17+
function start(_a) {
18+
var taskPosition = _a.taskPosition, handleResult = _a.handleResult;
19+
var dir = settings.dir;
20+
runner_1.default({
21+
dir: dir,
22+
taskPosition: taskPosition,
23+
handleResult: handleResult,
5124
});
5225
}
5326
Object.defineProperty(exports, "__esModule", { value: true });
54-
exports.default = runner;
27+
exports.default = start;

‎lib/reporter.js renamed to ‎lib/reporter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use strict";
2-
var constants_1 = require('./constants');
2+
var constants_1 = require('../helpers/constants');
33
exports = module.exports = reporter;
44
function reporter(runner) {
55
var result = {

‎lib/runner-process.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

‎lib/runner/constants.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@@@';

‎lib/runner/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
var start_runner_1 = require('./start-runner');
3+
var runner_process_1 = require('./runner-process');
4+
function runner(options) {
5+
var runner = runner_process_1.default(options);
6+
return start_runner_1.default(runner, options.handleResult);
7+
}
8+
Object.defineProperty(exports, "__esModule", { value: true });
9+
exports.default = runner;

‎lib/runner/paths/mocha.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
var node_file_exists_1 = require('node-file-exists');
3+
var path_1 = require('path');
4+
function getMocha() {
5+
var mocha = path_1.join(__dirname, '..', '..', '..', '..', 'mocha', 'bin', 'mocha');
6+
if (!node_file_exists_1.default(mocha)) {
7+
mocha = path_1.join(__dirname, '..', '..', '..', 'node_modules', 'mocha', 'bin', 'mocha');
8+
if (!node_file_exists_1.default(mocha)) {
9+
var error = 'Error finding mocha';
10+
throw (error);
11+
}
12+
}
13+
return mocha;
14+
}
15+
Object.defineProperty(exports, "__esModule", { value: true });
16+
exports.default = getMocha;

‎lib/runner/paths/node.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
function getNode() {
4+
if (process.platform === 'darwin' && process.resourcesPath) {
5+
return path_1.resolve(process.resourcesPath, '..', 'Frameworks', 'Atom Helper.app', 'Contents', 'MacOS', 'Atom Helper');
6+
}
7+
else if (process.platform.match(/win/)) {
8+
return 'node';
9+
}
10+
return process.execPath;
11+
}
12+
Object.defineProperty(exports, "__esModule", { value: true });
13+
exports.default = getNode;

‎lib/runner/runner-process.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var child_process_1 = require('child_process');
4+
var mocha_1 = require('./paths/mocha');
5+
var node_1 = require('./paths/node');
6+
var reporterPath = path_1.join(__dirname, '..', 'reporter', 'reporter.js');
7+
var node = node_1.default();
8+
var mocha = mocha_1.default();
9+
function spawnRunnerProcess(_a) {
10+
var dir = _a.dir, taskPosition = _a.taskPosition, testPath = _a.testPath;
11+
var options = {
12+
cwd: dir
13+
};
14+
if (options.env == null) {
15+
options.env = Object.create(process.env);
16+
}
17+
Object.assign(options.env, {
18+
ATOM_SHELL_INTERNAL_RUN_AS_NODE: 1,
19+
DIR: dir,
20+
TASK_POSITION: taskPosition,
21+
NODE_PATH: path_1.join(dir, 'node_modules'),
22+
});
23+
return child_process_1.spawn(node, [
24+
mocha,
25+
'--bail',
26+
'--harmony',
27+
'--no-colors',
28+
'--timeout=3000',
29+
'--require babelhook',
30+
("--reporter=" + reporterPath),
31+
testPath,
32+
], options);
33+
}
34+
Object.defineProperty(exports, "__esModule", { value: true });
35+
exports.default = spawnRunnerProcess;

‎lib/runner/start-runner.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
var process_console_log_1 = require('process-console-log');
3+
var constants_1 = require('./constants');
4+
function startRunner(runner, handleResult) {
5+
var final = null;
6+
var signalMatch = new RegExp(constants_1.signal);
7+
new Promise(function run(resolve, reject) {
8+
runner.stdout.on('data', function onData(data) {
9+
data = data.toString();
10+
var match = signalMatch.exec(data);
11+
if (!match) {
12+
try {
13+
process_console_log_1.parseLog(data);
14+
}
15+
catch (e) {
16+
process_console_log_1.parseLog(data);
17+
}
18+
return;
19+
}
20+
var resultString = data.substring(match.index + constants_1.signal.length);
21+
var result = JSON.parse(JSON.stringify(resultString));
22+
if (typeof result === 'string') {
23+
result = JSON.parse(result);
24+
}
25+
switch (result.pass) {
26+
case true:
27+
final = result.passes[result.passes.length - 1];
28+
break;
29+
case false:
30+
final = result.failures[0];
31+
break;
32+
default:
33+
console.log('error processing result: ', result);
34+
}
35+
final.change = final.taskPosition - config.taskPosition;
36+
final.pass = final.change > 0;
37+
final.completed = result.pass;
38+
handleResult(final);
39+
});
40+
runner.stderr.on('data', function onError(data) {
41+
console.log('test error', data.toString());
42+
});
43+
runner.on('close', function onClose(code) {
44+
resolve(final);
45+
});
46+
});
47+
}
48+
Object.defineProperty(exports, "__esModule", { value: true });
49+
exports.default = startRunner;

‎lib/testPath.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
function tmpTestName(_a) {
4+
var tutorial = _a.tutorial, step = _a.step;
5+
if (!tutorial || !tutorial.name || !tutorial.version || !step) {
6+
console.log('Error creating temporary test name');
7+
}
8+
return path_1.join(__dirname, '..', '..', '.tmp', tutorial.name, tutorial.version, step + '.js');
9+
}
10+
Object.defineProperty(exports, "__esModule", { value: true });
11+
exports.default = tmpTestName;

‎lib/write-test.js

Lines changed: 0 additions & 25 deletions
This file was deleted.
File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
var babelRegister = "require(\"babel-register\")({\"plugins\": [[\"transform-es2015-modules-commonjs\", {\"strict\": true,\"loose\": true}]]});";
3+
Object.defineProperty(exports, "__esModule", { value: true });
4+
exports.default = babelRegister;
File renamed without changes.

‎lib/writeTests/import-paths.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var importPathRegex = /require\(["'](BASE.+)["']\);?$|^import.+?\s?["'](BASE.+)["'];?$/;
4+
var relativePathRegex = /^BASE/;
5+
function fixImportPaths(dir, str) {
6+
var entries = new Set([]);
7+
var arr = str.split('\n').map(function (line) {
8+
var isMatch = line.match(importPathRegex);
9+
if (!isMatch) {
10+
return line;
11+
}
12+
var importPath = isMatch[1] || isMatch[2];
13+
if (importPath.match(relativePathRegex)) {
14+
var newPath = path_1.join(dir, importPath.replace('BASE', ''));
15+
entries.add(line.replace(importPath, newPath));
16+
return '';
17+
}
18+
return line;
19+
});
20+
return (Array.from(entries.keys())
21+
.concat(arr)
22+
.join('\n'));
23+
}
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.default = fixImportPaths;

‎lib/writeTests/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
var fs_1 = require('fs');
3+
var process_console_log_1 = require('process-console-log');
4+
var import_paths_1 = require('./import-paths');
5+
var exists_1 = require('./helpers/exists');
6+
var babel_register_1 = require('./helpers/babel-register');
7+
function writeTest(_a) {
8+
var dir = _a.dir, tests = _a.tests, testPath = _a.testPath;
9+
return new Promise(function (resolve, reject) {
10+
var fixImports = import_paths_1.default(dir, tests);
11+
var output = ''
12+
.concat(process_console_log_1.logger)
13+
.concat(exists_1.default(dir))
14+
.concat(babel_register_1.default)
15+
.concat(fixImports);
16+
fs_1.writeFile(testPath, output, function (err) {
17+
if (err) {
18+
reject(err);
19+
}
20+
resolve();
21+
});
22+
});
23+
}
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.default = writeTest;

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mocha-coderoad",
3-
"version": "0.9.3",
3+
"version": "0.10.0",
44
"description": "mocha test runner & reporter for atom-coderoad",
55
"keywords": [
66
"coderoad",

0 commit comments

Comments
 (0)