Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch CLI to async #10804

Merged
merged 10 commits into from May 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/prettier.js
Expand Up @@ -2,4 +2,4 @@

"use strict";

require("../src/cli").run(process.argv.slice(2));
module.exports = require("../src/cli").run(process.argv.slice(2));
2 changes: 1 addition & 1 deletion src/cli/index.js
Expand Up @@ -8,7 +8,7 @@ const stringify = require("fast-json-stable-stringify");
const prettier = require("../index");
const core = require("./core");

function run(rawArguments) {
async function run(rawArguments) {
// Create a default level logger, so we can log errors during `logLevel` parsing
let logger = core.createLogger();

Expand Down
14 changes: 7 additions & 7 deletions tests/integration/__tests__/__snapshots__/early-exit.js.snap
@@ -1,14 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`node version error (stderr) 1`] = `
"prettier requires at least version 10.13.0 of Node, please upgrade
"
exports[`node version error 1`] = `
Object {
"stderr": "prettier requires at least version 10.13.0 of Node, please upgrade
",
"stdout": "",
"write": Array [],
}
`;

exports[`node version error (stdout) 1`] = `""`;

exports[`node version error (write) 1`] = `Array []`;

exports[`show detailed usage with --help l (alias) (stderr) 1`] = `""`;

exports[`show detailed usage with --help l (alias) (stdout) 1`] = `
Expand Down
@@ -1,6 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[` 1`] = `
exports[`show detailed external option with \`--help foo-string\` (stderr) 1`] = `""`;

exports[`show detailed external option with \`--help foo-string\` (stdout) 1`] = `
"--foo-string <string>

foo description

Default: bar
"
`;

exports[`show detailed external option with \`--help foo-string\` (write) 1`] = `Array []`;

exports[`show external options with \`--help\` 1`] = `
"Snapshot Diff:
- First value
+ Second value
Expand Down Expand Up @@ -28,16 +41,3 @@ exports[` 1`] = `
--prose-wrap <always|never|preserve>
How to wrap prose."
`;

exports[`show detailed external option with \`--help foo-string\` (stderr) 1`] = `""`;

exports[`show detailed external option with \`--help foo-string\` (stdout) 1`] = `
"--foo-string <string>

foo description

Default: bar
"
`;

exports[`show detailed external option with \`--help foo-string\` (write) 1`] = `Array []`;
58 changes: 29 additions & 29 deletions tests/integration/__tests__/__snapshots__/plugin-options.js.snap
@@ -1,34 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[` 1`] = `
"Snapshot Diff:
- First value
+ Second value

@@ -20,18 +20,20 @@
Control how Prettier formats quoted code embedded in the file.
Defaults to auto.
--end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply.
Defaults to lf.
+ --foo-option <bar|baz> foo description
+ Defaults to bar.
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
--jsx-bracket-same-line Put > on the last line instead of at a new line.
Defaults to false.
--jsx-single-quote Use single quotes in JSX.
Defaults to false.
- --parser <flow|babel|babel-flow|babel-ts|typescript|espree|meriyah|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc>
+ --parser <flow|babel|babel-flow|babel-ts|typescript|espree|meriyah|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc|foo-parser>
Which parser to use.
--print-width <int> The line length where Prettier will try wrap.
Defaults to 80.
--prose-wrap <always|never|preserve>
How to wrap prose."
`;

exports[`include plugin's parsers to the values of the \`parser\` option\` (stderr) 1`] = `""`;

exports[`include plugin's parsers to the values of the \`parser\` option\` (stdout) 1`] = `
Expand Down Expand Up @@ -83,3 +54,32 @@ Default: bar
`;

exports[`show detailed external option with \`--help foo-option\` (write) 1`] = `Array []`;

exports[`show external options with \`--help\` 1`] = `
"Snapshot Diff:
- First value
+ Second value

@@ -20,18 +20,20 @@
Control how Prettier formats quoted code embedded in the file.
Defaults to auto.
--end-of-line <lf|crlf|cr|auto>
Which end of line characters to apply.
Defaults to lf.
+ --foo-option <bar|baz> foo description
+ Defaults to bar.
--html-whitespace-sensitivity <css|strict|ignore>
How to handle whitespaces in HTML.
Defaults to css.
--jsx-bracket-same-line Put > on the last line instead of at a new line.
Defaults to false.
--jsx-single-quote Use single quotes in JSX.
Defaults to false.
- --parser <flow|babel|babel-flow|babel-ts|typescript|espree|meriyah|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc>
+ --parser <flow|babel|babel-flow|babel-ts|typescript|espree|meriyah|css|less|scss|json|json5|json-stringify|graphql|markdown|mdx|vue|yaml|glimmer|html|angular|lwc|foo-parser>
Which parser to use.
--print-width <int> The line length where Prettier will try wrap.
Defaults to 80.
--prose-wrap <always|never|preserve>
How to wrap prose."
`;
4 changes: 3 additions & 1 deletion tests/integration/__tests__/check.js
Expand Up @@ -44,5 +44,7 @@ describe("--checks works in CI just as in a non-TTY mode", () => {
status: 1,
});

