Skip to content

Commit 26b74e6

Browse files
committed
move config paths into test load
1 parent dc04e95 commit 26b74e6

File tree

15 files changed

+135
-98
lines changed

15 files changed

+135
-98
lines changed

lib/modules/tests/actions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function testLoad() {
1111
type: types_1.TEST_LOAD, payload: {
1212
dir: dir,
1313
tasks: tasks,
14-
load: tutorial.config.load,
14+
tutorial: tutorial,
1515
testFile: testFile,
1616
}
1717
});
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
var path_1 = require('path');
3+
var system_1 = require('../../../polyfills/system');
4+
function configPath(_a) {
5+
var dir = _a.dir, tutorial = _a.tutorial, testPath = _a.testPath;
6+
if (system_1.isWindows) {
7+
testPath = testPath.split('/').join('\\');
8+
}
9+
if (tutorial.config.dir) {
10+
testPath = path_1.join(tutorial.config.dir, testPath);
11+
}
12+
else {
13+
testPath = path_1.join(dir, 'node_modules', tutorial.name, testPath);
14+
}
15+
if (tutorial.config.testSuffix) {
16+
testPath += tutorial.config.testSuffix;
17+
}
18+
return testPath;
19+
}
20+
Object.defineProperty(exports, "__esModule", { value: true });
21+
exports.default = configPath;

lib/modules/tests/test-run/load.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
"use strict";
22
var fs_1 = require('fs');
3+
var config_path_1 = require('./config-path');
34
function loadTaskTests(_a) {
4-
var dir = _a.dir, tasks = _a.tasks, load = _a.load, testFile = _a.testFile;
5+
var dir = _a.dir, tasks = _a.tasks, tutorial = _a.tutorial, testFile = _a.testFile;
56
var tests = [].concat.apply([], tasks.map(function (task) { return task.tests || []; })).reduce(function (output, file) {
67
try {
7-
output += fs_1.readFileSync(file, 'utf8') + '\n';
8+
var absoluteFilePath = config_path_1.default({
9+
dir: dir,
10+
tutorial: tutorial,
11+
testPath: file,
12+
});
13+
output += fs_1.readFileSync(absoluteFilePath, 'utf8') + '\n';
814
}
915
catch (e) {
1016
console.log('Error reading test file', e);
1117
}
1218
return output;
1319
}, '');
14-
load({ dir: dir, tests: tests, testFile: testFile });
20+
tutorial.config.load({ dir: dir, tests: tests, testFile: testFile });
1521
}
1622
Object.defineProperty(exports, "__esModule", { value: true });
1723
exports.default = loadTaskTests;

