forked from root/public
3 changed files with 153 additions and 69 deletions
@ -1,51 +1,83 @@
|
||||
import fs from 'fs/promises' |
||||
import { mkdir, writeFile } 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 { join, isAbsolute } from 'path' |
||||
import { promisify } from 'util' |
||||
import * as cp from 'child_process' |
||||
|
||||
const exec = promisify(cp.exec) |
||||
|
||||
export const tests = [] |
||||
const name = 'verydisco-reverso' |
||||
export const setup = async () => { |
||||
const dir = tmpdir() |
||||
// check if already exists and rm?
|
||||
await mkdir(`${dir}/${name}`) |
||||
const randomLetters = (number) => |
||||
Math.random() |
||||
.toString(36) |
||||
.substring(0, number) |
||||
export const setup = async ({ path }) => { |
||||
const dir = `${tmpdir()}/verydisco-reverso` |
||||
await mkdir(dir) |
||||
const run = async (cmd) => { |
||||
const cmdPath = isAbsolute(cmd || '') ? cmd : join(dir, cmd || '') |
||||
const { stdout } = await exec(`node ${path} ${cmdPath}`) |
||||
|
||||
return { tmpPath: `${dir}/${name}` } |
||||
return { stdout: stdout.trim() } |
||||
} |
||||
|
||||
return { tmpPath: dir, run } |
||||
} |
||||
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 }) |
||||
tests.push(async ({ path, eq, ctx }) => { |
||||
const trueWords = 'kisscool' |
||||
await writeFile(`${ctx.tmpPath}/fever.txt`, trueWords, 'utf8') |
||||
|
||||
return eq(stdout.trim(), expect) |
||||
} |
||||
const { stdout } = await ctx.run(`${ctx.tmpPath}/fever.txt`) |
||||
return eq(stdout, 'coolkiss') |
||||
}) |
||||
|
||||
tests.push(async ({ path, eq, ctx }) => { |
||||
const trueWords = 'verydisco' |
||||
await writeFile(`${ctx.tmpPath}/fever.txt`, trueWords, 'utf8') |
||||
|
||||
const { stdout } = await ctx.run(`${ctx.tmpPath}/fever.txt`) |
||||
return eq(stdout, 'discovery') |
||||
}) |
||||
|
||||
tests.push(async ({ path, eq, ctx }) => { |
||||
const trueWords = 'deNo si omeawes' |
||||
await writeFile(`${ctx.tmpPath}/myTruth.txt`, trueWords, 'utf8') |
||||
|
||||
const { stdout } = await ctx.run(`${ctx.tmpPath}/myTruth.txt`) |
||||
return eq(stdout, 'Node is awesome') |
||||
}) |
||||
|
||||
tests.push(async ({ path, eq, ctx }) => { |
||||
const recto = randomLetters(3) |
||||
const verso = randomLetters(3) |
||||
const meaningOfLife = `${recto}${verso}` |
||||
await writeFile(`${ctx.tmpPath}/theTruth.txt`, meaningOfLife, 'utf8') |
||||
|
||||
const { stdout } = await ctx.run(`${ctx.tmpPath}/theTruth.txt`) |
||||
return eq(stdout, `${verso}${recto}`) |
||||
}) |
||||
|
||||
tests.push(async ({ path, eq, ctx }) => { |
||||
const heads = randomLetters(3) |
||||
const tails = randomLetters(4) |
||||
const coinFlipping = `${heads}${tails}` |
||||
await writeFile(`${ctx.tmpPath}/coins-flipping.txt`, coinFlipping, 'utf8') |
||||
|
||||
const { stdout } = await ctx.run(`${ctx.tmpPath}/coins-flipping.txt`) |
||||
return eq(stdout, `${tails}${heads}`) |
||||
}) |
||||
|
||||
tests.push(async ({ path, eq, ctx }) => { |
||||
const ying = randomLetters(8) |
||||
const yang = randomLetters(8) |
||||
const tic = randomLetters(5) |
||||
const tac = randomLetters(6) |
||||
const absurd = `${tic}${tac} ${ying}${yang}` |
||||
await writeFile(`${ctx.tmpPath}/absurd.txt`, absurd, 'utf8') |
||||
|
||||
tests.push(async ({ path, eq, ctx }) => |
||||
hasContentAndExpect({ |
||||
path, |
||||
eq, |
||||
ctx, |
||||
content: `deNo si omeawes`, |
||||
expect: 'Node is awesome', |
||||
}), |
||||
) |
||||
|
||||
// tests.push(async ({ path, eq, ctx }) => {
|
||||
// await hasContentAndExpect({
|
||||
// path,
|
||||
// eq,
|
||||
// ctx,
|
||||
// content: // newfilename,
|
||||
// expect: // reverso
|
||||
// })
|
||||
// })
|
||||
const { stdout } = await ctx.run(`${ctx.tmpPath}/absurd.txt`) |
||||
return eq(stdout, `${tac}${tic} ${yang}${ying}`) |
||||
}) |
||||
|
||||
Object.freeze(tests) |
||||
|
Loading…
Reference in new issue