Skip to content

Commit 1355b82

Browse files
committed
load files above tests
1 parent 2a99028 commit 1355b82

File tree

10 files changed

+120
-72
lines changed

10 files changed

+120
-72
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"use strict";
2+
var path = require('path');
3+
var fs = require('fs');
4+
var supportedFileTypes = ['js', 'jsx', 'ts', 'py'];
5+
var js = /^\/\/\s?load\(['"`](.+)['"`]\)$/;
6+
var loaderMatch = {
7+
js: js,
8+
ts: js,
9+
jsx: js,
10+
py: /\#\s?load\(['"`](.+)['"`]\)$/
11+
};
12+
function parseLoaders(data, fileType) {
13+
if (supportedFileTypes.indexOf(fileType) < 0) {
14+
console.log("File type \"" + fileType + "\" not supported.");
15+
return '';
16+
}
17+
var imports = '';
18+
var i = 0;
19+
var lines = data.split('\n');
20+
while (i < lines.length - 1) {
21+
i += 1;
22+
var loader = lines[i].match(loaderMatch[fileType]);
23+
if (loader) {
24+
var fileToLoad = loader[1];
25+
var pathToFile = path.normalize(path.join(window.coderoad.dir, fileToLoad));
26+
var file = fs.readFileSync(pathToFile, 'utf8');
27+
imports += file;
28+
}
29+
}
30+
var output = null;
31+
if (imports.length > 0) {
32+
output = imports + '\n'.concat(data);
33+
}
34+
else {
35+
output = data;
36+
}
37+
return output;
38+
}
39+
Object.defineProperty(exports, "__esModule", { value: true });
40+
exports.default = parseLoaders;

lib/reducers/run-tests/run.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
"use strict";
22
var test_result_1 = require('./test-result');
33
var _base_1 = require('../../_base');
4+
var fs = require('fs');
5+
var path = require('path');
6+
var parse_loaders_1 = require('./parse-loaders');
47
function runTaskTests(setup) {
5-
var tests = _base_1.store.getState().taskTests;
6-
if (tests && tests.length) {
8+
var testFile = _base_1.store.getState().taskTests;
9+
if (testFile) {
710
var config = window.coderoad;
811
config.taskPosition = _base_1.store.getState().taskPosition;
9-
window.coderoad.runner(tests, config, test_result_1.handleResult);
12+
var fileType = testFile.substr(testFile.lastIndexOf('.') + 1, testFile.length) || null;
13+
var tests = fs.readFileSync(testFile, 'utf8');
14+
var output = parse_loaders_1.default(tests, fileType);
15+
var target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, "_tmp" + window.coderoad.suffix);
16+
fs.writeFileSync(target, output, 'utf8');
17+
window.coderoad.runner(target, config, test_result_1.handleResult);
1018
}
1119
return true;
1220
}

lib/reducers/task-tests/loader.js

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
"use strict";
22
var fs = require('fs');
3-
var path = require('path');
4-
var loaderMatch = {
5-
js: /^\/\/\s?load\(['"`](.+)['"`]\)$/,
6-
py: /\#\s?load\(['"`](.+)['"`]\)$/
7-
};
83
function unlink(targetFile) {
94
return new Promise(function (resolve) {
105
if (fs.existsSync(targetFile)) {
@@ -13,34 +8,10 @@ function unlink(targetFile) {
138
resolve();
149
});
1510
}
16-
function parseLoaders(data, fileType) {
17-
var lines = data.split('\n');
18-
var imports = '';
19-
var i = 0;
20-
while (i < lines.length - 1) {
21-
i += 1;
22-
var loader = lines[i].match(loaderMatch[fileType]);
23-
if (loader) {
24-
var fileToLoad = loader[1];
25-
var pathToFile = path.normalize(path.join(window.coderoad.dir, fileToLoad));
26-
imports.concat(fs.readFileSync(pathToFile, 'utf8'));
27-
}
28-
}
29-
var output = null;
30-
if (imports.length > 0) {
31-
output = imports + '\n'.concat(data);
32-
}
33-
else {
34-
output = data;
35-
}
36-
return output;
37-
}
3811
function readAppend(targetFile, file) {
3912
try {
4013
var data = fs.readFileSync(file, 'utf8');
41-
var fileType = targetFile.substr(targetFile.lastIndexOf('.') + 1, targetFile.length);
42-
var output = parseLoaders(data, fileType);
43-
fs.appendFileSync(targetFile, output, 'utf8');
14+
fs.appendFileSync(targetFile, data, 'utf8');
4415
return true;
4516
}
4617
catch (e) {

lib/reducers/task-tests/task-tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function taskTestsReducer(taskTests, action) {
66
if (taskTests === void 0) { taskTests = ''; }
77
switch (action.type) {
88
case Type.SET_PAGE:
9-
var target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, "_tmp" + window.coderoad.suffix);
9+
var target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, "_tmpTests" + window.coderoad.suffix);
1010
loader_1.default(target, action.payload.taskTests);
1111
return target;
1212
default:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import * as path from 'path';
2+
import * as fs from 'fs';
3+
4+
const supportedFileTypes = ['js', 'jsx', 'ts', 'py'];
5+
const js = /^\/\/\s?load\(['"`](.+)['"`]\)$/;
6+
const loaderMatch = {
7+
js, // // load('file'),
8+
ts: js,
9+
jsx: js,
10+
py: /\#\s?load\(['"`](.+)['"`]\)$/ // # load('file')
11+
};
12+
13+
export default function parseLoaders(data: string, fileType: string) {
14+
15+
if (supportedFileTypes.indexOf(fileType) < 0) {
16+
console.log(`File type "${fileType}" not supported.`);
17+
return '';
18+
}
19+
20+
let imports = '';
21+
// loop over lines and add editor files
22+
let i = 0;
23+
let lines = data.split('\n');
24+
25+
while (i < lines.length - 1) {
26+
i += 1;
27+
let loader: string[] = lines[i].match(loaderMatch[fileType]);
28+
29+
if (loader) {
30+
// loader found
31+
let fileToLoad: string = loader[1];
32+
let pathToFile: string = path.normalize(path.join(window.coderoad.dir, fileToLoad));
33+
let file: string = fs.readFileSync(pathToFile, 'utf8');
34+
imports += file;
35+
}
36+
}
37+
38+
let output: string = null;
39+
if (imports.length > 0) {
40+
output = imports + '\n'.concat(data);
41+
} else {
42+
output = data;
43+
}
44+
return output;
45+
}

src/reducers/run-tests/run.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import {handleResult} from './test-result';
22
import {store} from '../../_base';
3+
import * as fs from 'fs';
4+
import * as path from 'path';
5+
import parseLoaders from './parse-loaders';
6+
7+
// TODO: use memcache for test files instead of read/writes
38

49
export function runTaskTests(setup?: boolean): boolean {
5-
const tests: CR.TaskTest[] = store.getState().taskTests;
6-
if (tests && tests.length) {
10+
const testFile: string = store.getState().taskTests;
11+
12+
if (testFile) {
713
let config = window.coderoad;
814
config.taskPosition = store.getState().taskPosition;
15+
16+
let fileType: string = testFile.substr(testFile.lastIndexOf('.') + 1, testFile.length) || null;
17+
let tests = fs.readFileSync(testFile, 'utf8');
18+
let output = parseLoaders(tests, fileType);
19+
20+
let target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, `_tmp${window.coderoad.suffix}`);
21+
fs.writeFileSync(target, output, 'utf8');
22+
923
// call test runner
10-
window.coderoad.runner(tests, config, handleResult);
24+
window.coderoad.runner(target, config, handleResult);
1125
}
1226
return true;
1327
}

src/reducers/task-tests/loader.ts

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33

4-
const loaderMatch = {
5-
js: /^\/\/\s?load\(['"`](.+)['"`]\)$/,
6-
py: /\#\s?load\(['"`](.+)['"`]\)$/
7-
};
8-
94
function unlink(targetFile: string) {
105
return new Promise((resolve) => {
116
if (fs.existsSync(targetFile)) {
@@ -15,38 +10,10 @@ function unlink(targetFile: string) {
1510
});
1611
}
1712

18-
function parseLoaders(data: string, fileType: string) {
19-
let lines = data.split('\n');
20-
let imports = '';
21-
// loop over lines and add editor files
22-
let i = 0;
23-
while (i < lines.length - 1) {
24-
i += 1;
25-
let loader: string[] = lines[i].match(loaderMatch[fileType]);
26-
27-
if (loader) {
28-
// loader found
29-
let fileToLoad: string = loader[1];
30-
let pathToFile: string = path.normalize(path.join(window.coderoad.dir, fileToLoad));
31-
imports.concat(fs.readFileSync(pathToFile, 'utf8'));
32-
}
33-
}
34-
35-
let output: string = null;
36-
if (imports.length > 0) {
37-
output = imports + '\n'.concat(data);
38-
} else {
39-
output = data;
40-
}
41-
return output;
42-
}
43-
4413
function readAppend(targetFile: string, file: string): boolean {
4514
try {
4615
let data = fs.readFileSync(file, 'utf8');
47-
let fileType = targetFile.substr(targetFile.lastIndexOf('.') + 1, targetFile.length);
48-
let output = parseLoaders(data, fileType);
49-
fs.appendFileSync(targetFile, output, 'utf8');
16+
fs.appendFileSync(targetFile, data, 'utf8');
5017
return true;
5118
} catch (e) {
5219
console.log('Error reading test file', e);

src/reducers/task-tests/task-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import load from './loader';
66
export default function taskTestsReducer(taskTests = '', action: CR.Action): string {
77
switch (action.type) {
88
case Type.SET_PAGE:
9-
let target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, `_tmp${window.coderoad.suffix}`);
9+
let target = path.join(window.coderoad.tutorialDir || window.coderoad.dir, `_tmpTests${window.coderoad.suffix}`);
1010
load(target, action.payload.taskTests);
1111
return target;
1212
default:

src/typings/cr/globals.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ interface IntrinsicAttributes {
2424
primaryText: string;
2525
primaryTogglesNestedList: any;
2626
}
27+
28+
type fileType = 'js'|'jsx'|'ts'|'py';

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"src/reducers/project/project.ts",
8080
"src/reducers/reducer.ts",
8181
"src/reducers/route/route.ts",
82+
"src/reducers/run-tests/parse-loaders.ts",
8283
"src/reducers/run-tests/run-tests.ts",
8384
"src/reducers/run-tests/run.ts",
8485
"src/reducers/run-tests/test-result.ts",

0 commit comments

Comments
 (0)