diff --git a/js/tests/tell-me-how-many_test.mjs b/js/tests/tell-me-how-many_test.mjs index a0c02c6fd..2bb63faea 100644 --- a/js/tests/tell-me-how-many_test.mjs +++ b/js/tests/tell-me-how-many_test.mjs @@ -1,70 +1,48 @@ import * as cp from 'child_process' -import fs from 'fs/promises' -import { join, resolve, isAbsolute } from 'path' +import { mkdir, writeFile } from 'fs/promises' +import { join, isAbsolute } from 'path' import { tmpdir } from 'os' import { promisify } from 'util' -const mkdir = fs.mkdir -const rmdir = fs.rmdir -const writeFile = fs.writeFile const exec = promisify(cp.exec) export const tests = [] -export const setup = async () => { +export const setup = async ({ path }) => { const randomFilesNumber = Math.floor(Math.random() * (30 - 1) + 1) - const dir = tmpdir() + const dir = `${tmpdir()}/tell-me-how-many` - // check if already exists and rm? - await mkdir(`${dir}/random`) + await mkdir(dir) for (let i = 0; i < randomFilesNumber; i++) { - await writeFile(`${dir}/random/${i}.txt`, '', 'utf8') + await writeFile(`${dir}/${i}.txt`, '', 'utf8') } + const run = async (cmd) => { + const cmdPath = isAbsolute(cmd || '') ? cmd : join(dir, cmd || '') + const { stdout } = await exec(`node ${path} ${cmdPath}`) - return { randomFilesNumber, tmpPath: dir } -} -const countDirLength = async ({ dirLength, arg, ctx, path, eq }) => { - const scriptPath = join(resolve(), path) - const { stdout } = await exec(`node ${scriptPath} ${arg}`, { - cwd: arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/random`, - }) + return { stdout: stdout.trim() } + } - // await rmdir(`${ctx.tmpPath}/random`, { recursive: true }) - return eq(Number(stdout.trim()), dirLength) + return { randomFilesNumber, tmpPath: dir, run } } tests.push(async ({ path, eq, ctx }) => { - // will execute the script with "random" as an argument - // `random` folder has a random file number - return countDirLength({ - path, - eq, - ctx, - arg: 'random', - dirLength: ctx.randomFilesNumber, - }) + // will execute the script in a folder named `tell-me-how-many` + // '../tell-me-how-many' in the argument passed + // `tell-me-how-many` folder has a random file number + const { stdout } = await ctx.run('../tell-me-how-many') + return eq(Number(stdout), ctx.randomFilesNumber) }) tests.push(async ({ path, eq, ctx }) => { // will execute the script without argument - // in the `random` folder - return countDirLength({ - path, - eq, - ctx, - arg: '', - dirLength: ctx.randomFilesNumber, - }) + const { stdout } = await ctx.run() + return eq(Number(stdout), ctx.randomFilesNumber) }) tests.push(async ({ path, eq, ctx }) => { - // will execute the script with `random` folder's absolute path as argument - return countDirLength({ - path, - eq, - ctx, - arg: `${ctx.tmpPath}/random`, - dirLength: ctx.randomFilesNumber, - }) + // will execute the script with `tell-me-how-many` folder's absolute path as argument + const { stdout } = await ctx.run(ctx.tmpPath) + return eq(Number(stdout), ctx.randomFilesNumber) }) Object.freeze(tests) diff --git a/subjects/tell-me-how-many/README.md b/subjects/tell-me-how-many/README.md index 2c136be96..9705deca2 100644 --- a/subjects/tell-me-how-many/README.md +++ b/subjects/tell-me-how-many/README.md @@ -2,32 +2,27 @@ ### Instructions -Your very special partner's birthday is coming soon. So you've decided to organise a very special party. Invitations has been sent for a while... and good news: answers are back. We didn't count it, you've been too generous. But we saved every one of them as a file in a special folder for you. Have fun! +Your very favorite person's birthday is coming soon. So you've decided to organise a very special party 🥳🪅🎤 + +Invitations has been sent for a while... + +Good news: answers are back! +Psst: Sorry buddy, we didn't count it, you've been too generous. But we saved every one of them as a file in a special folder for you. Have fun! Create a `tell-me-how-many.mjs` script that: -- takes a relative or absolute folder path as argument from the command line -- read this directory path -- get the number of entries in this folder -- print the result in console +- Take a relative or absolute folder path as argument from the command line. +- Read this directory path. +- Get the number of entries in this folder. +- Print the result in console. + +If there is no argument passed, the script must execute itself in the current directory ### Notions - [Node file system: `readdir`](https://nodejs.org/api/fs.html#fs_fspromises_readdir_path_options) -- [Node path: `isAbsolute`](https://nodejs.org/api/path.html#path_path_isabsolute_path) - - -- [Node stat](https://nodejs.org/docs/latest/api/fs.html#fs_fspromises_stat_path_options) -- [Node stat: `isDirectory`](https://nodejs.org/docs/latest/api/fs.html#fs_stats_isdirectory) - - ### Provided files -Download [`tell-me-how-many.zip`](https://assets.01-edu.org/tell-me-how-many) to have at your disposal the `guests` folder containing the files to count in your script. You must save it in your `tell-me-how-many` exercise folder to test your script on it. +Download [`guests.zip`](https://assets.01-edu.org/tell-me-how-many/guests.zip) to have at your disposal the `guests` folder containing the files to count in your script. You must save it in your `tell-me-how-many` exercise folder to test your script on it.