Browse Source

update tell-me-how-many subject and tests

pull/761/head
Louise Foussat 4 years ago committed by Clément
parent
commit
a4f920a920
  1. 66
      js/tests/tell-me-how-many_test.mjs
  2. 31
      subjects/tell-me-how-many/README.md

66
js/tests/tell-me-how-many_test.mjs

@ -1,70 +1,48 @@
import * as cp from 'child_process' import * as cp from 'child_process'
import fs from 'fs/promises' import { mkdir, writeFile } from 'fs/promises'
import { join, resolve, isAbsolute } from 'path' import { join, isAbsolute } from 'path'
import { tmpdir } from 'os' import { tmpdir } from 'os'
import { promisify } from 'util' import { promisify } from 'util'
const mkdir = fs.mkdir
const rmdir = fs.rmdir
const writeFile = fs.writeFile
const exec = promisify(cp.exec) const exec = promisify(cp.exec)
export const tests = [] export const tests = []
export const setup = async () => { export const setup = async ({ path }) => {
const randomFilesNumber = Math.floor(Math.random() * (30 - 1) + 1) 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)
await mkdir(`${dir}/random`)
for (let i = 0; i < randomFilesNumber; i++) { 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 } return { stdout: stdout.trim() }
} }
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 { randomFilesNumber, tmpPath: dir, run }
return eq(Number(stdout.trim()), dirLength)
} }
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
// will execute the script with "random" as an argument // will execute the script in a folder named `tell-me-how-many`
// `random` folder has a random file number // '../tell-me-how-many' in the argument passed
return countDirLength({ // `tell-me-how-many` folder has a random file number
path, const { stdout } = await ctx.run('../tell-me-how-many')
eq, return eq(Number(stdout), ctx.randomFilesNumber)
ctx,
arg: 'random',
dirLength: ctx.randomFilesNumber,
})
}) })
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
// will execute the script without argument // will execute the script without argument
// in the `random` folder const { stdout } = await ctx.run()
return countDirLength({ return eq(Number(stdout), ctx.randomFilesNumber)
path,
eq,
ctx,
arg: '',
dirLength: ctx.randomFilesNumber,
})
}) })
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
// will execute the script with `random` folder's absolute path as argument // will execute the script with `tell-me-how-many` folder's absolute path as argument
return countDirLength({ const { stdout } = await ctx.run(ctx.tmpPath)
path, return eq(Number(stdout), ctx.randomFilesNumber)
eq,
ctx,
arg: `${ctx.tmpPath}/random`,
dirLength: ctx.randomFilesNumber,
})
}) })
Object.freeze(tests) Object.freeze(tests)

31
subjects/tell-me-how-many/README.md

@ -2,32 +2,27 @@
### Instructions ### 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: Create a `tell-me-how-many.mjs` script that:
- takes a relative or absolute folder path as argument from the command line - Take a relative or absolute folder path as argument from the command line.
- read this directory path - Read this directory path.
- get the number of entries in this folder - Get the number of entries in this folder.
- print the result in console - Print the result in console.
If there is no argument passed, the script must execute itself in the current directory
### Notions ### Notions
- [Node file system: `readdir`](https://nodejs.org/api/fs.html#fs_fspromises_readdir_path_options) - [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)
<!-- + check if is directory ?? -->
- [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)
<!--
again?
- [Node process: `argv`](https://nodejs.org/docs/latest/api/process.html#process_process_argv)
- [Node path: `resolve`](https://nodejs.org/api/path.html#path_path_resolve_paths)
- [Node path: `join`](https://nodejs.org/api/path.html#path_path_join_paths)
-->
### Provided files ### 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.

Loading…
Cancel
Save