Skip to content

Commit aeae944

Browse files
committed
setup runner and test-run reducers
1 parent 1d3a32e commit aeae944

File tree

11 files changed

+238
-0
lines changed

11 files changed

+238
-0
lines changed

‎lib/modules/runner/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
var RUNNER_SET = 'RUNNER_SET';
3+
function runnerSet(name) {
4+
return { type: RUNNER_SET, payload: { name: name } };
5+
}
6+
exports.runnerSet = runnerSet;
7+
var r = function () {
8+
alert('Runner not installed. Run "npm install"');
9+
};
10+
function runner(runner, action) {
11+
if (runner === void 0) { runner = r; }
12+
switch (action.type) {
13+
case RUNNER_SET:
14+
var name_1 = action.payload.name;
15+
console.log(name_1);
16+
return runner;
17+
default:
18+
return runner;
19+
}
20+
}
21+
exports.runner = runner;

‎lib/modules/test-run/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
var types_1 = require('../types');
3+
var run_1 = require('./run');
4+
function runTest(testRun, action) {
5+
if (testRun === void 0) { testRun = false; }
6+
switch (action.type) {
7+
case types_1.TEST_RUN:
8+
var _a = action.payload, taskTests = _a.taskTests, dir = _a.dir, tutorial = _a.tutorial, taskPosition = _a.taskPosition;
9+
return run_1.default(taskTests, dir, tutorial, taskPosition);
10+
default:
11+
return testRun;
12+
}
13+
}
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.default = runTest;

‎lib/modules/test-run/parse-loaders.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var fs_1 = require('fs');
4+
var comments = {
5+
py: '#',
6+
js: '\/{2,3}',
7+
};
8+
function loaderRegex(fileType) {
9+
var comment = '\/{2,3}';
10+
if (comments[fileType]) {
11+
comment = comments[fileType];
12+
}
13+
return new RegExp("^" + comment + " ?load\\(['\"](.+)['\"](, ?true)?\\)", 'm');
14+
}
15+
function parseLoaders(data, fileType, tutorial, dir) {
16+
var i = -1;
17+
var lines = data.split('\n');
18+
var filesLoaded = [];
19+
var loaderMatch = loaderRegex(fileType);
20+
while (i < lines.length - 1) {
21+
i += 1;
22+
var loader = lines[i].match(loaderMatch);
23+
if (loader) {
24+
var fileToLoad = loader[1];
25+
if (filesLoaded.indexOf(fileToLoad) > -1) {
26+
console.log("\"" + fileToLoad + "\" already loaded.");
27+
continue;
28+
}
29+
var pathToFile = null;
30+
if (loader[2]) {
31+
pathToFile = path_1.normalize(path_1.join(tutorial.config.dir, fileToLoad));
32+
}
33+
else {
34+
pathToFile = path_1.normalize(path_1.join(dir, fileToLoad));
35+
}
36+
try {
37+
lines[i] = fs_1.readFileSync(pathToFile, 'utf8');
38+
}
39+
catch (e) {
40+
var message = 'File not found: ' + pathToFile;
41+
lines[i] = message;
42+
console.log(message);
43+
}
44+
}
45+
}
46+
return lines.join('\n');
47+
}
48+
Object.defineProperty(exports, "__esModule", { value: true });
49+
exports.default = parseLoaders;

‎lib/modules/test-run/run.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
var parse_loaders_1 = require('./parse-loaders');
3+
function handleResult() { return; }
4+
function runTaskTests(taskTests, dir, taskPosition) {
5+
if (taskPosition === void 0) { taskPosition = 0; }
6+
var tests = taskTests;
7+
if (tests && tests.length) {
8+
var tutorialConfig = tutorial.config;
9+
var output = parse_loaders_1.default(tests, tutorialConfig.testSuffix, tutorial, dir);
10+
var config = {
11+
dir: dir,
12+
tutorialDir: tutorialConfig.dir,
13+
taskPosition: taskPosition
14+
};
15+
tutorialConfig.run(output, config, handleResult);
16+
}
17+
return true;
18+
}
19+
Object.defineProperty(exports, "__esModule", { value: true });
20+
exports.default = runTaskTests;

