//-----------------------------------------------------------------------------
describe("LintResultCache", () => {
- const fixturePath = path.resolve(__dirname, "../../fixtures/lint-result-cache");
+ const fixturePath = path.resolve(
+ __dirname,
+ "../../fixtures/lint-result-cache"
+ );
const cacheFileLocation = path.join(fixturePath, ".eslintcache");
const fileEntryCacheStubs = {};
let LintResultCache,
hashStub,
+ sandbox,
fakeConfig,
fakeErrorResults,
fakeErrorResultsAutofix;
before(() => {
- hashStub = sinon.stub();
+ sandbox = sinon.createSandbox();
+ hashStub = sandbox.stub();
let shouldFix = false;
});
// Get results without autofixing...
- fakeErrorResults = cliEngine.executeOnFiles([path.join(fixturePath, "test-with-errors.js")]).results[0];
+ fakeErrorResults = cliEngine.executeOnFiles([
+ path.join(fixturePath, "test-with-errors.js")
+ ]).results[0];
// ...and with autofixing
shouldFix = true;
- fakeErrorResultsAutofix = cliEngine.executeOnFiles([path.join(fixturePath, "test-with-errors.js")]).results[0];
+ fakeErrorResultsAutofix = cliEngine.executeOnFiles([
+ path.join(fixturePath, "test-with-errors.js")
+ ]).results[0];
// Set up LintResultCache with fake fileEntryCache module
- LintResultCache = proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
- "file-entry-cache": fileEntryCacheStubs,
- "./hash": hashStub
- });
+ LintResultCache = proxyquire(
+ "../../../lib/cli-engine/lint-result-cache.js",
+ {
+ "file-entry-cache": fileEntryCacheStubs,
+ "./hash": hashStub
+ }
+ );
});
afterEach(done => {
- sinon.reset();
+ sandbox.reset();
fs.unlink(cacheFileLocation, err => {
if (err && err.code !== "ENOENT") {
describe("constructor", () => {
it("should throw an error if cache file path is not provided", () => {
- assert.throws(() => new LintResultCache(), /Cache file location is required/u);
+ assert.throws(
+ () => new LintResultCache(),
+ /Cache file location is required/u
+ );
+ });
+
+ it("should throw an error if cacheStrategy is not provided", () => {
+ assert.throws(
+ () => new LintResultCache(cacheFileLocation),
+ /Cache strategy is required/u
+ );
});
- it("should successfully create an instance if cache file location is provided", () => {
- const instance = new LintResultCache(cacheFileLocation);
+ it("should throw an error if cacheStrategy is an invalid value", () => {
+ assert.throws(
+ () => new LintResultCache(cacheFileLocation, "foo"),
+ /Cache strategy must be one of/u
+ );
+ });
+
+ it("should successfully create an instance if cache file location and cache strategy provided", () => {
+ const instance = new LintResultCache(cacheFileLocation, "metadata");
assert.ok(instance, "Instance should have been created successfully");
});
const filePath = path.join(fixturePath, "test-with-errors.js");
const hashOfConfig = "hashOfConfig";
- let cacheEntry,
- getFileDescriptorStub,
- lintResultsCache;
+ let cacheEntry, getFileDescriptorStub, lintResultsCache;
before(() => {
- getFileDescriptorStub = sinon.stub();
+ getFileDescriptorStub = sandbox.stub();
fileEntryCacheStubs.create = () => ({
getFileDescriptor: getFileDescriptorStub
}
};
- getFileDescriptorStub.withArgs(filePath)
- .returns(cacheEntry);
+ getFileDescriptorStub.withArgs(filePath).returns(cacheEntry);
fakeConfig = {};
- lintResultsCache = new LintResultCache(cacheFileLocation);
+ lintResultsCache = new LintResultCache(cacheFileLocation, "metadata");
});
describe("when calculating the hashing", () => {
"../../package.json": { version },
"./hash": hashStub
});
- const newLintResultCache = new NewLintResultCache(cacheFileLocation);
+ const newLintResultCache = new NewLintResultCache(cacheFileLocation, "metadata");
newLintResultCache.getCachedLintResults(filePath, fakeConfig);
assert.ok(hashStub.calledOnce);
it("contains node version during hashing", () => {
const version = "node-=-version";
- sinon.stub(process, "version").value(version);
+ sandbox.stub(process, "version").value(version);
const NewLintResultCache = proxyquire("../../../lib/cli-engine/lint-result-cache.js", {
"./hash": hashStub
});
- const newLintResultCache = new NewLintResultCache(cacheFileLocation);
+ const newLintResultCache = new NewLintResultCache(cacheFileLocation, "metadata");
newLintResultCache.getCachedLintResults(filePath, fakeConfig);
+
assert.ok(hashStub.calledOnce);
assert.ok(hashStub.calledWithMatch(version));
});
});
it("should return null", () => {
- const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+ const result = lintResultsCache.getCachedLintResults(
+ filePath,
+ fakeConfig
+ );
assert.ok(getFileDescriptorStub.calledOnce);
assert.isNull(result);
});
it("should return null", () => {
- const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+ const result = lintResultsCache.getCachedLintResults(
+ filePath,
+ fakeConfig
+ );
assert.ok(getFileDescriptorStub.calledOnce);
assert.isNull(result);
});
it("should return null", () => {
- const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+ const result = lintResultsCache.getCachedLintResults(
+ filePath,
+ fakeConfig
+ );
assert.ok(getFileDescriptorStub.calledOnce);
assert.isNull(result);
});
it("should return expected results", () => {
- const result = lintResultsCache.getCachedLintResults(filePath, fakeConfig);
+ const result = lintResultsCache.getCachedLintResults(
+ filePath,
+ fakeConfig
+ );
assert.deepStrictEqual(result, fakeErrorResults);
- assert.ok(result.source, "source property should be hydrated from filesystem");
+ assert.ok(
+ result.source,
+ "source property should be hydrated from filesystem"
+ );
});
});
});
const filePath = path.join(fixturePath, "test-with-errors.js");
const hashOfConfig = "hashOfConfig";
- let cacheEntry,
- getFileDescriptorStub,
- lintResultsCache;
+ let cacheEntry, getFileDescriptorStub, lintResultsCache;
before(() => {
- getFileDescriptorStub = sinon.stub();
+ getFileDescriptorStub = sandbox.stub();
fileEntryCacheStubs.create = () => ({
getFileDescriptor: getFileDescriptorStub
meta: {}
};
- getFileDescriptorStub.withArgs(filePath)
- .returns(cacheEntry);
+ getFileDescriptorStub.withArgs(filePath).returns(cacheEntry);
fakeConfig = {};
hashStub.returns(hashOfConfig);
- lintResultsCache = new LintResultCache(cacheFileLocation);
+ lintResultsCache = new LintResultCache(cacheFileLocation, "metadata");
});
describe("When lint result has output property", () => {
it("does not modify file entry", () => {
- lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResultsAutofix);
+ lintResultsCache.setCachedLintResults(
+ filePath,
+ fakeConfig,
+ fakeErrorResultsAutofix
+ );
assert.notProperty(cacheEntry.meta, "results");
assert.notProperty(cacheEntry.meta, "hashOfConfig");
});
it("does not modify file entry", () => {
- lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResults);
+ lintResultsCache.setCachedLintResults(
+ filePath,
+ fakeConfig,
+ fakeErrorResults
+ );
assert.notProperty(cacheEntry.meta, "results");
assert.notProperty(cacheEntry.meta, "hashOfConfig");
describe("When file is found on filesystem", () => {
beforeEach(() => {
- lintResultsCache.setCachedLintResults(filePath, fakeConfig, fakeErrorResults);
+ lintResultsCache.setCachedLintResults(
+ filePath,
+ fakeConfig,
+ fakeErrorResults
+ );
});
it("stores hash of config in file entry", () => {
});
it("stores results (except source) in file entry", () => {
- const expectedCachedResults = Object.assign(
- {},
- fakeErrorResults,
- { source: null }
- );
+ const expectedCachedResults = Object.assign({}, fakeErrorResults, {
+ source: null
+ });
assert.deepStrictEqual(cacheEntry.meta.results, expectedCachedResults);
});
});
it("stores results (except source) in file entry", () => {
- const expectedCachedResults = Object.assign(
- {},
- fakeErrorResults,
- { source: null }
- );
+ const expectedCachedResults = Object.assign({}, fakeErrorResults, {
+ source: null
+ });
assert.deepStrictEqual(cacheEntry.meta.results, expectedCachedResults);
});
});
describe("reconcile", () => {
- let reconcileStub,
- lintResultsCache;
+ let reconcileStub, lintResultsCache;
before(() => {
- reconcileStub = sinon.stub();
+ reconcileStub = sandbox.stub();
fileEntryCacheStubs.create = () => ({
reconcile: reconcileStub
});
beforeEach(() => {
- lintResultsCache = new LintResultCache(cacheFileLocation);
+ lintResultsCache = new LintResultCache(cacheFileLocation, "metadata");
});
it("calls reconcile on the underlying cache", () => {