Files
test-repo/tests/integration/__tests__/infer-parser.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

350 lines
7.9 KiB
JavaScript
Raw Normal View History

2025-05-20 13:19:15 +08:00
import url from "node:url";
2022-07-20 23:18:24 +08:00
import prettier from "../../config/prettier-entry.js";
2023-03-31 09:04:33 +08:00
import jestPathSerializer from "../path-serializer.js";
expect.addSnapshotSerializer(jestPathSerializer);
2022-05-04 11:08:51 +08:00
2018-05-23 16:55:06 -03:00
describe("stdin no path and no parser", () => {
describe("logs error and exits with 2", () => {
runCli("cli/infer-parser/", [], { input: "foo" }).test({
2018-05-23 16:55:06 -03:00
status: 2,
stdout: "",
write: [],
});
});
2018-12-19 14:03:30 +00:00
describe("--check logs error but exits with 0", () => {
runCli("cli/infer-parser/", ["--check"], {
2018-12-19 14:03:30 +00:00
input: "foo",
}).test({
status: 0,
stdout: "",
write: [],
});
});
2018-05-23 16:55:06 -03:00
describe("--list-different logs error but exits with 0", () => {
runCli("cli/infer-parser/", ["--list-different"], {
2018-05-23 16:55:06 -03:00
input: "foo",
}).test({
status: 0,
stdout: "",
write: [],
});
});
});
describe("stdin with unknown path and no parser", () => {
describe("logs error and exits with 2", () => {
runCli("cli/infer-parser/", ["--stdin-filepath", "foo"], {
2018-05-23 16:55:06 -03:00
input: "foo",
}).test({
status: 2,
stdout: "",
write: [],
});
});
2018-12-19 14:03:30 +00:00
describe("--check logs error but exits with 0", () => {
runCli("cli/infer-parser/", ["--check", "--stdin-filepath", "foo"], {
2020-02-27 02:19:52 +02:00
input: "foo",
}).test({
2018-12-19 14:03:30 +00:00
status: 0,
stdout: "",
write: [],
});
});
2018-05-23 16:55:06 -03:00
describe("--list-different logs error but exits with 0", () => {
runCli(
2018-05-23 16:55:06 -03:00
"cli/infer-parser/",
2020-02-27 02:19:52 +02:00
["--list-different", "--stdin-filepath", "foo"],
2018-05-23 16:55:06 -03:00
{ input: "foo" },
).test({
status: 0,
stdout: "",
write: [],
});
});
});
describe("unknown path and no parser", () => {
describe("specific file", () => {
runCli("cli/infer-parser/", ["--end-of-line", "lf", "FOO"]).test({
2018-05-23 16:55:06 -03:00
status: 2,
stdout: "",
write: [],
});
});
describe("multiple files", () => {
runCli("cli/infer-parser/", ["--end-of-line", "lf", "*"]).test({
2018-05-23 16:55:06 -03:00
status: 2,
write: [],
});
});
});
2018-12-19 14:03:30 +00:00
describe("--check with unknown path and no parser", () => {
describe("specific file", () => {
runCli("cli/infer-parser/", ["--check", "FOO"]).test({
status: 2,
2018-12-19 14:03:30 +00:00
write: [],
});
});
describe("multiple files", () => {
runCli("cli/infer-parser/", ["--check", "*"]).test({
status: 2,
2018-12-19 14:03:30 +00:00
write: [],
});
});
});
2018-05-23 16:55:06 -03:00
describe("--list-different with unknown path and no parser", () => {
describe("specific file", () => {
runCli("cli/infer-parser/", ["--list-different", "FOO"]).test({
status: 2,
2018-05-23 16:55:06 -03:00
stdout: "",
write: [],
});
});
describe("multiple files", () => {
runCli("cli/infer-parser/", ["--list-different", "*"]).test({
status: 2,
stdout: "foo.js",
2018-05-23 16:55:06 -03:00
write: [],
});
});
});
describe("--write with unknown path and no parser", () => {
describe("specific file", () => {
runCli("cli/infer-parser/", ["--write", "FOO"]).test({
2018-05-23 16:55:06 -03:00
status: 2,
stdout: "",
write: [],
});
});
describe("multiple files", () => {
runCli("cli/infer-parser/", ["--write", "*"]).test({
2018-05-23 16:55:06 -03:00
status: 2,
});
});
});
2018-12-19 14:03:30 +00:00
describe("--write and --check with unknown path and no parser", () => {
describe("specific file", () => {
runCli("cli/infer-parser/", ["--check", "--write", "FOO"]).test({
status: 2,
2018-12-19 14:03:30 +00:00
write: [],
});
});
describe("multiple files", () => {
runCli("cli/infer-parser/", ["--check", "--write", "*"]).test({
status: 2,
2018-12-19 14:03:30 +00:00
});
});
});
2018-05-23 16:55:06 -03:00
describe("--write and --list-different with unknown path and no parser", () => {
describe("specific file", () => {
runCli("cli/infer-parser/", ["--list-different", "--write", "FOO"]).test({
status: 2,
2018-05-23 16:55:06 -03:00
stdout: "",
write: [],
});
});
describe("multiple files", () => {
runCli("cli/infer-parser/", ["--list-different", "--write", "*"]).test({
status: 2,
});
2018-05-23 16:55:06 -03:00
});
});
describe("Allow plugin to override builtin plugins", () => {
runCli(
"cli/infer-parser/override-builtin-plugin-languages",
["--stdin-filepath=foo.js"],
{
input: "foo( )",
},
).test({ write: [], status: 0, stderr: "", stdout: "foo();" });
runCli(
"cli/infer-parser/override-builtin-plugin-languages",
["--stdin-filepath=foo.js", "--plugin=./dummy-js-plugin.js"],
{
input: "foo( )",
},
).test({
write: [],
status: 0,
stderr: "",
stdout: "foo( )\nformatted by 'dummy-js-parser' parser",
});
});
2018-05-23 16:55:06 -03:00
describe("API with no path and no parser", () => {
test("prettier.format", async () => {
await expect(prettier.format(" foo ( )")).rejects.toThrow(
/No parser and no file path given, couldn't infer a parser\./,
);
2018-05-29 02:43:44 -03:00
});
test("prettier.check", async () => {
await expect(prettier.check(" foo ( )")).rejects.toThrow(
/No parser and no file path given, couldn't infer a parser\./,
);
2018-05-29 02:43:44 -03:00
});
2018-05-23 16:55:06 -03:00
});
describe("Known/Unknown", () => {
runCli("cli/infer-parser/known-unknown", [
"--end-of-line",
"lf",
"--list-different",
".",
]).test({
status: 1,
stderr: "",
write: [],
});
});
2021-07-22 09:44:42 +08:00
describe("Interpreters", () => {
runCli("cli/infer-parser/interpreters", ["--file-info", "zx-script"]).test({
2021-07-22 09:44:42 +08:00
status: 0,
stderr: "",
write: [],
});
});
describe("isSupported", () => {
runCli("cli/infer-parser", [
"--plugin",
"../../plugins/languages/is-supported.js",
"--file-info",
".husky/pre-commit",
]).test({
status: 0,
stderr: "",
write: [],
});
runCli("cli/infer-parser", [
"--plugin",
"../../plugins/languages/is-supported.js",
".husky/pre-commit",
]).test({
status: 0,
stderr: "",
write: [],
});
runCli(
"cli/infer-parser",
[
"--plugin",
"../../plugins/languages/is-supported.js",
"--stdin-filepath",
".husky/pre-commit",
],
{ input: "content from stdin" },
).test({
status: 0,
stderr: "",
write: [],
});
2025-05-20 13:19:15 +08:00
test("API", async () => {
const fileUrl = new URL("foo.unknown", import.meta.url);
const filePath = url.fileURLToPath(fileUrl);
expect(await getIsSupportedReceivedFilepath({ filepath: fileUrl })).toBe(
filePath,
);
expect(
await getIsSupportedReceivedFilepath({ filepath: fileUrl.href }),
).toBe(filePath);
expect(await getIsSupportedReceivedFilepath({ filepath: filePath })).toBe(
filePath,
);
// Relative path
expect(
await getIsSupportedReceivedFilepath({ filepath: "./foo.unknown" }),
).toBe("./foo.unknown");
expect(
await getIsSupportedReceivedFilepath({
filepath: "",
}),
).toBeUndefined();
expect(
await getIsSupportedReceivedFilepath({
filepath: Buffer.from("foo.unknown"),
}),
).toBeUndefined();
expect(
await getIsSupportedReceivedFilepath({
filepath: { toString: () => "foo.unknown" },
}),
).toBeUndefined();
expect(
await getIsSupportedReceivedFilepath({
filepath: new (class {
toString() {
return "foo.unknown";
}
})(),
}),
).toBeUndefined();
expect(
await getIsSupportedReceivedFilepath({ filepath: "file://%0" }),
).toBeUndefined();
expect(
await getIsSupportedReceivedFilepath({
filepath: new URL("https://example.com/foo.unknown"),
}),
).toBeUndefined();
expect(
await getIsSupportedReceivedFilepath({
filepath: "https://example.com/foo.unknown",
}),
).toBe("https://example.com/foo.unknown");
});
});
2025-05-20 13:19:15 +08:00
const getIsSupportedReceivedFilepath = async (options) => {
let received;
try {
await prettier.format("foo", {
plugins: [
{
languages: [
{
isSupported({ filepath }) {
received = filepath;
},
},
],
},
],
...options,
});
} catch (error) {
if (error.name !== "UndefinedParserError") {
throw error;
}
}
return received;
};