|
| 1 | +import { exec } from "child_process"; |
| 2 | +import { promisify } from "util"; |
| 3 | +import { expect, use } from "chai"; |
| 4 | +import chaiAsPromised = require("chai-as-promised"); |
| 5 | + |
| 6 | +use(chaiAsPromised); |
| 7 | + |
| 8 | +/** |
| 9 | + * Returns a boolean indicating whether the given command ran successfully or not. |
| 10 | + * @param args |
| 11 | + */ |
| 12 | +async function runCommandWithArgs(args: string[]): Promise<boolean> { |
| 13 | + const childProcPromise = promisify(exec)( |
| 14 | + ["node", "dist/index.js", ...args].join(" ") |
| 15 | + ); |
| 16 | + try { |
| 17 | + await childProcPromise; |
| 18 | + } catch (e: unknown) { |
| 19 | + return false; |
| 20 | + } |
| 21 | + |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +async function runCommandWithArgsForOutput( |
| 26 | + args: string[] |
| 27 | +): Promise<{ stdout: string; stderr: string }> { |
| 28 | + const childProcPromise = promisify(exec)( |
| 29 | + ["node", "dist/index.js", ...args].join(" ") |
| 30 | + ); |
| 31 | + try { |
| 32 | + return await childProcPromise; |
| 33 | + } catch (e: any) { |
| 34 | + // Error object has stdout/stderr |
| 35 | + return e; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +describe("github-run-script base tests", () => { |
| 40 | + it("should not run with no arguments", () => |
| 41 | + expect(runCommandWithArgs([])).to.eventually.equal(false)); |
| 42 | + it("should do nothing", () => |
| 43 | + expect(runCommandWithArgs(["env"])).to.eventually.equal(true)); |
| 44 | + it("should have the repo name", () => |
| 45 | + expect( |
| 46 | + runCommandWithArgsForOutput(["env", "PythonCoderAS/github-run-script"]) |
| 47 | + ) |
| 48 | + .to.eventually.have.property("stdout") |
| 49 | + .which.contain("PythonCoderAS/github-run-script")); |
| 50 | +}); |
0 commit comments