diff --git a/js/tests/tell-me-how-many_test.mjs b/js/tests/tell-me-how-many_test.mjs new file mode 100644 index 000000000..a0c02c6fd --- /dev/null +++ b/js/tests/tell-me-how-many_test.mjs @@ -0,0 +1,70 @@ +import * as cp from 'child_process' +import fs from 'fs/promises' +import { join, resolve, 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 () => { + const randomFilesNumber = Math.floor(Math.random() * (30 - 1) + 1) + const dir = tmpdir() + + // check if already exists and rm? + await mkdir(`${dir}/random`) + for (let i = 0; i < randomFilesNumber; i++) { + await writeFile(`${dir}/random/${i}.txt`, '', 'utf8') + } + + 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`, + }) + + // await rmdir(`${ctx.tmpPath}/random`, { recursive: true }) + return eq(Number(stdout.trim()), dirLength) +} + +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, + }) +}) + +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, + }) +}) + +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, + }) +}) + +Object.freeze(tests) diff --git a/subjects/tell-me-how-many/README.md b/subjects/tell-me-how-many/README.md new file mode 100644 index 000000000..2c136be96 --- /dev/null +++ b/subjects/tell-me-how-many/README.md @@ -0,0 +1,33 @@ +## tell-me-how-many + +### 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! + +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 + +### 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. + +