|
| 1 | +import{exec}from"child_process"; |
| 2 | +import{promisify}from"util"; |
| 3 | +import{expect,use}from"chai"; |
| 4 | +importchaiAsPromised= 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 | +asyncfunctionrunCommandWithArgs(args:string[]):Promise<boolean>{ |
| 13 | +constchildProcPromise=promisify(exec)( |
| 14 | +["node","dist/index.js", ...args].join(" ") |
| 15 | +); |
| 16 | +try{ |
| 17 | +awaitchildProcPromise; |
| 18 | +}catch(e:unknown){ |
| 19 | +returnfalse; |
| 20 | +} |
| 21 | + |
| 22 | +returntrue; |
| 23 | +} |
| 24 | + |
| 25 | +asyncfunctionrunCommandWithArgsForOutput( |
| 26 | +args:string[] |
| 27 | +):Promise<{stdout:string;stderr:string}>{ |
| 28 | +constchildProcPromise=promisify(exec)( |
| 29 | +["node","dist/index.js", ...args].join(" ") |
| 30 | +); |
| 31 | +try{ |
| 32 | +returnawaitchildProcPromise; |
| 33 | +}catch(e:any){ |
| 34 | +// Error object has stdout/stderr |
| 35 | +returne; |
| 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 | +}); |