fix: extract a test updating logic and logging

This commit is contained in:
Valeriy S
2018-12-05 12:37:48 +03:00
committed by Stuart Taylor
parent 779d2034f3
commit 0a7ac8935e

View File

@ -22,64 +22,78 @@ const testWorker = new WorkerExecutor('test-evaluator');
const testTimeout = 5000; const testTimeout = 5000;
function* ExecuteChallengeSaga() { function* ExecuteChallengeSaga() {
const { js, bonfire, backend } = challengeTypes; try {
const { challengeType } = yield select(challengeMetaSelector); const { js, bonfire, backend } = challengeTypes;
switch (challengeType) { const { challengeType } = yield select(challengeMetaSelector);
case js:
case bonfire: // TODO: ExecuteBackendChallengeSaga and ExecuteDOMChallengeSaga
yield* ExecuteJSChallengeSaga(); if (challengeType !== js && challengeType !== bonfire) {
break; return;
case backend: }
// yield* ExecuteBackendChallengeSaga();
break; yield put(initLogs());
default: yield put(initConsole('// running tests'));
// yield* ExecuteDOMChallengeSaga();
const tests = yield select(challengeTestsSelector);
let testResults;
switch (challengeType) {
case js:
case bonfire:
testResults = yield ExecuteJSChallengeSaga(tests);
break;
case backend:
// yield ExecuteBackendChallengeSaga();
break;
default:
// yield ExecuteDOMChallengeSaga();
}
yield put(updateTests(testResults));
yield put(updateConsole('// tests completed'));
yield put(logsToConsole('// console output'));
} catch (e) {
yield put(updateConsole(e));
} }
} }
function* ExecuteJSChallengeSaga() { function* ExecuteJSChallengeSaga(tests) {
yield put(initLogs()); const testResults = [];
yield put(initConsole('// running tests')); const files = yield select(challengeFilesSelector);
try { const { code, solution } = yield call(buildJSFromFiles, files);
const files = yield select(challengeFilesSelector);
const { code, solution } = yield call(buildJSFromFiles, files); for (const { text, testString } of tests) {
const tests = yield select(challengeTestsSelector); const newTest = { text, testString };
const testResults = []; try {
for (const { text, testString } of tests) {
const newTest = { text, testString };
const { pass, err, logs } = yield call( const { pass, err, logs } = yield call(
testWorker.execute, testWorker.execute,
{ script: solution + '\n' + testString, code }, { script: solution + '\n' + testString, code },
testTimeout testTimeout
); );
for (const log of logs) {
yield put(updateLogs(log));
}
if (pass) { if (pass) {
newTest.pass = true; newTest.pass = true;
} else {
throw err;
}
} catch (err) {
newTest.message = text.replace(/<code>(.*?)<\/code>/g, '$1');
if (err === 'timeout') {
newTest.err = 'Test timed out';
newTest.message = `${newTest.message} (${newTest.err})`;
} else { } else {
const { message, stack } = err; const { message, stack } = err;
newTest.err = message + '\n' + stack; newTest.err = message + '\n' + stack;
newTest.stack = stack; newTest.stack = stack;
newTest.message = text.replace(/<code>(.*?)<\/code>/g, '$1');
yield put(updateConsole(newTest.message));
} }
yield put(updateConsole(newTest.message));
} finally {
testResults.push(newTest); testResults.push(newTest);
for (const log of logs) {
yield put(updateLogs(log));
}
// kill worker for independent tests
yield call(testWorker.killWorker); yield call(testWorker.killWorker);
} }
yield put(updateTests(testResults));
yield put(updateConsole('// tests completed'));
yield put(logsToConsole('// console output'));
} catch (e) {
if (e === 'timeout') {
yield put(updateConsole('Test timed out'));
} else {
yield put(updateConsole(e));
}
} finally {
yield call(testWorker.killWorker);
} }
return testResults;
} }
export function createExecuteChallengeSaga(types) { export function createExecuteChallengeSaga(types) {