prettier/tests_integration/__tests__/config-resolution.js

288 lines
7.7 KiB
JavaScript
Raw Normal View History

"use strict";
const path = require("path");
const runPrettier = require("../runPrettier");
const prettier = require("prettier/local");
2017-09-28 12:30:51 +03:00
expect.addSnapshotSerializer(require("../path-serializer"));
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
});
});
describe("accepts configuration from --config", () => {
runPrettier("cli/config/", ["--config", ".prettierrc", "./js/file.js"]).test({
status: 0
});
});
describe("resolves external configuration from package.json", () => {
runPrettier("cli/config/", ["external-config/index.js"]).test({
status: 0
});
});
describe("resolves configuration file with --find-config-path file", () => {
runPrettier("cli/config/", ["--find-config-path", "no-config/file.js"]).test({
status: 0
});
});
describe("resolves json configuration file with --find-config-path file", () => {
runPrettier("cli/config/", ["--find-config-path", "rc-json/file.js"]).test({
status: 0
});
});
describe("resolves yaml configuration file with --find-config-path file", () => {
runPrettier("cli/config/", ["--find-config-path", "rc-yaml/file.js"]).test({
status: 0
});
});
describe("resolves toml configuration file with --find-config-path file", () => {
runPrettier("cli/config/", ["--find-config-path", "rc-toml/file.js"]).test({
status: 0
});
});
describe("prints nothing when no file found with --find-config-path", () => {
runPrettier("cli/config/", [
"--end-of-line",
"lf",
"--find-config-path",
".."
]).test({
stdout: "",
status: 1
});
});
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", () => {
return prettier.resolveConfig().then(result => {
expect(result).toEqual({});
});
});
test("API resolveConfig.sync with no args", () => {
expect(prettier.resolveConfig.sync()).toEqual({});
});
test("API resolveConfig with file arg", () => {
const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js"));
return prettier.resolveConfig(file).then(result => {
expect(result).toMatchObject({
tabWidth: 8
});
});
});
test("API resolveConfig.sync with file arg", () => {
const file = path.resolve(path.join(__dirname, "../cli/config/js/file.js"));
expect(prettier.resolveConfig.sync(file)).toMatchObject({
tabWidth: 8
});
});
test("API resolveConfig with file arg and extension override", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/no-config/file.ts")
);
return prettier.resolveConfig(file).then(result => {
expect(result).toMatchObject({
semi: true
});
});
});
test("API resolveConfig.sync with file arg and extension override", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/no-config/file.ts")
);
expect(prettier.resolveConfig.sync(file)).toMatchObject({
semi: true
});
});
Re-add EditorConfig support (undo #3213) (#3255) * Revert "Revert "Respect EditorConfig settings" (#3213)" This reverts commit d2241fc0d52d807701d9a5ac580bc01010e7a93b. * Comment out EditorConfig docs See https://github.com/prettier/prettier/pull/3213#issuecomment-343009769 * editorconfig: Support `indent_size = 0` See https://github.com/prettier/prettier/pull/2760#discussion_r137447715 and https://github.com/josephfrazier/editorconfig-to-prettier/commit/c38b84c42a2e022067c104c494298a8c9533e7a7 * Revert "Comment out EditorConfig docs" This reverts commit ddfa529c55cac4853a1e76e00c8b5e3ef158c01f. * Mark EditorConfig functionality as v1.9.0+ See https://github.com/prettier/prettier/pull/3255#discussion_r150432508 * editorconfig: Upgrade editorconfig-to-prettier to 0.0.4 * editorconfig: Only enable for CLI, by default https://github.com/prettier/prettier/pull/3255#issuecomment-348420546 * editorconfig: Add tests confirming that editorconfig is ignored by default in the API https://github.com/prettier/prettier/pull/3255#issuecomment-348420546 * editorconfig: Add/fix CLI option parsing * editorconfig: Move docs from configuration.md to options.md * editorconfig: Add `oppositeDescription` to show docs for `--no-editorconfig` Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154542792 * editorconfig: Update test snapshots * editorconfig: Remove unnecessary options parsing code Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154544560 * editorconfig: Move docs from options.md to api.md and cli.md Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154545979 * resolveConfig: return null if both .prettierrc and .editorconfig are missing Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154574613 * Don't add now-failing tests The way these tests work, both `tests_integration/cli/config/.prettierrc` and `.prettierrc` apply to `tests_integration/cli/config/editorconfig/file.shouldnotexist`, so the test wouldn't work even on master. Here's a way to confirm that: ```js const path = require('path') const assert = require('assert') const prettier = require('./') const file = './tests_integration/cli/config/editorconfig/file.shouldnotexist' console.log(prettier.resolveConfig.sync(file)) assert(prettier.resolveConfig.sync(file) === null) ```
2017-12-05 01:28:27 +03:00
test("API resolveConfig with file arg and .editorconfig", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/file.js")
);
return prettier.resolveConfig(file, { editorconfig: true }).then(result => {
expect(result).toMatchObject({
useTabs: true,
tabWidth: 8,
printWidth: 100
});
});
});
test("API resolveConfig.sync with file arg and .editorconfig", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/file.js")
);
expect(prettier.resolveConfig.sync(file)).toMatchObject({
semi: false
});
expect(
prettier.resolveConfig.sync(file, { editorconfig: true })
).toMatchObject({
useTabs: true,
tabWidth: 8,
printWidth: 100
});
});
test("API resolveConfig.sync with file arg and .editorconfig (key = unset)", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/tab_width=unset.js")
);
expect(
prettier.resolveConfig.sync(file, { editorconfig: true })
).not.toMatchObject({ tabWidth: "unset" });
});
Re-add EditorConfig support (undo #3213) (#3255) * Revert "Revert "Respect EditorConfig settings" (#3213)" This reverts commit d2241fc0d52d807701d9a5ac580bc01010e7a93b. * Comment out EditorConfig docs See https://github.com/prettier/prettier/pull/3213#issuecomment-343009769 * editorconfig: Support `indent_size = 0` See https://github.com/prettier/prettier/pull/2760#discussion_r137447715 and https://github.com/josephfrazier/editorconfig-to-prettier/commit/c38b84c42a2e022067c104c494298a8c9533e7a7 * Revert "Comment out EditorConfig docs" This reverts commit ddfa529c55cac4853a1e76e00c8b5e3ef158c01f. * Mark EditorConfig functionality as v1.9.0+ See https://github.com/prettier/prettier/pull/3255#discussion_r150432508 * editorconfig: Upgrade editorconfig-to-prettier to 0.0.4 * editorconfig: Only enable for CLI, by default https://github.com/prettier/prettier/pull/3255#issuecomment-348420546 * editorconfig: Add tests confirming that editorconfig is ignored by default in the API https://github.com/prettier/prettier/pull/3255#issuecomment-348420546 * editorconfig: Add/fix CLI option parsing * editorconfig: Move docs from configuration.md to options.md * editorconfig: Add `oppositeDescription` to show docs for `--no-editorconfig` Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154542792 * editorconfig: Update test snapshots * editorconfig: Remove unnecessary options parsing code Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154544560 * editorconfig: Move docs from options.md to api.md and cli.md Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154545979 * resolveConfig: return null if both .prettierrc and .editorconfig are missing Addresses https://github.com/prettier/prettier/pull/3255#discussion_r154574613 * Don't add now-failing tests The way these tests work, both `tests_integration/cli/config/.prettierrc` and `.prettierrc` apply to `tests_integration/cli/config/editorconfig/file.shouldnotexist`, so the test wouldn't work even on master. Here's a way to confirm that: ```js const path = require('path') const assert = require('assert') const prettier = require('./') const file = './tests_integration/cli/config/editorconfig/file.shouldnotexist' console.log(prettier.resolveConfig.sync(file)) assert(prettier.resolveConfig.sync(file) === null) ```
2017-12-05 01:28:27 +03:00
test("API resolveConfig with nested file arg and .editorconfig", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/lib/file.js")
);
return prettier.resolveConfig(file, { editorconfig: true }).then(result => {
expect(result).toMatchObject({
useTabs: false,
tabWidth: 2,
printWidth: 100
});
});
});
test("API resolveConfig.sync with nested file arg and .editorconfig", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/lib/file.js")
);
expect(prettier.resolveConfig.sync(file)).toMatchObject({
semi: false
});
expect(
prettier.resolveConfig.sync(file, { editorconfig: true })
).toMatchObject({
useTabs: false,
tabWidth: 2,
printWidth: 100
});
});
test("API resolveConfig with nested file arg and .editorconfig and indent_size = tab", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/lib/indent_size=tab.js")
);
return prettier.resolveConfig(file, { editorconfig: true }).then(result => {
expect(result).toMatchObject({
useTabs: false,
tabWidth: 8,
printWidth: 100
});
});
});
test("API resolveConfig.sync with nested file arg and .editorconfig and indent_size = tab", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/editorconfig/lib/indent_size=tab.js")
);
expect(prettier.resolveConfig.sync(file)).toMatchObject({
semi: false
});
expect(
prettier.resolveConfig.sync(file, { editorconfig: true })
).toMatchObject({
useTabs: false,
tabWidth: 8,
printWidth: 100
});
});
test("API clearConfigCache", () => {
expect(() => prettier.clearConfigCache()).not.toThrowError();
});
2019-06-07 15:45:56 +03:00
test("API resolveConfig overrides work with dotfiles", () => {
const folder = path.join(__dirname, "../cli/config/dot-overrides");
return expect(
prettier.resolveConfig(path.join(folder, ".foo.json"))
).resolves.toMatchObject({
tabWidth: 4
});
});
test("API resolveConfig.sync overrides work with absolute paths", () => {
// Absolute path
const file = path.join(__dirname, "../cli/config/filepath/subfolder/file.js");
expect(prettier.resolveConfig.sync(file)).toMatchObject({
tabWidth: 6
});
});
2017-10-18 15:27:04 +03:00
test("API resolveConfig removes $schema option", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/$schema/index.js")
);
return prettier.resolveConfig(file).then(result => {
expect(result).toEqual({
tabWidth: 42
});
});
});
test("API resolveConfig.sync removes $schema option", () => {
const file = path.resolve(
path.join(__dirname, "../cli/config/$schema/index.js")
);
expect(prettier.resolveConfig.sync(file)).toEqual({
tabWidth: 42
});
});
test("API resolveConfig resolves relative path values based on config filepath", () => {
const currentDir = path.join(__dirname, "../cli/config/resolve-relative");
const parentDir = path.resolve(currentDir, "..");
expect(prettier.resolveConfig.sync(`${currentDir}/index.js`)).toMatchObject({
plugins: [path.join(parentDir, "path-to-plugin")],
pluginSearchDirs: [path.join(parentDir, "path-to-plugin-search-dir")]
});
});
test("API resolveConfig de-references to an external module", () => {
const currentDir = path.join(__dirname, "../cli/config/external-config");
expect(prettier.resolveConfig.sync(`${currentDir}/index.js`)).toEqual({
printWidth: 77,
semi: false
});
});