Skip to content

Commit

Permalink
feat: Unify filepaths to always use posix path separator (#81)
Browse files Browse the repository at this point in the history
* feat: Unify filepaths to always use posix path separator

* Remove usage of fromEntries to support older node versions

* Satisfy the code coverage gods

* Switch eslint to ecma 2018 parser

* Add full coverage to the pathUtils

* Add integration test on unifying pathnames between windows and posix
  • Loading branch information
woutervanvliet authored Apr 30, 2020
1 parent ef3be21 commit 6c8e959
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
},
extends: ["eslint:recommended", "prettier"],
parserOptions: {
ecmaVersion: 2017
ecmaVersion: 2018
},
plugins: ["jest"],
rules: {
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
coverage/
.vscode
.idea/
21 changes: 21 additions & 0 deletions __tests__/fileSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,27 @@ describe("createFileSet", () => {
"bar.js": {}
});
});

it("should unify windows- and posix paths into posix paths", () => {
const result = createFileSet(
[
{
filePath: "\\absolute\\path\\windows\\foo.js",
messages: []
},
{
filePath: "/absolute/path/posix/bar.js",
messages: []
}
],
[]
);

expect(result).toEqual({
"windows/foo.js": {},
"posix/bar.js": {}
});
});
});

describe("cleanUpDeletedFilesInFileSet", () => {
Expand Down
14 changes: 14 additions & 0 deletions __tests__/integration/engine/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ describe("engine.run", () => {
})
);

it(
"should convert windows- to posix paths when reading a record",
setup("windows-paths-in-record", ({ fixturePath }) => {
const { run } = require("../../../lib/engine");
const { results } = run({}, ["."]);
expect(results).toHaveLength(0);

const rewrittenRecord = readRecord(fixturePath);
expect(rewrittenRecord.files).toEqual({
"src/main.js": { "no-console": 1 }
});
})
);

it(
"ignores eslint rules not listed in config",
setup("no-rules", () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
rules: {
"no-console": "warn"
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"recordVersion": 1,
"configHash": "773a8b36f5d74ada1b0144c983a6d725c71c9413",
"files": {
"src\\main.js": {
"no-console": 1
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
surfaceArea: ["."],
rules: ["no-console"]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("this does nothing");
31 changes: 31 additions & 0 deletions __tests__/pathUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { toPosixPath, toSystemPath, toWinPath } = require("../lib/pathUtils");
const helpers = require("../lib/helpers");

describe("toPosixPath", function() {
it("should convert windows to posix paths", () => {
expect(toPosixPath("\\windows\\path")).toEqual("/windows/path");
});
it("should not alter posix paths", () => {
expect(toPosixPath("/posix/path")).toEqual("/posix/path");
});
});

describe("toWinPath", function() {
it("should convert posix to windows path", () => {
expect(toWinPath("/posix/path")).toEqual("\\posix\\path");
});
it("not alter windows path", () => {
expect(toWinPath("\\windows\\path")).toEqual("\\windows\\path");
});
});

describe("toSystemPath", () => {
it("should convert posix to windows on windows", () => {
jest.spyOn(helpers, "isPosix").mockImplementation(() => false);
expect(toSystemPath("/posix/path")).toEqual("\\posix\\path");
});
it("should convert windows to posix on posix", () => {
jest.spyOn(helpers, "isPosix").mockImplementation(() => true);
expect(toSystemPath("\\windows\\path")).toEqual("/windows/path");
});
});
33 changes: 32 additions & 1 deletion __tests__/record.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { createRecord } = require("../lib/record");
const { createRecord, unifyFilePaths } = require("../lib/record");
jest.mock("../package.json", () => ({ version: "1.0.0" }));

describe("createRecord", () => {
Expand Down Expand Up @@ -28,3 +28,34 @@ describe("createRecord", () => {
expect(Object.keys(files)).toEqual(["a/b/c", "b/b/c", "z/a/c"]);
});
});

describe("unifyFilePaths", () => {
it("converts windows paths to posix", () => {
const record = {
hash: "aabbcc",
files: {
"\\windows\\path.js": {},
"\\another\\windows\\path.js": {}
}
};
const unifiedRecord = unifyFilePaths(record);
expect(unifiedRecord).toEqual({
hash: "aabbcc",
files: {
"/windows/path.js": {},
"/another/windows/path.js": {}
}
});
});
it("keeps posix paths as posix", () => {
const record = {
hash: "aabbcc",
files: {
"/windows/path.js": {},
"/another/windows/path.js": {}
}
};
const unifiedRecord = unifyFilePaths(record);
expect(unifiedRecord).toEqual(record);
});
});
5 changes: 4 additions & 1 deletion lib/fileSet.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
const path = require("path");
const fs = require("fs");
const chalk = require("chalk");
const { toPosixPath, toSystemPath } = require("./pathUtils");

function createFileSet(results, trackedRules, getWarnings) {
const fileSet = {};
const trackedRuleSet = new Set(trackedRules);

results.forEach(({ messages, filePath }) => {
const relativePath = path.relative(process.cwd(), filePath);
const relativePath = toPosixPath(
path.relative(process.cwd(), toSystemPath(filePath))
);
const rules = {};

if (getWarnings) {
Expand Down
9 changes: 9 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require("path");

function isPosix() {
return path.sep === path.posix.sep;
}

module.exports = {
isPosix
};
24 changes: 24 additions & 0 deletions lib/pathUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require("path");
const helpers = require("./helpers");

function toPosixPath(filePath) {
return filePath.split(path.win32.sep).join(path.posix.sep);
}

function toWinPath(filePath) {
return filePath.split(path.posix.sep).join(path.win32.sep);
}

function toSystemPath(filePath) {
if (helpers.isPosix()) {
return toPosixPath(filePath);
} else {
return toWinPath(filePath);
}
}

module.exports = {
toPosixPath,
toWinPath,
toSystemPath
};
23 changes: 21 additions & 2 deletions lib/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const semver = require("semver");
const { hasGitConflict, resolveGitConflict } = require("./gitConflict");
const EsplintError = require("./EsplintError");
const git = require("simple-git/promise")();
const { toPosixPath } = require("./pathUtils");

function getRecordPath() {
const packageDir = pkgDir.sync();
Expand Down Expand Up @@ -38,6 +39,23 @@ function createRecord({ files, config }) {
};
}

function unifyFilePaths(record) {
if (record === null) {
return record;
}

return {
...record,
files: Object.entries(record.files).reduce(
(result, [filePath, warnings]) => {
result[toPosixPath(filePath)] = warnings;
return result;
},
{}
)
};
}

function getRecord(config) {
const { overwrite } = config;

Expand All @@ -47,7 +65,7 @@ function getRecord(config) {
}

const configHash = getConfigHash(config);
const record = readRecord();
const record = unifyFilePaths(readRecord());

if (record === null) {
return {};
Expand Down Expand Up @@ -165,5 +183,6 @@ module.exports = {
getRecord,
writeRecord,
readRecord,
stageRecord
stageRecord,
unifyFilePaths
};

0 comments on commit 6c8e959

Please sign in to comment.