Browse Source

Always use directory instead of both folder and directory

content-update
Clement Denis 3 years ago committed by Clément
parent
commit
396cd4a71f
  1. 65
      js/tests/happiness-manager_test.mjs
  2. 22
      js/tests/tell-me-how-many_test.mjs
  3. 34
      js/tests/tell-me-vip_test.mjs
  4. 14
      js/tests/tell-me-who_test.mjs
  5. 2
      subjects/happiness-manager/README.md
  6. 23
      subjects/tell-me-how-many/README.md
  7. 7
      subjects/tell-me-who/README.md

65
js/tests/happiness-manager_test.mjs

@ -11,13 +11,14 @@ export const setup = async ({ path }) => {
await mkdir(tmpPath)
await mkdir(`${tmpPath}/guests`)
const run = async (folder, file) => {
const run = async (dir, file) => {
const output = await exec(
`node ${path} ${tmpPath}/${folder} ${tmpPath}/${file}`,
)
const fileContent = await readFile(`${tmpPath}/${file}`, 'utf8').catch(
(err) => (err.code === 'ENOENT' ? 'output file not found' : err),
`node ${path} ${tmpPath}/${dir} ${tmpPath}/${file}`,
)
const fileContent = await readFile(
`${tmpPath}/${file}`,
'utf8',
).catch(err => (err.code === 'ENOENT' ? 'output file not found' : err))
return {
data:
@ -27,18 +28,18 @@ export const setup = async ({ path }) => {
stdout: output.stdout.trim(),
}
}
const resetAnswersIn = async ({ folder }) => {
const dir = await readdir(`${tmpPath}/${folder}`)
await Promise.all(dir.map((file) => rm(`${tmpPath}/${folder}/${file}`)))
const resetAnswersIn = async ({ dir }) => {
const dir = await readdir(`${tmpPath}/${dir}`)
await Promise.all(dir.map(file => rm(`${tmpPath}/${dir}/${file}`)))
}
const createAnswers = (nb, elem) => [...Array(nb).keys()].map(() => elem)
const setAnswersIn = async ({ answers, folder }) => {
await resetAnswersIn({ folder })
const setAnswersIn = async ({ answers, dir }) => {
await resetAnswersIn({ dir })
await Promise.all(
answers.map(
async (content, idx) =>
await writeFile(
`${tmpPath}/${folder}/${idx}.json`,
`${tmpPath}/${dir}/${idx}.json`,
JSON.stringify(content, null, '\t'),
'utf8',
),
@ -53,7 +54,7 @@ tests.push(async ({ eq, ctx }) => {
// test with no vips (no {answer: yes})
// no file should be created, a special message should appear in console
const answers = ctx.createAnswers(2, { answer: 'no' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { stdout, data } = await ctx.run('guests', 'happy-list.json')
return eq(
@ -66,7 +67,7 @@ tests.push(async ({ eq, ctx }) => {
// test when vips answer { food: 'carnivores' }
// should create a list with burgers and potatoes
const answers = ctx.createAnswers(2, { answer: 'yes', food: 'carnivore' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-carn-list.json')
return eq(data, { burgers: 2, potatoes: 2 })
@ -79,7 +80,7 @@ tests.push(async ({ eq, ctx }) => {
{ answer: 'no', food: 'fish' },
...ctx.createAnswers(3, { answer: 'yes', food: 'fish' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-fish-list.json')
return eq(data, { potatoes: 3, sardines: 3 })
@ -92,7 +93,7 @@ tests.push(async ({ eq, ctx }) => {
{ answer: 'no', food: 'everything' },
...ctx.createAnswers(3, { answer: 'yes', food: 'everything' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-omni-list.json')
return eq(data, { potatoes: 3, kebabs: 3 })
@ -105,7 +106,7 @@ tests.push(async ({ eq, ctx }) => {
{ answer: 'no', drink: 'beer' },
...ctx.createAnswers(1, { answer: 'yes', drink: 'beer' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-beer-list.json')
return eq(data, { potatoes: 1, '6-packs-beers': 1 })
@ -118,7 +119,7 @@ tests.push(async ({ eq, ctx }) => {
{ answer: 'no', drink: 'beer' },
...ctx.createAnswers(6, { answer: 'yes', drink: 'beer' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-beer-pack-list.json')
return eq(data, { potatoes: 6, '6-packs-beers': 1 })
@ -131,7 +132,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(3, { answer: 'no', drink: 'wine' }),
...ctx.createAnswers(5, { answer: 'yes', drink: 'wine' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-wine-list.json')
return eq(data, { potatoes: 5, 'wine-bottles': 2 })
@ -141,7 +142,7 @@ tests.push(async ({ eq, ctx }) => {
// test when vips answer { drink: 'wine' }
// should create a list with wine-bottles and potatoes
const answers = ctx.createAnswers(8, { answer: 'yes', drink: 'wine' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-wine-bottle-list.json')
return eq(data, { potatoes: 8, 'wine-bottles': 2 })
@ -154,7 +155,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(2, { answer: 'no', drink: 'water' }),
...ctx.createAnswers(2, { answer: 'yes', drink: 'water' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-water-list.json')
return eq(data, { potatoes: 2, 'water-bottles': 1 })
@ -164,7 +165,7 @@ tests.push(async ({ eq, ctx }) => {
// test when vips answer { drink: 'water' }
// should create a list with water-bottles and potatoes
const answers = ctx.createAnswers(7, { answer: 'yes', drink: 'water' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-water-bottle-list.json')
return eq(data, { potatoes: 7, 'water-bottles': 2 })
@ -177,7 +178,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(8, { answer: 'no', drink: 'soft' }),
...ctx.createAnswers(12, { answer: 'yes', drink: 'soft' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-soft-list.json')
return eq(data, { potatoes: 12, 'soft-bottles': 3 })
@ -187,7 +188,7 @@ tests.push(async ({ eq, ctx }) => {
// test when vips answer { drink: 'soft' }
// should create a list with soft-bottles and potatoes
const answers = ctx.createAnswers(13, { answer: 'yes', drink: 'soft' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-soft-bottle-list.json')
return eq(data, { potatoes: 13, 'soft-bottles': 4 })
@ -202,7 +203,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(2, { answer: 'no', food: 'vegan' }),
...ctx.createAnswers(4, { answer: 'yes', food: 'vegan' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-vegan-list.json')
return eq(data, {
@ -218,7 +219,7 @@ tests.push(async ({ eq, ctx }) => {
// test when vips answer { food: 'vegan' }
// should create a list with eggplants, mushrooms, hummus, courgettes and potatoes
const answers = ctx.createAnswers(6, { answer: 'yes', food: 'vegan' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-vegan-list.json')
return eq(data, {
@ -238,7 +239,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(2, { answer: 'no', food: 'veggie' }),
...ctx.createAnswers(4, { answer: 'yes', food: 'veggie' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-veggie-list.json')
return eq(data, {
@ -254,7 +255,7 @@ tests.push(async ({ eq, ctx }) => {
// test when vips answer { food: 'veggie' }
// should create a list with eggplants, mushrooms, hummus, courgettes and potatoes
const answers = ctx.createAnswers(6, { answer: 'yes', food: 'veggie' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-veggie-list.json')
return eq(data, {
@ -274,7 +275,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(4, { answer: 'yes', food: 'vegan' }),
...ctx.createAnswers(2, { answer: 'yes', food: 'veggie' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-vegg-list.json')
return eq(data, {
@ -293,7 +294,7 @@ tests.push(async ({ eq, ctx }) => {
...ctx.createAnswers(6, { answer: 'yes', food: 'vegan' }),
...ctx.createAnswers(1, { answer: 'yes', food: 'veggie' }),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'happy-vegg-list.json')
return eq(data, {
@ -314,7 +315,7 @@ tests.push(async ({ eq, ctx }) => {
JSON.stringify({ candies: 2000 }),
)
const answers = ctx.createAnswers(1, { answer: 'yes', food: 'vegan' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'old-happy-list.json')
return eq(data, {
@ -335,7 +336,7 @@ tests.push(async ({ eq, ctx }) => {
JSON.stringify({ candies: 2000, potatoes: 32 }),
)
const answers = ctx.createAnswers(1, { answer: 'yes', food: 'vegan' })
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'old-happy-list.json')
return eq(data, {
@ -372,7 +373,7 @@ tests.push(async ({ eq, ctx }) => {
drink: 'beer',
}),
]
await ctx.setAnswersIn({ folder: 'guests', answers })
await ctx.setAnswersIn({ dir: 'guests', answers })
const { data } = await ctx.run('guests', 'party.json')
return eq(data, {

22
js/tests/tell-me-how-many_test.mjs

@ -19,31 +19,31 @@ export const setup = async ({ path }) => {
return { stdout: stdout.trim() }
}
const createXFilesIn = async ({ numberOfFiles, folderPath }) => {
const createXFilesIn = async ({ numberOfFiles, dirPath }) => {
for (let i = 0; i < numberOfFiles; i++) {
await writeFile(`${folderPath}/${i}.txt`, '', 'utf8')
await writeFile(`${dirPath}/${i}.txt`, '', 'utf8')
}
}
await createXFilesIn({ numberOfFiles: randomFilesNumber, folderPath: dir })
await createXFilesIn({ numberOfFiles: randomFilesNumber, dirPath: dir })
return { randomFilesNumber, tmpPath: dir, run, createXFilesIn }
}
tests.push(async ({ path, eq, ctx, between }) => {
const numberOfFiles = between(5, 13)
const folderName = `tell-me-how-many-${numberOfFiles}`
const folderPath = join(ctx.tmpPath, `../${folderName}`)
await mkdir(folderPath)
await ctx.createXFilesIn({ folderPath, numberOfFiles })
const dirName = `tell-me-how-many-${numberOfFiles}`
const dirPath = join(ctx.tmpPath, `../${dirName}`)
await mkdir(dirPath)
await ctx.createXFilesIn({ dirPath, numberOfFiles })
const { stdout } = await ctx.run(`../${folderName}`)
const { stdout } = await ctx.run(`../${dirName}`)
return eq(Number(stdout), numberOfFiles)
})
tests.push(async ({ path, eq, ctx }) => {
// will execute the script in a folder named `tell-me-how-many`
// will execute the script in a directory named `tell-me-how-many`
// '../tell-me-how-many' in the argument passed
// `tell-me-how-many` folder has a random file number
// `tell-me-how-many` directory has a random file number
const { stdout } = await ctx.run('../tell-me-how-many')
return eq(Number(stdout), ctx.randomFilesNumber)
})
@ -55,7 +55,7 @@ tests.push(async ({ path, eq, ctx }) => {
})
tests.push(async ({ path, eq, ctx }) => {
// will execute the script with `tell-me-how-many` folder's absolute path as argument
// will execute the script with `tell-me-how-many` directory's absolute path as argument
const { stdout } = await ctx.run(ctx.tmpPath)
return eq(Number(stdout), ctx.randomFilesNumber)
})

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

@ -12,10 +12,10 @@ export const setup = async ({ path }) => {
await mkdir(`${dir}/guests`, { recursive: true })
const createFilesIn = ({ files, folderPath }) =>
const createFilesIn = ({ files, dirPath }) =>
Promise.all(
files.map(([fileName, content]) =>
writeFile(`${folderPath}/${fileName}`, JSON.stringify(content)),
writeFile(`${dirPath}/${fileName}`, JSON.stringify(content)),
),
)
@ -31,12 +31,12 @@ export const setup = async ({ path }) => {
}
tests.push(async ({ randStr, eq, ctx }) => {
// test when no answers in the folder
const folderName = `guests-${randStr()}`
const folderPath = join(ctx.tmpPath, folderName)
await mkdir(folderPath)
// test when no answers in the dir
const dirName = `guests-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
const { data } = await ctx.run(folderName)
const { data } = await ctx.run(dirName)
return eq('', data)
})
@ -49,12 +49,12 @@ tests.push(async ({ randStr, eq, ctx }) => {
['Heath_Denton.json', { answer: 'no' }],
['Lilith_Hamilton.json', { answer: 'no' }],
]
const folderName = `guests-${randStr()}`
const folderPath = join(ctx.tmpPath, folderName)
await mkdir(folderPath)
await ctx.createFilesIn({ folderPath, files })
const dirName = `guests-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
await ctx.createFilesIn({ dirPath, files })
const { data } = await ctx.run(folderName)
const { data } = await ctx.run(dirName)
return eq('', data)
})
@ -68,12 +68,12 @@ tests.push(async ({ randStr, eq, ctx }) => {
['Lilith_Hamilton.json', { answer: 'yes' }],
[`${random}_Random.json`, { answer: 'yes' }],
]
const folderName = `guests-${randStr()}`
const folderPath = join(ctx.tmpPath, folderName)
await mkdir(folderPath)
await ctx.createFilesIn({ folderPath, files })
const dirName = `guests-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
await ctx.createFilesIn({ dirPath, files })
const { data } = await ctx.run(folderName)
const { data } = await ctx.run(dirName)
return eq(
[
`1. Ballard Ubaid`,

14
js/tests/tell-me-who_test.mjs

@ -11,10 +11,10 @@ export const setup = async ({ path }) => {
const dir = `${tmpdir()}/tell-me-who`
await mkdir(dir)
const createFilesIn = async ({ fileNames, folderPath }) =>
const createFilesIn = async ({ fileNames, dirPath }) =>
await Promise.all(
fileNames.map(
async (fileName) => await writeFile(`${folderPath}/${fileName}`, ''),
async (fileName) => await writeFile(`${dirPath}/${fileName}`, ''),
),
)
@ -37,12 +37,12 @@ tests.push(async ({ eq, ctx, randStr }) => {
'Heath_Denton.json',
`${random}_Hamilton.json`,
]
const folderName = `them-${randStr()}`
const folderPath = join(ctx.tmpPath, folderName)
await mkdir(folderPath)
await ctx.createFilesIn({ folderPath, fileNames })
const dirName = `them-${randStr()}`
const dirPath = join(ctx.tmpPath, dirName)
await mkdir(dirPath)
await ctx.createFilesIn({ dirPath, fileNames })
const { stdout } = await ctx.run(folderName)
const { stdout } = await ctx.run(dirName)
return eq(
[
`1. Ballard Ubaid`,

2
subjects/happiness-manager/README.md

@ -14,7 +14,7 @@ who wants to eat what and integrate that in your barbecue's shopping list!
The script must:
- Take a folder as first argument (the `guest` folder)
- Take a directory as first argument (the `guest` directory)
- Take a file `.json` as second argument:
- If the file already exists, it will add the informations to it.
- If it doesn't, the script must handle the creation of the file.

23
subjects/tell-me-how-many/README.md

@ -2,20 +2,24 @@
### Instructions
Your very favorite person's birthday is coming soon. So you've decided to organise a very special party 🥳🪅🎤
Your very favorite person's birthday is coming soon. So you've decided to
organise a very special party 🥳🪅🎤
Invitations has been sent for a while...
Good news: answers are back!
Psst: Sorry buddy, we didn't count it, you've been too generous. But we saved every one of them as a file in a special folder for you. Have fun!
Good news: answers are back! Psst: Sorry buddy, we didn't count it, you've been
too generous. But we saved every one of them as a file in a special directory for
you. Have fun!
Create a `tell-me-how-many.mjs` script that:
- Take a relative or absolute folder path as argument from the command line.
- Take a relative or absolute directory path as argument from the command line.
- Read this directory path.
- Get the number of entries in this folder.
- Get the number of entries in this directory.
- Print the result in console.
If there is no argument passed, the script must execute itself in the current directory
If there is no argument passed, the script must execute itself in the current
directory
### Notions
@ -23,6 +27,7 @@ If there is no argument passed, the script must execute itself in the current di
### Provided files
Download [`guests.zip`](https://assets.01-edu.org/tell-me-how-many/guests.zip) to have at your disposal the `guests` folder containing the files to count in your script. You must save it in your `tell-me-how-many` exercise folder to test your script on it.
Download [`guests.zip`](https://assets.01-edu.org/tell-me-how-many/guests.zip)
to have at your disposal the `guests` directory containing the files to count in
your script. You must save it in your `tell-me-how-many` exercise directory to test
your script on it.

7
subjects/tell-me-who/README.md

@ -2,16 +2,17 @@
### Instructions
Create a `tell-me-who.mjs` script that takes your folder path as an argument
Create a `tell-me-who.mjs` script that takes your directory path as an argument
and print the names of the guests that answered to your invitation in the
console.
The output must print one guest per line, in ascending alphabetic
order, and formated as following: `Number. Lastname Firstname` (starting from 1).
The output must print one guest per line, in ascending alphabetic order, and
formated as following: `Number. Lastname Firstname` (starting from 1).
### Notions
- [Node file system: `readdir`](https://nodejs.org/api/fs.html#fs_fspromises_readdir_path_options)
- [`Promise.all()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all)
- [`Array.prototype.map()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)
- [`String.prototype.split()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)
- [`Array.prototype.join()` method](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

Loading…
Cancel
Save