diff --git a/js/tests/verydisco-forever_test.mjs b/js/tests/verydisco-forever_test.mjs new file mode 100644 index 00000000..dad6dd33 --- /dev/null +++ b/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) diff --git a/subjects/verydisco-forever/README.md b/subjects/verydisco-forever/README.md new file mode 100644 index 00000000..179ffa53 --- /dev/null +++ b/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) + +