2022-02-10 10:52:10 +08:00
|
|
|
import runPrettier from "../run-prettier.js";
|
2018-05-28 21:46:55 +10:00
|
|
|
const EOL = "\n";
|
2018-01-05 21:09:51 +11:00
|
|
|
|
2018-05-09 12:17:12 +01:00
|
|
|
describe("automatically loads 'prettier-plugin-*'", () => {
|
2018-01-05 21:09:51 +11:00
|
|
|
runPrettier("plugins/automatic", ["file.txt", "--parser=bar"]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
2018-01-05 21:09:51 +11:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-05-09 12:17:12 +01:00
|
|
|
|
2019-05-24 14:23:07 -03:00
|
|
|
describe("automatically loads '@prettier/plugin-*'", () => {
|
|
|
|
|
runPrettier("plugins/automatic", ["file.txt", "--parser=foo"]).test({
|
|
|
|
|
stdout: "foo+contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2019-03-10 10:34:04 +01:00
|
|
|
describe("automatically loads '@<name>/prettier-plugin-*'", () => {
|
|
|
|
|
runPrettier("plugins/automatic", ["file.txt", "--parser=foobar"]).test({
|
|
|
|
|
stdout: "foobar+contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-05-09 12:17:12 +01:00
|
|
|
describe("automatically loads 'prettier-plugin-*' from --plugin-search-dir (same as autoload dir)", () => {
|
|
|
|
|
runPrettier("plugins/automatic", [
|
|
|
|
|
"file.txt",
|
|
|
|
|
"--parser=foo",
|
2018-05-10 01:27:41 +01:00
|
|
|
"--plugin-search-dir=.",
|
2018-05-09 12:17:12 +01:00
|
|
|
]).test({
|
|
|
|
|
stdout: "foo+contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("automatically loads '@prettier/plugin-*' from --plugin-search-dir (same as autoload dir)", () => {
|
|
|
|
|
runPrettier("plugins/automatic", [
|
|
|
|
|
"file.txt",
|
|
|
|
|
"--parser=bar",
|
2018-05-10 01:27:41 +01:00
|
|
|
"--plugin-search-dir=.",
|
2018-05-09 12:17:12 +01:00
|
|
|
]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
2019-03-10 10:34:04 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("automatically loads '@<name>/prettier-plugin-*' from --plugin-search-dir (same as autoload dir)", () => {
|
|
|
|
|
runPrettier("plugins/automatic", [
|
|
|
|
|
"file.txt",
|
|
|
|
|
"--parser=foobar",
|
|
|
|
|
"--plugin-search-dir=.",
|
|
|
|
|
]).test({
|
|
|
|
|
stdout: "foobar+contents" + EOL,
|
2018-05-09 12:17:12 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("automatically loads 'prettier-plugin-*' from --plugin-search-dir (different to autoload dir)", () => {
|
|
|
|
|
runPrettier("plugins", [
|
|
|
|
|
"automatic/file.txt",
|
|
|
|
|
"--parser=foo",
|
2018-05-10 01:27:41 +01:00
|
|
|
"--plugin-search-dir=automatic",
|
2018-05-09 12:17:12 +01:00
|
|
|
]).test({
|
|
|
|
|
stdout: "foo+contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("automatically loads '@prettier/plugin-*' from --plugin-search-dir (different to autoload dir)", () => {
|
|
|
|
|
runPrettier("plugins", [
|
|
|
|
|
"automatic/file.txt",
|
|
|
|
|
"--parser=bar",
|
2018-05-10 01:27:41 +01:00
|
|
|
"--plugin-search-dir=automatic",
|
2018-05-09 12:17:12 +01:00
|
|
|
]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
2018-05-09 12:17:12 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("does not crash when --plugin-search-dir does not contain node_modules", () => {
|
2018-11-14 07:55:50 -05:00
|
|
|
runPrettier(
|
|
|
|
|
"plugins/extensions",
|
|
|
|
|
[
|
|
|
|
|
"file.foo",
|
|
|
|
|
"--end-of-line",
|
|
|
|
|
"lf",
|
2021-12-03 15:41:12 +08:00
|
|
|
"--plugin=./plugin.cjs",
|
2018-11-14 07:55:50 -05:00
|
|
|
"--plugin-search-dir=.",
|
|
|
|
|
],
|
|
|
|
|
{ ignoreLineEndings: true }
|
|
|
|
|
).test({
|
2018-05-09 12:17:12 +01:00
|
|
|
stdout: "!contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("crashes when one of --plugin-search-dir does not exist", () => {
|
|
|
|
|
runPrettier("plugins/automatic", [
|
|
|
|
|
"file.txt",
|
|
|
|
|
"--parser=foo",
|
2018-05-10 01:27:41 +01:00
|
|
|
"--plugin-search-dir=non-existing-dir",
|
|
|
|
|
"--plugin-search-dir=.",
|
2018-05-09 12:17:12 +01:00
|
|
|
]).test({
|
|
|
|
|
stdout: "",
|
2021-02-02 09:25:36 +08:00
|
|
|
stderr: "[error] non-existing-dir does not exist or is not a directory\n",
|
2018-05-09 12:17:12 +01:00
|
|
|
status: 1,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
2018-05-10 01:27:41 +01:00
|
|
|
|
|
|
|
|
describe("loads --plugin by its relative path", () => {
|
|
|
|
|
runPrettier("plugins", [
|
|
|
|
|
"automatic/file.txt",
|
|
|
|
|
"--parser=bar",
|
|
|
|
|
"--plugin=./automatic/node_modules/prettier-plugin-bar/index.js",
|
|
|
|
|
]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
2018-05-10 01:27:41 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("loads --plugin by its relative path without leading ./", () => {
|
|
|
|
|
runPrettier("plugins", [
|
|
|
|
|
"automatic/file.txt",
|
|
|
|
|
"--parser=bar",
|
|
|
|
|
"--plugin=automatic/node_modules/prettier-plugin-bar/index.js",
|
|
|
|
|
]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
2018-05-10 01:27:41 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("loads --plugin by relative path to its directory (assuming index.js)", () => {
|
|
|
|
|
runPrettier("plugins", [
|
|
|
|
|
"automatic/file.txt",
|
|
|
|
|
"--parser=bar",
|
|
|
|
|
"--plugin=./automatic/node_modules/prettier-plugin-bar",
|
|
|
|
|
]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
2018-05-10 01:27:41 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("loads --plugin by relative path to its directory without leading ./ (assuming index.js)", () => {
|
|
|
|
|
runPrettier("plugins", [
|
|
|
|
|
"automatic/file.txt",
|
|
|
|
|
"--parser=bar",
|
|
|
|
|
"--plugin=automatic/node_modules/prettier-plugin-bar",
|
|
|
|
|
]).test({
|
2020-02-18 22:36:17 +08:00
|
|
|
stdout: "content from `prettier-plugin-bar` package + contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2022-08-01 10:48:37 +08:00
|
|
|
describe("loads --plugin by package name", () => {
|
|
|
|
|
runPrettier("plugins/automatic", [
|
|
|
|
|
"file.txt",
|
|
|
|
|
"--parser=foobar",
|
|
|
|
|
"--plugin=@user/prettier-plugin-foobar",
|
|
|
|
|
]).test({
|
|
|
|
|
stdout: "foobar+contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-18 22:36:17 +08:00
|
|
|
describe("loads --plugin by filename without leading ./ and ext, should resolve to file, not package", () => {
|
|
|
|
|
runPrettier("plugins/automatic", [
|
|
|
|
|
"file.txt",
|
|
|
|
|
"--parser=bar",
|
|
|
|
|
"--plugin=prettier-plugin-bar",
|
|
|
|
|
]).test({
|
|
|
|
|
stdout: "content from `prettier-plugin-bar.js` file + contents" + EOL,
|
2018-05-10 01:27:41 +01:00
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("loads --plugin by bespoke plugin name (assuming it is installed in cwd)", () => {
|
|
|
|
|
runPrettier("plugins/bespoke", [
|
|
|
|
|
"../automatic/file.txt",
|
|
|
|
|
"--parser=bespoke",
|
|
|
|
|
"--plugin=@company/prettier-plugin-bespoke",
|
|
|
|
|
]).test({
|
|
|
|
|
stdout: "bespoke+contents" + EOL,
|
|
|
|
|
stderr: "",
|
|
|
|
|
status: 0,
|
|
|
|
|
write: [],
|
|
|
|
|
});
|
|
|
|
|
});
|
2022-02-18 16:36:53 +08:00
|
|
|
|
|
|
|
|
test("--no-plugin-search", async () => {
|
|
|
|
|
async function getParser(args = []) {
|
|
|
|
|
const { stdout } = await runPrettier("plugins/automatic", [
|
|
|
|
|
"--file-info=file.foo",
|
|
|
|
|
...args,
|
|
|
|
|
]);
|
|
|
|
|
return JSON.parse(stdout).inferredParser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(await getParser()).toBe("foo");
|
|
|
|
|
expect(await getParser(["--plugin-search-dir=."])).toBe("foo");
|
|
|
|
|
expect(await getParser(["--no-plugin-search"])).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("--no-plugin-search still allow use --plugin", async () => {
|
|
|
|
|
const args = ["file.txt", "--parser=bar"];
|
|
|
|
|
const { stdout: stdoutWithoutPlugin } = await runPrettier(
|
|
|
|
|
"plugins/automatic",
|
|
|
|
|
args
|
|
|
|
|
);
|
|
|
|
|
const argsWithPlugin = [...args, "--plugin=./prettier-plugin-bar.js"];
|
|
|
|
|
const { stdout: stdoutWithPlugin } = await runPrettier(
|
|
|
|
|
"plugins/automatic",
|
|
|
|
|
argsWithPlugin
|
|
|
|
|
);
|
|
|
|
|
const { stdout: stdoutWithoutPluginAndNoPluginSearch } = await runPrettier(
|
|
|
|
|
"plugins/automatic",
|
|
|
|
|
[...args, "--no-plugin-search"]
|
|
|
|
|
);
|
|
|
|
|
const { stdout: stdoutWithPluginButNoPluginSearch } = await runPrettier(
|
|
|
|
|
"plugins/automatic",
|
|
|
|
|
[...argsWithPlugin, "--no-plugin-search"]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(stdoutWithoutPlugin).not.toBe(stdoutWithPlugin);
|
|
|
|
|
expect(stdoutWithoutPluginAndNoPluginSearch).toBe("");
|
|
|
|
|
expect(stdoutWithPlugin).toBe(stdoutWithPluginButNoPluginSearch);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("--no-plugin-search together with --plugin-search-dir", async () => {
|
|
|
|
|
const result1 = await runPrettier("plugins/automatic", [
|
|
|
|
|
"--no-plugin-search",
|
|
|
|
|
"--plugin-search-dir",
|
|
|
|
|
]);
|
|
|
|
|
const result2 = await runPrettier("plugins/automatic", [
|
|
|
|
|
"--no-plugin-search",
|
|
|
|
|
"--plugin-search-dir=foo",
|
|
|
|
|
]);
|
|
|
|
|
const result3 = await runPrettier("plugins/automatic", [
|
|
|
|
|
"--no-plugin-search",
|
|
|
|
|
"--plugin-search-dir=foo",
|
|
|
|
|
"--plugin-search-dir=bar",
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
expect(result1).toMatchInlineSnapshot(`
|
2022-04-27 17:56:05 +08:00
|
|
|
{
|
2022-02-18 16:36:53 +08:00
|
|
|
"status": 1,
|
|
|
|
|
"stderr": "[error] Cannot use --no-plugin-search and --plugin-search-dir together.
|
|
|
|
|
",
|
|
|
|
|
"stdout": "",
|
2022-04-27 17:56:05 +08:00
|
|
|
"write": [],
|
2022-02-18 16:36:53 +08:00
|
|
|
}
|
|
|
|
|
`);
|
|
|
|
|
expect(result2).toEqual(result1);
|
|
|
|
|
expect(result3).toEqual(result1);
|
|
|
|
|
});
|