Skip to content

Commit

Permalink
Merge pull request #5068 from MethodGrab/dont-cache-js-config
Browse files Browse the repository at this point in the history
Fix: Do not cache `.eslintrc.js` (fixes #5067)
  • Loading branch information
ilyavolodin committed Feb 18, 2016
2 parents 5243d41 + 2977248 commit 798ac54
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/config/config-file.js
Expand Up @@ -22,7 +22,8 @@ var debug = require("debug"),
stripComments = require("strip-json-comments"),
stringify = require("json-stable-stringify"),
isAbsolutePath = require("path-is-absolute"),
defaultOptions = require("../../conf/eslint.json");
defaultOptions = require("../../conf/eslint.json"),
requireUncached = require("require-uncached");


//------------------------------------------------------------------------------
Expand Down Expand Up @@ -153,7 +154,7 @@ function loadLegacyConfigFile(filePath) {
function loadJSConfigFile(filePath) {
debug("Loading JS config file: " + filePath);
try {
return require(filePath);
return requireUncached(filePath);
} catch (e) {
debug("Error reading JavaScript file: " + filePath);
e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
Expand Down
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -59,9 +59,10 @@
"optionator": "^0.8.1",
"path-is-absolute": "^1.0.0",
"path-is-inside": "^1.0.1",
"resolve": "^1.1.6",
"progress": "^1.1.8",
"pluralize": "^1.2.1",
"progress": "^1.1.8",
"require-uncached": "^1.0.2",
"resolve": "^1.1.6",
"shelljs": "^0.5.3",
"strip-json-comments": "~1.0.1",
"table": "^3.7.8",
Expand Down Expand Up @@ -96,6 +97,7 @@
"semver": "^5.0.3",
"shelljs-nodecli": "~0.1.0",
"sinon": "^1.17.2",
"temp": "^0.8.3",
"through": "^2.3.6",
"tmp": "0.0.28"
},
Expand Down
51 changes: 51 additions & 0 deletions tests/lib/config/config-file.js
Expand Up @@ -16,13 +16,16 @@ var assert = require("chai").assert,
path = require("path"),
fs = require("fs"),
tmp = require("tmp"),
temp = require("temp"),
yaml = require("js-yaml"),
resolve = require("resolve"),
userHome = require("user-home"),
proxyquire = require("proxyquire"),
environments = require("../../../conf/environments"),
ConfigFile = require("../../../lib/config/config-file");

temp = temp.track();

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -74,6 +77,22 @@ function writeTempConfigFile(config, filename, existingTmpDir) {
return tmpFilePath;
}

/**
* Helper function to write JS configs to temp file.
* @param {object} config Config to write out to temp file.
* @param {string} filename Name of file to write in temp dir.
* @param {string} existingTmpDir Optional dir path if temp file exists.
* @returns {string} Full path to the temp file.
* @private
*/
function writeTempJsConfigFile(config, filename, existingTmpDir) {
var tmpFileDir = existingTmpDir || temp.mkdirSync("eslint-tests-"),
tmpFilePath = path.join(tmpFileDir, filename),
tmpFileContents = "module.exports = " + JSON.stringify(config);
fs.writeFileSync(tmpFilePath, tmpFileContents);
return tmpFilePath;
}

/**
* Creates a module path relative to the current working directory.
* @param {string} moduleName The full module name.
Expand Down Expand Up @@ -141,6 +160,9 @@ describe("ConfigFile", function() {
opts.isFile = getFileCheck();
return resolve.sync(filename, opts);
}
},
"require-uncached": function(filename) {
return configDeps[filename];
}
};

Expand Down Expand Up @@ -208,6 +230,9 @@ describe("ConfigFile", function() {
opts.isFile = getFileCheck();
return resolve.sync(filename, opts);
}
},
"require-uncached": function(filename) {
return configDeps[filename];
}
};

Expand Down Expand Up @@ -497,6 +522,32 @@ describe("ConfigFile", function() {
assert.deepEqual(config, updatedConfig.eslintConfig);
});

it("should load fresh information from a .eslintrc.js file", function() {
var initialConfig = {
parserOptions: {},
env: {},
globals: {},
rules: {
quotes: [2, "double"]
}
},
updatedConfig = {
parserOptions: {},
env: {},
globals: {},
rules: {
quotes: 0
}
},
tmpFilename = ".eslintrc.js",
tmpFilePath = writeTempJsConfigFile(initialConfig, tmpFilename),
config = ConfigFile.load(tmpFilePath);
assert.deepEqual(config, initialConfig);
writeTempJsConfigFile(updatedConfig, tmpFilename, path.dirname(tmpFilePath));
config = ConfigFile.load(tmpFilePath);
assert.deepEqual(config, updatedConfig);
});

it("should load information from a YAML file", function() {
var config = ConfigFile.load(getFixturePath("yaml/.eslintrc.yaml"));
assert.deepEqual(config, {
Expand Down

0 comments on commit 798ac54

Please sign in to comment.