‎src/actions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export {editorMarkdownOpen, editorTestOpen, editorPjOpen} from './modules/editor-paths';
22
export {pageSet} from './modules/page-position';
33
export {pjSave, pjLoad} from './modules/package-json';
4+
export {runnerSet} from './modules/runner';
45
export {setupVerify, setupPackage} from './modules/setup';
56
export {
67
tutorialInit, tutorialLoad, tutorialBuild, tutorialPageAdd,

‎src/modules/runner/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const RUNNER_SET = 'RUNNER_SET';
2+
3+
export function runnerSet(name: string) {
4+
return { type: RUNNER_SET, payload: { name } };
5+
}
6+
7+
const r = function() {
8+
alert('Runner not installed. Run "npm install"');
9+
};
10+
11+
export function reducer(runner = r, action: Action) {
12+
switch (action.type) {
13+
case RUNNER_SET:
14+
const {name} = action.payload;
15+
console.log(name);
16+
return runner;
17+
default:
18+
return runner;
19+
}
20+
}

‎src/modules/test-run/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {TEST_RUN, TEST_COMPLETE} from '../types';
2+
import runTaskTests from './run';
3+
4+
export default function runTest(
5+
testRun = false, action: Action
6+
): boolean {
7+
switch (action.type) {
8+
9+
case TEST_RUN:
10+
const {taskTests, dir, tutorial, taskPosition} = action.payload;
11+
return runTaskTests(taskTests, dir, tutorial, taskPosition);
12+
13+
default:
14+
return testRun;
15+
}
16+
}

‎src/modules/test-run/parse-loaders.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {normalize, join} from 'path';
2+
import {readFileSync} from 'fs';
3+
4+
// TODO: load comments from core-coderoad
5+
6+
const comments = {
7+
py: '#',
8+
js: '\/{2,3}',
9+
};
10+
11+
function loaderRegex(fileType: string): RegExp {
12+
let comment = '\/{2,3}';
13+
if (comments[fileType]) {
14+
comment = comments[fileType];
15+
}
16+
return new RegExp(`^${comment} ?load\\(['"](.+)['"](\, ?true)?\\)`, 'm');
17+
}
18+
19+
export default function parseLoaders(
20+
data: string, fileType: string, tutorial: CR.Tutorial, dir: string
21+
): string {
22+
23+
// loop over lines and add editor files
24+
let i = -1;
25+
let lines = data.split('\n');
26+
27+
let filesLoaded = [];
28+
let loaderMatch = loaderRegex(fileType);
29+
30+
while (i < lines.length - 1) {
31+
i += 1;
32+
let loader: string[] = lines[i].match(loaderMatch);
33+
34+
if (loader) {
35+
// loader found
36+
let fileToLoad: string = loader[1];
37+
38+
if (filesLoaded.indexOf(fileToLoad) > -1) {
39+
console.log(`"${fileToLoad}" already loaded.`);
40+
continue;
41+
}
42+
43+
let pathToFile: string = null;
44+
if (loader[2]) {
45+
// path to file from config specified dir
46+
pathToFile = normalize(join(tutorial.config.dir, fileToLoad));
47+
} else {
48+
// path to file from working directory
49+
pathToFile = normalize(join(dir, fileToLoad));
50+
}
51+
52+
try {
53+
lines[i] = readFileSync(pathToFile, 'utf8');
54+
} catch (e) {
55+
let message = 'File not found: ' + pathToFile;
56+
lines[i] = message;
57+
console.log(message);
58+
}
59+
}
60+
}
61+
return lines.join('\n');
62+
}

‎src/modules/test-run/run.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {join} from 'path';
2+
import parseLoaders from './parse-loaders';
3+
4+
// placeholder empty function
5+
function handleResult() { return; }
6+
7+
export default function runTaskTests(
8+
taskTests: string, dir: string, taskPosition = 0
9+
): boolean {
10+
const tests: string = taskTests;
11+
12+
if (tests && tests.length) {
13+
const tutorialConfig: Tutorial.Config = tutorial.config;
14+
const output = parseLoaders(
15+
tests, tutorialConfig.testSuffix, tutorial, dir
16+
);
17+
18+
const config: Test.Config = {
19+
dir,
20+
tutorialDir: tutorialConfig.dir,
21+
taskPosition
22+
};
23+
24+
// call test runner
25+
tutorialConfig.run(output, config, handleResult);
26+
}
27+
return true;
28+
}

‎src/reducers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {reducer as form} from 'redux-form';
55
import {reducer as pagePosition} from './modules/page-position';
66
import {reducer as checks} from './modules/setup';
77
import {reducer as packageJson} from './modules/package-json';
8+
import {reducer as runner} from './modules/runner';
89
import {reducer as tutorial} from './modules/tutorial';
910
import {reducer as windowToggle} from './modules/window';
1011
import {reducer as validation} from './modules/validate-tutorial';
@@ -22,4 +23,5 @@ export default combineReducers({
2223
alert, checks, editor, dir, form,
2324
packageJson, pagePosition, route,
2425
tutorial, updated, validation, windowToggle,
26+
runner,
2527
});

‎tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"src/modules/page-position/index.ts",
4343
"src/modules/page-position/reducer.ts",
4444
"src/modules/page-position/types.ts",
45+
"src/modules/runner/index.ts",
4546
"src/modules/setup/actions.ts",
4647
"src/modules/setup/index.ts",
4748
"src/modules/setup/reducer.ts",
@@ -50,6 +51,9 @@
5051
"src/modules/setup/utils/action-system.ts",
5152
"src/modules/setup/utils/check-system.ts",
5253
"src/modules/setup/utils/verify.ts",
54+
"src/modules/test-run/index.ts",
55+
"src/modules/test-run/parse-loaders.ts",
56+
"src/modules/test-run/run.ts",
5357
"src/modules/tutorial/actions.ts",
5458
"src/modules/tutorial/index.ts",
5559
"src/modules/tutorial/reducer.ts",

0 commit comments

Comments
 (0)