Skip to content

Commit

Permalink
chore: add cli unit test (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
npalm committed Mar 7, 2021
1 parent ef1759b commit 71fadec
Show file tree
Hide file tree
Showing 2 changed files with 354 additions and 840 deletions.
65 changes: 65 additions & 0 deletions __tests__/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as cp from "child_process";
import * as path from "path";

import { readFileSync, writeFileSync } from "fs";

test("CLI: update readme default", async () => {
await testReadme(
"__tests__/fixtures/all_fields_action.yml",
"__tests__/fixtures/all_fields_readme.input",
"__tests__/fixtures/all_fields_readme.output"
);
});

test("CLI: update readme CRLF", async () => {
await testReadme(
"__tests__/fixtures/all_fields_action.yml.crlf",
"__tests__/fixtures/all_fields_readme.input.crlf",
"__tests__/fixtures/all_fields_readme.output.crlf",
"--lb CRLF"
);
});

interface CliRespone {
code: number;
error: cp.ExecException | null;
stdout: string;
stderr: string;
}

function cli(args: string): Promise<CliRespone> {
return new Promise((resolve) => {
cp.exec(
`ts-node ${path.resolve("src/cli.ts")} ${args}`,
(error, stdout, stderr) => {
resolve({
code: error && error.code ? error.code : 0,
error,
stdout,
stderr,
});
}
);
});
}

async function testReadme(
actionFile: string,
originalReadme: string,
fixtureReadme: string,
extraArgs = "",
exitCode = 0
) {
const expected = <string>readFileSync(fixtureReadme, "utf-8");
const original = <string>readFileSync(originalReadme, "utf-8");

const result = await cli(
`-u ${originalReadme} -a ${actionFile} ${extraArgs}`
);
expect(result.code).toBe(exitCode);

const updated = <string>readFileSync(originalReadme, "utf-8");

writeFileSync(originalReadme, original);
expect(updated).toEqual(expected);
}
Loading

0 comments on commit 71fadec

Please sign in to comment.