Rewrite config resolution tests (#13146)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-07-21 15:21:59 +08:00
1 parent 6af3df61f8
commit 6a1d1d093b
11 files changed
+100 -411

No files matched your search

@@ -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`] = `
@@ -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.
"
@@ -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({});
});
@@ -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<typeof X>()" } // 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",
@@ -0,0 +1,3 @@
function foo () {
return bar;
}
@@ -0,0 +1,8 @@
endOfLine: 'auto'
overrides:
- files: "*.js"
options:
semi: false
- files: "*.ts"
options:
semi: true
@@ -0,0 +1 @@
{"tabWidth": 3}
@@ -0,0 +1,3 @@
function foo () {
return bar;
}
@@ -0,0 +1 @@
"./real-config.cjs"
@@ -0,0 +1,9 @@
"use strict";
module.exports = {
tabWidth: 3,
overrides: [
{ files: "*.js", options: { semi: false } },
{ files: "*.ts", options: { semi: true } },
],
};