diff --git a/.eslintignore b/.eslintignore index 46ef00d6a..6e9adefbb 100644 --- a/.eslintignore +++ b/.eslintignore @@ -6,3 +6,4 @@ /dist/ **/node_modules/** /website/static/lib/ +/tests_integration/cli/ diff --git a/src/cli-util.js b/src/cli-util.js index dff83a1b3..a73b94389 100644 --- a/src/cli-util.js +++ b/src/cli-util.js @@ -256,8 +256,8 @@ function eachFilename(argv, patterns, callback) { patterns = patterns.concat(["!**/node_modules/**", "!./node_modules/**"]); } - const filePaths = globby.sync(patterns, { dot: true }); try { + const filePaths = globby.sync(patterns, { dot: true }); if (filePaths.length === 0) { console.error(`No matching files. Patterns tried: ${patterns.join(" ")}`); process.exitCode = 2; diff --git a/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap b/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap index 0d546b34c..88c70b7e4 100644 --- a/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap +++ b/tests_integration/__tests__/__snapshots__/arg-parsing.js.snap @@ -1,27 +1,21 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`boolean flags do not swallow the next argument 1`] = ` -"/* eslint-disable */ - -console.log('could be single quote and without semi'); +"console.log('could be single quote and without semi'); " `; exports[`boolean flags do not swallow the next argument 2`] = `""`; exports[`negated options work 1`] = ` -"/* eslint-disable */ - -console.log(\\"could be single quote and without semi\\") +"console.log(\\"could be single quote and without semi\\") " `; exports[`negated options work 2`] = `""`; exports[`unknown negated options are warned 1`] = ` -"/* eslint-disable */ - -console.log(\\"could be single quote and without semi\\"); +"console.log(\\"could be single quote and without semi\\"); " `; @@ -32,9 +26,7 @@ exports[`unknown negated options are warned 2`] = ` `; exports[`unknown options are warned 1`] = ` -"/* eslint-disable */ - -console.log(\\"could be single quote and without semi\\"); +"console.log(\\"could be single quote and without semi\\"); " `; diff --git a/tests_integration/__tests__/__snapshots__/config-invalid.js.snap b/tests_integration/__tests__/__snapshots__/config-invalid.js.snap new file mode 100644 index 000000000..aae7981bf --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/config-invalid.js.snap @@ -0,0 +1,15 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`throw error with invalid config format 1`] = `"Failed to parse \\"/tests_integration/cli/config/invalid/file/.prettierrc\\" as JSON, JS, or YAML."`; + +exports[`throw error with invalid config option (int) 1`] = ` +"Invalid --tab-width value. Expected an integer, but received: 0.5 +" +`; + +exports[`throw error with invalid config option (trailingComma) 1`] = ` +"Invalid configuration: +" +`; + +exports[`throw error with invalid config target (directory) 1`] = `"EISDIR: illegal operation on a directory, read"`; diff --git a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap index 57d5acb37..da0b82de5 100644 --- a/tests_integration/__tests__/__snapshots__/config-resolution.js.snap +++ b/tests_integration/__tests__/__snapshots__/config-resolution.js.snap @@ -1,20 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`CLI overrides take precedence 1`] = ` -"/* eslint-disable */ -console.log( +"console.log( \\"should have semi\\" ); -/* eslint-disable */ console.log( \\"should not have semi\\" ) -/* eslint-disable */ console.log( \\"should have semi\\" ); -/* eslint-disable */ - function f() { console.log( \\"should have tab width 8\\" @@ -25,22 +20,16 @@ function f() { module.exports = { tabWidth: 8 }; -/* eslint-disable */ - function f() { console.log( \\"should have no semicolons\\" ) } -/* eslint-disable */ - function f() { console.log( \\"should have tab width 3\\" ); } -/* eslint-disable */ - function f() { console.log.apply( null, @@ -55,9 +44,7 @@ function f() { `; exports[`accepts configuration from --config 1`] = ` -"/* eslint-disable */ - -function f() { +"function f() { console.log(\\"should have tab width 8\\") } " @@ -69,14 +56,9 @@ exports[`resolves configuration file with --find-config-path file 1`] = ` `; exports[`resolves configuration from external files 1`] = ` -"/* eslint-disable */ -console.log(\\"should have semi\\"); -/* eslint-disable */ +"console.log(\\"should have semi\\"); console.log(\\"should not have semi\\") -/* eslint-disable */ console.log(\\"should have semi\\"); -/* eslint-disable */ - function f() { console.log(\\"should have tab width 8\\"); } @@ -85,18 +67,12 @@ function f() { module.exports = { tabWidth: 8 }; -/* eslint-disable */ - function f() { console.log(\\"should have no semicolons\\") } -/* eslint-disable */ - function f() { console.log(\\"should have tab width 3\\"); } -/* eslint-disable */ - function f() { console.log.apply(null, [ 'this file', diff --git a/tests_integration/__tests__/__snapshots__/early-exit.js.snap b/tests_integration/__tests__/__snapshots__/early-exit.js.snap new file mode 100644 index 000000000..32c8d4105 --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/early-exit.js.snap @@ -0,0 +1,54 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`show usage with --help 1`] = ` +"Usage: prettier [opts] [filename ...] + +Available options: + --write Edit the file in-place. (Beware!) + --list-different or -l Print filenames of files that are different from Prettier formatting. + --config Path to a prettier configuration file (.prettierrc, package.json, prettier.config.js). + --no-config Do not look for a configuration file. + --find-config-path + Finds and prints the path to a configuration file for a given input file. + --ignore-path Path to a file containing patterns that describe files to ignore. + Defaults to ./.prettierignore. + --stdin Read input from stdin. + --stdin-filepath Path to the file used to read from stdin. + --print-width Specify the length of line that the printer will wrap on. Defaults to 80. + --tab-width Specify the number of spaces per indentation-level. Defaults to 2. + --use-tabs Indent lines with tabs instead of spaces. + --no-semi Do not print semicolons, except at the beginning of lines which may need them. + --single-quote Use single quotes instead of double quotes. + --no-bracket-spacing Do not print spaces between brackets. + --jsx-bracket-same-line Put > on the last line instead of at a new line. + --trailing-comma + Print trailing commas wherever possible when multi-line. Defaults to none. + --parser + Specify which parse to use. Defaults to babylon. + --cursor-offset Print (to stderr) where a cursor at the given position would move to after formatting. + This option cannot be used with --range-start and --range-end + --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. + This option cannot be used with --cursor-offset. + Defaults to 0. + --range-end Format code ending at a given character offset (exclusive). + The range will extend forwards to the end of the selected statement. + This option cannot be used with --cursor-offset. + Defaults to Infinity. + --no-color Do not colorize error messages. + --with-node-modules Process files inside 'node_modules' directory. + --version or -v Print Prettier version. + + +" +`; + +exports[`throw error with --find-config-path + multiple files 1`] = ` +"Cannot use --find-config-path with multiple files +" +`; + +exports[`throw error with --write + --debug-check 1`] = ` +"Cannot use --write and --debug-check together. +" +`; diff --git a/tests_integration/__tests__/__snapshots__/invalid-ignore.js.snap b/tests_integration/__tests__/__snapshots__/invalid-ignore.js.snap new file mode 100644 index 000000000..ba39faeba --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/invalid-ignore.js.snap @@ -0,0 +1,6 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`throw error with invalid ignore 1`] = ` +"Unable to read /tests_integration/cli/invalid-ignore/.prettierignore: +" +`; diff --git a/tests_integration/__tests__/__snapshots__/write.js.snap b/tests_integration/__tests__/__snapshots__/write.js.snap new file mode 100644 index 000000000..c4d93e198 --- /dev/null +++ b/tests_integration/__tests__/__snapshots__/write.js.snap @@ -0,0 +1,18 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`do not write file with --write + invalid file 1`] = ` +"invalid.js: SyntaxError: Unexpected token, expected ; (1:6) +> 1 | this is invalid! +  |  ^ +" +`; + +exports[`write file with --write + unformated file 1`] = ` +Array [ + Object { + "content": "var x = 1; +", + "filename": "unformated.js", + }, +] +`; diff --git a/tests_integration/__tests__/config-invalid.js b/tests_integration/__tests__/config-invalid.js new file mode 100644 index 000000000..f2252ab45 --- /dev/null +++ b/tests_integration/__tests__/config-invalid.js @@ -0,0 +1,38 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +expect.addSnapshotSerializer(require("../cwd-serializer")); + +test("throw error with invalid config format", () => { + const output = runPrettier("cli/config/invalid", [ + "--config", + "file/.prettierrc" + ]); + expect(output.stderr).toMatchSnapshot(); + expect(output.status).not.toBe(0); +}); + +test("throw error with invalid config target (directory)", () => { + const output = runPrettier("cli/config/invalid", [ + "--config", + "folder/.prettierrc" // this is a directory + ]); + expect(output.stderr).toMatchSnapshot(); + expect(output.status).not.toBe(0); +}); + +test("throw error with invalid config option (int)", () => { + const output = runPrettier("cli/config/invalid", ["--config", "option/int"]); + expect(output.stderr).toMatchSnapshot(); + expect(output.status).not.toBe(0); +}); + +test("throw error with invalid config option (trailingComma)", () => { + const output = runPrettier("cli/config/invalid", [ + "--config", + "option/trailingComma" + ]); + expect(output.stderr).toMatchSnapshot(); + expect(output.status).not.toBe(0); +}); diff --git a/tests_integration/__tests__/early-exit.js b/tests_integration/__tests__/early-exit.js new file mode 100644 index 000000000..95bdc75b0 --- /dev/null +++ b/tests_integration/__tests__/early-exit.js @@ -0,0 +1,32 @@ +"use strict"; + +const prettier = require("../.."); +const runPrettier = require("../runPrettier"); + +test("show version with --version", () => { + const result = runPrettier("cli/with-shebang", ["--version"]); + + expect(result.stdout).toBe(prettier.version + "\n"); + expect(result.status).toEqual(0); +}); + +test("show usage with --help", () => { + const result = runPrettier("cli", ["--help"]); + + expect(result.stdout).toMatchSnapshot(); + expect(result.status).toEqual(0); +}); + +test("throw error with --write + --debug-check", () => { + const result = runPrettier("cli", ["--write", "--debug-check"]); + + expect(result.stderr).toMatchSnapshot(); + expect(result.status).toEqual(1); +}); + +test("throw error with --find-config-path + multiple files", () => { + const result = runPrettier("cli", ["--find-config-path", "abc.js", "def.js"]); + + expect(result.stderr).toMatchSnapshot(); + expect(result.status).toEqual(1); +}); diff --git a/tests_integration/__tests__/invalid-ignore.js b/tests_integration/__tests__/invalid-ignore.js new file mode 100644 index 000000000..255f94805 --- /dev/null +++ b/tests_integration/__tests__/invalid-ignore.js @@ -0,0 +1,12 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +expect.addSnapshotSerializer(require("../cwd-serializer")); + +test("throw error with invalid ignore", () => { + const result = runPrettier("cli/invalid-ignore", ["something.js"]); + + expect(result.stderr).toMatchSnapshot(); + expect(result.status).not.toEqual(0); +}); diff --git a/tests_integration/__tests__/write.js b/tests_integration/__tests__/write.js new file mode 100644 index 000000000..be9544f72 --- /dev/null +++ b/tests_integration/__tests__/write.js @@ -0,0 +1,25 @@ +"use strict"; + +const runPrettier = require("../runPrettier"); + +test("write file with --write + unformated file", () => { + const result = runPrettier("cli/write", ["--write", "unformated.js"]); + + expect(result.write).toMatchSnapshot(); + expect(result.status).toEqual(0); +}); + +test("do not write file with --write + formated file", () => { + const result = runPrettier("cli/write", ["--write", "formated.js"]); + + expect(result.write).toHaveLength(0); + expect(result.status).toEqual(0); +}); + +test("do not write file with --write + invalid file", () => { + const result = runPrettier("cli/write", ["--write", "invalid.js"]); + + expect(result.stderr).toMatchSnapshot(); + expect(result.write).toHaveLength(0); + expect(result.status).not.toEqual(0); +}); diff --git a/tests_integration/cli/arg-parsing/file.js b/tests_integration/cli/arg-parsing/file.js index 5a555e494..354349263 100644 --- a/tests_integration/cli/arg-parsing/file.js +++ b/tests_integration/cli/arg-parsing/file.js @@ -1,3 +1 @@ -/* eslint-disable */ - console.log("could be single quote and without semi") diff --git a/tests_integration/cli/config/invalid/file/.prettierrc b/tests_integration/cli/config/invalid/file/.prettierrc new file mode 100644 index 000000000..34224b1fa --- /dev/null +++ b/tests_integration/cli/config/invalid/file/.prettierrc @@ -0,0 +1 @@ +--invalid-- \ No newline at end of file diff --git a/tests_integration/cli/config/invalid/folder/.prettierrc/placeholder b/tests_integration/cli/config/invalid/folder/.prettierrc/placeholder new file mode 100644 index 000000000..e69de29bb diff --git a/tests_integration/cli/config/invalid/option/int b/tests_integration/cli/config/invalid/option/int new file mode 100644 index 000000000..8169b913c --- /dev/null +++ b/tests_integration/cli/config/invalid/option/int @@ -0,0 +1,3 @@ +{ + "tabWidth": 0.5 +} \ No newline at end of file diff --git a/tests_integration/cli/config/invalid/option/trailingComma b/tests_integration/cli/config/invalid/option/trailingComma new file mode 100644 index 000000000..8c4f8616d --- /dev/null +++ b/tests_integration/cli/config/invalid/option/trailingComma @@ -0,0 +1,3 @@ +{ + "trailing-comma": "wow" +} \ No newline at end of file diff --git a/tests_integration/cli/config/jest/Component.js b/tests_integration/cli/config/jest/Component.js index a06d12cad..190c83a1c 100644 --- a/tests_integration/cli/config/jest/Component.js +++ b/tests_integration/cli/config/jest/Component.js @@ -1,2 +1 @@ -/* eslint-disable */ console.log("should not have semi") diff --git a/tests_integration/cli/config/jest/Component.test.js b/tests_integration/cli/config/jest/Component.test.js index 018894b0d..c4a064694 100644 --- a/tests_integration/cli/config/jest/Component.test.js +++ b/tests_integration/cli/config/jest/Component.test.js @@ -1,2 +1 @@ -/* eslint-disable */ console.log("should have semi"); diff --git a/tests_integration/cli/config/jest/__best-tests__/file.js b/tests_integration/cli/config/jest/__best-tests__/file.js index 018894b0d..c4a064694 100644 --- a/tests_integration/cli/config/jest/__best-tests__/file.js +++ b/tests_integration/cli/config/jest/__best-tests__/file.js @@ -1,2 +1 @@ -/* eslint-disable */ console.log("should have semi"); diff --git a/tests_integration/cli/config/js/file.js b/tests_integration/cli/config/js/file.js index ded607e0e..2e2b4c359 100644 --- a/tests_integration/cli/config/js/file.js +++ b/tests_integration/cli/config/js/file.js @@ -1,5 +1,3 @@ -/* eslint-disable */ - function f() { console.log("should have tab width 8"); } diff --git a/tests_integration/cli/config/no-config/file.js b/tests_integration/cli/config/no-config/file.js index da197b3c6..e5f18dccc 100644 --- a/tests_integration/cli/config/no-config/file.js +++ b/tests_integration/cli/config/no-config/file.js @@ -1,5 +1,3 @@ -/* eslint-disable */ - function f() { console.log("should have no semicolons"); } diff --git a/tests_integration/cli/config/package/file.js b/tests_integration/cli/config/package/file.js index d0bedd4f8..668d86f50 100644 --- a/tests_integration/cli/config/package/file.js +++ b/tests_integration/cli/config/package/file.js @@ -1,5 +1,3 @@ -/* eslint-disable */ - function f() { console.log("should have tab width 3"); } diff --git a/tests_integration/cli/config/rc-json/file.js b/tests_integration/cli/config/rc-json/file.js index f530b344e..e3340e7e9 100644 --- a/tests_integration/cli/config/rc-json/file.js +++ b/tests_integration/cli/config/rc-json/file.js @@ -1,5 +1,3 @@ -/* eslint-disable */ - function f() { console.log.apply(null, [ "this file", diff --git a/tests_integration/cli/ignore-path/other-regular-modules.js b/tests_integration/cli/ignore-path/other-regular-modules.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/ignore-path/other-regular-modules.js +++ b/tests_integration/cli/ignore-path/other-regular-modules.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/ignore-path/regular-module.js b/tests_integration/cli/ignore-path/regular-module.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/ignore-path/regular-module.js +++ b/tests_integration/cli/ignore-path/regular-module.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/invalid-ignore/.prettierignore/placeholder b/tests_integration/cli/invalid-ignore/.prettierignore/placeholder new file mode 100644 index 000000000..e69de29bb diff --git a/tests_integration/cli/invalid-ignore/something.js b/tests_integration/cli/invalid-ignore/something.js new file mode 100644 index 000000000..e69de29bb diff --git a/tests_integration/cli/multiple-patterns/directory/file.js b/tests_integration/cli/multiple-patterns/directory/file.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/multiple-patterns/directory/file.js +++ b/tests_integration/cli/multiple-patterns/directory/file.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/multiple-patterns/directory/nested-directory/nested-directory-file.js b/tests_integration/cli/multiple-patterns/directory/nested-directory/nested-directory-file.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/multiple-patterns/directory/nested-directory/nested-directory-file.js +++ b/tests_integration/cli/multiple-patterns/directory/nested-directory/nested-directory-file.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/multiple-patterns/other-directory/file.js b/tests_integration/cli/multiple-patterns/other-directory/file.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/multiple-patterns/other-directory/file.js +++ b/tests_integration/cli/multiple-patterns/other-directory/file.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/multiple-patterns/other-directory/nested-directory/nested-directory-file.js b/tests_integration/cli/multiple-patterns/other-directory/nested-directory/nested-directory-file.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/multiple-patterns/other-directory/nested-directory/nested-directory-file.js +++ b/tests_integration/cli/multiple-patterns/other-directory/nested-directory/nested-directory-file.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/multiple-patterns/other-regular-modules.js b/tests_integration/cli/multiple-patterns/other-regular-modules.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/multiple-patterns/other-regular-modules.js +++ b/tests_integration/cli/multiple-patterns/other-regular-modules.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/multiple-patterns/regular-module.js b/tests_integration/cli/multiple-patterns/regular-module.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/multiple-patterns/regular-module.js +++ b/tests_integration/cli/multiple-patterns/regular-module.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/with-node-modules/not_node_modules/file.js b/tests_integration/cli/with-node-modules/not_node_modules/file.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/with-node-modules/not_node_modules/file.js +++ b/tests_integration/cli/with-node-modules/not_node_modules/file.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/with-node-modules/regular-module.js b/tests_integration/cli/with-node-modules/regular-module.js index 3d0ca1bd1..ad9a93a7c 100644 --- a/tests_integration/cli/with-node-modules/regular-module.js +++ b/tests_integration/cli/with-node-modules/regular-module.js @@ -1,2 +1 @@ -/* eslint-disable */ 'use strict'; diff --git a/tests_integration/cli/write/formated.js b/tests_integration/cli/write/formated.js new file mode 100644 index 000000000..b506100a9 --- /dev/null +++ b/tests_integration/cli/write/formated.js @@ -0,0 +1 @@ +var x = 1; diff --git a/tests_integration/cli/write/invalid.js b/tests_integration/cli/write/invalid.js new file mode 100644 index 000000000..67376e5de --- /dev/null +++ b/tests_integration/cli/write/invalid.js @@ -0,0 +1 @@ +this is invalid! \ No newline at end of file diff --git a/tests_integration/cli/write/unformated.js b/tests_integration/cli/write/unformated.js new file mode 100644 index 000000000..646611c06 --- /dev/null +++ b/tests_integration/cli/write/unformated.js @@ -0,0 +1 @@ +var x = 1; \ No newline at end of file diff --git a/tests_integration/cwd-serializer.js b/tests_integration/cwd-serializer.js new file mode 100644 index 000000000..88027d12e --- /dev/null +++ b/tests_integration/cwd-serializer.js @@ -0,0 +1,8 @@ +"use strict"; + +module.exports = { + test: value => + typeof value === "string" && value.indexOf(process.cwd()) !== -1, + print: (value, serializer) => + serializer(value.replace(process.cwd(), "")) +}; diff --git a/tests_integration/runPrettier.js b/tests_integration/runPrettier.js index 5646dc914..ce91e5965 100644 --- a/tests_integration/runPrettier.js +++ b/tests_integration/runPrettier.js @@ -1,6 +1,7 @@ "use strict"; const path = require("path"); +const fs = require("fs"); function runPrettier(dir, args, options) { let status; @@ -49,6 +50,13 @@ function runPrettier(dir, args, options) { } }); + const write = []; + + const spiedFsWriteFileSync = jest.spyOn(fs, "writeFileSync"); + spiedFsWriteFileSync.mockImplementation((filename, content) => { + write.push({ filename, content }); + }); + const originalCwd = process.cwd(); const originalIsTTY = process.stdin.isTTY; const originalArgv = process.argv; @@ -81,9 +89,10 @@ function runPrettier(dir, args, options) { spiedConsoleLog.mockRestore(); spiedConsoleWarn.mockRestore(); spiedConsoleError.mockRestore(); + spiedFsWriteFileSync.mockRestore(); } - return { status, stdout, stderr }; + return { status, stdout, stderr, write }; } function normalizeDir(dir) {