2018-05-09 19:53:44 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const path = require("path");
|
2018-11-29 06:04:44 +03:00
|
|
|
const tempy = require("tempy");
|
|
|
|
const fs = require("fs");
|
2018-05-09 19:53:44 +03:00
|
|
|
|
|
|
|
const runPrettier = require("../runPrettier");
|
2018-05-27 14:36:12 +03:00
|
|
|
const prettier = require("prettier/local");
|
2018-05-09 19:53:44 +03:00
|
|
|
|
|
|
|
expect.addSnapshotSerializer(require("../path-serializer"));
|
|
|
|
|
|
|
|
describe("extracts file-info for a js file", () => {
|
|
|
|
runPrettier("cli/", ["--file-info", "something.js"]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info for a markdown file", () => {
|
|
|
|
runPrettier("cli/", ["--file-info", "README.md"]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info for a known markdown file with no extension", () => {
|
|
|
|
runPrettier("cli/", ["--file-info", "README"]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with ignored=true for a file in .prettierignore", () => {
|
|
|
|
runPrettier("cli/ignore-path/", ["--file-info", "regular-module.js"]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with ignored=true for a file in a hand-picked .prettierignore", () => {
|
|
|
|
runPrettier("cli/", [
|
|
|
|
"--file-info",
|
|
|
|
"regular-module.js",
|
|
|
|
"--ignore-path=ignore-path/.prettierignore"
|
|
|
|
]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info for a file in not_node_modules", () => {
|
|
|
|
runPrettier("cli/with-node-modules/", [
|
|
|
|
"--file-info",
|
|
|
|
"not_node_modules/file.js"
|
|
|
|
]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with with ignored=true for a file in node_modules", () => {
|
|
|
|
runPrettier("cli/with-node-modules/", [
|
|
|
|
"--file-info",
|
|
|
|
"node_modules/file.js"
|
|
|
|
]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with ignored=false for a file in node_modules when --with-node-modules provided", () => {
|
|
|
|
runPrettier("cli/with-node-modules/", [
|
|
|
|
"--file-info",
|
|
|
|
"node_modules/file.js",
|
|
|
|
"--with-node-modules"
|
|
|
|
]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with inferredParser=null for file.foo", () => {
|
|
|
|
runPrettier("cli/", ["--file-info", "file.foo"]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with inferredParser=foo when plugins are autoloaded", () => {
|
|
|
|
runPrettier("plugins/automatic/", ["--file-info", "file.foo"]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with inferredParser=foo when plugins are loaded with --plugin-search-dir", () => {
|
|
|
|
runPrettier("cli/", [
|
|
|
|
"--file-info",
|
|
|
|
"file.foo",
|
|
|
|
"--plugin-search-dir",
|
|
|
|
"../plugins/automatic"
|
|
|
|
]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info with inferredParser=foo when a plugin is hand-picked", () => {
|
|
|
|
runPrettier("cli/", [
|
|
|
|
"--file-info",
|
|
|
|
"file.foo",
|
|
|
|
"--plugin",
|
|
|
|
"../plugins/automatic/node_modules/@prettier/plugin-foo"
|
|
|
|
]).test({
|
|
|
|
status: 0
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo with no args", () => {
|
2018-05-19 03:43:54 +03:00
|
|
|
// TODO: change this to `rejects.toThrow()` when we upgrade to Jest >= 22
|
|
|
|
// https://github.com/facebook/jest/issues/3601
|
|
|
|
expect.assertions(1);
|
|
|
|
return prettier.getFileInfo().catch(err => {
|
|
|
|
expect(err).toBeDefined();
|
|
|
|
});
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo.sync with no args", () => {
|
|
|
|
expect(() => prettier.getFileInfo.sync()).toThrow();
|
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo with filepath only", () => {
|
|
|
|
expect(prettier.getFileInfo("README")).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: "markdown"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo.sync with filepath only", () => {
|
|
|
|
expect(prettier.getFileInfo.sync("README")).toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: "markdown"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo with ignorePath", () => {
|
|
|
|
const file = path.resolve(
|
|
|
|
path.join(__dirname, "../cli/ignore-path/regular-module.js")
|
|
|
|
);
|
|
|
|
const ignorePath = path.resolve(
|
|
|
|
path.join(__dirname, "../cli/ignore-path/.prettierignore")
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(prettier.getFileInfo(file)).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo(file, {
|
|
|
|
ignorePath
|
|
|
|
})
|
|
|
|
).resolves.toMatchObject({
|
|
|
|
ignored: true,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo.sync with ignorePath", () => {
|
|
|
|
const file = path.resolve(
|
|
|
|
path.join(__dirname, "../cli/ignore-path/regular-module.js")
|
|
|
|
);
|
|
|
|
const ignorePath = path.resolve(
|
|
|
|
path.join(__dirname, "../cli/ignore-path/.prettierignore")
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(prettier.getFileInfo.sync(file)).toMatchObject({
|
|
|
|
ignored: false,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync(file, {
|
|
|
|
ignorePath
|
|
|
|
})
|
|
|
|
).toMatchObject({
|
|
|
|
ignored: true,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-29 06:04:44 +03:00
|
|
|
describe("API getFileInfo.sync with ignorePath", () => {
|
|
|
|
let cwd;
|
|
|
|
let filePath;
|
|
|
|
let options;
|
|
|
|
beforeAll(() => {
|
|
|
|
cwd = process.cwd();
|
|
|
|
const tempDir = tempy.directory();
|
|
|
|
process.chdir(tempDir);
|
|
|
|
const fileDir = "src";
|
|
|
|
filePath = `${fileDir}/should-be-ignored.js`;
|
|
|
|
const ignorePath = path.join(tempDir, ".prettierignore");
|
|
|
|
fs.writeFileSync(ignorePath, filePath, "utf8");
|
|
|
|
options = { ignorePath };
|
|
|
|
});
|
|
|
|
afterAll(() => {
|
|
|
|
process.chdir(cwd);
|
|
|
|
});
|
|
|
|
test("with relative filePath", () => {
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync(filePath, options).ignored
|
|
|
|
).toMatchInlineSnapshot(`true`);
|
|
|
|
});
|
|
|
|
test("with relative filePath starts with dot", () => {
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync(`./${filePath}`, options).ignored
|
|
|
|
).toMatchInlineSnapshot(`true`);
|
|
|
|
});
|
|
|
|
test("with absolute filePath", () => {
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync(path.resolve(filePath), options).ignored
|
|
|
|
).toMatchInlineSnapshot(`true`);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-09 19:53:44 +03:00
|
|
|
test("API getFileInfo with withNodeModules", () => {
|
|
|
|
const file = path.resolve(
|
|
|
|
path.join(__dirname, "../cli/with-node-modules/node_modules/file.js")
|
|
|
|
);
|
|
|
|
expect(prettier.getFileInfo(file)).resolves.toMatchObject({
|
|
|
|
ignored: true,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo(file, {
|
|
|
|
withNodeModules: true
|
|
|
|
})
|
|
|
|
).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-05-09 19:53:44 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-10-05 03:47:23 +03:00
|
|
|
describe("extracts file-info for a JS file with no extension but a standard shebang", () => {
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync("tests_integration/cli/shebang/node-shebang")
|
|
|
|
).toMatchObject({
|
|
|
|
ignored: false,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-10-05 03:47:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("extracts file-info for a JS file with no extension but an env-based shebang", () => {
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync("tests_integration/cli/shebang/env-node-shebang")
|
|
|
|
).toMatchObject({
|
|
|
|
ignored: false,
|
2018-12-27 16:05:19 +03:00
|
|
|
inferredParser: "babel"
|
2018-10-05 03:47:23 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("returns null parser for unknown shebang", () => {
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo.sync("tests_integration/cli/shebang/nonsense-shebang")
|
|
|
|
).toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-09 19:53:44 +03:00
|
|
|
test("API getFileInfo with plugins loaded using pluginSearchDir", () => {
|
|
|
|
const file = "file.foo";
|
|
|
|
const pluginsPath = path.resolve(
|
|
|
|
path.join(__dirname, "../plugins/automatic")
|
|
|
|
);
|
|
|
|
expect(prettier.getFileInfo(file)).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: null
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo(file, {
|
|
|
|
pluginSearchDirs: [pluginsPath]
|
|
|
|
})
|
|
|
|
).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: "foo"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test("API getFileInfo with hand-picked plugins", () => {
|
|
|
|
const file = "file.foo";
|
|
|
|
const pluginPath = path.resolve(
|
|
|
|
path.join(
|
|
|
|
__dirname,
|
|
|
|
"../plugins/automatic/node_modules/@prettier/plugin-foo"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
expect(prettier.getFileInfo(file)).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: null
|
|
|
|
});
|
|
|
|
expect(
|
|
|
|
prettier.getFileInfo(file, {
|
|
|
|
plugins: [pluginPath]
|
|
|
|
})
|
|
|
|
).resolves.toMatchObject({
|
|
|
|
ignored: false,
|
|
|
|
inferredParser: "foo"
|
|
|
|
});
|
|
|
|
});
|