Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): normalize file path for getFileInfo #5570

Merged
merged 2 commits into from Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/common/get-file-info.js
Expand Up @@ -2,6 +2,7 @@

const createIgnorer = require("./create-ignorer");
const options = require("../main/options");
const path = require("path");

/**
* @typedef {{ ignorePath?: string, withNodeModules?: boolean, plugins: object }} FileInfoOptions
Expand All @@ -19,7 +20,11 @@ const options = require("../main/options");
*/
function getFileInfo(filePath, opts) {
return createIgnorer(opts.ignorePath, opts.withNodeModules).then(ignorer =>
_getFileInfo(ignorer, filePath, opts.plugins)
_getFileInfo(
ignorer,
normalizeFilePath(filePath, opts.ignorePath),
opts.plugins
)
);
}

Expand All @@ -30,7 +35,11 @@ function getFileInfo(filePath, opts) {
*/
getFileInfo.sync = function(filePath, opts) {
const ignorer = createIgnorer.sync(opts.ignorePath, opts.withNodeModules);
return _getFileInfo(ignorer, filePath, opts.plugins);
return _getFileInfo(
ignorer,
normalizeFilePath(filePath, opts.ignorePath),
opts.plugins
);
};

function _getFileInfo(ignorer, filePath, plugins) {
Expand All @@ -43,4 +52,10 @@ function _getFileInfo(ignorer, filePath, plugins) {
};
}

function normalizeFilePath(filePath, ignorePath) {
return ignorePath
? path.relative(path.dirname(ignorePath), filePath)
: filePath;
}

module.exports = getFileInfo;
36 changes: 36 additions & 0 deletions tests_integration/__tests__/file-info.js
@@ -1,6 +1,8 @@
"use strict";

const path = require("path");
const tempy = require("tempy");
const fs = require("fs");

const runPrettier = require("../runPrettier");
const prettier = require("prettier/local");
Expand Down Expand Up @@ -176,6 +178,40 @@ test("API getFileInfo.sync with ignorePath", () => {
});
});

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`);
});
});

test("API getFileInfo with withNodeModules", () => {
const file = path.resolve(
path.join(__dirname, "../cli/with-node-modules/node_modules/file.js")
Expand Down