From 0a7ac8935e1e936130b73f78d7f7971c42975db9 Mon Sep 17 00:00:00 2001 From: Valeriy S Date: Wed, 5 Dec 2018 12:37:48 +0300 Subject: [PATCH] fix: extract a test updating logic and logging --- .../redux/execute-challenge-saga.js | 92 +++++++++++-------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/client/src/templates/Challenges/redux/execute-challenge-saga.js b/client/src/templates/Challenges/redux/execute-challenge-saga.js index 873c564082..014538525c 100644 --- a/client/src/templates/Challenges/redux/execute-challenge-saga.js +++ b/client/src/templates/Challenges/redux/execute-challenge-saga.js @@ -22,64 +22,78 @@ const testWorker = new WorkerExecutor('test-evaluator'); const testTimeout = 5000; function* ExecuteChallengeSaga() { - const { js, bonfire, backend } = challengeTypes; - const { challengeType } = yield select(challengeMetaSelector); - switch (challengeType) { - case js: - case bonfire: - yield* ExecuteJSChallengeSaga(); - break; - case backend: - // yield* ExecuteBackendChallengeSaga(); - break; - default: - // yield* ExecuteDOMChallengeSaga(); + try { + const { js, bonfire, backend } = challengeTypes; + const { challengeType } = yield select(challengeMetaSelector); + + // TODO: ExecuteBackendChallengeSaga and ExecuteDOMChallengeSaga + if (challengeType !== js && challengeType !== bonfire) { + return; + } + + yield put(initLogs()); + yield put(initConsole('// running tests')); + + 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() { - yield put(initLogs()); - yield put(initConsole('// running tests')); - try { - const files = yield select(challengeFilesSelector); - const { code, solution } = yield call(buildJSFromFiles, files); - const tests = yield select(challengeTestsSelector); - const testResults = []; - for (const { text, testString } of tests) { - const newTest = { text, testString }; +function* ExecuteJSChallengeSaga(tests) { + const testResults = []; + const files = yield select(challengeFilesSelector); + const { code, solution } = yield call(buildJSFromFiles, files); + + for (const { text, testString } of tests) { + const newTest = { text, testString }; + try { const { pass, err, logs } = yield call( testWorker.execute, { script: solution + '\n' + testString, code }, testTimeout ); + for (const log of logs) { + yield put(updateLogs(log)); + } if (pass) { newTest.pass = true; + } else { + throw err; + } + } catch (err) { + newTest.message = text.replace(/(.*?)<\/code>/g, '$1'); + if (err === 'timeout') { + newTest.err = 'Test timed out'; + newTest.message = `${newTest.message} (${newTest.err})`; } else { const { message, stack } = err; newTest.err = message + '\n' + stack; newTest.stack = stack; - newTest.message = text.replace(/(.*?)<\/code>/g, '$1'); - yield put(updateConsole(newTest.message)); } + yield put(updateConsole(newTest.message)); + } finally { testResults.push(newTest); - for (const log of logs) { - yield put(updateLogs(log)); - } - // kill worker for independent tests 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) {