Browse Source

rework tell-me-vip-tests & subject

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
749c68c65a
  1. 207
      js/tests/tell-me-vip_test.mjs
  2. 1
      subjects/tell-me-vip/README.md

207
js/tests/tell-me-vip_test.mjs

@ -1,12 +1,8 @@
import * as cp from 'child_process' import * as cp from 'child_process'
import fs from 'fs/promises' import { mkdir, writeFile, readFile } 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 readFile = fs.readFile
const exec = promisify(cp.exec) const exec = promisify(cp.exec)
@ -14,27 +10,6 @@ export const tests = []
const name = 'tell-me-vip' const name = 'tell-me-vip'
// maybe get the sames from an api? like https://parser.name/ // maybe get the sames from an api? like https://parser.name/
const guests = [ 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', 'Gladys Hardy',
'Laaibah Rogers', 'Laaibah Rogers',
'Zishan Randolph', 'Zishan Randolph',
@ -49,29 +24,6 @@ const guests = [
'Leela Solomon', 'Leela Solomon',
'Frederick David', 'Frederick David',
'Eryk Winters', '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', 'Christopher Haas',
'Olivier Galvan', 'Olivier Galvan',
'Esha Herring', 'Esha Herring',
@ -101,15 +53,12 @@ const shuffle = (arr) => {
} }
const getRandomList = (names) => const getRandomList = (names) =>
shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10)) shuffle(names).slice(0, Math.floor(Math.random() * (names.length - 10) + 10))
const getExpected = (list) => const getExpected = (list) => list
Object.entries(list) .filter(([n, {answer}]) => answer === 'yes')
.filter(([n, { answer }]) => answer === 'yes') .map(([n, _]) => {
.map(([n, _]) => const reversed = n.split('_').reverse()
n return `${reversed[0].slice(0, -5)} ${reversed[1]}`
.split(' ') })
.reverse()
.join(' '),
)
.sort() .sort()
.map((g, i) => `${i + 1}. ${g}`) .map((g, i) => `${i + 1}. ${g}`)
.join('\n') .join('\n')
@ -117,72 +66,108 @@ const generateObj = () => ({
answer: ['yes', 'no'][Math.floor(Math.random() * 2)], answer: ['yes', 'no'][Math.floor(Math.random() * 2)],
}) })
export const setup = async () => { const ranStr = () =>
const dir = tmpdir() 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}/guests`, { recursive: true })
await mkdir(`${dir}/${name}`)
const randomList = getRandomList(guests) const randomList = getRandomList(guests)
const randomAnswers = Object.fromEntries( const randomAnswers = randomList.map((g) => [
randomList.map((g) => [g, generateObj()]), g.replace(' ', '_').concat('.json'),
) generateObj(),
])
const expected = getExpected(randomAnswers) 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 createFilesIn = async ({ files, folderPath }) =>
} await Promise.all(
const printVIPGuestList = async ({ arg, ctx, path, eq }) => { files.map(
const scriptPath = join(resolve(), path) async ([fileName, content]) =>
const cwd = arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/${name}` await writeFile(`${folderPath}/${fileName}`, JSON.stringify(content)),
const { stdout } = await exec(`node ${scriptPath} ${arg}`, { ),
cwd, )
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, const run = async (cmd) => {
) const cmdPath = isAbsolute(cmd) ? cmd : join(dir, cmd)
await exec(`rm vip.txt`, { cwd }) const { stdout } = await exec(`node ${path} ${cmdPath}`)
return eq(out, ctx.expected) 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 }) => { tests.push(async ({ path, eq, ctx }) => {
// will execute the script with "tell-me-vip" as an argument // test when no answers in the folder
// `tell-me-vip` folder has random files names const folderName = `guests-${ranStr()}`
return printVIPGuestList({ const folderPath = join(ctx.tmpPath, folderName)
path, await mkdir(folderPath)
eq,
ctx, const { data } = await ctx.run(folderName)
arg: 'tell-me-vip', return eq('', data)
})
}) })
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {
// will execute the script without argument // test when no one said yes
// in the `tell-me-vip` folder const files = [
return printVIPGuestList({ ['Ubaid_Ballard.json', { answer: 'no' }],
path, ['Victoria_Chan.json', { answer: 'no' }],
eq, ['Dominika_Mullen.json', { answer: 'no' }],
ctx, ['Heath_Denton.json', { answer: 'no' }],
arg: '', ['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 }) => { tests.push(async ({ path, eq, ctx }) => {
// will execute the script with `tell-me-vip` folder's absolute path as argument const files = [
return printVIPGuestList({ ['Ubaid_Ballard.json', { answer: 'yes' }],
path, ['Victoria_Chan.json', { answer: 'yes' }],
eq, ['Dominika_Mullen.json', { answer: 'no' }],
ctx, ['Heath_Denton.json', { answer: 'no' }],
arg: `${ctx.tmpPath}/tell-me-vip`, ['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) Object.freeze(tests)

1
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 ### Notions
- [Node file system: `readFile`](https://nodejs.org/api/fs.html#fs_fspromises_readfile_path_options) - [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) - [`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) - [`Array.prototype.filter()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

Loading…
Cancel
Save