From cd4418df2129f99d32e896eea1c3e40f9fa4b817 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Wed, 29 Jan 2025 23:03:41 +0000 Subject: [PATCH] test: add `ignore-path` tests Adds the `ignore-path` tests from prettier. Also fixes a bug where non-existent ignore files should be treated as if we loaded an empty file (currently they throw). --- src/index.ts | 9 ++- .../ignore-path/file-info-test/.customignore | 1 + .../file-info-test/.prettierignore | 1 + .../ignore-path-test/.customignore | 1 + .../ignore-path/ignore-path-test/.gitignore | 1 + .../ignore-path-test/.prettierignore | 1 + test/__tests__/ignore-path.js | 60 +++++++++++++++++++ 7 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 test/__fixtures__/ignore-path/file-info-test/.customignore create mode 100644 test/__fixtures__/ignore-path/file-info-test/.prettierignore create mode 100644 test/__fixtures__/ignore-path/ignore-path-test/.customignore create mode 100644 test/__fixtures__/ignore-path/ignore-path-test/.gitignore create mode 100644 test/__fixtures__/ignore-path/ignore-path-test/.prettierignore create mode 100644 test/__tests__/ignore-path.js diff --git a/src/index.ts b/src/index.ts index 3d0428d..5da6f31 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,7 +88,14 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions, const ignoreManualFilesNames = options.ignore ? options.ignorePath || [] : []; const ignoreManualFilesPaths = ignoreManualFilesNames.map((fileName) => path.resolve(fileName)); - const ignoreManualFilesContents = await Promise.all(ignoreManualFilesPaths.map((filePath) => fs.readFile(filePath, "utf8"))); + const ignoreManualFilesContents = await Promise.all(ignoreManualFilesPaths.map(async (filePath) => { + try { + return await fs.readFile(filePath, "utf8"); + } catch { + // Treat a missing file as if it was empty (no ignores) + return ""; + } + })); const ignoreManualFoldersPaths = ignoreManualFilesPaths.map(path.dirname); const ignoreManual = getIgnoreBys(ignoreManualFoldersPaths, ignoreManualFilesContents.map(castArray)); diff --git a/test/__fixtures__/ignore-path/file-info-test/.customignore b/test/__fixtures__/ignore-path/file-info-test/.customignore new file mode 100644 index 0000000..b1f93ab --- /dev/null +++ b/test/__fixtures__/ignore-path/file-info-test/.customignore @@ -0,0 +1 @@ +ignored-by-customignore.js diff --git a/test/__fixtures__/ignore-path/file-info-test/.prettierignore b/test/__fixtures__/ignore-path/file-info-test/.prettierignore new file mode 100644 index 0000000..34fe7c3 --- /dev/null +++ b/test/__fixtures__/ignore-path/file-info-test/.prettierignore @@ -0,0 +1 @@ +ignored-by-prettierignore.js diff --git a/test/__fixtures__/ignore-path/ignore-path-test/.customignore b/test/__fixtures__/ignore-path/ignore-path-test/.customignore new file mode 100644 index 0000000..b1f93ab --- /dev/null +++ b/test/__fixtures__/ignore-path/ignore-path-test/.customignore @@ -0,0 +1 @@ +ignored-by-customignore.js diff --git a/test/__fixtures__/ignore-path/ignore-path-test/.gitignore b/test/__fixtures__/ignore-path/ignore-path-test/.gitignore new file mode 100644 index 0000000..a6c7c28 --- /dev/null +++ b/test/__fixtures__/ignore-path/ignore-path-test/.gitignore @@ -0,0 +1 @@ +*.js diff --git a/test/__fixtures__/ignore-path/ignore-path-test/.prettierignore b/test/__fixtures__/ignore-path/ignore-path-test/.prettierignore new file mode 100644 index 0000000..34fe7c3 --- /dev/null +++ b/test/__fixtures__/ignore-path/ignore-path-test/.prettierignore @@ -0,0 +1 @@ +ignored-by-prettierignore.js diff --git a/test/__tests__/ignore-path.js b/test/__tests__/ignore-path.js new file mode 100644 index 0000000..9d059ee --- /dev/null +++ b/test/__tests__/ignore-path.js @@ -0,0 +1,60 @@ +import { runCli } from "../utils"; +import fs from "node:fs/promises"; + +// `.js` files are ignored in `.gitignore` +const files = [ + "ignored-by-customignore.js", + "ignored-by-gitignore.js", + "ignored-by-prettierignore.js", +].map( + (file) => + new URL(`../__fixtures__/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"))); + +beforeAll(async () => { + await setup(); +}); +afterAll(clean); + +const getUnformattedFiles = async (args) => { + const { stdout } = await runCli("ignore-path/ignore-path-test/", [ + "**/*.js", + "-l", + ...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"], + ); +}); + +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([]); +}); + +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"]); +});