lib/modules/tutorial/utils/config-runner.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
var path_1 = require('path');
3-
var system_1 = require('./system');
3+
var system_1 = require('../../../polyfills/system');
44
var node_file_exists_1 = require('node-file-exists');
55
function configRunner(name, runner, dir) {
66
var flatDep = path_1.join(dir, 'node_modules', runner, 'package.json');
@@ -25,8 +25,8 @@ function configRunner(name, runner, dir) {
2525
runnerRoot = runnerRoot.substring(0, runnerRoot.lastIndexOf(slash));
2626
var pathToMain = path_1.join(runnerRoot, runnerMain);
2727
return {
28-
load: require(pathToMain).load || { load: function () { return console.log('invalid test loader'); } },
29-
run: require(pathToMain).run || { run: function () { return console.log('invalid test runner'); } },
28+
load: require(pathToMain).load || { load: function () { return console.log('Invalid test loader'); } },
29+
run: require(pathToMain).run || { run: function () { return console.log('Invalid test runner'); } },
3030
};
3131
}
3232
Object.defineProperty(exports, "__esModule", { value: true });

lib/modules/tutorial/utils/config.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ function tutorialConfig(tutorialPj, dir) {
88
var runner = config.runner;
99
var runnerOptions = config.runnerOptions || {};
1010
var configEdit = tutorialPj.config.edit;
11-
var getRunner = config_runner_1.default(name, config.runner, dir);
12-
if (!getRunner || !getRunner.run || !getRunner.load) {
13-
console.log('Error loading test runner', getRunner);
11+
var _a = config_runner_1.default(name, runner, dir), run = _a.run, load = _a.load;
12+
if (!run || !load) {
13+
console.log('Error loading test runner', "run: " + run + ", load: " + load);
1414
}
1515
return {
1616
dir: path_1.join(dir, 'node_modules', name, config.dir),
1717
runner: runner,
1818
runnerOptions: runnerOptions,
19-
run: getRunner.run,
20-
load: getRunner.load,
19+
run: run,
20+
load: load,
2121
testSuffix: configTestSuffix(config.testSuffix || 'js'),
2222
issuesPath: config_repo_1.configIssuesPath(tutorialPj.bugs),
2323
repo: repo,

lib/polyfills/object-values.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
function polyfillObjectValues() {
33
if (typeof Object.values !== 'function') {
44
Object.values = function (obj) {
5-
var vals = [];
5+
var vals = new Set();
66
for (var key in obj) {
7-
vals.push(obj[key]);
7+
vals.add(obj[key]);
88
}
9-
return vals;
9+
return Array.from(vals);
1010
};
1111
}
1212
}

lib/polyfills/system.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
exports.isWindows = window.navigator.appVersion.indexOf('Win') > -1 || false;

src/components/Alert/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const styles = {
2121
alert: state.alert || defaultAlert,
2222
}), {alertClose})
2323
export default class Alert extends React.Component<{
24-
alert?: CR.Alert, alertClose?: () => Redux.ActionCreator
24+
alert?: CR.Alert, alertClose?: () => Redux.ActionCreator<any>
2525
}, {}> {
2626
public render() {
2727
const {alert, alertClose} = this.props;

src/modules/tests/actions.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export function testLoad(): Redux.ThunkAction<any, any, {}> {
1111
type: TEST_LOAD, payload: {
1212
dir,
1313
tasks,
14-
load: tutorial.config.load,
14+
tutorial,
1515
testFile,
1616
}
1717
});
@@ -20,11 +20,13 @@ export function testLoad(): Redux.ThunkAction<any, any, {}> {
2020

2121
export function testRun(): Redux.ThunkAction<any, any, {}> {
2222
return (dispatch, getState): void => {
23+
2324
// less than a second since the last test run, skip
2425
const timeSinceLastTestRun = performance.now() - getState().testRun.time;
2526
if (timeSinceLastTestRun < 1000) {
2627
return;
2728
}
29+
2830
const {dir, tutorial, taskPosition, pagePosition} = getState();
2931
const tasks = tutorial.pages[pagePosition].tasks;
3032
const hasTasks = tasks && tasks.length > 0;
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import {join} from 'path';
2+
3+
import {isWindows} from '../../../polyfills/system';
4+
5+
/**
6+
* set paths to tests as absolute paths
7+
* @param {string} dir
8+
* @param {string} name
9+
* @param {Tutorial.Config} config
10+
* @param {string} testPath
11+
* @returns string
12+
*/
13+
export default function configPath({
14+
dir, tutorial, testPath
15+
}): string {
16+
17+
if (isWindows) {
18+
// adjust paths for windows slashes
19+
testPath = testPath.split('/').join('\\');
20+
}
21+
22+
// adjust absolute file path
23+
if (tutorial.config.dir) {
24+
testPath = join(tutorial.config.dir, testPath);
25+
} else {
26+
testPath = join(dir, 'node_modules', tutorial.name, testPath);
27+
}
28+
29+
if (tutorial.config.testSuffix) {
30+
// prevent repeat appending test suffix
31+
testPath += tutorial.config.testSuffix;
32+
}
33+
34+
return testPath;
35+
}
36+
/**
37+
* loops over task tests and set paths to absolute paths
38+
* @param {string} dir
39+
* @param {string} name
40+
* @param {Tutorial.Config} config
41+
* @param {CR.Page[]} pages
42+
* @returns CR
43+
*/
44+
// export default function configPaths(
45+
// dir: string, name: string, config: Tutorial.Config, pages: CR.Page[]
46+
// ): CR.Page[] {
47+
// return pages.map((page: CR.Page): CR.Page => {
48+
// if (!page.tasks) {
49+
// page.tasks = [];
50+
// }
51+
// page.tasks.map((task: CR.Task): CR.Task => {
52+
53+
// if (!task.tests) { return task; }
54+
55+
// // change testPaths to use absolute URLs
56+
// task.tests = task.tests.map((testPath: string) => {
57+
// // add unique string to tests
58+
// if (typeof testPath === 'string') {
59+
// return configTestString(dir, name, config, testPath);
60+
// } else {
61+
// console.error('Invalid task test', testPath);
62+
// return 'ERROR';
63+
// }
64+
// });
65+
// return task;
66+
// });
67+
// return page;
68+
// });
69+
// }

src/modules/tests/test-run/load.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readFileSync } from 'fs';
2+
import configPath from './config-path';
23

34
/**
45
* read files from paths and concat a data file together
@@ -8,7 +9,7 @@ import { readFileSync } from 'fs';
89
* @param {} load
910
* @param {} testFile}
1011
*/
11-
export default function loadTaskTests({dir, tasks, load, testFile}) {
12+
export default function loadTaskTests({dir, tasks, tutorial, testFile}) {
1213

1314
// first read files from paths and concat data together
1415

@@ -19,7 +20,12 @@ export default function loadTaskTests({dir, tasks, load, testFile}) {
1920
// concat test files together
2021
).reduce((output: string, file: string): string => {
2122
try {
22-
output += readFileSync(file, 'utf8') + '\n';
23+
const absoluteFilePath = configPath({
24+
dir,
25+
tutorial,
26+
testPath: file,
27+
});
28+
output += readFileSync(absoluteFilePath, 'utf8') + '\n';
2329
} catch (e) {
2430
console.log('Error reading test file', e);
2531
}
@@ -29,5 +35,5 @@ export default function loadTaskTests({dir, tasks, load, testFile}) {
2935

3036

3137
// save the file
32-
load({dir, tests, testFile});
38+
tutorial.config.load({dir, tests, testFile});
3339
}

src/modules/tutorial/utils/config-paths.ts

-69
This file was deleted.

src/modules/tutorial/utils/config-runner.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {join} from 'path';
2-
import {isWindows} from './system';
2+
import {isWindows} from '../../../polyfills/system';
33
import fileExists from 'node-file-exists';
44

55
/**
@@ -42,7 +42,7 @@ export default function configRunner(name: string, runner: string, dir: string):
4242
let pathToMain = join(runnerRoot, runnerMain);
4343

4444
return {
45-
load: require(pathToMain).load || { load: () => console.log('invalid test loader')},
46-
run: require(pathToMain).run || { run: () => console.log('invalid test runner')},
45+
load: require(pathToMain).load || { load: () => console.log('Invalid test loader')},
46+
run: require(pathToMain).run || { run: () => console.log('Invalid test runner')},
4747
};
4848
}

src/modules/tutorial/utils/config.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {join} from 'path';
22

33
import {configIssuesPath, configRepo} from './config-repo';
44
import configRunner from './config-runner';
5-
import {isWindows} from './system';
5+
import {isWindows} from '../../../polyfills/system';
66
import fileExists from 'node-file-exists';
77

88
/**
@@ -21,18 +21,18 @@ export function tutorialConfig(
2121
const runnerOptions: Object = config.runnerOptions || {};
2222
const configEdit = tutorialPj.config.edit;
2323

24-
const getRunner = configRunner(name, config.runner, dir);
24+
const { run, load } = configRunner(name, runner, dir);
2525

26-
if (!getRunner || !getRunner.run || !getRunner.load) {
27-
console.log('Error loading test runner', getRunner);
26+
if (!run || !load) {
27+
console.log('Error loading test runner', `run: ${run}, load: ${load}`);
2828
}
2929

3030
return {
3131
dir: join(dir, 'node_modules', name, config.dir),
3232
runner,
3333
runnerOptions,
34-
run: getRunner.run,
35-
load: getRunner.load,
34+
run,
35+
load,
3636
testSuffix: configTestSuffix(config.testSuffix || 'js'),
3737
issuesPath: configIssuesPath(tutorialPj.bugs),
3838
repo,
File renamed without changes.

0 commit comments

Comments
 (0)