Browse Source

piscine-js lite - node quest - exo 2: verydisco-forever

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
8568163a53
  1. 40
      js/tests/verydisco-forever_test.mjs
  2. 13
      subjects/verydisco-forever/README.md

40
js/tests/verydisco-forever_test.mjs

@ -0,0 +1,40 @@
import fs from 'fs/promises'
const readFile = fs.readFile
import { promisify } from 'util'
import * as cp from 'child_process'
const exec = promisify(cp.exec)
export const tests = []
const name = 'verydisco-forever'
const withArgAndExpect = async ({ arg, expect, path, eq }) => {
const cwd = `${path
.split('/')
.slice(0, -1)
.join('/')}/`
await exec(`node ${name}.mjs "${arg}"`, { cwd })
const out = await readFile(`${cwd}${name}.txt`, 'utf8').catch((err) =>
err.code === 'ENOENT' ? 'output file not found' : err,
)
await exec(`rm ${name}.txt`, { cwd })
return eq(out, expect)
}
tests.push(async ({ path, eq }) =>
withArgAndExpect({ path, eq, arg: 'discovery', expect: 'verydisco' }),
)
tests.push(async ({ path, eq }) =>
withArgAndExpect({ path, eq, arg: 'kiss cool', expect: 'sski olco' }),
)
tests.push(async ({ path, eq }) =>
withArgAndExpect({
path,
eq,
arg: 'Node is awesome',
expect: 'deNo si omeawes',
}),
)
Object.freeze(tests)

13
subjects/verydisco-forever/README.md

@ -0,0 +1,13 @@
## verydisco-forever
### Instructions
Create a `verydisco-forever.mjs` script that does exactly the same as you verydisco script, but write the result in a `verydisco-forever.txt` file instead of printing it in the console.
### Notions
- [Node file system: `writeFile`](https://nodejs.org/api/fs.html#fs_fspromises_writefile_file_data_options)
- [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)
Loading…
Cancel
Save