fix: extract a test updating logic and logging
This commit is contained in:
@ -22,65 +22,79 @@ const testWorker = new WorkerExecutor('test-evaluator');
|
||||
const testTimeout = 5000;
|
||||
|
||||
function* ExecuteChallengeSaga() {
|
||||
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:
|
||||
yield* ExecuteJSChallengeSaga();
|
||||
testResults = yield ExecuteJSChallengeSaga(tests);
|
||||
break;
|
||||
case backend:
|
||||
// yield* ExecuteBackendChallengeSaga();
|
||||
// yield ExecuteBackendChallengeSaga();
|
||||
break;
|
||||
default:
|
||||
// yield* ExecuteDOMChallengeSaga();
|
||||
// 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 {
|
||||
function* ExecuteJSChallengeSaga(tests) {
|
||||
const testResults = [];
|
||||
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 };
|
||||
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>(.*?)<\/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>(.*?)<\/code>/g, '$1');
|
||||
}
|
||||
yield put(updateConsole(newTest.message));
|
||||
}
|
||||
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 {
|
||||
testResults.push(newTest);
|
||||
yield call(testWorker.killWorker);
|
||||
}
|
||||
}
|
||||
return testResults;
|
||||
}
|
||||
|
||||
export function createExecuteChallengeSaga(types) {
|
||||
return [takeLatest(types.executeChallenge, ExecuteChallengeSaga)];
|
||||
|
Reference in New Issue
Block a user