From 6a1d1d093b66331db74a1a32d40c1cbee4abe95e Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 21 Jul 2022 15:21:59 +0800 Subject: [PATCH] Rewrite config resolution tests (#13146) --- .../__snapshots__/config-resolution.js.snap | 163 --------------- .../with-config-precedence.js.snap | 187 ------------------ .../__tests__/config-resolution.js | 33 ++-- .../__tests__/with-config-precedence.js | 103 ++++++---- .../{ => invalid-config}/.prettierrc | 0 .../config-precedence/invalid-config/foo.js | 3 + .../config-precedence/overrides/.prettierrc | 8 + .../valid-config/.prettierrc | 1 + .../cli/config-precedence/valid-config/foo.js | 3 + .../cli/config/external-overrides/.prettierrc | 1 + .../config/external-overrides/real-config.cjs | 9 + 11 files changed, 100 insertions(+), 411 deletions(-) rename tests/integration/cli/config-precedence/{ => invalid-config}/.prettierrc (100%) create mode 100644 tests/integration/cli/config-precedence/invalid-config/foo.js create mode 100644 tests/integration/cli/config-precedence/overrides/.prettierrc create mode 100644 tests/integration/cli/config-precedence/valid-config/.prettierrc create mode 100644 tests/integration/cli/config-precedence/valid-config/foo.js create mode 100644 tests/integration/cli/config/external-overrides/.prettierrc create mode 100644 tests/integration/cli/config/external-overrides/real-config.cjs diff --git a/tests/integration/__tests__/__snapshots__/config-resolution.js.snap b/tests/integration/__tests__/__snapshots__/config-resolution.js.snap index 80d29085a..625006c65 100644 --- a/tests/integration/__tests__/__snapshots__/config-resolution.js.snap +++ b/tests/integration/__tests__/__snapshots__/config-resolution.js.snap @@ -1,93 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`CLI overrides take precedence (stderr) 1`] = `""`; - -exports[`CLI overrides take precedence (stdout) 1`] = ` -"function f() { - console.log( - "should have tab width 8" - ) -} -function f() { - console.log( - "should have space width 2" - ) -} -function f() { - console.log( - "should have space width 8" - ) -} -function f() { - console.log( - "should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present" - ) -} -console.log( - "should have no semi" -) -console.log( - "jest/__best-tests__/.file.js should have semi" -); -console.log( - "jest/__best-tests__/file.js should have semi" -); -console.log( - "jest/Component.js should not have semi" -) -console.log( - "jest/Component.test.js should have semi" -); -function js() { - console.log( - "js/file.js should have tab width 8 (1 if CLI)" - ); -} -function noConfigJs() { - console.log( - "no-config/file.js should have no semicolons" - ) -} -function packageJs() { - console.log( - "package/file.js should have tab width 3" - ); -} -function rcJson() { - console.log.apply( - null, - [ - 'rc-json/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -function rcToml() { - console.log.apply( - null, - [ - 'rc-toml/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -function rcYaml() { - console.log.apply( - null, - [ - 'rc-yaml/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -" -`; - -exports[`CLI overrides take precedence (write) 1`] = `[]`; - exports[`accepts configuration from --config (stderr) 1`] = `""`; exports[`accepts configuration from --config (stdout) 1`] = ` @@ -115,81 +27,6 @@ exports[`resolves configuration file with --find-config-path file (stdout) 1`] = exports[`resolves configuration file with --find-config-path file (write) 1`] = `[]`; -exports[`resolves configuration from external files (stderr) 1`] = `""`; - -exports[`resolves configuration from external files (stdout) 1`] = ` -"function f() { - console.log("should have tab width 8") -} -function f() { - console.log("should have space width 2") -} -function f() { - console.log("should have space width 8") -} -function f() { - console.log( - "should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present" - ) -} -console.log("should have no semi") -console.log("jest/__best-tests__/.file.js should have semi"); -console.log("jest/__best-tests__/file.js should have semi"); -console.log("jest/Component.js should not have semi") -console.log("jest/Component.test.js should have semi"); -function js() { - console.log("js/file.js should have tab width 8 (1 if CLI)"); -} -function noConfigJs() { - console.log("no-config/file.js should have no semicolons") -} -function packageJs() { - console.log("package/file.js should have tab width 3"); -} -function rcJson() { - console.log.apply(null, [ - 'rc-json/file.js', - 'should have trailing comma', - 'and single quotes', - ]); -} -function rcToml() { - console.log.apply(null, [ - 'rc-toml/file.js', - 'should have trailing comma', - 'and single quotes', - ]); -} -function rcYaml() { - console.log.apply(null, [ - 'rc-yaml/file.js', - 'should have trailing comma', - 'and single quotes', - ]); -} -" -`; - -exports[`resolves configuration from external files (write) 1`] = `[]`; - -exports[`resolves configuration from external files and overrides by extname (stderr) 1`] = `""`; - -exports[`resolves configuration from external files and overrides by extname (stdout) 1`] = ` -"function noConfigTs() { - console.log( - "no-config/file.ts should have semicolons because it has a .ts extension" - ); -} -function packageTs() { - console.log( - "package/file.ts should have tab width 5 because it has .ts extension" - ); -} -" -`; - -exports[`resolves configuration from external files and overrides by extname (write) 1`] = `[]`; - exports[`resolves external configuration from package.json (stderr) 1`] = `""`; exports[`resolves external configuration from package.json (stdout) 1`] = ` diff --git a/tests/integration/__tests__/__snapshots__/with-config-precedence.js.snap b/tests/integration/__tests__/__snapshots__/with-config-precedence.js.snap index 92ad2127f..cf279b986 100644 --- a/tests/integration/__tests__/__snapshots__/with-config-precedence.js.snap +++ b/tests/integration/__tests__/__snapshots__/with-config-precedence.js.snap @@ -56,193 +56,6 @@ exports[`CLI overrides gets ignored when config exists with --config-precedence exports[`CLI overrides gets ignored when config exists with --config-precedence prefer-file (write) 1`] = `[]`; -exports[`CLI overrides take lower precedence with --config-precedence file-override (stderr) 1`] = `""`; - -exports[`CLI overrides take lower precedence with --config-precedence file-override (stdout) 1`] = ` -"function js() { - console.log("js/file.js should have tab width 8 (1 if CLI)"); -} -" -`; - -exports[`CLI overrides take lower precedence with --config-precedence file-override (write) 1`] = `[]`; - -exports[`CLI overrides take precedence with --config-precedence cli-override (stderr) 1`] = `""`; - -exports[`CLI overrides take precedence with --config-precedence cli-override (stdout) 1`] = ` -"function f() { - console.log( - "should have tab width 8" - ) -} -function f() { - console.log( - "should have space width 2" - ) -} -function f() { - console.log( - "should have space width 8" - ) -} -function f() { - console.log( - "should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present" - ) -} -console.log( - "should have no semi" -) -console.log( - "jest/__best-tests__/.file.js should have semi" -); -console.log( - "jest/__best-tests__/file.js should have semi" -); -console.log( - "jest/Component.js should not have semi" -) -console.log( - "jest/Component.test.js should have semi" -); -function js() { - console.log( - "js/file.js should have tab width 8 (1 if CLI)" - ); -} -function noConfigJs() { - console.log( - "no-config/file.js should have no semicolons" - ) -} -function packageJs() { - console.log( - "package/file.js should have tab width 3" - ); -} -function rcJson() { - console.log.apply( - null, - [ - 'rc-json/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -function rcToml() { - console.log.apply( - null, - [ - 'rc-toml/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -function rcYaml() { - console.log.apply( - null, - [ - 'rc-yaml/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -" -`; - -exports[`CLI overrides take precedence with --config-precedence cli-override (write) 1`] = `[]`; - -exports[`CLI overrides take precedence without --config-precedence (stderr) 1`] = `""`; - -exports[`CLI overrides take precedence without --config-precedence (stdout) 1`] = ` -"function f() { - console.log( - "should have tab width 8" - ) -} -function f() { - console.log( - "should have space width 2" - ) -} -function f() { - console.log( - "should have space width 8" - ) -} -function f() { - console.log( - "should have space width 2 despite ../.editorconfig specifying 8, because ./.hg is present" - ) -} -console.log( - "should have no semi" -) -console.log( - "jest/__best-tests__/.file.js should have semi" -); -console.log( - "jest/__best-tests__/file.js should have semi" -); -console.log( - "jest/Component.js should not have semi" -) -console.log( - "jest/Component.test.js should have semi" -); -function js() { - console.log( - "js/file.js should have tab width 8 (1 if CLI)" - ); -} -function noConfigJs() { - console.log( - "no-config/file.js should have no semicolons" - ) -} -function packageJs() { - console.log( - "package/file.js should have tab width 3" - ); -} -function rcJson() { - console.log.apply( - null, - [ - 'rc-json/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -function rcToml() { - console.log.apply( - null, - [ - 'rc-toml/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -function rcYaml() { - console.log.apply( - null, - [ - 'rc-yaml/file.js', - 'should have trailing comma', - 'and single quotes', - ], - ); -} -" -`; - -exports[`CLI overrides take precedence without --config-precedence (write) 1`] = `[]`; - exports[`CLI validate options with --config-precedence cli-override (stderr) 1`] = ` "[error] Invalid printWidth value. Expected an integer, but received 0.5. " diff --git a/tests/integration/__tests__/config-resolution.js b/tests/integration/__tests__/config-resolution.js index 5fd7ba99a..e39cb0641 100644 --- a/tests/integration/__tests__/config-resolution.js +++ b/tests/integration/__tests__/config-resolution.js @@ -8,16 +8,17 @@ const { __dirname } = createEsmUtils(import.meta); expect.addSnapshotSerializer(jestPathSerializer); -describe("resolves configuration from external files", () => { - runPrettier("cli/config/", ["--end-of-line", "lf", "**/*.js"]).test({ - status: 0, - }); -}); - -describe("resolves configuration from external files and overrides by extname", () => { - runPrettier("cli/config/", ["--end-of-line", "lf", "**/*.ts"]).test({ - status: 0, - }); +test("resolves configuration from external files and overrides by extname", async () => { + await expect( + prettier.resolveConfig( + path.join(__dirname, "../cli/config/external-overrides/file.js") + ) + ).resolves.toEqual({ tabWidth: 3, semi: false }); + await expect( + prettier.resolveConfig( + path.join(__dirname, "../cli/config/external-overrides/file.ts") + ) + ).resolves.toEqual({ tabWidth: 3, semi: true }); }); describe("accepts configuration from --config", () => { @@ -68,18 +69,6 @@ describe("prints error message when no file found with --find-config-path", () = }); }); -describe("CLI overrides take precedence", () => { - runPrettier("cli/config/", [ - "--end-of-line", - "lf", - "--print-width", - "1", - "**/*.js", - ]).test({ - status: 0, - }); -}); - test("API resolveConfig with no args", async () => { await expect(prettier.resolveConfig()).resolves.toEqual({}); }); diff --git a/tests/integration/__tests__/with-config-precedence.js b/tests/integration/__tests__/with-config-precedence.js index b1e112a39..f4a307a6d 100644 --- a/tests/integration/__tests__/with-config-precedence.js +++ b/tests/integration/__tests__/with-config-precedence.js @@ -1,41 +1,66 @@ -describe("CLI overrides take precedence without --config-precedence", () => { - runPrettier("cli/config/", [ - "--end-of-line", - "lf", - "--print-width", - "1", - "**/*.js", - ]).test({ - status: 0, - }); +import { outdent } from "outdent"; + +const TAB_WIDTH_3_OUTPUT = + outdent` + function foo() { + ${" ".repeat(3)}return bar; + } + ` + "\n"; +const TAB_WIDTH_5_OUTPUT = + outdent` + function foo() { + ${" ".repeat(5)}return bar; + } + ` + "\n"; + +test("CLI overrides take precedence --config-precedence", async () => { + const withoutFlag = await runPrettier("cli/config-precedence/valid-config", [ + "foo.js", + ]).stdout; + expect(withoutFlag).toBe(TAB_WIDTH_3_OUTPUT); + + const withFlag = await runPrettier("cli/config-precedence/valid-config", [ + "--tab-width", + "5", + "foo.js", + ]).stdout; + expect(withFlag).toBe(TAB_WIDTH_5_OUTPUT); }); -describe("CLI overrides take precedence with --config-precedence cli-override", () => { - runPrettier("cli/config/", [ - "--end-of-line", - "lf", - "--print-width", - "1", +test("CLI overrides take precedence with --config-precedence cli-override", async () => { + const withoutFlag = await runPrettier("cli/config-precedence/valid-config", [ "--config-precedence", "cli-override", - "**/*.js", - ]).test({ - status: 0, - }); + "foo.js", + ]).stdout; + expect(withoutFlag).toBe(TAB_WIDTH_3_OUTPUT); + + const withFlag = await runPrettier("cli/config-precedence/valid-config", [ + "--config-precedence", + "cli-override", + "--tab-width", + "5", + "foo.js", + ]).stdout; + expect(withFlag).toBe(TAB_WIDTH_5_OUTPUT); }); -describe("CLI overrides take lower precedence with --config-precedence file-override", () => { - runPrettier("cli/config/js/", [ - "--end-of-line", - "crlf", - "--tab-width", - "1", +test("CLI overrides take lower precedence with --config-precedence file-override", async () => { + const withoutFlag = await runPrettier("cli/config-precedence/valid-config", [ "--config-precedence", "file-override", - "**/*.js", - ]).test({ - status: 0, - }); + "foo.js", + ]).stdout; + expect(withoutFlag).toBe(TAB_WIDTH_3_OUTPUT); + + const withFlag = await runPrettier("cli/config-precedence/valid-config", [ + "--config-precedence", + "file-override", + "--tab-width", + "5", + "foo.js", + ]).stdout; + expect(withFlag).toBe(TAB_WIDTH_3_OUTPUT); }); describe("CLI overrides are still applied when no config is found with --config-precedence file-override", () => { @@ -46,7 +71,7 @@ describe("CLI overrides are still applied when no config is found with --config- "6", "--config-precedence", "file-override", - "**/*.js", + "file.js", "--no-editorconfig", ]).test({ status: 0, @@ -61,7 +86,7 @@ describe("CLI overrides gets ignored when config exists with --config-precedence "1", "--config-precedence", "prefer-file", - "**/*.js", + "file.js", ]).test({ status: 0, }); @@ -78,14 +103,14 @@ describe("CLI overrides gets applied when no config exists with --config-precede "--no-config", "--config-precedence", "prefer-file", - "**/*.js", + "file.js", ]).test({ status: 0, }); }); describe("CLI validate options with --config-precedence cli-override", () => { - runPrettier("cli/config-precedence", [ + runPrettier("cli/config-precedence/invalid-config", [ "--config-precedence", "cli-override", ]).test({ @@ -94,7 +119,7 @@ describe("CLI validate options with --config-precedence cli-override", () => { }); describe("CLI validate options with --config-precedence file-override", () => { - runPrettier("cli/config-precedence", [ + runPrettier("cli/config-precedence/invalid-config", [ "--config-precedence", "file-override", ]).test({ @@ -103,7 +128,7 @@ describe("CLI validate options with --config-precedence file-override", () => { }); describe("CLI validate options with --config-precedence prefer-file", () => { - runPrettier("cli/config-precedence", [ + runPrettier("cli/config-precedence/invalid-config", [ "--config-precedence", "prefer-file", ]).test({ @@ -113,7 +138,7 @@ describe("CLI validate options with --config-precedence prefer-file", () => { describe("CLI --stdin-filepath works with --config-precedence prefer-file", () => { runPrettier( - "cli/config/", + "cli/config-precedence/overrides", ["--stdin-filepath=abc.ts", "--no-semi", "--config-precedence=prefer-file"], { input: "let x: keyof Y = foo()" } // typescript ).test({ @@ -124,7 +149,7 @@ describe("CLI --stdin-filepath works with --config-precedence prefer-file", () = describe("CLI --stdin-filepath works with --config-precedence file-override", () => { runPrettier( - "cli/config/", + "cli/config-precedence/overrides", [ "--stdin-filepath=abc.ts", "--no-semi", @@ -139,7 +164,7 @@ describe("CLI --stdin-filepath works with --config-precedence file-override", () describe("CLI --stdin-filepath works with --config-precedence cli-override", () => { runPrettier( - "cli/config/", + "cli/config-precedence/overrides", [ "--stdin-filepath=abc.ts", "--no-semi", diff --git a/tests/integration/cli/config-precedence/.prettierrc b/tests/integration/cli/config-precedence/invalid-config/.prettierrc similarity index 100% rename from tests/integration/cli/config-precedence/.prettierrc rename to tests/integration/cli/config-precedence/invalid-config/.prettierrc diff --git a/tests/integration/cli/config-precedence/invalid-config/foo.js b/tests/integration/cli/config-precedence/invalid-config/foo.js new file mode 100644 index 000000000..89d793a3d --- /dev/null +++ b/tests/integration/cli/config-precedence/invalid-config/foo.js @@ -0,0 +1,3 @@ +function foo () { +return bar; +} diff --git a/tests/integration/cli/config-precedence/overrides/.prettierrc b/tests/integration/cli/config-precedence/overrides/.prettierrc new file mode 100644 index 000000000..43de9b5e6 --- /dev/null +++ b/tests/integration/cli/config-precedence/overrides/.prettierrc @@ -0,0 +1,8 @@ +endOfLine: 'auto' +overrides: +- files: "*.js" + options: + semi: false +- files: "*.ts" + options: + semi: true diff --git a/tests/integration/cli/config-precedence/valid-config/.prettierrc b/tests/integration/cli/config-precedence/valid-config/.prettierrc new file mode 100644 index 000000000..b612e027c --- /dev/null +++ b/tests/integration/cli/config-precedence/valid-config/.prettierrc @@ -0,0 +1 @@ +{"tabWidth": 3} diff --git a/tests/integration/cli/config-precedence/valid-config/foo.js b/tests/integration/cli/config-precedence/valid-config/foo.js new file mode 100644 index 000000000..89d793a3d --- /dev/null +++ b/tests/integration/cli/config-precedence/valid-config/foo.js @@ -0,0 +1,3 @@ +function foo () { +return bar; +} diff --git a/tests/integration/cli/config/external-overrides/.prettierrc b/tests/integration/cli/config/external-overrides/.prettierrc new file mode 100644 index 000000000..ddd7a0726 --- /dev/null +++ b/tests/integration/cli/config/external-overrides/.prettierrc @@ -0,0 +1 @@ +"./real-config.cjs" diff --git a/tests/integration/cli/config/external-overrides/real-config.cjs b/tests/integration/cli/config/external-overrides/real-config.cjs new file mode 100644 index 000000000..7924b4278 --- /dev/null +++ b/tests/integration/cli/config/external-overrides/real-config.cjs @@ -0,0 +1,9 @@ +"use strict"; + +module.exports = { + tabWidth: 3, + overrides: [ + { files: "*.js", options: { semi: false } }, + { files: "*.ts", options: { semi: true } }, + ], +};