From f9d7f6c8600bc721a4899c3ecfee5dcc60cfddcb Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 4 Mar 2025 14:44:17 +0100 Subject: [PATCH] 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`) --- package-lock.json | 1 + package.json | 1 + test/__fixtures__/config/.prettierrc | 8 + .../config/editorconfig/.editorconfig | 18 ++ test/__fixtures__/config/editorconfig/file.js | 3 + .../config/editorconfig/lib/file.js | 3 + .../editorconfig/lib/indent_size=tab.js | 3 + .../editorconfig/repo-root/.hg/.gitkeep | 2 + .../config/editorconfig/repo-root/file.js | 3 + .../__fixtures__/stdin-ignore/.prettierignore | 1 + .../__snapshots__/stdin-filepath.js.snap | 85 ++++++++++ test/__tests__/stdin-filepath.js | 155 ++++++++++++++++++ test/utils.js | 2 +- 13 files changed, 284 insertions(+), 1 deletion(-) create mode 100644 test/__fixtures__/config/.prettierrc create mode 100644 test/__fixtures__/config/editorconfig/.editorconfig create mode 100644 test/__fixtures__/config/editorconfig/file.js create mode 100644 test/__fixtures__/config/editorconfig/lib/file.js create mode 100644 test/__fixtures__/config/editorconfig/lib/indent_size=tab.js create mode 100644 test/__fixtures__/config/editorconfig/repo-root/.hg/.gitkeep create mode 100644 test/__fixtures__/config/editorconfig/repo-root/file.js create mode 100644 test/__fixtures__/stdin-ignore/.prettierignore create mode 100644 test/__tests__/__snapshots__/stdin-filepath.js.snap create mode 100644 test/__tests__/stdin-filepath.js diff --git a/package-lock.json b/package-lock.json index 80ddbc1..6e75d30 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,6 +41,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", diff --git a/package.json b/package.json index 1d2bd17..24e5ac4 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/__fixtures__/config/.prettierrc b/test/__fixtures__/config/.prettierrc new file mode 100644 index 0000000..43de9b5 --- /dev/null +++ b/test/__fixtures__/config/.prettierrc @@ -0,0 +1,8 @@ +endOfLine: 'auto' +overrides: +- files: "*.js" + options: + semi: false +- files: "*.ts" + options: + semi: true diff --git a/test/__fixtures__/config/editorconfig/.editorconfig b/test/__fixtures__/config/editorconfig/.editorconfig new file mode 100644 index 0000000..3157081 --- /dev/null +++ b/test/__fixtures__/config/editorconfig/.editorconfig @@ -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 diff --git a/test/__fixtures__/config/editorconfig/file.js b/test/__fixtures__/config/editorconfig/file.js new file mode 100644 index 0000000..2e2b4c3 --- /dev/null +++ b/test/__fixtures__/config/editorconfig/file.js @@ -0,0 +1,3 @@ +function f() { + console.log("should have tab width 8"); +} diff --git a/test/__fixtures__/config/editorconfig/lib/file.js b/test/__fixtures__/config/editorconfig/lib/file.js new file mode 100644 index 0000000..b905055 --- /dev/null +++ b/test/__fixtures__/config/editorconfig/lib/file.js @@ -0,0 +1,3 @@ +function f() { + console.log("should have space width 2"); +} diff --git a/test/__fixtures__/config/editorconfig/lib/indent_size=tab.js b/test/__fixtures__/config/editorconfig/lib/indent_size=tab.js new file mode 100644 index 0000000..1b4f9e7 --- /dev/null +++ b/test/__fixtures__/config/editorconfig/lib/indent_size=tab.js @@ -0,0 +1,3 @@ +function f() { + console.log("should have space width 8"); +} diff --git a/test/__fixtures__/config/editorconfig/repo-root/.hg/.gitkeep b/test/__fixtures__/config/editorconfig/repo-root/.hg/.gitkeep new file mode 100644 index 0000000..e8eb2dc --- /dev/null +++ b/test/__fixtures__/config/editorconfig/repo-root/.hg/.gitkeep @@ -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 diff --git a/test/__fixtures__/config/editorconfig/repo-root/file.js b/test/__fixtures__/config/editorconfig/repo-root/file.js new file mode 100644 index 0000000..0fb15fe --- /dev/null +++ b/test/__fixtures__/config/editorconfig/repo-root/file.js @@ -0,0 +1,3 @@ +function f() { + console.log("should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present"); +} diff --git a/test/__fixtures__/stdin-ignore/.prettierignore b/test/__fixtures__/stdin-ignore/.prettierignore new file mode 100644 index 0000000..9973a57 --- /dev/null +++ b/test/__fixtures__/stdin-ignore/.prettierignore @@ -0,0 +1 @@ +ignore/ diff --git a/test/__tests__/__snapshots__/stdin-filepath.js.snap b/test/__tests__/__snapshots__/stdin-filepath.js.snap new file mode 100644 index 0000000..edf3697 --- /dev/null +++ b/test/__tests__/__snapshots__/stdin-filepath.js.snap @@ -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`] = `[]`; diff --git a/test/__tests__/stdin-filepath.js b/test/__tests__/stdin-filepath.js new file mode 100644 index 0000000..e6b2915 --- /dev/null +++ b/test/__tests__/stdin-filepath.js @@ -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: [], + }); +}); diff --git a/test/utils.js b/test/utils.js index 9ee7e30..a7e38d7 100644 --- a/test/utils.js +++ b/test/utils.js @@ -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" });