UNPKG

poku

Version:

🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.

134 lines (133 loc) 3.8 kB
import { stat } from "node:fs/promises"; import { exit, argv } from "node:process"; import { hr, log$1 as log, format, GLOBAL, getArg, reporter, hasArg } from "../modules/_shared.js"; import "node:path"; import "node:os"; import "node:child_process"; import "node:assert"; import "node:assert/strict"; import "node:net"; const errors = [], pathExists = async (arg, path) => { try { await stat(path); } catch { errors.push(`--${arg}: ./${path} doesn't exists.`); } }, checkUselessValue = (arg) => { const prefix = arg.length === 1 ? "-" : "--"; typeof getArg(arg, prefix) < "u" && errors.push(`${prefix}${arg}: this flag shouldn't receive a value.`); }, checkRequiredValue = (arg) => { const prefix = arg.length === 1 ? "-" : "--"; hasArg(arg, prefix) && typeof getArg(arg, prefix) > "u" && errors.push(`${prefix}${arg}: this flag require a value.`); }, checkFlags = () => { const allowedFlags = /* @__PURE__ */ new Set([ "--concurrency", "--config", "--coverage", "--coverageConfig", "--debug", "--denoAllow", "--denoDeny", "--enforce", "--envFile", "--exclude", "--failFast", "--filter", "--isolation", "--killPid", "--killPort", "--killRange", "--only", "--quiet", "--reporter", "--sequential", "--testNamePattern", "--testSkipPattern", "--timeout", "--watch", "--watchInterval", "-c", "-d", "-q", "-r", "-t", "-w", "-x" ]), args = argv.slice(2); for (const arg of args) { const flag = arg.split("=")[0]; !allowedFlags.has(flag) && flag.startsWith("-") && errors.push(`${flag}: unrecognized flag.`); } }, checkValues = async () => { for (const flag of [ "debug", "enforce", "failFast", "only", "quiet", "sequential", "watch", "d", "x", "q", "w" ]) checkUselessValue(flag); for (const flag of [ "concurrency", "config", "isolation", "killPid", "killPort", "reporter", "testNamePattern", "testSkipPattern", "timeout", "watchInterval", "c", "r", "t" ]) checkRequiredValue(flag); GLOBAL.configFile && await pathExists("config", GLOBAL.configFile), GLOBAL.envFile && await pathExists("envFile", GLOBAL.envFile), getArg("concurrency") && typeof GLOBAL.configs.concurrency > "u" && errors.push("--concurrency: expects for a valid integer."), getArg("timeout") && typeof GLOBAL.configs.timeout > "u" && errors.push("--timeout: expects for a valid integer."); const isolationValue = getArg("isolation"); isolationValue && isolationValue !== "none" && isolationValue !== "process" && errors.push( `--isolation: "${isolationValue}" is not valid. Available: none, process.` ); const reporterValue = getArg("reporter") ?? getArg("r", "-"); reporterValue && !(reporterValue in reporter) && errors.push( `--reporter: "${reporterValue}" is not a valid reporter. Available: ${Object.keys(reporter).join(", ")}.` ); }, checkConfigFile = () => { const allowedProps = /* @__PURE__ */ new Set([ "$schema", "include", "sequential", "debug", "filter", "exclude", "failFast", "envFile", "exclude", "failFast", "concurrency", "quiet", "timeout", "envFile", "kill", "isolation", "platform", "deno", "testNamePattern", "testSkipPattern" ]); for (const prop in GLOBAL.configsFromFile) allowedProps.has(prop) || errors.push(`${prop}: unrecognized property in the config file.`); }, enforce = async () => { checkFlags(), checkConfigFile(), await checkValues(), errors.length > 0 && (hr(), log(`${format("Ensure Enabled").bold()} `), log(errors.map((flag) => format(flag).fail()).join(` `)), hr(), exit(1)); }; export { enforce };