Browse Source

update verydisco tests -> execute scripts in tmpdir

content-update
Louise Foussat 3 years ago committed by Clément
parent
commit
f8d01c34b8
  1. 36
      js/tests/verydisco-forever_test.mjs
  2. 29
      js/tests/verydisco-reverso_test.mjs

36
js/tests/verydisco-forever_test.mjs

@ -1,37 +1,45 @@
import fs from 'fs/promises' import fs from 'fs/promises'
import { join, resolve } from 'path'
import { tmpdir } from 'os'
const readFile = fs.readFile const readFile = fs.readFile
const mkdir = fs.mkdir
import { promisify } from 'util' import { promisify } from 'util'
import * as cp from 'child_process' import * as cp from 'child_process'
const exec = promisify(cp.exec) const exec = promisify(cp.exec)
export const tests = []
const name = 'verydisco-forever' const name = 'verydisco-forever'
const withArgAndExpect = async ({ arg, expect, path, eq }) => { export const tests = []
const cwd = `${path export const setup = async () => {
.split('/') const dir = tmpdir()
.slice(0, -1) // check if already exists and rm?
.join('/')}/` await mkdir(`${dir}/${name}`)
await exec(`node ${name}.mjs "${arg}"`, { cwd })
const out = await readFile(`${cwd}${name}.txt`, 'utf8').catch((err) => return { tmpPath: `${dir}/${name}` }
}
const withArgAndExpect = async ({ arg, expect, path, eq, ctx }) => {
const scriptPath = join(resolve(), path)
await exec(`node ${scriptPath} "${arg}"`, { cwd: ctx.tmpPath }) // ${name}.mjs
const out = await readFile(`${ctx.tmpPath}/${name}.txt`, 'utf8').catch((err) =>
err.code === 'ENOENT' ? 'output file not found' : err, err.code === 'ENOENT' ? 'output file not found' : err,
) )
await exec(`rm ${name}.txt`, { cwd }) await exec(`rm ${name}.txt`, { cwd: ctx.tmpPath })
return eq(out, expect) return eq(out, expect)
} }
tests.push(async ({ path, eq }) => tests.push(async ({ path, eq, ctx }) =>
withArgAndExpect({ path, eq, arg: 'discovery', expect: 'verydisco' }), withArgAndExpect({ path, eq, ctx, arg: 'discovery', expect: 'verydisco' }),
) )
tests.push(async ({ path, eq }) => tests.push(async ({ path, eq, ctx }) =>
withArgAndExpect({ path, eq, arg: 'kiss cool', expect: 'sski olco' }), withArgAndExpect({ path, eq, ctx, arg: 'kiss cool', expect: 'sski olco' }),
) )
tests.push(async ({ path, eq }) => tests.push(async ({ path, eq, ctx }) =>
withArgAndExpect({ withArgAndExpect({
path, path,
eq, eq,
ctx,
arg: 'Node is awesome', arg: 'Node is awesome',
expect: 'deNo si omeawes', expect: 'deNo si omeawes',
}), }),

29
js/tests/verydisco-reverso_test.mjs

@ -1,6 +1,9 @@
import fs from 'fs/promises' import fs from 'fs/promises'
import { tmpdir } from 'os'
import { join, resolve } from 'path'
const readFile = fs.readFile const readFile = fs.readFile
const writeFile = fs.writeFile const writeFile = fs.writeFile
const mkdir = fs.mkdir
import { promisify } from 'util' import { promisify } from 'util'
import * as cp from 'child_process' import * as cp from 'child_process'
@ -8,32 +11,38 @@ const exec = promisify(cp.exec)
export const tests = [] export const tests = []
const name = 'verydisco-reverso' const name = 'verydisco-reverso'
const hasContentAndExpect = async ({ content, expect, path, eq }) => { export const setup = async () => {
const cwd = `${path const dir = tmpdir()
.split('/') // check if already exists and rm?
.slice(0, -1) await mkdir(`${dir}/${name}`)
.join('/')}/`
await writeFile(`${cwd}${name}.txt`, content, 'utf8') return { tmpPath: `${dir}/${name}` }
const { stdout } = await exec(`node ${name}.mjs "${name}.txt"`, { cwd }) }
await exec(`rm ${name}.txt`, { cwd }) const hasContentAndExpect = async ({ content, expect, path, eq, ctx }) => {
const scriptPath = join(resolve(), path)
await writeFile(`${ctx.tmpPath}/${name}.txt`, content, 'utf8')
const { stdout } = await exec(`node ${scriptPath} "${name}.txt"`, { cwd: ctx.tmpPath })
await exec(`rm ${name}.txt`, { cwd: ctx.tmpPath })
return eq(stdout.trim(), expect) return eq(stdout.trim(), expect)
} }
tests.push(async ({ path, eq }) => tests.push(async ({ path, eq, ctx }) =>
hasContentAndExpect({ hasContentAndExpect({
path, path,
eq, eq,
ctx,
content: `deNo si omeawes`, content: `deNo si omeawes`,
expect: 'Node is awesome', expect: 'Node is awesome',
}), }),
) )
// tests.push(async ({ path, eq }) => { // tests.push(async ({ path, eq, ctx }) => {
// await hasContentAndExpect({ // await hasContentAndExpect({
// path, // path,
// eq, // eq,
// ctx,
// content: // newfilename, // content: // newfilename,
// expect: // reverso // expect: // reverso
// }) // })

Loading…
Cancel
Save