Browse Source

update tell-me-vip subject and tests -> ask to write a file with the vip list now instead of log it

pull/761/head
Louise Foussat 4 years ago committed by Clément
parent
commit
eb955b8d13
  1. 24
      js/tests/tell-me-vip_test.mjs
  2. 2
      subjects/tell-me-vip/README.md

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

@ -6,6 +6,7 @@ import { promisify } from 'util'
const mkdir = fs.mkdir const mkdir = fs.mkdir
const rmdir = fs.rmdir const rmdir = fs.rmdir
const writeFile = fs.writeFile const writeFile = fs.writeFile
const readFile = fs.readFile
const exec = promisify(cp.exec) const exec = promisify(cp.exec)
@ -87,7 +88,7 @@ const guests = [
'Gabriela Tucker', 'Gabriela Tucker',
'Kiri Wilcox', 'Kiri Wilcox',
] ]
const shuffle = arr => { const shuffle = (arr) => {
let i = arr.length let i = arr.length
let j, tmp let j, tmp
while (--i > 0) { while (--i > 0) {
@ -102,7 +103,7 @@ 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) =>
Object.entries(list) Object.entries(list)
.filter(([n, {answer}]) => answer === 'yes') .filter(([n, { answer }]) => answer === 'yes')
.map(([n, _]) => .map(([n, _]) =>
n n
.split(' ') .split(' ')
@ -110,10 +111,10 @@ const getExpected = (list) =>
.join(' '), .join(' '),
) )
.sort() .sort()
.map((g, i) => `${i +1}. ${g}`) .map((g, i) => `${i + 1}. ${g}`)
.join('\n') .join('\n')
const generateObj = () => ({ const generateObj = () => ({
answer: ['yes', 'no'][Math.floor(Math.random() * 2)] answer: ['yes', 'no'][Math.floor(Math.random() * 2)],
}) })
export const setup = async () => { export const setup = async () => {
@ -122,7 +123,9 @@ export const setup = async () => {
// check if already exists and rm? // check if already exists and rm?
await mkdir(`${dir}/${name}`) await mkdir(`${dir}/${name}`)
const randomList = getRandomList(guests) const randomList = getRandomList(guests)
const randomAnswers = Object.fromEntries(randomList.map(g => [g, generateObj()])) const randomAnswers = Object.fromEntries(
randomList.map((g) => [g, generateObj()]),
)
const expected = getExpected(randomAnswers) const expected = getExpected(randomAnswers)
await Promise.all( await Promise.all(
Object.entries(randomAnswers).map( Object.entries(randomAnswers).map(
@ -139,12 +142,15 @@ export const setup = async () => {
} }
const printVIPGuestList = async ({ arg, ctx, path, eq }) => { const printVIPGuestList = async ({ arg, ctx, path, eq }) => {
const scriptPath = join(resolve(), path) const scriptPath = join(resolve(), path)
const cwd = arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/${name}`
const { stdout } = await exec(`node ${scriptPath} ${arg}`, { const { stdout } = await exec(`node ${scriptPath} ${arg}`, {
cwd: arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/${name}`, cwd,
}) })
const out = await readFile(`${cwd}/vip.txt`, 'utf8').catch((err) =>
// await rmdir(`${ctx.tmpPath}/${name}`, { recursive: true }) err.code === 'ENOENT' ? 'output file not found' : err,
return eq(stdout.trim(), ctx.expected) )
await exec(`rm vip.txt`, { cwd })
return eq(out, ctx.expected)
} }
tests.push(async ({ path, eq, ctx }) => { tests.push(async ({ path, eq, ctx }) => {

2
subjects/tell-me-vip/README.md

@ -2,7 +2,7 @@
### Instructions ### Instructions
Create a `tell-me-vip.mjs` script that filters the guests who actually answered 'YES' to your invitation before rendering the precious output list. Create a `tell-me-vip.mjs` script that filters the guests who actually answered 'YES' to your invitation, and save this list in a `vip.txt` file.
### Notions ### Notions

Loading…
Cancel
Save