From ef7510f28fe8133bd34ee33c1ce868170b9c2b27 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 28 Jan 2025 12:28:36 +0000 Subject: [PATCH 1/3] test: add early-exit tests These are copied from prettier. The following have been removed: - show detailed usage with `--help l` - show usage per option (e.g. `--help tab-width`) - throw error when usage requested for unknown option (e.g. `--help foo`) The following have been changed: - throw error and show usage when called with no args - we do not currently show usage --- .../__snapshots__/early-exit.js.snap | 165 ++++++++++++++++++ test/__tests__/early-exit.js | 32 ++++ 2 files changed, 197 insertions(+) create mode 100644 test/__tests__/__snapshots__/early-exit.js.snap create mode 100644 test/__tests__/early-exit.js diff --git a/test/__tests__/__snapshots__/early-exit.js.snap b/test/__tests__/__snapshots__/early-exit.js.snap new file mode 100644 index 0000000..9d35d92 --- /dev/null +++ b/test/__tests__/__snapshots__/early-exit.js.snap @@ -0,0 +1,165 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`show usage with --help (stderr) 1`] = `""`; + +exports[`show usage with --help (stdout) 1`] = ` +" + prettier 3.3.3 + + USAGE + + prettier [file/dir/glob...] [options] + prettier "src/**/*.js" --check + prettier "src/**/*.js" -l --no-cache + prettier "src/**/*.js" --write --no-parallel + prettier ./path/to/target/file.js --cache-location ./path/to/cache/file.json + + ARGUMENTS + + [file/dir/glob...] Files, directories or globs to format + + OUTPUT OPTIONS + + --check, -c Check if the given files are formatted, print a human-friendly summary (see also --list-different) + --list-different, -l Print the names of files that are different from Prettier's formatting (see also --check) + --write, -w Edit files in-place (Beware!) + + FORMAT OPTIONS + + --arrow-parens + Include parentheses around a sole arrow function parameter + Defaults to "always" + --bracket-same-line Put ">" of opening tags on the last line instead of on a new line + Defaults to "false" + --no-bracket-spacing Do not print spaces between brackets + Defaults to "true" + --embedded-language-formatting + Control how Prettier formats quoted code embedded in the file + Defaults to "auto" + --end-of-line + Which end of line characters to apply + Defaults to "lf" + --experimental-ternaries + Use curious ternaries, with the question mark after the condition + Defaults to "false" + --html-whitespace-sensitivity + How to handle whitespaces in HTML + Defaults to "css" + --jsx-single-quote Use single quotes in JSX + Defaults to "false" + --parser + Which parser to use + --print-width The line length where Prettier will try wrap + Defaults to "80" + --prose-wrap + How to wrap prose + Defaults to "preserve" + --quote-props + Change when properties in objects are quoted + Defaults to "as-needed" + --no-semi Do not print semicolons, except at the beginning of lines which may need them + Defaults to "true" + --single-attribute-per-line + Enforce single attribute per line in HTML, Vue and JSX + Defaults to "false" + --single-quote Use single quotes instead of double quotes + Defaults to "false" + --tab-width Number of spaces per indentation level + Defaults to "2" + --trailing-comma + Print trailing commas wherever possible when multi-line + Defaults to "all" + --use-tabs Indent with tabs instead of spaces + Defaults to "false" + --vue-indent-script-and-style + Indent script and style tags in Vue files + Defaults to "false" + + CONFIG OPTIONS + + --no-config Do not look for a configuration file + --config-path Path to a Prettier configuration file (.prettierrc, package.json, prettier.config.js) + --config-precedence + Define in which order config files and CLI options should be evaluated. + Defaults to "cli-override" + --no-editorconfig Don't take .editorconfig into account when parsing configuration + --no-ignore Do not look for an ignore file + --ignore-path Path to a file with patterns describing files to ignore + Multiple values are accepted + Defaults to [.gitignore, .prettierignore] + --plugin Add a plugin + Multiple plugins are accepted + Defaults to [] + --with-node-modules Process files inside the "node_modules" directory + + EDITOR OPTIONS + + --cursor-offset Print (to stderr) where a cursor at the given position would move to after formatting + Defaults to "-1" + --range-end Format code ending at a given character offset (exclusive) + The range will extend forwards to the end of the selected statement + Defaults to "Infinity" + --range-start Format code starting at a given character offset + The range will extend backwards to the start of the first line containing the selected statement + Defaults to "0" + + OTHER OPTIONS + + --help Display help for the command + --version, -v Display the version number + --no-cache Do not use the built-in caching mechanism + --cache-location Path to the cache file + --no-color Do not colorize output messages + --no-error-on-unmatched-pattern + Prevent errors when pattern is unmatched + --ignore-unknown, -u Ignore unknown files + --insert-pragma Insert @format pragma into file's first docblock comment + Defaults to "false" + --log-level + What level of logs to report + Defaults to "log" + --no-parallel Process files in parallel + Defaults to "true" + --parallel-workers Number of parallel workers to use + Defaults to "0" + --require-pragma Require either "@prettier" or "@format" to be present in the file's first docblock comment in order for it to be formatted + Defaults to "false" + --stdin-filepath Path to the file to pretend that stdin comes from + " +`; + +exports[`show usage with --help (write) 1`] = `[]`; + +exports[`show version with --version (stderr) 1`] = `""`; + +exports[`show version with --version (write) 1`] = `[]`; + +exports[`throw error with --check + --list-different (stderr) 1`] = `""`; + +exports[`throw error with --check + --list-different (stdout) 1`] = ` +" + Incompatible options: "check" and "list-different" cannot be used together +" +`; + +exports[`throw error with --check + --list-different (write) 1`] = `[]`; + +exports[`throw error with something unexpected (stderr) 1`] = `""`; + +exports[`throw error with something unexpected (stdout) 1`] = ` +" + Expected at least one target file/dir/glob +" +`; + +exports[`throw error with something unexpected (write) 1`] = `[]`; + +exports[`throw unsupported error with --file-info (stderr) 1`] = `""`; + +exports[`throw unsupported error with --file-info (stdout) 1`] = ` +" + The "--file-info" option is not currently supported, please open an issue on GitHub if you need it +" +`; + +exports[`throw unsupported error with --file-info (write) 1`] = `[]`; diff --git a/test/__tests__/early-exit.js b/test/__tests__/early-exit.js new file mode 100644 index 0000000..ba801ca --- /dev/null +++ b/test/__tests__/early-exit.js @@ -0,0 +1,32 @@ +import { runCli } from "../utils"; + +describe.skip("show version with --version", () => { + runCli("with-shebang", ["--version"]).test({ + stdout: "???", + status: 0, + }); +}); + +describe("show usage with --help", () => { + runCli("", ["--help"]).test({ + status: 0, + }); +}); + +describe("throw error with --check + --list-different", () => { + runCli("", ["--check", "--list-different"]).test({ + status: 1, + }); +}); + +describe("throw unsupported error with --file-info", () => { + runCli("", ["--file-info", "abc.js", "def.js"]).test({ + status: 1, + }); +}); + +describe("throw error with something unexpected", () => { + runCli("", [], { isTTY: true }).test({ + status: "non-zero", + }); +}); From 2e612319e378f47f9c0beb9622fec7d535513ede Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Sun, 2 Feb 2025 19:14:00 +0000 Subject: [PATCH 2/3] test: re-enable version test --- test/__tests__/early-exit.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/__tests__/early-exit.js b/test/__tests__/early-exit.js index ba801ca..8ed3f7e 100644 --- a/test/__tests__/early-exit.js +++ b/test/__tests__/early-exit.js @@ -1,8 +1,9 @@ import { runCli } from "../utils"; +import { PRETTIER_VERSION } from "../../dist/constants.js"; -describe.skip("show version with --version", () => { +describe("show version with --version", () => { runCli("with-shebang", ["--version"]).test({ - stdout: "???", + stdout: PRETTIER_VERSION, status: 0, }); }); From 4bf76abe6b1be2e60e7a0629c27fcbda9e2ec415 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Sun, 2 Feb 2025 19:22:58 +0000 Subject: [PATCH 3/3] test: update early exit snapshot --- .../__tests__/__snapshots__/early-exit.js.snap | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/__tests__/__snapshots__/early-exit.js.snap b/test/__tests__/__snapshots__/early-exit.js.snap index 9d35d92..f8d141a 100644 --- a/test/__tests__/__snapshots__/early-exit.js.snap +++ b/test/__tests__/__snapshots__/early-exit.js.snap @@ -134,32 +134,32 @@ exports[`show version with --version (stderr) 1`] = `""`; exports[`show version with --version (write) 1`] = `[]`; -exports[`throw error with --check + --list-different (stderr) 1`] = `""`; - -exports[`throw error with --check + --list-different (stdout) 1`] = ` +exports[`throw error with --check + --list-different (stderr) 1`] = ` " Incompatible options: "check" and "list-different" cannot be used together " `; -exports[`throw error with --check + --list-different (write) 1`] = `[]`; +exports[`throw error with --check + --list-different (stdout) 1`] = `""`; -exports[`throw error with something unexpected (stderr) 1`] = `""`; +exports[`throw error with --check + --list-different (write) 1`] = `[]`; -exports[`throw error with something unexpected (stdout) 1`] = ` +exports[`throw error with something unexpected (stderr) 1`] = ` " Expected at least one target file/dir/glob " `; -exports[`throw error with something unexpected (write) 1`] = `[]`; +exports[`throw error with something unexpected (stdout) 1`] = `""`; -exports[`throw unsupported error with --file-info (stderr) 1`] = `""`; +exports[`throw error with something unexpected (write) 1`] = `[]`; -exports[`throw unsupported error with --file-info (stdout) 1`] = ` +exports[`throw unsupported error with --file-info (stderr) 1`] = ` " The "--file-info" option is not currently supported, please open an issue on GitHub if you need it " `; +exports[`throw unsupported error with --file-info (stdout) 1`] = `""`; + exports[`throw unsupported error with --file-info (write) 1`] = `[]`;