From 749c68c65a4229192c439bdab4bc433cb9f2d8cf Mon Sep 17 00:00:00 2001 From: Louise Foussat Date: Tue, 9 Mar 2021 11:37:04 +0000 Subject: [PATCH] rework tell-me-vip-tests & subject --- js/tests/tell-me-vip_test.mjs | 207 +++++++++++++++------------------ subjects/tell-me-vip/README.md | 1 + 2 files changed, 97 insertions(+), 111 deletions(-) diff --git a/js/tests/tell-me-vip_test.mjs b/js/tests/tell-me-vip_test.mjs index 359d63bda..f260dda8b 100644 --- a/js/tests/tell-me-vip_test.mjs +++ b/js/tests/tell-me-vip_test.mjs @@ -1,12 +1,8 @@ import * as cp from 'child_process' -import fs from 'fs/promises' -import { join, resolve, isAbsolute } from 'path' +import { mkdir, writeFile, readFile } 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 readFile = fs.readFile const exec = promisify(cp.exec) @@ -14,27 +10,6 @@ export const tests = [] const name = 'tell-me-vip' // maybe get the sames from an api? like https://parser.name/ const guests = [ - 'Shyam Langley', - 'Austin Harwood', - 'Reem Morgan', - 'Neal Chamberlain', - 'Ryan Walters', - 'Ocean Battle', - 'Ubaid Ballard', - 'Victoria Chan', - 'Dominika Mullen', - 'Heath Denton', - 'Lilith Hamilton', - 'Aisling Bailey', - 'Maizie Love', - 'Nathanial Franco', - 'Charmaine Bernard', - 'Sohail Downes', - 'Rabia Gomez', - 'Brendan Brennan', - 'Shannen Atherton', - 'Esa Villarreal', - 'Kayla Wynn', 'Gladys Hardy', 'Laaibah Rogers', 'Zishan Randolph', @@ -49,29 +24,6 @@ const guests = [ 'Leela Solomon', 'Frederick David', 'Eryk Winters', - 'Olli Obrien', - 'Jagoda Avalos', - 'Bethanie Emery', - 'Kenya Medina', - 'Ava-Mai Estes', - 'Robyn Jimenez', - 'Carly Alexander', - 'Jed Newman', - 'Marianna Sullivan', - 'Alicja Scott', - 'Isaac Guerrero', - 'Dion Huff', - 'Milly Quintero', - 'Kwabena Cairns', - 'Rukhsar Conley', - 'Glyn Townsend', - 'Colby Holmes', - 'Zeynep East', - 'Miriam Higgins', - 'Kaelan Clegg', - 'Sharna English', - 'Uma Ortega', - 'Crystal Bird', 'Christopher Haas', 'Olivier Galvan', 'Esha Herring', @@ -101,15 +53,12 @@ const shuffle = (arr) => { } const getRandomList = (names) => shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10)) -const getExpected = (list) => - Object.entries(list) - .filter(([n, { answer }]) => answer === 'yes') - .map(([n, _]) => - n - .split(' ') - .reverse() - .join(' '), - ) +const getExpected = (list) => list + .filter(([n, {answer}]) => answer === 'yes') + .map(([n, _]) => { + const reversed = n.split('_').reverse() + return `${reversed[0].slice(0, -5)} ${reversed[1]}` + }) .sort() .map((g, i) => `${i + 1}. ${g}`) .join('\n') @@ -117,72 +66,108 @@ const generateObj = () => ({ answer: ['yes', 'no'][Math.floor(Math.random() * 2)], }) -export const setup = async () => { - const dir = tmpdir() +const ranStr = () => + Math.random() + .toString(36) + .substring(7) +export const setup = async ({ path }) => { + const dir = `${tmpdir()}/tell-me-vip` - // check if already exists and rm? - await mkdir(`${dir}/${name}`) + await mkdir(`${dir}/guests`, { recursive: true }) const randomList = getRandomList(guests) - const randomAnswers = Object.fromEntries( - randomList.map((g) => [g, generateObj()]), - ) + const randomAnswers = randomList.map((g) => [ + g.replace(' ', '_').concat('.json'), + generateObj(), + ]) + const expected = getExpected(randomAnswers) - await Promise.all( - Object.entries(randomAnswers).map( - async ([n, answer]) => - await writeFile( - `${dir}/${name}/${n.replace(' ', '_')}.json`, - JSON.stringify(answer, null, '\t'), - 'utf8', - ), - ), - ) - return { tmpPath: dir, expected } -} -const printVIPGuestList = async ({ arg, ctx, path, eq }) => { - const scriptPath = join(resolve(), path) - const cwd = arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/${name}` - const { stdout } = await exec(`node ${scriptPath} ${arg}`, { - cwd, + const createFilesIn = async ({ files, folderPath }) => + await Promise.all( + files.map( + async ([fileName, content]) => + await writeFile(`${folderPath}/${fileName}`, JSON.stringify(content)), + ), + ) + await createFilesIn({ + files: randomAnswers, + folderPath: `${dir}/guests`, }) - const out = await readFile(`${cwd}/vip.txt`, 'utf8').catch((err) => - err.code === 'ENOENT' ? 'output file not found' : err, - ) - await exec(`rm vip.txt`, { cwd }) - return eq(out, ctx.expected) + + const run = async (cmd) => { + const cmdPath = isAbsolute(cmd) ? cmd : join(dir, cmd) + const { stdout } = await exec(`node ${path} ${cmdPath}`) + const fileContent = await readFile(`vip.txt`, 'utf8').catch((err) => + err.code === 'ENOENT' ? 'output file not found' : err, + ) + return { data: fileContent } + } + + return { tmpPath: dir, expected, run, createFilesIn } } tests.push(async ({ path, eq, ctx }) => { - // will execute the script with "tell-me-vip" as an argument - // `tell-me-vip` folder has random files names - return printVIPGuestList({ - path, - eq, - ctx, - arg: 'tell-me-vip', - }) + // test when no answers in the folder + const folderName = `guests-${ranStr()}` + const folderPath = join(ctx.tmpPath, folderName) + await mkdir(folderPath) + + const { data } = await ctx.run(folderName) + return eq('', data) }) tests.push(async ({ path, eq, ctx }) => { - // will execute the script without argument - // in the `tell-me-vip` folder - return printVIPGuestList({ - path, - eq, - ctx, - arg: '', - }) + // test when no one said yes + const files = [ + ['Ubaid_Ballard.json', { answer: 'no' }], + ['Victoria_Chan.json', { answer: 'no' }], + ['Dominika_Mullen.json', { answer: 'no' }], + ['Heath_Denton.json', { answer: 'no' }], + ['Lilith_Hamilton.json', { answer: 'no' }], + ] + const folderName = `guests-${ranStr()}` + const folderPath = join(ctx.tmpPath, folderName) + await mkdir(folderPath) + await ctx.createFilesIn({ folderPath, files }) + + const { data } = await ctx.run(folderName) + return eq('', data) }) tests.push(async ({ path, eq, ctx }) => { - // will execute the script with `tell-me-vip` folder's absolute path as argument - return printVIPGuestList({ - path, - eq, - ctx, - arg: `${ctx.tmpPath}/tell-me-vip`, - }) + const files = [ + ['Ubaid_Ballard.json', { answer: 'yes' }], + ['Victoria_Chan.json', { answer: 'yes' }], + ['Dominika_Mullen.json', { answer: 'no' }], + ['Heath_Denton.json', { answer: 'no' }], + ['Lilith_Hamilton.json', { answer: 'yes' }], + ] + const folderName = `guests-${ranStr()}` + const folderPath = join(ctx.tmpPath, folderName) + await mkdir(folderPath) + await ctx.createFilesIn({ folderPath, files }) + + const { data } = await ctx.run(folderName) + return eq( + [`1. Ballard Ubaid`, `2. Chan Victoria`, `3. Hamilton Lilith`], + data.split('\n'), + ) +}) + +tests.push(async ({ path, eq, ctx }) => { + // will execute the script in a folder named `guests` + // 'guests' in the argument passed + // `guests` folder has a random file number with random answers 'yes' or 'no' + const { data } = await ctx.run('guests') + return eq(data, ctx.expected) +}) + +tests.push(async ({ path, eq, ctx }) => { + // will execute the script with `guests` folder's absolute path as argument + const { data } = await ctx.run(`${ctx.tmpPath}/guests`) + return eq(data, ctx.expected) }) +// test error when no arg?... + Object.freeze(tests) diff --git a/subjects/tell-me-vip/README.md b/subjects/tell-me-vip/README.md index fd9a030b9..3692b8b6e 100644 --- a/subjects/tell-me-vip/README.md +++ b/subjects/tell-me-vip/README.md @@ -7,5 +7,6 @@ Create a `tell-me-vip.mjs` script that filters the guests who actually answered ### Notions - [Node file system: `readFile`](https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options) +- [Node path: `join`](https://nodejs.org/api/path.html#path_path_join_paths) - [`JSON.parse()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) - [`Array.prototype.filter()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)