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-17 18:23:21 -04:00
|
|
|
function normalizeSourceCode(source: string): string {
|
|
|
|
return source.replace(/\r/g, "");
|
|
|
|
}
|
|
|
|
|
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-24 11:26:47 -04:00
|
|
|
const files = [
|
|
|
|
join(testdata, "0.ts"),
|
|
|
|
join(testdata, "1.js"),
|
|
|
|
join(testdata, "2.ts")
|
|
|
|
];
|
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();
|
|
|
|
});
|
2019-03-17 18:23:21 -04:00
|
|
|
|
|
|
|
test(async function testPrettierOptions() {
|
|
|
|
await clearTestdataChanges();
|
|
|
|
|
|
|
|
const file0 = join(testdata, "opts", "0.ts");
|
|
|
|
const file1 = join(testdata, "opts", "1.ts");
|
|
|
|
const file2 = join(testdata, "opts", "2.ts");
|
|
|
|
const file3 = join(testdata, "opts", "3.md");
|
|
|
|
|
2019-04-14 10:53:19 -04:00
|
|
|
const getSourceCode = async (f: string): Promise<string> =>
|
|
|
|
decoder.decode(await Deno.readFile(f));
|
2019-03-17 18:23:21 -04:00
|
|
|
|
|
|
|
await run([...cmd, "--no-semi", file0]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0)
|
|
|
|
console.log([function foo() {}, function baz() {}, a => {}])
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--print-width", "30", "--tab-width", "4", file0]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([
|
|
|
|
function foo() {},
|
|
|
|
function baz() {},
|
|
|
|
a => {}
|
|
|
|
]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--print-width", "30", "--use-tabs", file0]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([
|
|
|
|
function foo() {},
|
|
|
|
function baz() {},
|
|
|
|
a => {}
|
|
|
|
]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--single-quote", file1]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file1)),
|
|
|
|
`console.log('1');
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--print-width", "30", "--trailing-comma", "all", file0]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([
|
|
|
|
function foo() {},
|
|
|
|
function baz() {},
|
|
|
|
a => {},
|
|
|
|
]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--no-bracket-spacing", file2]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file2)),
|
|
|
|
`console.log({a: 1});
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--arrow-parens", "always", file0]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([function foo() {}, function baz() {}, (a) => {}]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--prose-wrap", "always", file3]);
|
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file3)),
|
|
|
|
`Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
|
|
|
|
incididunt ut labore et dolore magna aliqua.
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
|
|
|
await run([...cmd, "--end-of-line", "crlf", file2]);
|
|
|
|
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");
|
|
|
|
|
|
|
|
await clearTestdataChanges();
|
|
|
|
});
|