mirror of https://github.com/01-edu/public.git
2 changed files with 35 additions and 62 deletions
@ -1,70 +1,48 @@ |
|||||||
import * as cp from 'child_process' |
import * as cp from 'child_process' |
||||||
import fs from 'fs/promises' |
import { mkdir, writeFile } from 'fs/promises' |
||||||
import { join, resolve, isAbsolute } from 'path' |
import { join, isAbsolute } from 'path' |
||||||
import { tmpdir } from 'os' |
import { tmpdir } from 'os' |
||||||
import { promisify } from 'util' |
import { promisify } from 'util' |
||||||
const mkdir = fs.mkdir |
|
||||||
const rmdir = fs.rmdir |
|
||||||
const writeFile = fs.writeFile |
|
||||||
|
|
||||||
const exec = promisify(cp.exec) |
const exec = promisify(cp.exec) |
||||||
|
|
||||||
export const tests = [] |
export const tests = [] |
||||||
export const setup = async () => { |
export const setup = async ({ path }) => { |
||||||
const randomFilesNumber = Math.floor(Math.random() * (30 - 1) + 1) |
const randomFilesNumber = Math.floor(Math.random() * (30 - 1) + 1) |
||||||
const dir = tmpdir() |
const dir = `${tmpdir()}/tell-me-how-many` |
||||||
|
|
||||||
// check if already exists and rm?
|
await mkdir(dir) |
||||||
await mkdir(`${dir}/random`) |
|
||||||
for (let i = 0; i < randomFilesNumber; i++) { |
for (let i = 0; i < randomFilesNumber; i++) { |
||||||
await writeFile(`${dir}/random/${i}.txt`, '', 'utf8') |
await writeFile(`${dir}/${i}.txt`, '', 'utf8') |
||||||
} |
} |
||||||
|
const run = async (cmd) => { |
||||||
|
const cmdPath = isAbsolute(cmd || '') ? cmd : join(dir, cmd || '') |
||||||
|
const { stdout } = await exec(`node ${path} ${cmdPath}`) |
||||||
|
|
||||||
return { randomFilesNumber, tmpPath: dir } |
return { stdout: stdout.trim() } |
||||||
} |
} |
||||||
const countDirLength = async ({ dirLength, arg, ctx, path, eq }) => { |
|
||||||
const scriptPath = join(resolve(), path) |
|
||||||
const { stdout } = await exec(`node ${scriptPath} ${arg}`, { |
|
||||||
cwd: arg ? `${ctx.tmpPath}` : `${ctx.tmpPath}/random`, |
|
||||||
}) |
|
||||||
|
|
||||||
// await rmdir(`${ctx.tmpPath}/random`, { recursive: true })
|
return { randomFilesNumber, tmpPath: dir, run } |
||||||
return eq(Number(stdout.trim()), dirLength) |
|
||||||
} |
} |
||||||
|
|
||||||
tests.push(async ({ path, eq, ctx }) => { |
tests.push(async ({ path, eq, ctx }) => { |
||||||
// will execute the script with "random" as an argument
|
// will execute the script in a folder named `tell-me-how-many`
|
||||||
// `random` folder has a random file number
|
// '../tell-me-how-many' in the argument passed
|
||||||
return countDirLength({ |
// `tell-me-how-many` folder has a random file number
|
||||||
path, |
const { stdout } = await ctx.run('../tell-me-how-many') |
||||||
eq, |
return eq(Number(stdout), ctx.randomFilesNumber) |
||||||
ctx, |
|
||||||
arg: 'random', |
|
||||||
dirLength: ctx.randomFilesNumber, |
|
||||||
}) |
|
||||||
}) |
}) |
||||||
|
|
||||||
tests.push(async ({ path, eq, ctx }) => { |
tests.push(async ({ path, eq, ctx }) => { |
||||||
// will execute the script without argument
|
// will execute the script without argument
|
||||||
// in the `random` folder
|
const { stdout } = await ctx.run() |
||||||
return countDirLength({ |
return eq(Number(stdout), ctx.randomFilesNumber) |
||||||
path, |
|
||||||
eq, |
|
||||||
ctx, |
|
||||||
arg: '', |
|
||||||
dirLength: ctx.randomFilesNumber, |
|
||||||
}) |
|
||||||
}) |
}) |
||||||
|
|
||||||
tests.push(async ({ path, eq, ctx }) => { |
tests.push(async ({ path, eq, ctx }) => { |
||||||
// will execute the script with `random` folder's absolute path as argument
|
// will execute the script with `tell-me-how-many` folder's absolute path as argument
|
||||||
return countDirLength({ |
const { stdout } = await ctx.run(ctx.tmpPath) |
||||||
path, |
return eq(Number(stdout), ctx.randomFilesNumber) |
||||||
eq, |
|
||||||
ctx, |
|
||||||
arg: `${ctx.tmpPath}/random`, |
|
||||||
dirLength: ctx.randomFilesNumber, |
|
||||||
}) |
|
||||||
}) |
}) |
||||||
|
|
||||||
Object.freeze(tests) |
Object.freeze(tests) |
||||||
|
Loading…
Reference in new issue