|
|
@ -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', |
|
|
|
}), |
|
|
|
}), |
|
|
|