| 1 | /* eslint-disable no-await-in-loop */ |
| 2 | import nodeOS from "os"; |
| 3 | import fs from "fs"; |
| 4 | import path from "path"; |
| 5 | import pc from "picocolors"; |
| 6 | import { fileURLToPath } from "url"; |
| 7 | import util from "util"; |
| 8 | import { GenerationError } from "../src/errors.js"; |
| 9 | import { run } from "../src/run.js"; |
| 10 | import { Config } from "./types.js"; |
| 11 | import testHelpers from "../src/test-helpers.js"; |
| 12 | import { envReplacingCwdValues } from "./utils.js"; |
| 13 | |
| 14 | const dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 15 | |
| 16 | const fixturesPath = path.join(dirname, "fixtures"); |
| 17 | const dirs = fs |
| 18 | .readdirSync(fixturesPath, { withFileTypes: true }) |
| 19 | .filter((file) => file.isDirectory() && file.name !== ".DS_Store"); |
| 20 | |
| 21 | const originalOSTypeMethod = nodeOS.type; |
| 22 | const originalProcessEnv = process.env; |
| 23 | const originalgetAssertDataHeader = testHelpers.getAssertDataHeader; |
| 24 | const originalConsoleLog = console.log; |
| 25 | |
| 26 | async function runFixtures() { |
| 27 | let hadErrors = false; |
| 28 | for (const dir of dirs) { |
| 29 | const fixtureDirPath = path.join(fixturesPath, dir.name); |
| 30 | const outputPath = path.join(fixtureDirPath, "output.txt"); // gitignored |
| 31 | const expectedPath = path.join(fixtureDirPath, "expected.txt"); |
| 32 | const configPath = path.join(fixtureDirPath, "config.json"); |
| 33 | |
| 34 | // If assert is undefined it means that the request should never touch the server |
| 35 | const { options, assert, os, env }: Config = JSON.parse( |
| 36 | fs.readFileSync(configPath, { encoding: "utf-8" }) |
| 37 | ); |
| 38 | if (options.specPath) options.specPath = path.resolve(fixtureDirPath, options.specPath); |
| 39 | if (options.binaryPath) options.binaryPath = path.resolve(fixtureDirPath, options.binaryPath); |
| 40 | |
| 41 | // Mock Functions and assign env variables |
| 42 | const encodedAssertData = Buffer.from(JSON.stringify(assert ?? "{}")).toString("base64"); |
| 43 | |
| 44 | testHelpers.getAssertDataHeader = () => |
| 45 | encodedAssertData ? { "assert-data": encodedAssertData } : {}; |
| 46 | if (env) { |
| 47 | process.env = { ...process.env, ...envReplacingCwdValues(env, fixtureDirPath) }; |
| 48 | } |
| 49 | if (os) { |
| 50 | nodeOS.type = () => (os === "Linux" ? "Linux" : os === "MacOS" ? "Darwin" : "Windows_NT"); |
| 51 | } |
| 52 | |
| 53 | let out = ""; |
| 54 | try { |
| 55 | console.log = (...args) => { |
| 56 | out += `${util.format(...args)}\n`; |
| 57 | }; |
| 58 | await run(options); |
| 59 | } catch (error) { |
| 60 | const { name, message } = error as Error; |
| 61 | if (error instanceof GenerationError) { |
| 62 | out = |
| 63 | "GenerationError: some kind of generation error was produced, run the tests with VERBOSE=true env variable to see more"; |
| 64 | } else { |
| 65 | out = `${name}: ${message}`; |
| 66 | } |
| 67 | if (process.env.VERBOSE) { |
| 68 | console.error(error); |
| 69 | } |
| 70 | } finally { |
| 71 | console.log = originalConsoleLog; |
| 72 | } |
| 73 | |
| 74 | // Unmock Functions and assign env variables |
| 75 | process.env = originalProcessEnv; |
| 76 | nodeOS.type = originalOSTypeMethod; |
| 77 | testHelpers.getAssertDataHeader = originalgetAssertDataHeader; |
| 78 | |
| 79 | fs.writeFileSync(outputPath, out); |
| 80 | if (process.env.OVERWRITE || !fs.existsSync(expectedPath)) { |
| 81 | fs.copyFileSync(outputPath, expectedPath); |
| 82 | console.log(pc.yellow(`Fixture "${dir.name}" was regenerated`)); |
| 83 | } else { |
| 84 | const expected = fs.readFileSync(expectedPath); |
| 85 | if (!Buffer.from(out).equals(expected)) { |
| 86 | hadErrors = true; |
| 87 | console.log("\n"); |
| 88 | console.log(pc.red(`Fixture "${dir.name}" is failing:`)); |
| 89 | console.log(pc.red(`- ${expected.toString("utf-8")}`)); |
| 90 | console.log(pc.green(`+ ${out}`)); |
| 91 | } else { |
| 92 | console.log(pc.green(`Fixture "${dir.name}" is passing`)); |
| 93 | } |
| 94 | } |
| 95 | console.log(pc.bold("\n-----")); |
| 96 | } |
| 97 | if (hadErrors) { |
| 98 | process.exit(1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | await runFixtures(); |