prettier/tests_integration/__tests__/config-resolution.js

229 lines
6.0 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/", ["**/*.js"]).test({
status: 0
});
});
describe("resolves configuration from external files and overrides by extname", () => {
runPrettier("cli/config/", ["**/*.ts"]).test({
status: 0
});
});
describe("accepts configuration from --config", () => {
runPrettier("cli/config/", ["--config", ".prettierrc", "./js/file.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("prints nothing when no file found with --find-config-path", () => {
runPrettier("cli/config/", ["--find-config-path", ".."]).test({
stdout: "",
status: 1
});
});
describe("CLI overrides take precedence", () => {
runPrettier("cli/config/", ["--print-width", "1", "**/*.js"]).test({
status: 0
});
});
test("API resolveConfig with no args", () => {
return prettier.resolveConfig().then(result => {
expect(result).toBeNull();
});
});
test("API resolveConfig.sync with no args", () => {
expect(prettier.resolveConfig.sync()).toBeNull();
});
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 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();
});
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
});
});