Skip to content

Commit 21b4663

Browse files
43081jfabiospampinato
authored andcommitted
test: migrate stdin-filepath tests
Copies the stdin-filepath tests from prettier. Notable differences: - Blocked by #21 - Syntax errors output the `stdin-filepath` basename in prettier but not in this CLI (e.g. `[Error] foo.js SyntaxError blah`)
1 parent 4b1a66a commit 21b4663

File tree

11 files changed

+269
-2
lines changed

11 files changed

+269
-2
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
import type { FormatOptions, Options, PluginsOptions } from "./types.js";
2828

2929
async function run(options: Options, pluginsDefaultOptions: PluginsOptions, pluginsCustomOptions: PluginsOptions): Promise<void> {
30-
if (options.globs.length || !isString(await getStdin())) {
30+
if (options.globs.length || (!isString(await getStdin()) && !("stdinFilepath" in options))) {
3131
return runGlobs(options, pluginsDefaultOptions, pluginsCustomOptions);
3232
} else {
3333
return runStdin(options, pluginsDefaultOptions, pluginsCustomOptions);

src/utils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ async function normalizeOptions(options: unknown, targets: unknown[]): Promise<O
353353

354354
const stdin = await getStdin();
355355

356-
if (!isString(stdin) && !globs.length) exit("Expected at least one target file/dir/glob");
356+
if (!isString(stdin) && !globs.length && !("stdinFilepath" in options)) {
357+
exit("Expected at least one target file/dir/glob");
358+
}
357359

358360
const check = "check" in options && !!options.check;
359361
const list = "listDifferent" in options && !!options.listDifferent;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*.js]
4+
indent_style = tab
5+
tab_width = 8
6+
indent_size = 2 # overridden by tab_width since indent_style = tab
7+
max_line_length = 100
8+
9+
# Indentation override for all JS under lib directory
10+
[lib/**.js]
11+
indent_style = space
12+
indent_size = 2
13+
14+
[lib/indent_size=tab.js]
15+
indent_size = tab
16+
17+
[tab_width=unset.js]
18+
tab_width = unset
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f() {
2+
console.log("should have tab width 8");
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f() {
2+
console.log("should have space width 2");
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f() {
2+
console.log("should have space width 8");
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This isn't really a Mercurial repo, but we want to pretend it is for testing purposes.
2+
See https://github.com/prettier/prettier/pull/3559#issuecomment-353857109
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function f() {
2+
console.log("should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present");
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ignore/
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`apply editorconfig for stdin-filepath with a deep path (stderr) 1`] = `""`;
4+
5+
exports[`apply editorconfig for stdin-filepath with a deep path (stderr) 2`] = `""`;
6+
7+
exports[`apply editorconfig for stdin-filepath with a deep path (stdout) 1`] = `
8+
"function f() {
9+
console.log("should be indented with a tab")
10+
}"
11+
`;
12+
13+
exports[`apply editorconfig for stdin-filepath with a deep path (stdout) 2`] = `
14+
"function f() {
15+
console.log("should be indented with a tab")
16+
}"
17+
`;
18+
19+
exports[`apply editorconfig for stdin-filepath with a deep path (write) 1`] = `[]`;
20+
21+
exports[`apply editorconfig for stdin-filepath with a deep path (write) 2`] = `[]`;
22+
23+
exports[`apply editorconfig for stdin-filepath with nonexistent directory (stderr) 1`] = `""`;
24+
25+
exports[`apply editorconfig for stdin-filepath with nonexistent directory (stdout) 1`] = `
26+
"function f() {
27+
console.log("should be indented with a tab")
28+
}"
29+
`;
30+
31+
exports[`apply editorconfig for stdin-filepath with nonexistent directory (write) 1`] = `[]`;
32+
33+
exports[`apply editorconfig for stdin-filepath with nonexistent file (stderr) 1`] = `""`;
34+
35+
exports[`apply editorconfig for stdin-filepath with nonexistent file (stdout) 1`] = `
36+
"function f() {
37+
console.log("should be indented with a tab")
38+
}"
39+
`;
40+
41+
exports[`apply editorconfig for stdin-filepath with nonexistent file (write) 1`] = `[]`;
42+
43+
exports[`don’t apply editorconfig outside project for stdin-filepath with nonexistent directory (stderr) 1`] = `""`;
44+
45+
exports[`don’t apply editorconfig outside project for stdin-filepath with nonexistent directory (stdout) 1`] = `
46+
"function f() {
47+
console.log("should be indented with 2 spaces")
48+
}"
49+
`;
50+
51+
exports[`don’t apply editorconfig outside project for stdin-filepath with nonexistent directory (write) 1`] = `[]`;
52+
53+
exports[`format correctly if stdin content compatible with stdin-filepath (stderr) 1`] = `""`;
54+
55+
exports[`format correctly if stdin content compatible with stdin-filepath (stdout) 1`] = `
56+
".name {
57+
display: none;
58+
}"
59+
`;
60+
61+
exports[`format correctly if stdin content compatible with stdin-filepath (write) 1`] = `[]`;
62+
63+
exports[`gracefully handle stdin-filepath with nonexistent directory (stderr) 1`] = `""`;
64+
65+
exports[`gracefully handle stdin-filepath with nonexistent directory (stdout) 1`] = `
66+
".name {
67+
display: none;
68+
}"
69+
`;
70+
71+
exports[`gracefully handle stdin-filepath with nonexistent directory (write) 1`] = `[]`;
72+
73+
exports[`output file as-is if stdin-filepath matched patterns in ignore-path (stderr) 1`] = `""`;
74+
75+
exports[`output file as-is if stdin-filepath matched patterns in ignore-path (write) 1`] = `[]`;
76+
77+
exports[`throw error if stdin content incompatible with stdin-filepath (stderr) 1`] = `
78+
"[error] SyntaxError: Unexpected token (1:1)
79+
[error] > 1 | .name { display: none; }
80+
[error] | ^"
81+
`;
82+
83+
exports[`throw error if stdin content incompatible with stdin-filepath (stdout) 1`] = `""`;
84+
85+
exports[`throw error if stdin content incompatible with stdin-filepath (write) 1`] = `[]`;

0 commit comments

Comments
 (0)