2019-02-01 10:16:39 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-03-08 02:01:40 -05:00
|
|
|
import { join } from "../fs/path.ts";
|
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-03-06 16:39:50 -05:00
|
|
|
import { test } from "../testing/mod.ts";
|
2019-03-08 12:41:47 -05:00
|
|
|
import { xrun } from "./util.ts";
|
|
|
|
const { readAll, execPath } = Deno;
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
async function run(
|
|
|
|
args: string[]
|
|
|
|
): Promise<{ stdout: string; code: number | undefined }> {
|
2019-02-01 10:16:39 -05:00
|
|
|
const p = xrun({ args, stdout: "piped" });
|
|
|
|
|
|
|
|
const stdout = decoder.decode(await readAll(p.stdout));
|
|
|
|
const { code } = await p.status();
|
|
|
|
|
|
|
|
return { stdout, code };
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmd = [
|
2019-03-08 12:41:47 -05:00
|
|
|
execPath,
|
2019-02-01 10:16:39 -05:00
|
|
|
"--allow-run",
|
|
|
|
"--allow-write",
|
2019-02-09 15:41:05 -05:00
|
|
|
"--allow-read",
|
2019-02-01 10:16:39 -05:00
|
|
|
"prettier/main.ts"
|
|
|
|
];
|
2019-03-08 02:01:40 -05:00
|
|
|
const testdata = join("prettier", "testdata");
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
function normalizeOutput(output: string): string {
|
|
|
|
return output
|
|
|
|
.replace(/\r/g, "")
|
|
|
|
.replace(/\\/g, "/")
|
|
|
|
.trim()
|
|
|
|
.split("\n")
|
|
|
|
.sort()
|
|
|
|
.join("\n");
|
|
|
|
}
|
|
|
|
|
2019-03-04 19:53:35 -05:00
|
|
|
async function clearTestdataChanges(): Promise<void> {
|
2019-02-01 10:16:39 -05:00
|
|
|
await xrun({ args: ["git", "checkout", testdata] }).status();
|
|
|
|
}
|
|
|
|
|
|
|
|
test(async function testPrettierCheckAndFormatFiles() {
|
|
|
|
await clearTestdataChanges();
|
|
|
|
|
2019-03-08 02:01:40 -05:00
|
|
|
const files = [join(testdata, "0.ts"), join(testdata, "1.js")];
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
var { code, stdout } = await run([...cmd, "--check", ...files]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 1);
|
|
|
|
assertEquals(normalizeOutput(stdout), "Some files are not formatted");
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
var { code, stdout } = await run([...cmd, ...files]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 0);
|
|
|
|
assertEquals(
|
2019-02-01 10:16:39 -05:00
|
|
|
normalizeOutput(stdout),
|
2019-03-08 02:01:40 -05:00
|
|
|
`Formatting ./prettier/testdata/0.ts
|
|
|
|
Formatting ./prettier/testdata/1.js`
|
2019-02-01 10:16:39 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
var { code, stdout } = await run([...cmd, "--check", ...files]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 0);
|
|
|
|
assertEquals(normalizeOutput(stdout), "Every file is formatted");
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
await clearTestdataChanges();
|
|
|
|
});
|
|
|
|
|
|
|
|
test(async function testPrettierCheckAndFormatDirs() {
|
|
|
|
await clearTestdataChanges();
|
|
|
|
|
2019-03-08 02:01:40 -05:00
|
|
|
const dirs = [join(testdata, "foo"), join(testdata, "bar")];
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
var { code, stdout } = await run([...cmd, "--check", ...dirs]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 1);
|
|
|
|
assertEquals(normalizeOutput(stdout), "Some files are not formatted");
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
var { code, stdout } = await run([...cmd, ...dirs]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 0);
|
|
|
|
assertEquals(
|
2019-02-01 10:16:39 -05:00
|
|
|
normalizeOutput(stdout),
|
2019-03-08 02:01:40 -05:00
|
|
|
`Formatting ./prettier/testdata/bar/0.ts
|
|
|
|
Formatting ./prettier/testdata/bar/1.js
|
|
|
|
Formatting ./prettier/testdata/foo/0.ts
|
|
|
|
Formatting ./prettier/testdata/foo/1.js`
|
2019-02-01 10:16:39 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
var { code, stdout } = await run([...cmd, "--check", ...dirs]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 0);
|
|
|
|
assertEquals(normalizeOutput(stdout), "Every file is formatted");
|
2019-02-01 10:16:39 -05:00
|
|
|
|
|
|
|
await clearTestdataChanges();
|
|
|
|
});
|