Merge pull request #2738 from ikatyang/test/more-cli-tests
test(integration): add more tests
This commit is contained in:
commit
fd025a855e
41 files changed
+237
-64
No files matched your search
@@ -6,3 +6,4 @@
|
||||
/dist/
|
||||
**/node_modules/**
|
||||
/website/static/lib/
|
||||
/tests_integration/cli/
|
||||
+1
-1
@@ -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;
|
||||
|
||||
@@ -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\\");
|
||||
"
|
||||
`;
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`throw error with invalid config format 1`] = `"Failed to parse \\"<cwd>/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"`;
|
||||
@@ -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',
|
||||
|
||||
@@ -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 <path>
|
||||
Finds and prints the path to a configuration file for a given input file.
|
||||
--ignore-path <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 <int> Specify the length of line that the printer will wrap on. Defaults to 80.
|
||||
--tab-width <int> 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 <none|es5|all>
|
||||
Print trailing commas wherever possible when multi-line. Defaults to none.
|
||||
--parser <flow|babylon|typescript|postcss|json|graphql>
|
||||
Specify which parse to use. Defaults to babylon.
|
||||
--cursor-offset <int> 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 <int> 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 <int> 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.
|
||||
"
|
||||
`;
|
||||
@@ -0,0 +1,6 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`throw error with invalid ignore 1`] = `
|
||||
"Unable to read <cwd>/tests_integration/cli/invalid-ignore/.prettierignore:
|
||||
"
|
||||
`;
|
||||
@@ -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)
|
||||
[0m[31m[1m>[22m[39m[90m 1 | [39m[36mthis[39m is invalid[33m![39m
|
||||
[90m | [39m [31m[1m^[22m[39m[0m
|
||||
"
|
||||
`;
|
||||
|
||||
exports[`write file with --write + unformated file 1`] = `
|
||||
Array [
|
||||
Object {
|
||||
"content": "var x = 1;
|
||||
",
|
||||
"filename": "unformated.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);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -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);
|
||||
});
|
||||
@@ -1,3 +1 @@
|
||||
/* eslint-disable */
|
||||
|
||||
console.log("could be single quote and without semi")
|
||||
@@ -0,0 +1 @@
|
||||
--invalid--
|
||||
Whitespace-only changes.
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"tabWidth": 0.5
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"trailing-comma": "wow"
|
||||
}
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
console.log("should not have semi")
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
console.log("should have semi");
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
console.log("should have semi");
|
||||
@@ -1,5 +1,3 @@
|
||||
/* eslint-disable */
|
||||
|
||||
function f() {
|
||||
console.log("should have tab width 8");
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
/* eslint-disable */
|
||||
|
||||
function f() {
|
||||
console.log("should have no semicolons");
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
/* eslint-disable */
|
||||
|
||||
function f() {
|
||||
console.log("should have tab width 3");
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
/* eslint-disable */
|
||||
|
||||
function f() {
|
||||
console.log.apply(null, [
|
||||
"this file",
|
||||
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
Whitespace-only changes.
Whitespace-only changes.
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
-1
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
-1
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -1,2 +1 @@
|
||||
/* eslint-disable */
|
||||
'use strict';
|
||||
@@ -0,0 +1 @@
|
||||
var x = 1;
|
||||
@@ -0,0 +1 @@
|
||||
this is invalid!
|
||||
@@ -0,0 +1 @@
|
||||
var x = 1;
|
||||
@@ -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(), "<cwd>"))
|
||||
};
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user