diff --git a/changelog_unreleased/cli/14731.md b/changelog_unreleased/cli/14731.md new file mode 100644 index 000000000..2d7b11ef5 --- /dev/null +++ b/changelog_unreleased/cli/14731.md @@ -0,0 +1,8 @@ +#### [Breaking] Ignore `.gitignore`d files by default (#14731 by @fisker) + +Prettier ignores files ignored by `.gitignore` by default. +If you want the old behavior(only ignore files ignored by `.prettierignore`), use + +```console +prettier . --write --ignore-path=.prettierignore +``` diff --git a/cspell.json b/cspell.json index f54d60e7f..5a2abf2f1 100644 --- a/cspell.json +++ b/cspell.json @@ -62,6 +62,7 @@ "concat", "concating", "cond", + "customignore", "daleroy", "danez", "Dara", diff --git a/scripts/tools/eslint-plugin-prettier-internal-rules/await-cli-tests.js b/scripts/tools/eslint-plugin-prettier-internal-rules/await-cli-tests.js index 7a932fcca..45fef7f93 100644 --- a/scripts/tools/eslint-plugin-prettier-internal-rules/await-cli-tests.js +++ b/scripts/tools/eslint-plugin-prettier-internal-rules/await-cli-tests.js @@ -31,6 +31,18 @@ module.exports = { return; } + // wrapped + // `const runCliWithoutGitignore = () => runCli()` + // `const runCliWithoutGitignore = () => { return runCli() }` + if ( + (parent.type === "ArrowFunctionExpression" && + parent.body === callExpression) || + (parent.type === "ReturnStatement" && + parent.argument === callExpression) + ) { + return; + } + if ( parent.type === "MemberExpression" && parent.object === callExpression && diff --git a/src/cli/cli-options.evaluate.js b/src/cli/cli-options.evaluate.js index 9973cceea..a433582cf 100644 --- a/src/cli/cli-options.evaluate.js +++ b/src/cli/cli-options.evaluate.js @@ -195,7 +195,7 @@ const options = { ignorePath: { array: true, category: optionCategories.CATEGORY_CONFIG, - default: [{ value: [".prettierignore"] }], + default: [{ value: [".gitignore", ".prettierignore"] }], description: outdent` Path to a file with patterns describing files to ignore. Multiple values are accepted. diff --git a/tests/integration/__tests__/__snapshots__/early-exit.js.snap b/tests/integration/__tests__/__snapshots__/early-exit.js.snap index c1e40c9d3..895a760d3 100644 --- a/tests/integration/__tests__/__snapshots__/early-exit.js.snap +++ b/tests/integration/__tests__/__snapshots__/early-exit.js.snap @@ -112,7 +112,7 @@ Config options: Find and print the path to a configuration file for the given input file. --ignore-path Path to a file with patterns describing files to ignore. Multiple values are accepted. - Defaults to [.prettierignore]. + Defaults to [.gitignore, .prettierignore]. --plugin Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s. Defaults to []. --plugin-search-dir @@ -283,7 +283,7 @@ Config options: Find and print the path to a configuration file for the given input file. --ignore-path Path to a file with patterns describing files to ignore. Multiple values are accepted. - Defaults to [.prettierignore]. + Defaults to [.gitignore, .prettierignore]. --plugin Add a plugin. Multiple plugins can be passed as separate \`--plugin\`s. Defaults to []. --plugin-search-dir diff --git a/tests/integration/__tests__/__snapshots__/file-info.js.snap b/tests/integration/__tests__/__snapshots__/file-info.js.snap index bfb84d5cc..c391e688c 100644 --- a/tests/integration/__tests__/__snapshots__/file-info.js.snap +++ b/tests/integration/__tests__/__snapshots__/file-info.js.snap @@ -54,14 +54,14 @@ exports[`extracts file-info with ignored=true for a file in .prettierignore (std exports[`extracts file-info with ignored=true for a file in .prettierignore (write) 1`] = `[]`; -exports[`extracts file-info with ignored=true for a file in a hand-picked .prettierignore (stderr) 1`] = `""`; +exports[`extracts file-info with ignored=true for a file in a hand-picked ignore file (stderr) 1`] = `""`; -exports[`extracts file-info with ignored=true for a file in a hand-picked .prettierignore (stdout) 1`] = ` +exports[`extracts file-info with ignored=true for a file in a hand-picked ignore file (stdout) 1`] = ` "{ "ignored": true, "inferredParser": null } " `; -exports[`extracts file-info with ignored=true for a file in a hand-picked .prettierignore (write) 1`] = `[]`; +exports[`extracts file-info with ignored=true for a file in a hand-picked ignore file (write) 1`] = `[]`; exports[`extracts file-info with inferredParser=foo when a plugin is hand-picked (stderr) 1`] = `""`; diff --git a/tests/integration/__tests__/__snapshots__/help-options.js.snap b/tests/integration/__tests__/__snapshots__/help-options.js.snap index da6c6ef12..f2c01a39c 100644 --- a/tests/integration/__tests__/__snapshots__/help-options.js.snap +++ b/tests/integration/__tests__/__snapshots__/help-options.js.snap @@ -251,7 +251,7 @@ exports[`show detailed usage with --help ignore-path (stdout) 1`] = ` Path to a file with patterns describing files to ignore. Multiple values are accepted. -Default: [.prettierignore]" +Default: [.gitignore, .prettierignore]" `; exports[`show detailed usage with --help ignore-path (write) 1`] = `[]`; diff --git a/tests/integration/__tests__/__snapshots__/ignore-path.js.snap b/tests/integration/__tests__/__snapshots__/ignore-path.js.snap deleted file mode 100644 index e79829cec..000000000 --- a/tests/integration/__tests__/__snapshots__/ignore-path.js.snap +++ /dev/null @@ -1,9 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`ignore file when using --debug-check (stdout) 1`] = `"other-regular-modules.js"`; - -exports[`ignore path (stdout) 1`] = `"regular-module.js"`; - -exports[`outputs files as-is if no --write (stdout) 1`] = `"'use strict';"`; - -exports[`support .prettierignore (stdout) 1`] = `"other-regular-modules.js"`; diff --git a/tests/integration/__tests__/cache.js b/tests/integration/__tests__/cache.js index c2e526fbc..3fe016210 100644 --- a/tests/integration/__tests__/cache.js +++ b/tests/integration/__tests__/cache.js @@ -6,6 +6,9 @@ function resolveDir(dir) { return fileURLToPath(new URL(`../${dir}/`, import.meta.url)); } +const runCliWithoutGitignore = (dir, args, options) => + runCli(dir, [...args, "--ignore-path", ".prettierignore"], options); + describe("--cache option", () => { const dir = resolveDir("cli/cache"); const defaultCacheFile = path.join( @@ -55,7 +58,7 @@ describe("--cache option", () => { afterAll(clean); it("throw error when cache-strategy is invalid", async () => { - const { stderr } = await runCli(dir, [ + const { stderr } = await runCliWithoutGitignore(dir, [ "--cache", "--cache-strategy", "invalid", @@ -67,7 +70,7 @@ describe("--cache option", () => { }); it("throws error when use with --stdin-filepath", async () => { - const { stderr } = await runCli( + const { stderr } = await runCliWithoutGitignore( dir, ["--cache", "--stdin-filepath", "foo.js"], { input: "const a = a;" } @@ -76,7 +79,7 @@ describe("--cache option", () => { }); it("throws error when use `--cache-strategy` without `--cache`.", async () => { - const { stderr } = await runCli( + const { stderr } = await runCliWithoutGitignore( dir, ["foo.js", "--cache-strategy", "content"], { @@ -89,7 +92,7 @@ describe("--cache option", () => { }); it("throws error when `--cache-location` is a directory.", async () => { - const { stderr } = await runCli(dir, [ + const { stderr } = await runCliWithoutGitignore(dir, [ "foo.js", "--cache", "--cache-location", @@ -108,12 +111,17 @@ describe("--cache option", () => { "code", "ENOENT" ); - await runCli(dir, ["--cache", "--cache-strategy", "metadata", "*.js"]); + await runCliWithoutGitignore(dir, [ + "--cache", + "--cache-strategy", + "metadata", + "*.js", + ]); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); }); it("doesn't format when cache is available", async () => { - const { stdout: firstStdout } = await runCli(dir, [ + const { stdout: firstStdout } = await runCliWithoutGitignore(dir, [ "--cache", "--write", "--cache-strategy", @@ -127,7 +135,7 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--cache", "--write", "--cache-strategy", @@ -150,7 +158,10 @@ describe("--cache option", () => { "metadata", "*.js", ]; - const { stdout: firstStdout } = await runCli(dir, cliArguments); + const { stdout: firstStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(firstStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -161,7 +172,10 @@ describe("--cache option", () => { // Update `a.js` await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;"); - const { stdout: secondStdout } = await runCli(dir, cliArguments); + const { stdout: secondStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(secondStdout.split("\n")).toEqual( // the cache of `b.js` is only available. expect.arrayContaining([ @@ -179,7 +193,10 @@ describe("--cache option", () => { "metadata", "*.js", ]; - const { stdout: firstStdout } = await runCli(dir, cliArguments); + const { stdout: firstStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(firstStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -191,7 +208,10 @@ describe("--cache option", () => { const time = new Date(); await fs.utimes(path.join(dir, "a.js"), time, time); - const { stdout: secondStdout } = await runCli(dir, cliArguments); + const { stdout: secondStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(secondStdout.split("\n")).toEqual( // the cache of `b.js` is only available. expect.arrayContaining([ @@ -202,7 +222,7 @@ describe("--cache option", () => { }); it("re-formats when options has been updated.", async () => { - const { stdout: firstStdout } = await runCli(dir, [ + const { stdout: firstStdout } = await runCliWithoutGitignore(dir, [ "--cache", "--write", "--cache-strategy", @@ -216,7 +236,7 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--cache", "--cache-strategy", "metadata", @@ -234,9 +254,14 @@ describe("--cache option", () => { }); it("re-formats after execution without write.", async () => { - await runCli(dir, ["--cache", "--cache-strategy", "metadata", "*.js"]); + await runCliWithoutGitignore(dir, [ + "--cache", + "--cache-strategy", + "metadata", + "*.js", + ]); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--write", "--cache", "--cache-strategy", @@ -259,7 +284,7 @@ describe("--cache option", () => { "metadata", "*.js", ]; - await runCli(dir, cliArguments); + await runCliWithoutGitignore(dir, cliArguments); // Update `a.js` to unformatted await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; "); @@ -268,9 +293,17 @@ describe("--cache option", () => { const time = new Date(); await fs.utimes(path.join(dir, "b.js"), time, time); - await runCli(dir, ["--cache", "--cache-strategy", "metadata", "*.js"]); + await runCliWithoutGitignore(dir, [ + "--cache", + "--cache-strategy", + "metadata", + "*.js", + ]); - const { stdout: thirdStdout } = await runCli(dir, cliArguments); + const { stdout: thirdStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(thirdStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -284,7 +317,7 @@ describe("--cache option", () => { stdout: firstStdout, stderr: firstStderr, status: firstStatus, - } = await runCli( + } = await runCliWithoutGitignore( dir, ["--write", "--cache", "--cache-strategy", "metadata", "*.js"], { @@ -305,7 +338,7 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--list-different", "--cache", "--cache-strategy", @@ -316,7 +349,7 @@ describe("--cache option", () => { }); it("removes cache file when run Prettier without `--cache` option", async () => { - await runCli(dir, [ + await runCliWithoutGitignore(dir, [ "--cache", "--write", "--cache-strategy", @@ -324,7 +357,7 @@ describe("--cache option", () => { "*.js", ]); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); - await runCli(dir, ["--write", "*.js"]); + await runCliWithoutGitignore(dir, ["--write", "*.js"]); await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); }); }); @@ -335,7 +368,12 @@ describe("--cache option", () => { "code", "ENOENT" ); - await runCli(dir, ["--cache", "--cache-strategy", "content", "*.js"]); + await runCliWithoutGitignore(dir, [ + "--cache", + "--cache-strategy", + "content", + "*.js", + ]); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); }); @@ -347,7 +385,10 @@ describe("--cache option", () => { "--write", "*.js", ]; - const { stdout: firstStdout } = await runCli(dir, cliArguments); + const { stdout: firstStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(firstStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -355,7 +396,10 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, cliArguments); + const { stdout: secondStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(secondStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms \(cached\)$/), @@ -372,7 +416,10 @@ describe("--cache option", () => { "--write", "*.js", ]; - const { stdout: firstStdout } = await runCli(dir, cliArguments); + const { stdout: firstStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(firstStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -383,7 +430,10 @@ describe("--cache option", () => { // Update `a.js` await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;"); - const { stdout: secondStdout } = await runCli(dir, cliArguments); + const { stdout: secondStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(secondStdout.split("\n")).toEqual( // the cache of `b.js` is only available. expect.arrayContaining([ @@ -401,7 +451,10 @@ describe("--cache option", () => { "--write", "*.js", ]; - const { stdout: firstStdout } = await runCli(dir, cliArguments); + const { stdout: firstStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(firstStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -413,7 +466,10 @@ describe("--cache option", () => { const time = new Date(); await fs.utimes(path.join(dir, "a.js"), time, time); - const { stdout: secondStdout } = await runCli(dir, cliArguments); + const { stdout: secondStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(secondStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms \(cached\)$/), @@ -423,7 +479,7 @@ describe("--cache option", () => { }); it("re-formats when options has been updated.", async () => { - const { stdout: firstStdout } = await runCli(dir, [ + const { stdout: firstStdout } = await runCliWithoutGitignore(dir, [ "--cache", "--cache-strategy", "content", @@ -437,7 +493,7 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--cache", "--write", "--cache-strategy", @@ -455,9 +511,14 @@ describe("--cache option", () => { }); it("re-formats after execution without write.", async () => { - await runCli(dir, ["--cache", "--cache-strategy", "content", "*.js"]); + await runCliWithoutGitignore(dir, [ + "--cache", + "--cache-strategy", + "content", + "*.js", + ]); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--write", "--cache", "--cache-strategy", @@ -480,7 +541,7 @@ describe("--cache option", () => { "content", "*.js", ]; - await runCli(dir, cliArguments); + await runCliWithoutGitignore(dir, cliArguments); // Update `a.js` to unformatted await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; "); @@ -489,9 +550,17 @@ describe("--cache option", () => { const time = new Date(); await fs.utimes(path.join(dir, "b.js"), time, time); - await runCli(dir, ["--cache", "--cache-strategy", "content", "*.js"]); + await runCliWithoutGitignore(dir, [ + "--cache", + "--cache-strategy", + "content", + "*.js", + ]); - const { stdout: thirdStdout } = await runCli(dir, cliArguments); + const { stdout: thirdStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(thirdStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -505,7 +574,7 @@ describe("--cache option", () => { stdout: firstStdout, stderr: firstStderr, status: firstStatus, - } = await runCli( + } = await runCliWithoutGitignore( dir, ["--write", "--cache", "--cache-strategy", "content", "*.js"], { @@ -526,7 +595,7 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, [ + const { stdout: secondStdout } = await runCliWithoutGitignore(dir, [ "--list-different", "--cache", "--cache-strategy", @@ -537,9 +606,9 @@ describe("--cache option", () => { }); it("removes cache file when run Prettier without `--cache` option", async () => { - await runCli(dir, ["--cache", "--write", "*.js"]); + await runCliWithoutGitignore(dir, ["--cache", "--write", "*.js"]); await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); - await runCli(dir, ["--write", "*.js"]); + await runCliWithoutGitignore(dir, ["--write", "*.js"]); await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); }); }); @@ -550,7 +619,7 @@ describe("--cache option", () => { "code", "ENOENT" ); - await runCli(dir, [ + await runCliWithoutGitignore(dir, [ "--cache", "--cache-location", nonDefaultCacheFileName, @@ -563,7 +632,7 @@ describe("--cache option", () => { }); it("throws error for invalid JSON file", async () => { - const { stderr } = await runCli(dir, [ + const { stderr } = await runCliWithoutGitignore(dir, [ "--cache", "--cache-location", "a.js", @@ -580,7 +649,7 @@ describe("--cache option", () => { "code", "ENOENT" ); - await runCli(dir, [ + await runCliWithoutGitignore(dir, [ "--cache", "--cache-location", nonDefaultCacheFileName, @@ -599,7 +668,10 @@ describe("--cache option", () => { nonDefaultCacheFileName, "*.js", ]; - const { stdout: firstStdout } = await runCli(dir, cliArguments); + const { stdout: firstStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(firstStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms$/), @@ -607,7 +679,10 @@ describe("--cache option", () => { ]) ); - const { stdout: secondStdout } = await runCli(dir, cliArguments); + const { stdout: secondStdout } = await runCliWithoutGitignore( + dir, + cliArguments + ); expect(secondStdout.split("\n")).toEqual( expect.arrayContaining([ expect.stringMatching(/^a\.js .+ms \(cached\)$/), diff --git a/tests/integration/__tests__/file-info.js b/tests/integration/__tests__/file-info.js index f056790f3..848bf3a93 100644 --- a/tests/integration/__tests__/file-info.js +++ b/tests/integration/__tests__/file-info.js @@ -25,7 +25,10 @@ describe("extracts file-info for a known markdown file with no extension", () => }); describe("extracts file-info with ignored=true for a file in .prettierignore", () => { - runCli("cli/ignore-path/", ["--file-info", "regular-module.js"]).test({ + runCli("cli/ignore-path/file-info-test/", [ + "--file-info", + "ignored-by-prettierignore.js", + ]).test({ status: 0, }); }); @@ -48,11 +51,11 @@ describe("file-info should not try resolve config with --no-config", () => { }); }); -describe("extracts file-info with ignored=true for a file in a hand-picked .prettierignore", () => { +describe("extracts file-info with ignored=true for a file in a hand-picked ignore file", () => { runCli("cli/", [ "--file-info", - "regular-module.js", - "--ignore-path=ignore-path/.prettierignore", + "ignored-by-customignore.js", + "--ignore-path=ignore-path/file-info-test/.customignore", ]).test({ status: 0, }); @@ -62,7 +65,7 @@ describe("non-exists ignore path", () => { runCli("cli/", [ "--file-info", "regular-module.js", - "--ignore-path=ignore-path/non-exists/.prettierignore", + "--ignore-path=ignore-path/file-info-test/.non-exists-ignore-file", ]).test({ status: 0, }); @@ -233,10 +236,13 @@ describe("API getFileInfo resolveConfig when no config is present", () => { test("API getFileInfo with ignorePath", async () => { const file = path.resolve( - path.join(__dirname, "../cli/ignore-path/regular-module.js") + path.join( + __dirname, + "../cli/ignore-path/file-info-test/ignored-by-customignore.js" + ) ); const ignorePath = path.resolve( - path.join(__dirname, "../cli/ignore-path/.prettierignore") + path.join(__dirname, "../cli/ignore-path/file-info-test/.customignore") ); await expect(prettier.getFileInfo(file)).resolves.toMatchObject({ diff --git a/tests/integration/__tests__/ignore-path.js b/tests/integration/__tests__/ignore-path.js index abadcc92c..89546a97f 100644 --- a/tests/integration/__tests__/ignore-path.js +++ b/tests/integration/__tests__/ignore-path.js @@ -1,67 +1,76 @@ -import path from "node:path"; -import fs from "node:fs"; -import createEsmUtils from "esm-utils"; -const { __dirname } = createEsmUtils(import.meta); +import fs from "node:fs/promises"; -fs.writeFileSync( - // This file is in `.gitignore`, just copy from `regular-module.js` - path.join(__dirname, "../cli/ignore-path/other-regular-modules.js"), - fs.readFileSync(path.join(__dirname, "../cli/ignore-path/regular-module.js")) +// `.js` files are ignored in `.gitignore` +const files = [ + "ignored-by-customignore.js", + "ignored-by-gitignore.js", + "ignored-by-prettierignore.js", +].map( + (file) => + new URL(`../cli/ignore-path/ignore-path-test/${file}`, import.meta.url) ); +const clean = () => + Promise.all(files.map((file) => fs.rm(file, { force: true }))); +const setup = () => + Promise.all(files.map((file) => fs.writeFile(file, " a+ b"))); -describe("ignore path", () => { - runCli("cli/ignore-path", [ +beforeAll(async () => { + await clean(); + await setup(); +}); +afterAll(clean); + +const getUnformattedFiles = async (args) => { + const { stdout } = await runCli("cli/ignore-path/ignore-path-test/", [ "**/*.js", - "--ignore-path", - ".gitignore", "-l", - ]).test({ - status: 1, - stderr: "", - write: [], - }); + ...args, + ]); + return stdout ? stdout.split("\n").sort() : []; +}; + +test("custom ignore path", async () => { + expect(await getUnformattedFiles(["--ignore-path", ".customignore"])).toEqual( + ["ignored-by-gitignore.js", "ignored-by-prettierignore.js"] + ); }); -describe("support .prettierignore", () => { - runCli("cli/ignore-path", ["**/*.js", "-l"]).test({ - status: 1, - stderr: "", - write: [], - }); +test("ignore files by .prettierignore and .gitignore by default", async () => { + expect( + await getUnformattedFiles(["--ignore-path", ".non-exists-ignore-file"]) + ).toEqual([ + "ignored-by-customignore.js", + "ignored-by-gitignore.js", + "ignored-by-prettierignore.js", + ]); + expect(await getUnformattedFiles([])).toEqual([]); }); describe("ignore file when using --debug-check", () => { - runCli("cli/ignore-path", ["**/*.js", "--debug-check"]).test({ - status: 0, - stderr: "", - write: [], - }); -}); - -describe("outputs files as-is if no --write", () => { - runCli("cli/ignore-path", ["regular-module.js"], { - ignoreLineEndings: true, - }).test({ - status: 0, - stderr: "", - write: [], - }); -}); - -describe("multiple `--ignore-path`", () => { - runCli("cli/ignore-path", [ + runCli("cli/ignore-path/ignore-path-test/", [ "**/*.js", "--debug-check", "--ignore-path", - ".gitignore", - "--ignore-path", ".prettierignore", - "--ignore-path", - ".non-exists-ignore-file", ]).test({ status: 0, - stdout: "", stderr: "", + stdout: ["ignored-by-customignore.js", "ignored-by-gitignore.js"].join( + "\n" + ), write: [], }); }); + +test("multiple `--ignore-path`", async () => { + expect( + await getUnformattedFiles([ + "--ignore-path", + ".customignore", + "--ignore-path", + ".prettierignore", + "--ignore-path", + ".non-exists-ignore-file", + ]) + ).toEqual(["ignored-by-gitignore.js"]); +}); diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index 2687f0471..28f8c533c 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -8,6 +8,9 @@ const { __dirname } = createEsmUtils(import.meta); expect.addSnapshotSerializer(jestPathSerializer); +const runCliWithoutGitignore = (dir, args, options) => + runCli(dir, [...args, "--ignore-path", ".prettierignore"], options); + // ESLint-like behavior // https://github.com/prettier/prettier/pull/6639#issuecomment-548949954 // @@ -97,7 +100,7 @@ const uppercaseRocksPlugin = path.join( "tests/config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js" ); describe("plugins `.`", () => { - runCli("cli/dirs/plugins", [ + runCliWithoutGitignore("cli/dirs/plugins", [ ".", "-l", "--plugin", @@ -109,7 +112,7 @@ describe("plugins `.`", () => { }); }); describe("plugins `*`", () => { - runCli("cli/dirs/plugins", [ + runCliWithoutGitignore("cli/dirs/plugins", [ "*", "-l", "--plugin", @@ -331,7 +334,7 @@ function testPatterns( .join(" "); describe(testName, () => { - runCli(cwd, [...cliArgs, "-l"]).test({ + runCliWithoutGitignore(cwd, [...cliArgs, "-l"]).test({ write: [], ...(!("status" in expected) && { stderr: "", status: 1 }), ...expected, diff --git a/tests/integration/cli/ignore-path/.gitignore b/tests/integration/cli/ignore-path/.gitignore deleted file mode 100644 index 507af97f1..000000000 --- a/tests/integration/cli/ignore-path/.gitignore +++ /dev/null @@ -1 +0,0 @@ -other-regular-modules.js diff --git a/tests/integration/cli/ignore-path/.prettierignore b/tests/integration/cli/ignore-path/.prettierignore deleted file mode 100644 index ca3fffe64..000000000 --- a/tests/integration/cli/ignore-path/.prettierignore +++ /dev/null @@ -1 +0,0 @@ -regular-module.js diff --git a/tests/integration/cli/ignore-path/file-info-test/.customignore b/tests/integration/cli/ignore-path/file-info-test/.customignore new file mode 100644 index 000000000..b1f93ab82 --- /dev/null +++ b/tests/integration/cli/ignore-path/file-info-test/.customignore @@ -0,0 +1 @@ +ignored-by-customignore.js diff --git a/tests/integration/cli/ignore-path/file-info-test/.prettierignore b/tests/integration/cli/ignore-path/file-info-test/.prettierignore new file mode 100644 index 000000000..34fe7c383 --- /dev/null +++ b/tests/integration/cli/ignore-path/file-info-test/.prettierignore @@ -0,0 +1 @@ +ignored-by-prettierignore.js diff --git a/tests/integration/cli/ignore-path/ignore-path-test/.customignore b/tests/integration/cli/ignore-path/ignore-path-test/.customignore new file mode 100644 index 000000000..b1f93ab82 --- /dev/null +++ b/tests/integration/cli/ignore-path/ignore-path-test/.customignore @@ -0,0 +1 @@ +ignored-by-customignore.js diff --git a/tests/integration/cli/ignore-path/ignore-path-test/.gitignore b/tests/integration/cli/ignore-path/ignore-path-test/.gitignore new file mode 100644 index 000000000..a6c7c2852 --- /dev/null +++ b/tests/integration/cli/ignore-path/ignore-path-test/.gitignore @@ -0,0 +1 @@ +*.js diff --git a/tests/integration/cli/ignore-path/ignore-path-test/.prettierignore b/tests/integration/cli/ignore-path/ignore-path-test/.prettierignore new file mode 100644 index 000000000..34fe7c383 --- /dev/null +++ b/tests/integration/cli/ignore-path/ignore-path-test/.prettierignore @@ -0,0 +1 @@ +ignored-by-prettierignore.js diff --git a/tests/integration/cli/ignore-path/regular-module.js b/tests/integration/cli/ignore-path/regular-module.js deleted file mode 100644 index ad9a93a7c..000000000 --- a/tests/integration/cli/ignore-path/regular-module.js +++ /dev/null @@ -1 +0,0 @@ -'use strict';