Skip to content

Commit

Permalink
Cache miss if config for file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Iaconis committed Apr 10, 2016
1 parent 48fba67 commit 537d7a5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 13 deletions.
19 changes: 12 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,19 @@ EslintValidationFilter.prototype.baseDir = function baseDir() {
};

EslintValidationFilter.prototype.cacheKeyProcessString = function cacheKeyProcessString(content, relativePath) {
return md5Hex([content, relativePath, stringify(this.options, {
replacer(key, value) {
if (typeof value === 'function') {
return value.toString();
}
return value;
function functionStringifier(key, value) {
if (typeof value === 'function') {
return value.toString();
}
})]);
return value;
}

return md5Hex([
content,
relativePath,
stringify(this.options, {replacer: functionStringifier}),
stringify(this.cli.getConfigForFile(path.join(this.eslintrc, relativePath)))
]);
};

EslintValidationFilter.prototype.processString = function processString(content, relativePath) {
Expand Down
75 changes: 69 additions & 6 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ const TEST_IGNORE_PATH = path.resolve(process.cwd(), './test/fixture/.eslintigno
const JS_FIXTURES = fs.readdirSync(FIXTURES).filter((name) => /\.js$/.test(name));

describe('EslintValidationFilter', function describeEslintValidationFilter() {
before(function beforeEslintValidationFilter() {
this.setupSpies = function setupSpies() {
// spy on filter process methods
const processStringSpy = this.sinon.spy(eslint.prototype, 'processString');
const postProcessSpy = this.sinon.spy(eslint.prototype, 'postProcess');

return {
processStringSpy,
postProcessSpy
};
};
});

function shouldReportErrors(inputTree, options) {
return function _shouldReportErrors() {
// lint test fixtures
Expand Down Expand Up @@ -133,9 +146,10 @@ describe('EslintValidationFilter', function describeEslintValidationFilter() {
});

it('should cache results, but still log errors', function shouldHaveCachedResults() {
// spy on filter process methods
const processStringSpy = this.sinon.spy(eslint.prototype, 'processString');
const postProcessSpy = this.sinon.spy(eslint.prototype, 'postProcess');
const {
processStringSpy,
postProcessSpy
} = this.setupSpies();

// run first test again, should use cache but still log errors
return shouldReportErrors(FIXTURES, {
Expand All @@ -153,9 +167,10 @@ describe('EslintValidationFilter', function describeEslintValidationFilter() {
});

it('should allow disabling the cache', function shouldAllowDisablingCache() {
// spy on filter process methods
const processStringSpy = this.sinon.spy(eslint.prototype, 'processString');
const postProcessSpy = this.sinon.spy(eslint.prototype, 'postProcess');
const {
processStringSpy,
postProcessSpy
} = this.setupSpies();

function runNonpersistent() {
return runEslint(FIXTURES, {
Expand All @@ -177,4 +192,52 @@ describe('EslintValidationFilter', function describeEslintValidationFilter() {
.to.have.callCount(2 * JS_FIXTURES.length);
});
});

it('should use the configuration to cache results', function shouldCacheByConfig() {
const {
processStringSpy,
postProcessSpy
} = this.setupSpies();
let processStringInitialCount;
let postProcessInitialCount;
const eslintrcPath = path.join(FIXTURES, '.eslintrc');
let eslintrcContent;

function runCustomRule() {
return runEslint(FIXTURES, {
options: {
rulePaths: ['conf/rules']
}
});
}

return runCustomRule().then(function retrieveCallCount() {
processStringInitialCount = processStringSpy.callCount;
postProcessInitialCount = postProcessSpy.callCount;
})
.then(function backupConfig() {
eslintrcContent = fs.readFileSync(eslintrcPath);
})
.then(function writeNewConfig() {
fs.writeFileSync(eslintrcPath, JSON.stringify({
extends: 'nightmare-mode/node',
rules: {
'custom-no-alert': 2
}
}));
})
.then(runCustomRule)
.then(function assertCaching() {
// check that it did not use the cache
expect(processStringSpy, 'Didn\'t use cache')
.to.have.callCount(processStringInitialCount + JS_FIXTURES.length);
expect(postProcessSpy, 'Logged errors')
.to.have.callCount(postProcessInitialCount + JS_FIXTURES.length);
})
.finally(function restoreConfig() {
if (typeof eslintrcContent !== 'undefined') {
fs.writeFileSync(eslintrcPath, eslintrcContent);
}
});
});
});

0 comments on commit 537d7a5

Please sign in to comment.