Commit b5569f3a authored by David Sveningsson's avatar David Sveningsson

fix(cli): create output directory as needed

parent 1fd37ffd
......@@ -13,9 +13,11 @@ jest.mock("../formatters/json", () => {
return (report: Report) => jsonFormatter(report);
});
const mkdirSync = jest.fn();
const writeFileSync = jest.fn();
jest.mock("fs", () => {
return {
mkdirSync: (...args: any[]) => mkdirSync(...args),
writeFileSync: (...args: any[]) => writeFileSync(...args),
};
});
......@@ -65,8 +67,9 @@ describe("cli/formatters", () => {
});
it("should redirect output", () => {
const wrapped = cli.getFormatter("text=foo.txt");
const wrapped = cli.getFormatter("text=mydir/foo.txt");
wrapped(report);
expect(writeFileSync).toHaveBeenCalledWith("foo.txt", "", "utf-8");
expect(mkdirSync).toHaveBeenCalledWith("mydir", { recursive: true });
expect(writeFileSync).toHaveBeenCalledWith("mydir/foo.txt", "", "utf-8");
});
});
import fs from "fs";
import path from "path";
import { Formatter } from "../formatters";
import { Report, Result } from "../reporter";
......@@ -11,6 +12,7 @@ function wrap(
return (results: Result[]) => {
const output = formatter(results);
if (dst) {
fs.mkdirSync(path.dirname(dst), { recursive: true });
fs.writeFileSync(dst, output, "utf-8");
return null;
} else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment