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__/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..b53ff60ff --- /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; // eslint-disable-line +", + "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/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/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/write/formated.js b/tests_integration/cli/write/formated.js new file mode 100644 index 000000000..f574abb13 --- /dev/null +++ b/tests_integration/cli/write/formated.js @@ -0,0 +1 @@ +var x = 1; // eslint-disable-line 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..825b1e313 --- /dev/null +++ b/tests_integration/cli/write/unformated.js @@ -0,0 +1 @@ +var x = 1; // eslint-disable-line \ 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) {