Skip to content
Open
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.15.10",
"cross-env": "^7.0.3",
"dedent": "^1.6.0",
"jest": "^30.0.0",
"jest-snapshot-serializer-ansi": "^2.2.1",
"jest-snapshot-serializer-raw": "^2.0.0",
Expand Down
8 changes: 8 additions & 0 deletions test/__fixtures__/config/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
endOfLine: 'auto'
overrides:
- files: "*.js"
options:
semi: false
- files: "*.ts"
options:
semi: true
18 changes: 18 additions & 0 deletions test/__fixtures__/config/editorconfig/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
root = true

[*.js]
indent_style = tab
tab_width = 8
indent_size = 2 # overridden by tab_width since indent_style = tab
max_line_length = 100

# Indentation override for all JS under lib directory
[lib/**.js]
indent_style = space
indent_size = 2

[lib/indent_size=tab.js]
indent_size = tab

[tab_width=unset.js]
tab_width = unset
3 changes: 3 additions & 0 deletions test/__fixtures__/config/editorconfig/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
console.log("should have tab width 8");
}
3 changes: 3 additions & 0 deletions test/__fixtures__/config/editorconfig/lib/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
console.log("should have space width 2");
}
3 changes: 3 additions & 0 deletions test/__fixtures__/config/editorconfig/lib/indent_size=tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
console.log("should have space width 8");
}
2 changes: 2 additions & 0 deletions test/__fixtures__/config/editorconfig/repo-root/.hg/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This isn't really a Mercurial repo, but we want to pretend it is for testing purposes.
See https://github.com/prettier/prettier/pull/3559#issuecomment-353857109
3 changes: 3 additions & 0 deletions test/__fixtures__/config/editorconfig/repo-root/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function f() {
console.log("should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present");
}
1 change: 1 addition & 0 deletions test/__fixtures__/stdin-ignore/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ignore/
85 changes: 85 additions & 0 deletions test/__tests__/__snapshots__/stdin-filepath.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`apply editorconfig for stdin-filepath with a deep path (stderr) 1`] = `""`;

exports[`apply editorconfig for stdin-filepath with a deep path (stderr) 2`] = `""`;

exports[`apply editorconfig for stdin-filepath with a deep path (stdout) 1`] = `
"function f() {
console.log("should be indented with a tab")
}"
`;

exports[`apply editorconfig for stdin-filepath with a deep path (stdout) 2`] = `
"function f() {
console.log("should be indented with a tab")
}"
`;

exports[`apply editorconfig for stdin-filepath with a deep path (write) 1`] = `[]`;

exports[`apply editorconfig for stdin-filepath with a deep path (write) 2`] = `[]`;

exports[`apply editorconfig for stdin-filepath with nonexistent directory (stderr) 1`] = `""`;

exports[`apply editorconfig for stdin-filepath with nonexistent directory (stdout) 1`] = `
"function f() {
console.log("should be indented with a tab")
}"
`;

exports[`apply editorconfig for stdin-filepath with nonexistent directory (write) 1`] = `[]`;

exports[`apply editorconfig for stdin-filepath with nonexistent file (stderr) 1`] = `""`;

exports[`apply editorconfig for stdin-filepath with nonexistent file (stdout) 1`] = `
"function f() {
console.log("should be indented with a tab")
}"
`;

exports[`apply editorconfig for stdin-filepath with nonexistent file (write) 1`] = `[]`;

exports[`don’t apply editorconfig outside project for stdin-filepath with nonexistent directory (stderr) 1`] = `""`;

exports[`don’t apply editorconfig outside project for stdin-filepath with nonexistent directory (stdout) 1`] = `
"function f() {
console.log("should be indented with 2 spaces")
}"
`;

exports[`don’t apply editorconfig outside project for stdin-filepath with nonexistent directory (write) 1`] = `[]`;

exports[`format correctly if stdin content compatible with stdin-filepath (stderr) 1`] = `""`;

exports[`format correctly if stdin content compatible with stdin-filepath (stdout) 1`] = `
".name {
display: none;
}"
`;

exports[`format correctly if stdin content compatible with stdin-filepath (write) 1`] = `[]`;

exports[`gracefully handle stdin-filepath with nonexistent directory (stderr) 1`] = `""`;

exports[`gracefully handle stdin-filepath with nonexistent directory (stdout) 1`] = `
".name {
display: none;
}"
`;

exports[`gracefully handle stdin-filepath with nonexistent directory (write) 1`] = `[]`;

exports[`output file as-is if stdin-filepath matched patterns in ignore-path (stderr) 1`] = `""`;

exports[`output file as-is if stdin-filepath matched patterns in ignore-path (write) 1`] = `[]`;

exports[`throw error if stdin content incompatible with stdin-filepath (stderr) 1`] = `
"[error] SyntaxError: Unexpected token (1:1)
[error] > 1 | .name { display: none; }
[error] | ^"
`;

exports[`throw error if stdin content incompatible with stdin-filepath (stdout) 1`] = `""`;

exports[`throw error if stdin content incompatible with stdin-filepath (write) 1`] = `[]`;
155 changes: 155 additions & 0 deletions test/__tests__/stdin-filepath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { runCli } from "../utils";
import dedent from "dedent";

describe("format correctly if stdin content compatible with stdin-filepath", () => {
runCli("", [
"--stdin-filepath",
"abc.css"
], {
input: ".name { display: none; }" // css
}).test({
status: 0,
});
});

describe("throw error if stdin content incompatible with stdin-filepath", () => {
runCli("", [
"--stdin-filepath",
"abc.js"
], {
input: ".name { display: none; }" // css
}).test({
status: "non-zero",
});
});

describe("gracefully handle stdin-filepath with nonexistent directory", () => {
runCli("", [
"--stdin-filepath",
"definitely/nonexistent/path.css"
], {
input: ".name { display: none; }" // css
}).test({
status: 0,
});
});

describe("apply editorconfig for stdin-filepath with nonexistent file", () => {
runCli("", [
"--stdin-filepath",
"config/editorconfig/nonexistent.js"
], {
input: dedent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}).test({
status: 0,
});
});

describe("apply editorconfig for stdin-filepath with nonexistent directory", () => {
runCli("", [
"--stdin-filepath",
"config/editorconfig/nonexistent/one/two/three.js"
], {
input: dedent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}).test({
status: 0,
});
});

describe("apply editorconfig for stdin-filepath with a deep path", () => {
runCli("", [
"--stdin-filepath",
"config/editorconfig/" + "a/".repeat(30) + "three.js"
], {
input: dedent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}).test({
status: 0,
});
});

describe("apply editorconfig for stdin-filepath in root", () => {
const code = dedent`
function f() {
console.log("should be indented with a tab");
}
`;
runCli("", [
"--stdin-filepath",
"/foo.js"
], {
input: code, // js
}).test({
status: 0,
stdout: code,
stderr: "",
write: [],
});
});

describe("apply editorconfig for stdin-filepath with a deep path", () => {
runCli("", [
"--stdin-filepath",
"config/editorconfig/" + "a/".repeat(30) + "three.js"
], {
input: dedent`
function f() {
console.log("should be indented with a tab");
}
`, // js
}).test({
status: 0,
});
});

describe("don’t apply editorconfig outside project for stdin-filepath with nonexistent directory", () => {
runCli("", [
"--stdin-filepath",
"config/editorconfig/repo-root/nonexistent/one/two/three.js",
], {
input: dedent`
function f() {
console.log("should be indented with 2 spaces");
}
`, // js
}).test({
status: 0,
});
});

describe.skip("output file as-is if stdin-filepath matched patterns in ignore-path", () => {
runCli("stdin-ignore", [
"--stdin-filepath",
"ignore/example.js"
], {
input: "hello_world( );",
}).test({
stdout: "hello_world( );",
status: 0,
});
});

describe.skip("Should format stdin even if it's empty", () => {
runCli("", [
"--stdin-filepath",
"example.js"
], {
isTTY: true,
}).test({
stdout: "",
status: 0,
stderr: "",
write: [],
});
});
2 changes: 1 addition & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function getNormalizedPath(filePath) {
async function runCommand(dir, args, options) {
const fixtures = dir ? await getIsolatedFixtures(dir) : undefined;
const archive = fixtures ? await getArchive(fixtures.path) : undefined;
const cwd = fixtures ? fixtures.path : TESTS_PATH;
const cwd = fixtures ? fixtures.path : FIXTURES_PATH;
const argsForTesting = ["--no-parallel"];
const argsWithReplacements = args.map((arg) => arg.replaceAll("$CWD", cwd));
const result = exec("node", [BIN_PATH, ...argsForTesting, ...argsWithReplacements], { cwd, stdio: "pipe" });
Expand Down
Loading