From 6e531663bbdc8b91f6568d631d03372289c9dca1 Mon Sep 17 00:00:00 2001 From: Clement Denis Date: Thu, 4 Mar 2021 21:27:35 +0000 Subject: [PATCH] js: udpate node LTS version, add better errors and tools for random --- js/tests/Dockerfile | 2 +- js/tests/test.mjs | 19 +++++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/js/tests/Dockerfile b/js/tests/Dockerfile index 9ac1cd56..225688c7 100644 --- a/js/tests/Dockerfile +++ b/js/tests/Dockerfile @@ -1,4 +1,4 @@ -FROM node:14.7.0-alpine3.12 +FROM node:14.16.0-alpine3.12 WORKDIR /app COPY . . diff --git a/js/tests/test.mjs b/js/tests/test.mjs index eb6a0242..244b177b 100644 --- a/js/tests/test.mjs +++ b/js/tests/test.mjs @@ -37,13 +37,14 @@ const eq = (a, b) => { return true } -const [submitPath, name] = process.argv.slice(2) +const [solutionPath, name] = process.argv.slice(2) const fatal = (...args) => { console.error(...args) process.exit(1) } -if (!name) fatal('missing exercise, usage:\nnode test exercise-name') +solutionPath || fatal('missing solution-path, usage:\nnode test solution-path exercise-name') +name || fatal('missing exercise, usage:\nnode test solution-path exercise-name') const ifNoEnt = fn => err => { if (err.code !== 'ENOENT') throw err @@ -77,7 +78,7 @@ const any = arr => }) const testNode = async ({ test, name }) => { - const path = `${submitPath}/${name}.mjs` + const path = `${solutionPath}/${name}.mjs` return { path, url: joinPath(root, `${name}_test.mjs`), @@ -90,8 +91,14 @@ const runTests = async ({ url, path, code }) => { fatal(`Unable to execute ${name} solution, error:\n${stackFmt(err, url)}`), ) - const ctx = (await (setup && setup({ path }))) || {} - const tools = { eq, fail, wait, code, ctx, path } + const randStr = (n = 7) => Math.random().toString(36).slice(2, n) + const between = (min, max) => { + max || (max = min, min = 0) + return Math.floor(Math.random() * (max - min) + min) + } + + const tools = { eq, fail, wait, code, path, randStr, between } + tools.ctx = (await (setup && setup(tools))) || {} for (const [i, t] of tests.entries()) { try { if (!(await t(tools))) { @@ -112,7 +119,7 @@ const main = async () => { ]).catch(ifNoEnt(() => fatal(`Missing test for ${name}`))) if (mode === "node") return runTests(await testNode({ test, name })) - const path = `${submitPath}/${name}.js` + const path = `${solutionPath}/${name}.js` const rawCode = await read(path, "student solution") // this is a very crude and basic removal of comments