# Conflicts: # src/language-css/parser-postcss.js # tests/integration/__tests__/arg-parsing.js # tests/integration/__tests__/check.js # tests/integration/__tests__/config-invalid.js # tests/integration/__tests__/config-resolution.js # tests/integration/__tests__/cursor-offset.js # tests/integration/__tests__/debug-check.js # tests/integration/__tests__/debug-print-ast.js # tests/integration/__tests__/debug-print-comments.js # tests/integration/__tests__/debug-print-doc.js # tests/integration/__tests__/early-exit.js # tests/integration/__tests__/error-on-unmatched-pattern.js # tests/integration/__tests__/file-info.js # tests/integration/__tests__/help-options.js # tests/integration/__tests__/ignore-absolute-path.js # tests/integration/__tests__/ignore-emoji.js # tests/integration/__tests__/ignore-in-subdirectories.js # tests/integration/__tests__/ignore-path.js # tests/integration/__tests__/ignore-relative-path.js # tests/integration/__tests__/ignore-unknown.js # tests/integration/__tests__/ignore-vcs-files.js # tests/integration/__tests__/infer-parser.js # tests/integration/__tests__/invalid-ignore.js # tests/integration/__tests__/line-suffix-boundary.js # tests/integration/__tests__/list-different.js # tests/integration/__tests__/loglevel.js # tests/integration/__tests__/parser-api.js # tests/integration/__tests__/patterns-dirs.js # tests/integration/__tests__/patterns-glob.js # tests/integration/__tests__/patterns.js # tests/integration/__tests__/piped-output.js # tests/integration/__tests__/plugin-default-options.js # tests/integration/__tests__/plugin-extensions.js # tests/integration/__tests__/plugin-flush-line-suffix.js # tests/integration/__tests__/plugin-options-string.js # tests/integration/__tests__/plugin-options.js # tests/integration/__tests__/plugin-precedence.js # tests/integration/__tests__/plugin-preprocess.js # tests/integration/__tests__/plugin-resolution.js # tests/integration/__tests__/plugin-virtual-directory.js # tests/integration/__tests__/skip-folders.js # tests/integration/__tests__/stdin-filepath.js # tests/integration/__tests__/support-info.js # tests/integration/__tests__/syntax-error.js # tests/integration/__tests__/with-config-precedence.js # tests/integration/__tests__/with-node-modules.js # tests/integration/__tests__/with-parser-inference.js # tests/integration/__tests__/with-shebang.js # tests/integration/__tests__/write.js
177 lines
5.5 KiB
JavaScript
177 lines
5.5 KiB
JavaScript
import fs from "node:fs";
|
||
import path from "node:path";
|
||
import createEsmUtils from "esm-utils";
|
||
import runPrettier from "../run-prettier.js";
|
||
import jestPathSerializer from "../path-serializer.js";
|
||
import { projectRoot } from "../env.js";
|
||
|
||
const { __dirname } = createEsmUtils(import.meta);
|
||
|
||
expect.addSnapshotSerializer(jestPathSerializer);
|
||
|
||
// ESLint-like behavior
|
||
// https://github.com/prettier/prettier/pull/6639#issuecomment-548949954
|
||
//
|
||
// 1. `prettier dir1 dir2` – prettify all files with supported extensions inside `dir1` and `dir2`.
|
||
//
|
||
// 2. `prettier dir1 "dir2/**/*"` – prettify all files with supported extensions inside `dir1`
|
||
// as well as all files matched by the `dir2/**/*` glob.
|
||
// If any of the latter files have unknown extensions – log an error for them. (*)
|
||
//
|
||
// 3. `prettier non-exists-dir "dir2/**/*""` – log an error that `non-exists-dir` resulted in 0 files
|
||
// and prettify all files matched by the `dir2/**/*` glob.
|
||
// If any of the latter files have unknown extensions – log an error for them. (*)
|
||
// (Note: ESLint just prints an error and doesn't process anything.)
|
||
//
|
||
// 4. `prettier . "dir2/**/*"` – prettify all files with supported extensions in `.`
|
||
// and all files matched by the `dir2/**/*` glob.
|
||
// If any of the latter files have unknown extensions – log an error for them. (*)
|
||
//
|
||
// (*) That error ("No parser could be inferred for file") doesn't affect the error code.
|
||
|
||
testPatterns("1", ["dir1", "dir2"]);
|
||
testPatterns("1a - with *.foo plugin", [
|
||
"dir1",
|
||
"dir2",
|
||
"--plugin=../../plugins/extensions/plugin.cjs",
|
||
]);
|
||
testPatterns("1b - special characters in dir name", ["dir1", "!dir"], {
|
||
stdout: expect.stringMatching(/!dir[/\\]a\.js/),
|
||
});
|
||
testPatterns("1c", ["dir1", "empty"], { status: 2 });
|
||
|
||
testPatterns("2", ["dir1", "dir2/**/*"], { status: 1 });
|
||
|
||
testPatterns("3", ["nonexistent-dir", "dir2/**/*"], { status: 2 });
|
||
|
||
testPatterns("4", [".", "dir2/**/*"], { status: 1 });
|
||
|
||
describe("Trailing slash", () => {
|
||
testPatterns("1", ["./"], { status: 1, stderr: "" });
|
||
testPatterns("2", [".//"], { status: 1, stderr: "" });
|
||
testPatterns("3", ["dir1/"], { status: 1, stderr: "" });
|
||
testPatterns("4", ["dir1//"], { status: 1, stderr: "" });
|
||
testPatterns("5", [".//dir2/..//./dir1//"], { status: 1, stderr: "" });
|
||
testPatterns(
|
||
"run in sub dir 1",
|
||
[".."],
|
||
{ status: 1, stderr: "" },
|
||
"cli/patterns-dirs/dir2"
|
||
);
|
||
testPatterns(
|
||
"run in sub dir 2",
|
||
["../"],
|
||
{ status: 1, stderr: "" },
|
||
"cli/patterns-dirs/dir2"
|
||
);
|
||
testPatterns(
|
||
"run in sub dir 3",
|
||
["../dir1"],
|
||
{ status: 1, stderr: "" },
|
||
"cli/patterns-dirs/dir2"
|
||
);
|
||
testPatterns(
|
||
"run in sub dir 4",
|
||
["../dir1/"],
|
||
{ status: 1, stderr: "" },
|
||
"cli/patterns-dirs/dir2"
|
||
);
|
||
});
|
||
|
||
describe("Negative patterns", () => {
|
||
testPatterns("1", ["dir1", "!dir1/nested1"]);
|
||
testPatterns("1a", ["dir1", "!dir1/nested1/*"]);
|
||
testPatterns("2", [".", "!dir1/nested1"]);
|
||
testPatterns("3", [".", "!dir1/nested1/an1.js"]);
|
||
testPatterns("4", ["!nonexistent-dir1 !nonexistent-dir2"], { status: 2 });
|
||
testPatterns("with explicit files", ["dir1/a1.js", "dir2/a2.js", "!dir1/*"], {
|
||
status: 2,
|
||
});
|
||
});
|
||
|
||
testPatterns("Exclude yarn.lock when expanding directories", ["."], {
|
||
stdout: expect.not.stringContaining("yarn.lock"),
|
||
});
|
||
|
||
const uppercaseRocksPlugin = path.join(
|
||
projectRoot,
|
||
"tests/config/prettier-plugins/prettier-plugin-uppercase-rocks"
|
||
);
|
||
describe("plugins `.`", () => {
|
||
runPrettier("cli/dirs/plugins", [
|
||
".",
|
||
"-l",
|
||
"--plugin",
|
||
uppercaseRocksPlugin,
|
||
]).test({
|
||
write: [],
|
||
stderr: "",
|
||
status: 1,
|
||
});
|
||
});
|
||
describe("plugins `*`", () => {
|
||
runPrettier("cli/dirs/plugins", [
|
||
"*",
|
||
"-l",
|
||
"--plugin",
|
||
uppercaseRocksPlugin,
|
||
]).test({
|
||
write: [],
|
||
status: 1,
|
||
});
|
||
});
|
||
|
||
if (path.sep === "/") {
|
||
// Don't use snapshots in these tests as they're conditionally executed on non-Windows only.
|
||
|
||
const base = path.resolve(__dirname, "../cli/patterns-dirs");
|
||
|
||
describe("Backslashes in names", () => {
|
||
// We can't commit these dirs without causing problems on Windows.
|
||
|
||
beforeAll(() => {
|
||
fs.mkdirSync(path.resolve(base, "test-a\\"));
|
||
fs.writeFileSync(path.resolve(base, "test-a\\", "test.js"), "x");
|
||
fs.mkdirSync(path.resolve(base, "test-b\\?"));
|
||
fs.writeFileSync(path.resolve(base, "test-b\\?", "test.js"), "x");
|
||
});
|
||
|
||
afterAll(() => {
|
||
fs.unlinkSync(path.resolve(base, "test-a\\", "test.js"));
|
||
fs.rmdirSync(path.resolve(base, "test-a\\"));
|
||
fs.unlinkSync(path.resolve(base, "test-b\\?", "test.js"));
|
||
fs.rmdirSync(path.resolve(base, "test-b\\?"));
|
||
});
|
||
|
||
testPatterns("", ["test-a\\/test.js"], { stdout: "test-a\\/test.js\n" });
|
||
testPatterns("", ["test-a\\"], { stdout: "test-a\\/test.js\n" });
|
||
testPatterns("", ["test-a*/*"], { stdout: "test-a\\/test.js\n" });
|
||
|
||
testPatterns("", ["test-b\\?/test.js"], { stdout: "test-b\\?/test.js\n" });
|
||
testPatterns("", ["test-b\\?"], { stdout: "test-b\\?/test.js\n" });
|
||
testPatterns("", ["test-b*/*"], { stdout: "test-b\\?/test.js\n" });
|
||
});
|
||
}
|
||
|
||
function testPatterns(
|
||
namePrefix,
|
||
cliArgs,
|
||
expected = {},
|
||
cwd = "cli/patterns-dirs"
|
||
) {
|
||
const testName =
|
||
(namePrefix ? namePrefix + ": " : "") +
|
||
"prettier " +
|
||
cliArgs
|
||
.map((arg) => (/^[\w./=-]+$/.test(arg) ? arg : `'${arg}'`))
|
||
.join(" ");
|
||
|
||
describe(testName, () => {
|
||
runPrettier(cwd, [...cliArgs, "-l"]).test({
|
||
write: [],
|
||
...(!("status" in expected) && { stderr: "", status: 1 }),
|
||
...expected,
|
||
});
|
||
});
|
||
}
|