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";
|
2019-05-21 08:23:23 -04:00
|
|
|
import { EOL } from "../fs/path/constants.ts";
|
2019-03-08 02:01:40 -05:00
|
|
|
import { assertEquals } from "../testing/asserts.ts";
|
2019-06-30 11:10:00 -04:00
|
|
|
import { test, runIfMain } from "../testing/mod.ts";
|
2019-03-08 12:41:47 -05:00
|
|
|
import { xrun } from "./util.ts";
|
2019-06-02 21:42:27 -04:00
|
|
|
import { copy, emptyDir } from "../fs/mod.ts";
|
2019-03-08 12:41:47 -05:00
|
|
|
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" });
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
const stdout = decoder.decode(await readAll(p.stdout!));
|
2019-02-01 10:16:39 -05:00
|
|
|
const { code } = await p.status();
|
|
|
|
|
|
|
|
return { stdout, code };
|
|
|
|
}
|
|
|
|
|
|
|
|
const cmd = [
|
2019-08-13 20:03:29 -04:00
|
|
|
execPath(),
|
2019-05-04 11:33:50 -04:00
|
|
|
"run",
|
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-04-24 07:41:23 -04:00
|
|
|
test(async function testPrettierCheckAndFormatFiles(): Promise<void> {
|
2019-06-02 21:42:27 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
await copy(testdata, tempDir, { overwrite: true });
|
2019-02-01 10:16:39 -05:00
|
|
|
|
2019-03-24 11:26:47 -04:00
|
|
|
const files = [
|
2019-06-02 21:42:27 -04:00
|
|
|
join(tempDir, "0.ts"),
|
|
|
|
join(tempDir, "1.js"),
|
|
|
|
join(tempDir, "2.ts")
|
2019-03-24 11:26:47 -04:00
|
|
|
];
|
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
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
var { code, stdout } = await run([...cmd, "--write", ...files]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 0);
|
|
|
|
assertEquals(
|
2019-02-01 10:16:39 -05:00
|
|
|
normalizeOutput(stdout),
|
2019-06-02 21:42:27 -04:00
|
|
|
normalizeOutput(`Formatting ${tempDir}/0.ts
|
|
|
|
Formatting ${tempDir}/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
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
emptyDir(tempDir);
|
2019-02-01 10:16:39 -05:00
|
|
|
});
|
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testPrettierCheckAndFormatDirs(): Promise<void> {
|
2019-06-02 21:42:27 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
await copy(testdata, tempDir, { overwrite: true });
|
2019-02-01 10:16:39 -05:00
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
const dirs = [join(tempDir, "foo"), join(tempDir, "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
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
var { code, stdout } = await run([...cmd, "--write", ...dirs]);
|
2019-03-06 19:42:24 -05:00
|
|
|
assertEquals(code, 0);
|
|
|
|
assertEquals(
|
2019-02-01 10:16:39 -05:00
|
|
|
normalizeOutput(stdout),
|
2019-06-02 21:42:27 -04:00
|
|
|
normalizeOutput(`Formatting ${tempDir}/bar/0.ts
|
|
|
|
Formatting ${tempDir}/bar/1.js
|
|
|
|
Formatting ${tempDir}/foo/0.ts
|
|
|
|
Formatting ${tempDir}/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
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
emptyDir(tempDir);
|
2019-02-01 10:16:39 -05:00
|
|
|
});
|
2019-03-17 18:23:21 -04:00
|
|
|
|
2019-04-24 07:41:23 -04:00
|
|
|
test(async function testPrettierOptions(): Promise<void> {
|
2019-06-02 21:42:27 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
await copy(testdata, tempDir, { overwrite: true });
|
2019-03-17 18:23:21 -04:00
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
const file0 = join(tempDir, "opts", "0.ts");
|
|
|
|
const file1 = join(tempDir, "opts", "1.ts");
|
|
|
|
const file2 = join(tempDir, "opts", "2.ts");
|
|
|
|
const file3 = join(tempDir, "opts", "3.md");
|
2019-03-17 18:23:21 -04:00
|
|
|
|
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
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--no-semi", "--write", file0]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0)
|
|
|
|
console.log([function foo() {}, function baz() {}, a => {}])
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([
|
|
|
|
...cmd,
|
|
|
|
"--print-width",
|
|
|
|
"30",
|
|
|
|
"--tab-width",
|
|
|
|
"4",
|
|
|
|
"--write",
|
|
|
|
file0
|
|
|
|
]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([
|
|
|
|
function foo() {},
|
|
|
|
function baz() {},
|
|
|
|
a => {}
|
|
|
|
]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--print-width", "30", "--use-tabs", "--write", file0]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([
|
|
|
|
function foo() {},
|
|
|
|
function baz() {},
|
|
|
|
a => {}
|
|
|
|
]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--single-quote", "--write", file1]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file1)),
|
|
|
|
`console.log('1');
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([
|
|
|
|
...cmd,
|
|
|
|
"--print-width",
|
|
|
|
"30",
|
|
|
|
"--trailing-comma",
|
|
|
|
"all",
|
|
|
|
"--write",
|
|
|
|
file0
|
|
|
|
]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([
|
|
|
|
function foo() {},
|
|
|
|
function baz() {},
|
|
|
|
a => {},
|
|
|
|
]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--no-bracket-spacing", "--write", file2]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file2)),
|
|
|
|
`console.log({a: 1});
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--arrow-parens", "always", "--write", file0]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file0)),
|
|
|
|
`console.log(0);
|
|
|
|
console.log([function foo() {}, function baz() {}, (a) => {}]);
|
|
|
|
`
|
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--prose-wrap", "always", "--write", file3]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(
|
|
|
|
normalizeSourceCode(await getSourceCode(file3)),
|
2019-06-19 00:22:01 -04:00
|
|
|
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " +
|
|
|
|
"sed do eiusmod tempor" +
|
|
|
|
"\nincididunt ut labore et dolore magna aliqua.\n"
|
2019-03-17 18:23:21 -04:00
|
|
|
);
|
|
|
|
|
2019-05-21 08:23:23 -04:00
|
|
|
await run([...cmd, "--end-of-line", "crlf", "--write", file2]);
|
2019-03-17 18:23:21 -04:00
|
|
|
assertEquals(await getSourceCode(file2), "console.log({ a: 1 });\r\n");
|
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
emptyDir(tempDir);
|
2019-03-17 18:23:21 -04:00
|
|
|
});
|
2019-05-21 08:23:23 -04:00
|
|
|
|
|
|
|
test(async function testPrettierPrintToStdout(): Promise<void> {
|
2019-06-02 21:42:27 -04:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
|
|
|
await copy(testdata, tempDir, { overwrite: true });
|
2019-05-21 08:23:23 -04:00
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
const file0 = join(tempDir, "0.ts");
|
|
|
|
const file1 = join(tempDir, "formatted.ts");
|
2019-05-21 08:23:23 -04:00
|
|
|
|
|
|
|
const getSourceCode = async (f: string): Promise<string> =>
|
|
|
|
decoder.decode(await Deno.readFile(f));
|
|
|
|
|
|
|
|
const { stdout } = await run([...cmd, file0]);
|
|
|
|
// The source file will not change without `--write` flags.
|
|
|
|
assertEquals(await getSourceCode(file0), "console.log (0)" + EOL);
|
|
|
|
// The output should be formatted code.
|
|
|
|
assertEquals(stdout, "console.log(0);" + EOL);
|
|
|
|
|
|
|
|
const { stdout: formattedCode } = await run([...cmd, file1]);
|
|
|
|
// The source file will not change without `--write` flags.
|
|
|
|
assertEquals(await getSourceCode(file1), "console.log(0);" + EOL);
|
2019-06-19 00:22:01 -04:00
|
|
|
// The output will be formatted code even it is the same as the source file's
|
|
|
|
// content.
|
2019-05-21 08:23:23 -04:00
|
|
|
assertEquals(formattedCode, "console.log(0);" + EOL);
|
|
|
|
|
2019-06-02 21:42:27 -04:00
|
|
|
emptyDir(tempDir);
|
2019-05-21 08:23:23 -04:00
|
|
|
});
|
2019-06-30 11:10:00 -04:00
|
|
|
|
|
|
|
test(async function testPrettierReadFromStdin(): Promise<void> {
|
|
|
|
interface TestCase {
|
|
|
|
stdin: string;
|
|
|
|
stdout: string;
|
|
|
|
stderr: string;
|
|
|
|
code: number;
|
|
|
|
success: boolean;
|
|
|
|
parser?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function readFromStdinAssertion(
|
|
|
|
stdin: string,
|
|
|
|
expectedStdout: string,
|
|
|
|
expectedStderr: string,
|
|
|
|
expectedCode: number,
|
|
|
|
expectedSuccess: boolean,
|
|
|
|
parser?: string
|
|
|
|
): Promise<void> {
|
|
|
|
const inputCode = stdin;
|
|
|
|
const p1 = Deno.run({
|
2019-08-13 20:03:29 -04:00
|
|
|
args: [execPath(), "./prettier/testdata/echox.ts", `${inputCode}`],
|
2019-06-30 11:10:00 -04:00
|
|
|
stdout: "piped"
|
|
|
|
});
|
|
|
|
|
|
|
|
const p2 = Deno.run({
|
|
|
|
args: [
|
2019-08-13 20:03:29 -04:00
|
|
|
execPath(),
|
2019-06-30 11:10:00 -04:00
|
|
|
"run",
|
|
|
|
"./prettier/main.ts",
|
|
|
|
"--stdin",
|
|
|
|
...(parser ? ["--stdin-parser", parser] : [])
|
|
|
|
],
|
|
|
|
stdin: "piped",
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped"
|
|
|
|
});
|
|
|
|
|
|
|
|
const n = await Deno.copy(p2.stdin!, p1.stdout!);
|
|
|
|
assertEquals(n, new TextEncoder().encode(stdin).length);
|
|
|
|
|
|
|
|
const status1 = await p1.status();
|
|
|
|
assertEquals(status1.code, 0);
|
|
|
|
assertEquals(status1.success, true);
|
|
|
|
p2.stdin!.close();
|
|
|
|
const status2 = await p2.status();
|
|
|
|
assertEquals(status2.code, expectedCode);
|
|
|
|
assertEquals(status2.success, expectedSuccess);
|
|
|
|
const decoder = new TextDecoder("utf-8");
|
|
|
|
assertEquals(
|
|
|
|
decoder.decode(await Deno.readAll(p2.stdout!)),
|
|
|
|
expectedStdout
|
|
|
|
);
|
|
|
|
assertEquals(
|
|
|
|
decoder.decode(await Deno.readAll(p2.stderr!)).split(EOL)[0],
|
|
|
|
expectedStderr
|
|
|
|
);
|
|
|
|
p2.close();
|
|
|
|
p1.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
const testCases: TestCase[] = [
|
|
|
|
{
|
|
|
|
stdin: `console.log("abc" )`,
|
|
|
|
stdout: `console.log("abc");\n`,
|
|
|
|
stderr: ``,
|
|
|
|
code: 0,
|
|
|
|
success: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stdin: `console.log("abc" )`,
|
|
|
|
stdout: `console.log("abc");\n`,
|
|
|
|
stderr: ``,
|
|
|
|
code: 0,
|
|
|
|
success: true,
|
|
|
|
parser: "babel"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stdin: `{\"a\":\"b\"}`,
|
|
|
|
stdout: `{ "a": "b" }\n`,
|
|
|
|
stderr: ``,
|
|
|
|
code: 0,
|
|
|
|
success: true,
|
|
|
|
parser: "json"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stdin: `## test`,
|
|
|
|
stdout: `## test\n`,
|
|
|
|
stderr: ``,
|
|
|
|
code: 0,
|
|
|
|
success: true,
|
|
|
|
parser: "markdown"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stdin: `invalid typescript code##!!@@`,
|
|
|
|
stdout: ``,
|
|
|
|
stderr: `SyntaxError: ';' expected. (1:9)`,
|
|
|
|
code: 1,
|
|
|
|
success: false
|
|
|
|
},
|
|
|
|
{
|
|
|
|
stdin: `console.log("foo");`,
|
|
|
|
stdout: ``,
|
|
|
|
stderr:
|
|
|
|
'Error: Couldn\'t resolve parser "invalid_parser". ' +
|
|
|
|
"Parsers must be explicitly added to the standalone bundle.",
|
|
|
|
code: 1,
|
|
|
|
success: false,
|
|
|
|
parser: "invalid_parser"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const t of testCases) {
|
|
|
|
await readFromStdinAssertion(
|
|
|
|
t.stdin,
|
|
|
|
t.stdout,
|
|
|
|
t.stderr,
|
|
|
|
t.code,
|
|
|
|
t.success,
|
|
|
|
t.parser
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
runIfMain(import.meta);
|