Skip to content

Commit 041e1e5

Browse files
43081jfabiospampinato
authored andcommitted
Ported test suite: infer-parser
- Files with unknown parsers are skipped and debug-logged instead of logging errors.
1 parent fbdab1e commit 041e1e5

12 files changed

Lines changed: 236 additions & 16 deletions

File tree

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
182182
const filePath = filesPathsTargets[i];
183183
const fileNameToDisplay = normalizeToPosix(fastRelativePath(rootPath, filePath));
184184
//TODO: Make sure the error is syntax-highlighted when possible
185-
if (options.check || options.write) {
185+
if (options.check || options.write || options.dump) {
186186
stderr.prefixed.error(`${fileNameToDisplay}: ${error}`);
187187
} else if (options.list) {
188188
stderr.error(fileNameToDisplay);

src/utils.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,7 @@ function zipObjectUnless<T extends Key, U>(keys: T[], values: U[], unless: (valu
736736
/**
737737
* Replace `\` with `/` on Windows
738738
*/
739-
const normalizeToPosix =
740-
path.sep === "\\"
741-
? (filepath: string): string => filepath.replaceAll("\\", "/")
742-
: (filepath: string): string => filepath;
739+
const normalizeToPosix = path.sep === "\\" ? (filepath: string): string => filepath.replaceAll("\\", "/") : (filepath: string): string => filepath;
743740

744741
export {
745742
castArray,

test/__fixtures__/infer-parser/FOO

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo ( )
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env zx
2+
3+
await $`cat package.json | grep name`
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello( 'world'
2+
)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]}
2+
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]}
3+
{"name": "May", "wins": []}
4+
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]}

test/__tests__/__snapshots__/early-exit.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`show usage with --help (stderr) 1`] = `""`;
4-
5-
exports[`show usage with --help (stdout) 1`] = `
3+
exports[`show usage with --help shows help text 1`] = `
64
"
7-
prettier 3.5.3
5+
prettier $VERSION
86
97
USAGE
108
@@ -134,7 +132,9 @@ exports[`show usage with --help (stdout) 1`] = `
134132
"
135133
`;
136134
137-
exports[`show usage with --help (write) 1`] = `[]`;
135+
exports[`show usage with --help shows help text 2`] = `""`;
136+
137+
exports[`show usage with --help shows help text 3`] = `[]`;
138138
139139
exports[`show version with --version (stderr) 1`] = `""`;
140140
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`--check with unknown path and no parser multiple files (stdout) 1`] = `"Checking formatting..."`;
4+
5+
exports[`--check with unknown path and no parser specific file is ignored (stderr) 1`] = `""`;
6+
7+
exports[`--check with unknown path and no parser specific file is ignored (stdout) 1`] = `
8+
"Checking formatting...
9+
All matched files use Prettier code style!"
10+
`;
11+
12+
exports[`--list-different with unknown path and no parser multiple files (stderr) 1`] = `"FOO"`;
13+
14+
exports[`--list-different with unknown path and no parser specific file should be ignored (stderr) 1`] = `""`;
15+
16+
exports[`--write with unknown path and no parser multiple files (stdout) 1`] = `"foo.js"`;
17+
18+
exports[`--write with unknown path and no parser multiple files (write) 1`] = `
19+
[
20+
{
21+
"content": "foo();
22+
",
23+
"filename": "foo.js",
24+
},
25+
]
26+
`;
27+
28+
exports[`--write with unknown path and no parser specific file should be ignored (stderr) 1`] = `""`;
29+
30+
exports[`Known/Unknown (stdout) 1`] = `"known.js"`;
31+
32+
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"."`;
33+
34+
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"."`;
35+
36+
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"."`;
37+
38+
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"."`;
39+
40+
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"."`;
41+
42+
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"."`;
43+
44+
exports[`unknown path and no parser multiple files are ignored (stderr) 1`] = `"[error] FOO: UndefinedParserError: No parser could be inferred for file "$CWD/FOO"."`;
45+
46+
exports[`unknown path and no parser multiple files are ignored (stdout) 1`] = `"foo();"`;
47+
48+
exports[`unknown path and no parser specific file is ignored (stderr) 1`] = `""`;

test/__tests__/early-exit.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ describe("show version with --version", () => {
1111
});
1212

1313
describe("show usage with --help", () => {
14-
runCli("", [
15-
"--help",
16-
]).test({
17-
status: 0,
14+
it("shows help text", async () => {
15+
const result = await runCli("", [
16+
"--help",
17+
]);
18+
expect(result.status).toBe(0);
19+
expect(result.stdout.replaceAll(PRETTIER_VERSION, "$VERSION")).toMatchSnapshot();
20+
expect(result.stderr).toMatchSnapshot();
21+
expect(result.write).toMatchSnapshot();
1822
});
1923
});
2024

0 commit comments

Comments
 (0)