expect(result0.stdout).toEqual(result1.stdout);
test("Should have same stdout", async () => {
expect(await result0.stdout).toEqual(await result1.stdout);
});
});
11 changes: 9 additions & 2 deletions tests/integration/__tests__/early-exit.js
Expand Up @@ -90,14 +90,21 @@ describe("throw error and show usage with something unexpected", () => {
});
});

describe("node version error", () => {
test("node version error", async () => {
const originalProcessVersion = process.version;

try {
Object.defineProperty(process, "version", {
value: "v8.0.0",
writable: false,
});
runPrettier("cli", ["--help"]).test({ status: 1 });
const result = runPrettier("cli", ["--help"]);
expect(await result.status).toEqual(1);
const snapshot = {};
for (const name of ["stderr", "stdout", "write"]) {
snapshot[name] = await result[name];
}
expect(snapshot).toMatchSnapshot();
} finally {
Object.defineProperty(process, "version", {
value: originalProcessVersion,
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/__tests__/list-different.js
Expand Up @@ -44,5 +44,7 @@ describe("--list-different works in CI just as in a non-TTY mode", () => {
status: 1,
});

expect(result0.stdout).toEqual(result1.stdout);
test("Should be the same", async () => {
expect(await result0.stdout).toEqual(await result1.stdout);
});
});
22 changes: 11 additions & 11 deletions tests/integration/__tests__/loglevel.js
Expand Up @@ -3,20 +3,20 @@
const stripAnsi = require("strip-ansi");
const runPrettier = require("../runPrettier");

test("do not show logs with --loglevel silent", () => {
runPrettierWithLogLevel("silent", null);
test("do not show logs with --loglevel silent", async () => {
await runPrettierWithLogLevel("silent", null);
});

test("do not show warnings with --loglevel error", () => {
runPrettierWithLogLevel("error", ["[error]"]);
test("do not show warnings with --loglevel error", async () => {
await runPrettierWithLogLevel("error", ["[error]"]);
});

test("show errors and warnings with --loglevel warn", () => {
runPrettierWithLogLevel("warn", ["[error]", "[warn]"]);
test("show errors and warnings with --loglevel warn", async () => {
await runPrettierWithLogLevel("warn", ["[error]", "[warn]"]);
});

test("show all logs with --loglevel debug", () => {
runPrettierWithLogLevel("debug", ["[error]", "[warn]", "[debug]"]);
test("show all logs with --loglevel debug", async () => {
await runPrettierWithLogLevel("debug", ["[error]", "[warn]", "[debug]"]);
});

describe("--write with --loglevel=silent doesn't log filenames", () => {
Expand All @@ -37,7 +37,7 @@ describe("Should use default level logger to log `--loglevel` error", () => {
});
});

function runPrettierWithLogLevel(logLevel, patterns) {
async function runPrettierWithLogLevel(logLevel, patterns) {
const result = runPrettier("cli/loglevel", [
"--loglevel",
logLevel,
Expand All @@ -47,9 +47,9 @@ function runPrettierWithLogLevel(logLevel, patterns) {
"not-found.js",
]);

expect(result.status).toEqual(2);
expect(await result.status).toEqual(2);

const stderr = stripAnsi(result.stderr);
const stderr = stripAnsi(await result.stderr);

if (patterns) {
for (const pattern of patterns) {
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/__tests__/patterns-dirs.js
Expand Up @@ -92,16 +92,16 @@ if (path.sep === "/") {

const base = path.resolve(__dirname, "../cli/patterns-dirs");

// We can't commit these dirs without causing problems on Windows.
describe("Backslashes in names", () => {
// We can't commit these dirs without causing problems on Windows.

// TODO: these should be moved to a `beforeAll`, but for that to be possible,
// `runPrettier` should be refactored to use `describe` and `beforeEach` for doing setup.
fs.mkdirSync(path.resolve(base, "test-a\\"));
fs.writeFileSync(path.resolve(base, "test-a\\", "test.js"), "x");
fs.mkdirSync(path.resolve(base, "test-b\\?"));
fs.writeFileSync(path.resolve(base, "test-b\\?", "test.js"), "x");
beforeAll(() => {
fs.mkdirSync(path.resolve(base, "test-a\\"));
fs.writeFileSync(path.resolve(base, "test-a\\", "test.js"), "x");
fs.mkdirSync(path.resolve(base, "test-b\\?"));
fs.writeFileSync(path.resolve(base, "test-b\\?", "test.js"), "x");
});

describe("Backslashes in names", () => {
afterAll(() => {
fs.unlinkSync(path.resolve(base, "test-a\\", "test.js"));
fs.rmdirSync(path.resolve(base, "test-a\\"));
Expand Down
36 changes: 26 additions & 10 deletions tests/integration/__tests__/piped-output.js
Expand Up @@ -19,8 +19,12 @@ describe("output with --check + unformatted differs when piped", () => {
status: 0,
});

expect(result0.stdout.length).toBeGreaterThan(result1.stdout.length);
expect(result0.write).toEqual(result1.write);
test("Result", async () => {
expect((await result0.stdout).length).toBeGreaterThan(
(await result1.stdout).length
);
expect(await result0.write).toEqual(await result1.write);
});
});

describe("no file diffs with --check + formatted file", () => {
Expand All @@ -40,9 +44,13 @@ describe("no file diffs with --check + formatted file", () => {
status: 0,
});

expect(result0.stdout).not.toEqual(result1.stdout);
expect(result0.stdout.length).toBeGreaterThan(result1.stdout.length);
expect(result0.write).toEqual(result1.write);
test("Result", async () => {
expect(await result0.stdout).not.toEqual(await result1.stdout);
expect((await result0.stdout).length).toBeGreaterThan(
(await result1.stdout).length
);
expect(await result0.write).toEqual(await result1.write);
});
});

describe("output with --list-different + unformatted differs when piped", () => {
Expand All @@ -62,8 +70,12 @@ describe("output with --list-different + unformatted differs when piped", () =>
status: 0,
});

expect(result0.stdout.length).toBeGreaterThan(result1.stdout.length);
expect(result0.write).toEqual(result1.write);
test("Result", async () => {
expect((await result0.stdout).length).toBeGreaterThan(
(await result1.stdout).length
);
expect(await result0.write).toEqual(await result1.write);
});
});

describe("no file diffs with --list-different + formatted file", () => {
Expand All @@ -83,7 +95,11 @@ describe("no file diffs with --list-different + formatted file", () => {
status: 0,
});

expect(result0.stdout).not.toEqual(result1.stdout);
expect(result0.stdout.length).toBeGreaterThan(result1.stdout.length);
expect(result0.write).toEqual(result1.write);
test("Result", async () => {
expect(await result0.stdout).not.toEqual(await result1.stdout);
expect((await result0.stdout).length).toBeGreaterThan(
(await result1.stdout).length
);
expect(await result0.write).toEqual(await result1.write);
});
});
6 changes: 3 additions & 3 deletions tests/integration/__tests__/plugin-options-string.js
Expand Up @@ -3,10 +3,10 @@
const snapshotDiff = require("snapshot-diff");
const runPrettier = require("../runPrettier");

describe("show external options with `--help`", () => {
const originalStdout = runPrettier("plugins/options-string", ["--help"])
test("show external options with `--help`", async () => {
const originalStdout = await runPrettier("plugins/options-string", ["--help"])
.stdout;
const pluggedStdout = runPrettier("plugins/options-string", [
const pluggedStdout = await runPrettier("plugins/options-string", [
"--help",
"--plugin=./plugin",
]).stdout;
Expand Down
8 changes: 5 additions & 3 deletions tests/integration/__tests__/plugin-options.js
Expand Up @@ -3,12 +3,14 @@
const snapshotDiff = require("snapshot-diff");
const runPrettier = require("../runPrettier");

describe("show external options with `--help`", () => {
const originalStdout = runPrettier("plugins/options", ["--help"]).stdout;
const pluggedStdout = runPrettier("plugins/options", [
test("show external options with `--help`", async () => {
const originalStdout = await runPrettier("plugins/options", ["--help"])
.stdout;
const pluggedStdout = await runPrettier("plugins/options", [
"--help",
"--plugin=./plugin",
]).stdout;

expect(snapshotDiff(originalStdout, pluggedStdout)).toMatchSnapshot();
});

Expand Down