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

29
js/tests/verydisco-reverso_test.mjs

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

Loading…
Cancel
Save