From c0eed476357451bfd9b71b5361df3b7c0bdf56c9 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Apr 2021 17:13:01 +0800 Subject: [PATCH 01/10] Switch CLI to async --- bin/prettier.js | 2 +- src/cli/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/prettier.js b/bin/prettier.js index 79fe9b23705a..7f3f91780b41 100755 --- a/bin/prettier.js +++ b/bin/prettier.js @@ -2,4 +2,4 @@ "use strict"; -require("../src/cli").run(process.argv.slice(2)); +module.exports = require("../src/cli").run(process.argv.slice(2)); diff --git a/src/cli/index.js b/src/cli/index.js index b38f6f511949..7ea19bd3abcf 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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(); From 3c2ed47cc0e8f683ca8309f0f02432aa7c8e6da4 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Apr 2021 17:59:44 +0800 Subject: [PATCH 02/10] Rewrite test --- tests/integration/runPrettier.js | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/tests/integration/runPrettier.js b/tests/integration/runPrettier.js index 7c83ae5dec31..80e032c355a9 100644 --- a/tests/integration/runPrettier.js +++ b/tests/integration/runPrettier.js @@ -6,7 +6,7 @@ const stripAnsi = require("strip-ansi"); const { SynchronousPromise } = require("synchronous-promise"); const { prettierCli, thirdParty } = require("./env"); -function runPrettier(dir, args = [], options = {}) { +async function run(dir, args, options) { args = Array.isArray(args) ? args : [args]; let status; @@ -99,7 +99,7 @@ function runPrettier(dir, args = [], options = {}) { .mockImplementation(() => process.cwd()); try { - require(prettierCli); + await require(prettierCli); status = (status === undefined ? process.exitCode : status) || 0; } catch (error) { status = 1; @@ -113,11 +113,29 @@ function runPrettier(dir, args = [], options = {}) { jest.restoreAllMocks(); } - const result = { status, stdout, stderr, write }; + return { status, stdout, stderr, write }; + + function appendStdout(text) { + if (status === undefined) { + stdout += text; + } + } + function appendStderr(text) { + if (status === undefined) { + stderr += text; + } + } +} + +function runPrettier(dir, args = [], options = {}) { + let result; const testResult = (testOptions) => { - for (const name of Object.keys(result)) { - test(`(${name})`, () => { + for (const name of ["status", "stdout", "stderr", "write"]) { + test(`(${name})`, async () => { + if (!result) { + result = await run(dir, args, options); + } const value = // \r is trimmed from jest snapshots by default; // manually replacing this character with /*CR*/ to test its true presence @@ -138,22 +156,9 @@ function runPrettier(dir, args = [], options = {}) { } }); } - - return result; }; - return { test: testResult, ...result }; - - function appendStdout(text) { - if (status === undefined) { - stdout += text; - } - } - function appendStderr(text) { - if (status === undefined) { - stderr += text; - } - } + return { test: testResult }; } function normalizeDir(dir) { From 06ae838c67b67ba1fda137145c030ba0aa24d319 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Apr 2021 19:28:11 +0800 Subject: [PATCH 03/10] Fix tests --- .../__snapshots__/early-exit.js.snap | 14 ++--- .../plugin-options-string.js.snap | 28 ++++----- .../__snapshots__/plugin-options.js.snap | 58 +++++++++---------- tests/integration/__tests__/check.js | 4 +- tests/integration/__tests__/early-exit.js | 11 +++- tests/integration/__tests__/list-different.js | 4 +- tests/integration/__tests__/loglevel.js | 22 +++---- tests/integration/__tests__/piped-output.js | 36 ++++++++---- .../__tests__/plugin-options-string.js | 6 +- tests/integration/__tests__/plugin-options.js | 8 ++- tests/integration/runPrettier.js | 44 +++++++++++--- 11 files changed, 147 insertions(+), 88 deletions(-) diff --git a/tests/integration/__tests__/__snapshots__/early-exit.js.snap b/tests/integration/__tests__/__snapshots__/early-exit.js.snap index 832a1af9f280..4faab0a77f4e 100644 --- a/tests/integration/__tests__/__snapshots__/early-exit.js.snap +++ b/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`] = ` diff --git a/tests/integration/__tests__/__snapshots__/plugin-options-string.js.snap b/tests/integration/__tests__/__snapshots__/plugin-options-string.js.snap index df76a8fa3423..2ed7ae8b698f 100644 --- a/tests/integration/__tests__/__snapshots__/plugin-options-string.js.snap +++ b/tests/integration/__tests__/__snapshots__/plugin-options-string.js.snap @@ -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 + + 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 @@ -28,16 +41,3 @@ exports[` 1`] = ` --prose-wrap 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 - - foo description - -Default: bar -" -`; - -exports[`show detailed external option with \`--help foo-string\` (write) 1`] = `Array []`; diff --git a/tests/integration/__tests__/__snapshots__/plugin-options.js.snap b/tests/integration/__tests__/__snapshots__/plugin-options.js.snap index 93d8477e51d7..276c950e742e 100644 --- a/tests/integration/__tests__/__snapshots__/plugin-options.js.snap +++ b/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 - Which end of line characters to apply. - Defaults to lf. -+ --foo-option foo description -+ Defaults to bar. - --html-whitespace-sensitivity - 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 -+ --parser - Which parser to use. - --print-width The line length where Prettier will try wrap. - Defaults to 80. - --prose-wrap - 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`] = ` @@ -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 + Which end of line characters to apply. + Defaults to lf. ++ --foo-option foo description ++ Defaults to bar. + --html-whitespace-sensitivity + 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 ++ --parser + Which parser to use. + --print-width The line length where Prettier will try wrap. + Defaults to 80. + --prose-wrap + How to wrap prose." +`; diff --git a/tests/integration/__tests__/check.js b/tests/integration/__tests__/check.js index 87773bff4ef0..b2d35336fc01 100644 --- a/tests/integration/__tests__/check.js +++ b/tests/integration/__tests__/check.js @@ -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 has same stdout", async () => { + expect(await result0.stdout).toEqual(await result1.stdout); + }); }); diff --git a/tests/integration/__tests__/early-exit.js b/tests/integration/__tests__/early-exit.js index 7b623828afc8..c9d068becda6 100644 --- a/tests/integration/__tests__/early-exit.js +++ b/tests/integration/__tests__/early-exit.js @@ -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, diff --git a/tests/integration/__tests__/list-different.js b/tests/integration/__tests__/list-different.js index 2774d6a865f1..c767e6d4dd25 100644 --- a/tests/integration/__tests__/list-different.js +++ b/tests/integration/__tests__/list-different.js @@ -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); + }); }); diff --git a/tests/integration/__tests__/loglevel.js b/tests/integration/__tests__/loglevel.js index 11280a8a963e..54b854dc4364 100644 --- a/tests/integration/__tests__/loglevel.js +++ b/tests/integration/__tests__/loglevel.js @@ -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", () => { @@ -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, @@ -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) { diff --git a/tests/integration/__tests__/piped-output.js b/tests/integration/__tests__/piped-output.js index 173cd72e1357..81604e21acb3 100644 --- a/tests/integration/__tests__/piped-output.js +++ b/tests/integration/__tests__/piped-output.js @@ -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", () => { @@ -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", () => { @@ -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", () => { @@ -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); + }); }); diff --git a/tests/integration/__tests__/plugin-options-string.js b/tests/integration/__tests__/plugin-options-string.js index 96e3008fa6c9..4d3a83dbd051 100644 --- a/tests/integration/__tests__/plugin-options-string.js +++ b/tests/integration/__tests__/plugin-options-string.js @@ -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; diff --git a/tests/integration/__tests__/plugin-options.js b/tests/integration/__tests__/plugin-options.js index 209bed0ac2fc..39aa86b60541 100644 --- a/tests/integration/__tests__/plugin-options.js +++ b/tests/integration/__tests__/plugin-options.js @@ -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(); }); diff --git a/tests/integration/runPrettier.js b/tests/integration/runPrettier.js index 80e032c355a9..b78b57aa5c0b 100644 --- a/tests/integration/runPrettier.js +++ b/tests/integration/runPrettier.js @@ -127,15 +127,45 @@ async function run(dir, args, options) { } } +let hasRunningCLi = false; function runPrettier(dir, args = [], options = {}) { - let result; + let promise; + const getters = { + get status() { + return runCli().then(({ status }) => status); + }, + get stdout() { + return runCli().then(({ stdout }) => stdout); + }, + get stderr() { + return runCli().then(({ stderr }) => stderr); + }, + get write() { + return runCli().then(({ write }) => write); + }, + test: testResult, + }; + + return getters; + + function runCli() { + if (hasRunningCLi) { + throw new Error("Please wait for previous CLI to exit."); + } - const testResult = (testOptions) => { + if (!promise) { + promise = run(dir, args, options); + promise.then(() => { + hasRunningCLi = false; + }); + } + return promise; + } + + function testResult(testOptions) { for (const name of ["status", "stdout", "stderr", "write"]) { test(`(${name})`, async () => { - if (!result) { - result = await run(dir, args, options); - } + const result = await runCli(); const value = // \r is trimmed from jest snapshots by default; // manually replacing this character with /*CR*/ to test its true presence @@ -156,9 +186,9 @@ function runPrettier(dir, args = [], options = {}) { } }); } - }; - return { test: testResult }; + return getters; + } } function normalizeDir(dir) { From d2bcb3230a0b39844f599cf2814d953f4f5ff2ab Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Apr 2021 19:42:12 +0800 Subject: [PATCH 04/10] Comment out failed test --- tests/integration/__tests__/patterns-dirs.js | 63 ++++++++++---------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index 9d500f746a98..4888f83f9ffb 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -87,37 +87,38 @@ describe("plugins `*`", () => { }); }); -if (path.sep === "/") { - // Don't use snapshots in these tests as they're conditionally executed on non-Windows only. - - const base = path.resolve(__dirname, "../cli/patterns-dirs"); - - // 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"); - - describe("Backslashes in names", () => { - afterAll(() => { - fs.unlinkSync(path.resolve(base, "test-a\\", "test.js")); - fs.rmdirSync(path.resolve(base, "test-a\\")); - fs.unlinkSync(path.resolve(base, "test-b\\?", "test.js")); - fs.rmdirSync(path.resolve(base, "test-b\\?")); - }); - - testPatterns("", ["test-a\\/test.js"], { stdout: "test-a\\/test.js\n" }); - testPatterns("", ["test-a\\"], { stdout: "test-a\\/test.js\n" }); - testPatterns("", ["test-a*/*"], { stdout: "test-a\\/test.js\n" }); - - testPatterns("", ["test-b\\?/test.js"], { stdout: "test-b\\?/test.js\n" }); - testPatterns("", ["test-b\\?"], { stdout: "test-b\\?/test.js\n" }); - testPatterns("", ["test-b*/*"], { stdout: "test-b\\?/test.js\n" }); - }); -} +// TODO: fix this test +// if (path.sep === "/") { +// // Don't use snapshots in these tests as they're conditionally executed on non-Windows only. + +// const base = path.resolve(__dirname, "../cli/patterns-dirs"); + +// // 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"); + +// describe("Backslashes in names", () => { +// afterAll(() => { +// fs.unlinkSync(path.resolve(base, "test-a\\", "test.js")); +// fs.rmdirSync(path.resolve(base, "test-a\\")); +// fs.unlinkSync(path.resolve(base, "test-b\\?", "test.js")); +// fs.rmdirSync(path.resolve(base, "test-b\\?")); +// }); + +// testPatterns("", ["test-a\\/test.js"], { stdout: "test-a\\/test.js\n" }); +// testPatterns("", ["test-a\\"], { stdout: "test-a\\/test.js\n" }); +// testPatterns("", ["test-a*/*"], { stdout: "test-a\\/test.js\n" }); + +// testPatterns("", ["test-b\\?/test.js"], { stdout: "test-b\\?/test.js\n" }); +// testPatterns("", ["test-b\\?"], { stdout: "test-b\\?/test.js\n" }); +// testPatterns("", ["test-b*/*"], { stdout: "test-b\\?/test.js\n" }); +// }); +// } function testPatterns(namePrefix, cliArgs, expected = {}) { const testName = From 752ad6f89e9c972bfd3facda8ab7b72ff82414cc Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 30 Apr 2021 19:45:55 +0800 Subject: [PATCH 05/10] Comment out failed test --- tests/integration/__tests__/patterns-dirs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index 4888f83f9ffb..39f7f986fbdd 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -1,7 +1,7 @@ "use strict"; const path = require("path"); -const fs = require("fs"); +// const fs = require("fs"); const runPrettier = require("../runPrettier"); const { projectRoot } = require("../env"); From a31a0aec0d68ba6463799ae599d0b34f33a05c03 Mon Sep 17 00:00:00 2001 From: fisker Date: Sat, 1 May 2021 11:38:37 +0800 Subject: [PATCH 06/10] Fix test Co-authored-by: Connor Peet --- tests/integration/__tests__/patterns-dirs.js | 66 ++++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index 39f7f986fbdd..e8fbd5d046e0 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -1,7 +1,7 @@ "use strict"; const path = require("path"); -// const fs = require("fs"); +const fs = require("fs"); const runPrettier = require("../runPrettier"); const { projectRoot } = require("../env"); @@ -87,38 +87,38 @@ describe("plugins `*`", () => { }); }); -// TODO: fix this test -// if (path.sep === "/") { -// // Don't use snapshots in these tests as they're conditionally executed on non-Windows only. - -// const base = path.resolve(__dirname, "../cli/patterns-dirs"); - -// // 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"); - -// describe("Backslashes in names", () => { -// afterAll(() => { -// fs.unlinkSync(path.resolve(base, "test-a\\", "test.js")); -// fs.rmdirSync(path.resolve(base, "test-a\\")); -// fs.unlinkSync(path.resolve(base, "test-b\\?", "test.js")); -// fs.rmdirSync(path.resolve(base, "test-b\\?")); -// }); - -// testPatterns("", ["test-a\\/test.js"], { stdout: "test-a\\/test.js\n" }); -// testPatterns("", ["test-a\\"], { stdout: "test-a\\/test.js\n" }); -// testPatterns("", ["test-a*/*"], { stdout: "test-a\\/test.js\n" }); - -// testPatterns("", ["test-b\\?/test.js"], { stdout: "test-b\\?/test.js\n" }); -// testPatterns("", ["test-b\\?"], { stdout: "test-b\\?/test.js\n" }); -// testPatterns("", ["test-b*/*"], { stdout: "test-b\\?/test.js\n" }); -// }); -// } +TODO: fix this test +if (path.sep === "/") { + // Don't use snapshots in these tests as they're conditionally executed on non-Windows only. + + const base = path.resolve(__dirname, "../cli/patterns-dirs"); + + describe("Backslashes in names", () => { + // We can't commit these dirs without causing problems on Windows. + + 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"); + }); + + afterAll(() => { + fs.unlinkSync(path.resolve(base, "test-a\\", "test.js")); + fs.rmdirSync(path.resolve(base, "test-a\\")); + fs.unlinkSync(path.resolve(base, "test-b\\?", "test.js")); + fs.rmdirSync(path.resolve(base, "test-b\\?")); + }); + + testPatterns("", ["test-a\\/test.js"], { stdout: "test-a\\/test.js\n" }); + testPatterns("", ["test-a\\"], { stdout: "test-a\\/test.js\n" }); + testPatterns("", ["test-a*/*"], { stdout: "test-a\\/test.js\n" }); + + testPatterns("", ["test-b\\?/test.js"], { stdout: "test-b\\?/test.js\n" }); + testPatterns("", ["test-b\\?"], { stdout: "test-b\\?/test.js\n" }); + testPatterns("", ["test-b*/*"], { stdout: "test-b\\?/test.js\n" }); + }); +} function testPatterns(namePrefix, cliArgs, expected = {}) { const testName = From a6bda9e425e14934d01d522e5b980d9911039553 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 1 May 2021 12:06:53 +0800 Subject: [PATCH 07/10] Update patterns-dirs.js --- tests/integration/__tests__/patterns-dirs.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/integration/__tests__/patterns-dirs.js b/tests/integration/__tests__/patterns-dirs.js index e8fbd5d046e0..29806ee081bc 100644 --- a/tests/integration/__tests__/patterns-dirs.js +++ b/tests/integration/__tests__/patterns-dirs.js @@ -87,7 +87,6 @@ describe("plugins `*`", () => { }); }); -TODO: fix this test if (path.sep === "/") { // Don't use snapshots in these tests as they're conditionally executed on non-Windows only. From 6e257a81a5db9b309d1dfbb839677caa639a52db Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 1 May 2021 15:13:01 +0800 Subject: [PATCH 08/10] Fix `hasRunningCli` flag --- tests/integration/runPrettier.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/integration/runPrettier.js b/tests/integration/runPrettier.js index b78b57aa5c0b..0420e26dcfec 100644 --- a/tests/integration/runPrettier.js +++ b/tests/integration/runPrettier.js @@ -127,7 +127,7 @@ async function run(dir, args, options) { } } -let hasRunningCLi = false; +let hasRunningCli = false; function runPrettier(dir, args = [], options = {}) { let promise; const getters = { @@ -149,14 +149,15 @@ function runPrettier(dir, args = [], options = {}) { return getters; function runCli() { - if (hasRunningCLi) { + if (hasRunningCli) { throw new Error("Please wait for previous CLI to exit."); } if (!promise) { + hasRunningCli = true; promise = run(dir, args, options); promise.then(() => { - hasRunningCLi = false; + hasRunningCli = false; }); } return promise; From b6c56258da6b979f49fd52b6a26592fda9e82bb5 Mon Sep 17 00:00:00 2001 From: Georgii Dolzhykov Date: Sat, 8 May 2021 21:24:10 +0300 Subject: [PATCH 09/10] Fix grammar --- tests/integration/__tests__/check.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration/__tests__/check.js b/tests/integration/__tests__/check.js index b2d35336fc01..4a1e3eebd387 100644 --- a/tests/integration/__tests__/check.js +++ b/tests/integration/__tests__/check.js @@ -44,7 +44,7 @@ describe("--checks works in CI just as in a non-TTY mode", () => { status: 1, }); - test("Should has same stdout", async () => { + test("Should have same stdout", async () => { expect(await result0.stdout).toEqual(await result1.stdout); }); }); From e50d5270103f4e540c360521bc028ef79a4da29e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 9 May 2021 11:03:21 +0800 Subject: [PATCH 10/10] Update tests/integration/runPrettier.js Co-authored-by: Georgii Dolzhykov --- tests/integration/runPrettier.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/integration/runPrettier.js b/tests/integration/runPrettier.js index 0420e26dcfec..d8edb2773e12 100644 --- a/tests/integration/runPrettier.js +++ b/tests/integration/runPrettier.js @@ -155,8 +155,7 @@ function runPrettier(dir, args = [], options = {}) { if (!promise) { hasRunningCli = true; - promise = run(dir, args, options); - promise.then(() => { + promise = run(dir, args, options).finally(() => { hasRunningCli = false; }); }