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

Chore: Cache fs reads in ignored-paths (fixes #8363) #8706

Merged
merged 2 commits into from
Jun 13, 2017
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
40 changes: 26 additions & 14 deletions lib/ignored-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const DEFAULT_OPTIONS = {
cwd: process.cwd()
};


//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -80,6 +79,7 @@ class IgnoredPaths {
*/
constructor(options) {
options = mergeDefaultOptions(options);
this.cache = {};

/**
* add pattern to node-ignore instance
Expand All @@ -91,17 +91,6 @@ class IgnoredPaths {
return ig.addPattern(pattern);
}

/**
* add ignore file to node-ignore instance
* @param {Object} ig, instance of node-ignore
* @param {string} filepath, file to add to ig
* @returns {array} raw ignore rules
*/
function addIgnoreFile(ig, filepath) {
ig.ignoreFiles.push(filepath);
return ig.add(fs.readFileSync(filepath, "utf8"));
}

this.defaultPatterns = [].concat(DEFAULT_IGNORE_DIRS, options.patterns || []);
this.baseDir = options.cwd;

Expand Down Expand Up @@ -155,8 +144,8 @@ class IgnoredPaths {
if (ignorePath) {
debug(`Adding ${ignorePath}`);
this.baseDir = path.dirname(path.resolve(options.cwd, ignorePath));
addIgnoreFile(this.ig.custom, ignorePath);
addIgnoreFile(this.ig.default, ignorePath);
this.addIgnoreFile(this.ig.custom, ignorePath);
this.addIgnoreFile(this.ig.default, ignorePath);
}

if (options.ignorePattern) {
Expand All @@ -168,6 +157,29 @@ class IgnoredPaths {
this.options = options;
}

/**
* read ignore filepath
* @param {string} filePath, file to add to ig
* @returns {array} raw ignore rules
*/
readIgnoreFile(filePath) {
if (typeof this.cache[filePath] === "undefined") {
this.cache[filePath] = fs.readFileSync(filePath, "utf8");
}
return this.cache[filePath];
}

/**
* add ignore file to node-ignore instance
* @param {Object} ig, instance of node-ignore
* @param {string} filePath, file to add to ig
* @returns {array} raw ignore rules
*/
addIgnoreFile(ig, filePath) {
ig.ignoreFiles.push(filePath);
return ig.add(this.readIgnoreFile(filePath));
}

/**
* Determine whether a file path is included in the default or custom ignore patterns
* @param {string} filepath Path to check
Expand Down
24 changes: 24 additions & 0 deletions tests/lib/ignored-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ describe("IgnoredPaths", () => {
)
);
});


});

describe("caching file reads", () => {

let readFileSyncCount;

before(() => {
readFileSyncCount = sinon.spy(fs, "readFileSync");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has a sinon sandbox been defined elsewhere in scope? If so, I would prefer we use that- otherwise this LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there isn't a sinon sandbox used elsewhere in the file, and just to double check, are you referring to sandbox as in http://sinonjs.org/releases/v1.17.7/sandbox/ ? @platinumazure

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the one. If one isn't being used elsewhere, I won't insist that you use one in this PR- we can always incorporate sandboxes later in a chore PR. Thanks for confirming!

});

after(() => {
readFileSyncCount.restore();
});

it("should cache readFileSync on same file paths", () => {
const ignoreFilePath = getFixturePath(".eslintignore");
const ignoredPaths = new IgnoredPaths({ ignore: true, cwd: getFixturePath() });

ignoredPaths.readIgnoreFile(ignoreFilePath);
assert.isTrue(ignoredPaths.contains(ignoreFilePath));
sinon.assert.calledOnce(readFileSyncCount);
});
});

describe("initialization with ignorePattern", () => {
Expand Down