diff --git a/src/index.ts b/src/index.ts index fd7319d..9db9737 100644 --- a/src/index.ts +++ b/src/index.ts @@ -182,7 +182,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions, const filePath = filesPathsTargets[i]; const fileNameToDisplay = normalizeToPosix(fastRelativePath(rootPath, filePath)); //TODO: Make sure the error is syntax-highlighted when possible - if (options.check || options.write) { + if (options.check || options.write || options.dump) { stderr.prefixed.error(`${fileNameToDisplay}: ${error}`); } else if (options.list) { stderr.error(fileNameToDisplay); diff --git a/src/utils.ts b/src/utils.ts index decbab3..454bcf0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -736,10 +736,7 @@ function zipObjectUnless(keys: T[], values: U[], unless: (valu /** * Replace `\` with `/` on Windows */ -const normalizeToPosix = - path.sep === "\\" - ? (filepath: string): string => filepath.replaceAll("\\", "/") - : (filepath: string): string => filepath; +const normalizeToPosix = path.sep === "\\" ? (filepath: string): string => filepath.replaceAll("\\", "/") : (filepath: string): string => filepath; export { castArray, diff --git a/test/__fixtures__/infer-parser/FOO b/test/__fixtures__/infer-parser/FOO new file mode 100644 index 0000000..e69de29 diff --git a/test/__fixtures__/infer-parser/foo.js b/test/__fixtures__/infer-parser/foo.js new file mode 100644 index 0000000..9da4d43 --- /dev/null +++ b/test/__fixtures__/infer-parser/foo.js @@ -0,0 +1 @@ +foo ( ) diff --git a/test/__fixtures__/infer-parser/interpreters/zx-script b/test/__fixtures__/infer-parser/interpreters/zx-script new file mode 100644 index 0000000..074fca5 --- /dev/null +++ b/test/__fixtures__/infer-parser/interpreters/zx-script @@ -0,0 +1,3 @@ +#!/usr/bin/env zx + +await $`cat package.json | grep name` diff --git a/test/__fixtures__/infer-parser/known-unknown/known.js b/test/__fixtures__/infer-parser/known-unknown/known.js new file mode 100644 index 0000000..a77f53e --- /dev/null +++ b/test/__fixtures__/infer-parser/known-unknown/known.js @@ -0,0 +1,2 @@ +hello( 'world' +) diff --git a/test/__fixtures__/infer-parser/known-unknown/unknown.jsonl b/test/__fixtures__/infer-parser/known-unknown/unknown.jsonl new file mode 100644 index 0000000..9720dd6 --- /dev/null +++ b/test/__fixtures__/infer-parser/known-unknown/unknown.jsonl @@ -0,0 +1,4 @@ +{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]} +{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]} +{"name": "May", "wins": []} +{"name": "Deloise", "wins": [["three of a kind", "5♣"]]} diff --git a/test/__tests__/__snapshots__/early-exit.js.snap b/test/__tests__/__snapshots__/early-exit.js.snap index 0012bdb..8f09756 100644 --- a/test/__tests__/__snapshots__/early-exit.js.snap +++ b/test/__tests__/__snapshots__/early-exit.js.snap @@ -1,10 +1,8 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`show usage with --help (stderr) 1`] = `""`; - -exports[`show usage with --help (stdout) 1`] = ` +exports[`show usage with --help shows help text 1`] = ` " - prettier 3.5.3 + prettier $VERSION USAGE @@ -134,7 +132,9 @@ exports[`show usage with --help (stdout) 1`] = ` " `; -exports[`show usage with --help (write) 1`] = `[]`; +exports[`show usage with --help shows help text 2`] = `""`; + +exports[`show usage with --help shows help text 3`] = `[]`; exports[`show version with --version (stderr) 1`] = `""`; diff --git a/test/__tests__/__snapshots__/infer-parser.js.snap b/test/__tests__/__snapshots__/infer-parser.js.snap new file mode 100644 index 0000000..f96466e --- /dev/null +++ b/test/__tests__/__snapshots__/infer-parser.js.snap @@ -0,0 +1,48 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`--check with unknown path and no parser multiple files (stdout) 1`] = `"Checking formatting..."`; + +exports[`--check with unknown path and no parser specific file is ignored (stderr) 1`] = `""`; + +exports[`--check with unknown path and no parser specific file is ignored (stdout) 1`] = ` +"Checking formatting... +All matched files use Prettier code style!" +`; + +exports[`--list-different with unknown path and no parser multiple files (stderr) 1`] = `"FOO"`; + +exports[`--list-different with unknown path and no parser specific file should be ignored (stderr) 1`] = `""`; + +exports[`--write with unknown path and no parser multiple files (stdout) 1`] = `"foo.js"`; + +exports[`--write with unknown path and no parser multiple files (write) 1`] = ` +[ + { + "content": "foo(); +", + "filename": "foo.js", + }, +] +`; + +exports[`--write with unknown path and no parser specific file should be ignored (stderr) 1`] = `""`; + +exports[`Known/Unknown (stdout) 1`] = `"known.js"`; + +exports[`stdin no path and no parser --check logs error but exits with 1 (stderr) 1`] = `"[error] UndefinedParserError: No parser could be inferred for file "stdin"."`; + +exports[`stdin no path and no parser --list-different logs error but exits with 1 (stderr) 1`] = `"[error] UndefinedParserError: No parser could be inferred for file "stdin"."`; + +exports[`stdin no path and no parser logs error and exits with 2 (stderr) 1`] = `"[error] UndefinedParserError: No parser could be inferred for file "stdin"."`; + +exports[`stdin with unknown path and no parser --check logs error but exits with 1 (stderr) 1`] = `"[error] UndefinedParserError: No parser could be inferred for file "foo"."`; + +exports[`stdin with unknown path and no parser --list-different logs error but exits with 1 (stderr) 1`] = `"[error] UndefinedParserError: No parser could be inferred for file "foo"."`; + +exports[`stdin with unknown path and no parser logs error and exits with 2 (stderr) 1`] = `"[error] UndefinedParserError: No parser could be inferred for file "foo"."`; + +exports[`unknown path and no parser multiple files are ignored (stderr) 1`] = `"[error] FOO: UndefinedParserError: No parser could be inferred for file "$CWD/FOO"."`; + +exports[`unknown path and no parser multiple files are ignored (stdout) 1`] = `"foo();"`; + +exports[`unknown path and no parser specific file is ignored (stderr) 1`] = `""`; diff --git a/test/__tests__/early-exit.js b/test/__tests__/early-exit.js index f6fb6f5..143b4ca 100644 --- a/test/__tests__/early-exit.js +++ b/test/__tests__/early-exit.js @@ -11,10 +11,14 @@ describe("show version with --version", () => { }); describe("show usage with --help", () => { - runCli("", [ - "--help", - ]).test({ - status: 0, + it("shows help text", async () => { + const result = await runCli("", [ + "--help", + ]); + expect(result.status).toBe(0); + expect(result.stdout.replaceAll(PRETTIER_VERSION, "$VERSION")).toMatchSnapshot(); + expect(result.stderr).toMatchSnapshot(); + expect(result.write).toMatchSnapshot(); }); }); diff --git a/test/__tests__/infer-parser.js b/test/__tests__/infer-parser.js new file mode 100644 index 0000000..2237bc1 --- /dev/null +++ b/test/__tests__/infer-parser.js @@ -0,0 +1,158 @@ +import { runCli } from "../utils"; +import { color } from "specialist"; + +describe("stdin no path and no parser", () => { + describe("logs error and exits with 2", () => { + runCli("infer-parser/", [], { input: "foo" }).test({ + status: 1, + stdout: "", + write: [], + }); + }); + + // TODO (43081j): in prettier, this tests that it exits with 0 for + // whatever reason. should we do the same? + describe("--check logs error but exits with 1", () => { + runCli("infer-parser/", ["--check"], { + input: "foo", + }).test({ + status: 1, + stdout: "", + write: [], + }); + }); + + // TODO (43081j): in prettier, this tests that it exits with 0 for + // whatever reason. should we do the same? + describe("--list-different logs error but exits with 1", () => { + runCli("infer-parser/", ["--list-different"], { + input: "foo", + }).test({ + status: 1, + stdout: "", + write: [], + }); + }); +}); + +describe("stdin with unknown path and no parser", () => { + describe("logs error and exits with 2", () => { + runCli("infer-parser/", ["--stdin-filepath", "foo"], { + input: "foo", + }).test({ + status: 1, + stdout: "", + write: [], + }); + }); + + // TODO (43081j): in prettier, this tests that it exits with 0 for + // whatever reason. should we do the same? + describe("--check logs error but exits with 1", () => { + runCli("infer-parser/", ["--check", "--stdin-filepath", "foo"], { + input: "foo", + }).test({ + status: 1, + stdout: "", + write: [], + }); + }); + + // TODO (43081j): in prettier, this tests that it exits with 0 for + // whatever reason. should we do the same? + describe("--list-different logs error but exits with 1", () => { + runCli( + "infer-parser/", + ["--list-different", "--stdin-filepath", "foo"], + { input: "foo" }, + ).test({ + status: 1, + stdout: "", + write: [], + }); + }); +}); + +describe("unknown path and no parser", () => { + describe("specific file is ignored", () => { + runCli("infer-parser/", ["--end-of-line", "lf", "FOO"]).test({ + status: 0, + stdout: "", + write: [], + }); + }); + + describe("multiple files are ignored", () => { + runCli("infer-parser/", ["--end-of-line", "lf", "*"]).test({ + status: 1, + write: [], + }); + }); +}); + +describe("--check with unknown path and no parser", () => { + describe("specific file is ignored", () => { + runCli("infer-parser/", ["--check", "FOO"]).test({ + status: 0, + write: [], + }); + }); + + describe("multiple files", () => { + runCli("infer-parser/", ["--check", "*"]).test({ + status: 1, + write: [], + stderr: `[${color.red('error')}] FOO: UndefinedParserError: No parser could be inferred for file "$CWD/FOO". +[${color.yellow('warn')}] foo.js +[${color.yellow('warn')}] Code style issues found in 1 file. Run Prettier with --write to fix.` + }); + }); +}); + +describe("--list-different with unknown path and no parser", () => { + describe("specific file should be ignored", () => { + runCli("infer-parser/", ["--list-different", "FOO"]).test({ + status: 0, + stdout: "", + write: [], + }); + }); + + describe("multiple files", () => { + runCli("infer-parser/", ["--list-different", "*"]).test({ + status: 1, + stdout: "foo.js", + write: [], + }); + }); +}); + +describe("--write with unknown path and no parser", () => { + describe("specific file should be ignored", () => { + runCli("infer-parser/", ["--write", "FOO"]).test({ + status: 0, + stdout: "", + write: [], + }); + }); + + describe("multiple files", () => { + runCli("infer-parser/", ["--write", "*"]).test({ + status: 1, + stderr: `[${color.red('error')}] FOO: UndefinedParserError: No parser could be inferred for file "$CWD/FOO".`, + }); + }); +}); + +describe("Known/Unknown", () => { + runCli("infer-parser/known-unknown", [ + "--end-of-line", + "lf", + "--list-different", + ".", + ]).test({ + status: 1, + stderr: "", + write: [], + }); +}); diff --git a/test/utils.js b/test/utils.js index 10dd8cc..2f9da29 100644 --- a/test/utils.js +++ b/test/utils.js @@ -97,12 +97,13 @@ async function getIsolatedFixtures(dir) { }; } -function getNormalizedOutput(output, options) { +function getNormalizedOutput(output, cwd) { // \r is trimmed from jest snapshots by default; // manually replacing this character with /*CR*/ to test its true presence // If ignoreLineEndings is specified, \r is simply deleted instead // output = output.replace(/\r/gu, options.ignoreLineEndings ? "" : "/*CR*/"); //TODO output = output.replace(/(\r?\n|\r)$/, ""); + output = output.replaceAll(cwd, "$CWD"); return output; } @@ -125,8 +126,8 @@ async function runCommand(dir, args, options) { } const status = await result.code; - const stdout = getNormalizedOutput((await result.stdout).toString()); - const stderr = getNormalizedOutput((await result.stderr).toString()); + const stdout = getNormalizedOutput((await result.stdout).toString(), cwd); + const stderr = getNormalizedOutput((await result.stderr).toString(), cwd); const write = (await archive?.getDiff()) || []; await fixtures?.dispose();