Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

importantly , this changed

this is what makes errors now log out when you're in dump mode (the same as prettier)

stderr.prefixed.error(`${fileNameToDisplay}: ${error}`);
} else if (options.list) {
stderr.error(fileNameToDisplay);
Expand Down
5 changes: 1 addition & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,10 +736,7 @@ function zipObjectUnless<T extends Key, U>(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,
Expand Down
Empty file.
1 change: 1 addition & 0 deletions test/__fixtures__/infer-parser/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo ( )
3 changes: 3 additions & 0 deletions test/__fixtures__/infer-parser/interpreters/zx-script
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env zx

await $`cat package.json | grep name`
2 changes: 2 additions & 0 deletions test/__fixtures__/infer-parser/known-unknown/known.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello( 'world'
)
4 changes: 4 additions & 0 deletions test/__fixtures__/infer-parser/known-unknown/unknown.jsonl
Original file line number Diff line number Diff line change
@@ -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♣"]]}
10 changes: 5 additions & 5 deletions test/__tests__/__snapshots__/early-exit.js.snap
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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`] = `""`;

Expand Down
48 changes: 48 additions & 0 deletions test/__tests__/__snapshots__/infer-parser.js.snap
Original file line number Diff line number Diff line change
@@ -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`] = `""`;
12 changes: 8 additions & 4 deletions test/__tests__/early-exit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

Expand Down
158 changes: 158 additions & 0 deletions test/__tests__/infer-parser.js
Original file line number Diff line number Diff line change
@@ -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: [],
});
});
7 changes: 4 additions & 3 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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();
Expand Down
Loading