fix(client): refactor a test runner

This commit is contained in:
Valeriy S
2018-12-10 17:29:58 +03:00
committed by Stuart Taylor
parent de7798c753
commit 7df4d06056
2 changed files with 46 additions and 58 deletions

View File

@ -22,7 +22,7 @@ import {
import { import {
buildJSFromFiles, buildJSFromFiles,
buildFromFiles, buildHtmlFromFiles,
buildBackendChallenge buildBackendChallenge
} from '../utils/build'; } from '../utils/build';
@ -82,11 +82,14 @@ function* ExecuteJSChallengeSaga() {
const log = args => consoleProxy.put(args); const log = args => consoleProxy.put(args);
testWorker.on('LOG', log); testWorker.on('LOG', log);
const testResults = yield call(executeTests, { const testResults = yield call(executeTests, (testString, testTimeout) =>
testRunner: testWorker, testWorker
code, .execute({ script: solution + '\n' + testString, code }, testTimeout)
solution .then(result => {
}); testWorker.killWorker();
return result;
})
);
testWorker.remove('LOG', log); testWorker.remove('LOG', log);
consoleProxy.close(); consoleProxy.close();
@ -97,30 +100,25 @@ function createTestFrame(state, ctx, proxyLogger) {
return new Promise(resolve => { return new Promise(resolve => {
const frameTest = createTestFramer(document, state, resolve, proxyLogger); const frameTest = createTestFramer(document, state, resolve, proxyLogger);
frameTest(ctx); frameTest(ctx);
}).then(() => console.log('Frame ready')); });
} }
function* ExecuteDOMChallengeSaga() { function* ExecuteDOMChallengeSaga() {
const state = yield select(); const state = yield select();
const ctx = yield call(buildFromFiles, state); const ctx = yield call(buildHtmlFromFiles, state);
const consoleProxy = yield channel(); const consoleProxy = yield channel();
yield fork(logToConsole, consoleProxy); yield fork(logToConsole, consoleProxy);
yield call(createTestFrame, state, ctx, consoleProxy); yield call(createTestFrame, state, ctx, consoleProxy);
const testResults = yield call(executeTests, { const testResults = yield call(executeTests, (testString, testTimeout) =>
testRunner: { Promise.race([
execute({ script }, testTimeout) { runTestInTestFrame(document, testString),
return Promise.race([
runTestInTestFrame(document, script),
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout(() => reject('timeout'), testTimeout) setTimeout(() => reject('timeout'), testTimeout)
) )
]); ])
}, );
killWorker() {}
}
});
consoleProxy.close(); consoleProxy.close();
return testResults; return testResults;
@ -134,51 +132,26 @@ function* ExecuteBackendChallengeSaga() {
yield call(createTestFrame, state, ctx, consoleProxy); yield call(createTestFrame, state, ctx, consoleProxy);
const testResults = yield call(executeTests, { const testResults = yield call(executeTests, (testString, testTimeout) =>
testRunner: { Promise.race([
execute({ script }, testTimeout) { runTestInTestFrame(document, testString),
return Promise.race([
runTestInTestFrame(document, script),
new Promise((_, reject) => new Promise((_, reject) =>
setTimeout(() => reject('timeout'), testTimeout) setTimeout(() => reject('timeout'), testTimeout)
) )
]); ])
}, );
killWorker() {}
}
});
consoleProxy.close(); consoleProxy.close();
return testResults; return testResults;
} }
function* updateMainSaga() { function* executeTests(testRunner) {
try {
const { html, modern } = challengeTypes;
const { challengeType } = yield select(challengeMetaSelector);
if (challengeType !== html && challengeType !== modern) {
return;
}
const state = yield select();
const frameMain = yield call(createMainFramer, document, state);
const ctx = yield call(buildFromFiles, state);
yield call(frameMain, ctx);
} catch (err) {
console.error(err);
}
}
function* executeTests({ testRunner, code = '', solution = '' }) {
const tests = yield select(challengeTestsSelector); const tests = yield select(challengeTestsSelector);
const testResults = []; const testResults = [];
for (const { text, testString } of tests) { for (const { text, testString } of tests) {
const newTest = { text, testString }; const newTest = { text, testString };
try { try {
const { pass, err } = yield call( const { pass, err } = yield call(testRunner, testString, testTimeout);
testRunner.execute,
{ script: solution + '\n' + testString, code },
testTimeout
);
if (pass) { if (pass) {
newTest.pass = true; newTest.pass = true;
} else { } else {
@ -197,12 +170,27 @@ function* executeTests({ testRunner, code = '', solution = '' }) {
yield put(updateConsole(newTest.message)); yield put(updateConsole(newTest.message));
} finally { } finally {
testResults.push(newTest); testResults.push(newTest);
yield call(testRunner.killWorker);
} }
} }
return testResults; return testResults;
} }
function* updateMainSaga() {
try {
const { html, modern } = challengeTypes;
const { challengeType } = yield select(challengeMetaSelector);
if (challengeType !== html && challengeType !== modern) {
return;
}
const state = yield select();
const frameMain = yield call(createMainFramer, document, state);
const ctx = yield call(buildHtmlFromFiles, state);
yield call(frameMain, ctx);
} catch (err) {
console.error(err);
}
}
export function createExecuteChallengeSaga(types) { export function createExecuteChallengeSaga(types) {
return [ return [
takeLatest(types.executeChallenge, ExecuteChallengeSaga), takeLatest(types.executeChallenge, ExecuteChallengeSaga),

View File

@ -63,7 +63,7 @@ const pipeLine = flow(
applyFunctions(toHtml) applyFunctions(toHtml)
); );
export function buildFromFiles(state) { export function buildHtmlFromFiles(state) {
const files = challengeFilesSelector(state); const files = challengeFilesSelector(state);
const { required = [], template } = challengeMetaSelector(state); const { required = [], template } = challengeMetaSelector(state);
const finalRequires = [...globalRequires, ...required]; const finalRequires = [...globalRequires, ...required];