Commit 5db6fe8a authored by David Sveningsson's avatar David Sveningsson Committed by David Sveningsson

fix: create directory only if missing

parent d5ae603f
Pipeline #108229831 passed with stages
in 9 minutes and 38 seconds
......@@ -17,6 +17,18 @@ if [[ $? = 0 ]]; then
fi
echo "Testing if html-validate will save report to file"
mkdir -p temp
./bin/html-validate.js -f json=temp/report.json test-files/elements/table-valid.html
if [[ $? != 0 ]]; then
echo "Compatibility test failed, expected command to exit with zero status"
exit 1
fi
if [[ "$(cat temp/report.json)" != "[]" ]]; then
echo "Compatibility test failed, expected command to write report to file"
exit 1
fi
echo "Testing if html-validate will save report to file in subdirectory"
./bin/html-validate.js -f json=temp/compatibility/report.json test-files/elements/table-valid.html
if [[ $? != 0 ]]; then
echo "Compatibility test failed, expected command to exit with zero status"
......
......@@ -13,12 +13,17 @@ jest.mock("../formatters/json", () => {
return (report: Report) => jsonFormatter(report);
});
const mkdirSync = jest.fn();
const writeFileSync = jest.fn();
const fs = {
existsSync: jest.fn().mockReturnValue(true),
mkdirSync: jest.fn(),
writeFileSync: jest.fn(),
};
jest.mock("fs", () => {
return {
mkdirSync: (...args: any[]) => mkdirSync(...args),
writeFileSync: (...args: any[]) => writeFileSync(...args),
existsSync: (...args: any[]) => fs.existsSync(...args),
mkdirSync: (...args: any[]) => fs.mkdirSync(...args),
writeFileSync: (...args: any[]) => fs.writeFileSync(...args),
};
});
......@@ -66,10 +71,18 @@ describe("cli/formatters", () => {
expect(jsonFormatter).toHaveBeenCalledWith(report.results);
});
it("should redirect output", () => {
it("should redirect output to file", () => {
const wrapped = cli.getFormatter("text=foo.txt");
wrapped(report);
expect(fs.mkdirSync).not.toHaveBeenCalled();
expect(fs.writeFileSync).toHaveBeenCalledWith("foo.txt", "", "utf-8");
});
it("should create directory if missing", () => {
fs.existsSync.mockReturnValue(false);
const wrapped = cli.getFormatter("text=mydir/foo.txt");
wrapped(report);
expect(mkdirSync).toHaveBeenCalledWith("mydir", { recursive: true });
expect(writeFileSync).toHaveBeenCalledWith("mydir/foo.txt", "", "utf-8");
expect(fs.mkdirSync).toHaveBeenCalledWith("mydir", { recursive: true });
expect(fs.writeFileSync).toHaveBeenCalledWith("mydir/foo.txt", "", "utf-8");
});
});
......@@ -12,7 +12,10 @@ function wrap(
return (results: Result[]) => {
const output = formatter(results);
if (dst) {
fs.mkdirSync(path.dirname(dst), { recursive: true });
const dir = path.dirname(dst);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { 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