From 0b7faf6cd2021323464e11bff526d166037ea547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 11 Mar 2017 22:39:58 -0800 Subject: [PATCH] fix(coverage): bumping coverage for verify (#71) --- lib/verify.js | 14 +++++----- test/verify.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/lib/verify.js b/lib/verify.js index 52053fb..c8ba6e0 100644 --- a/lib/verify.js +++ b/lib/verify.js @@ -15,13 +15,6 @@ const rimraf = BB.promisify(require('rimraf')) BB.promisifyAll(fs) -module.exports.lastRun = lastRun -function lastRun (cache) { - return fs.readFileAsync( - path.join(cache, '_lastverified'), 'utf8' - ).then(data => new Date(+data)) -} - module.exports = verify function verify (cache, opts) { opts = opts || {} @@ -211,3 +204,10 @@ function writeVerifile (cache, opts) { opts.log && opts.log.silly('verify', 'writing verifile to ' + verifile) return fs.writeFileAsync(verifile, '' + (+(new Date()))) } + +module.exports.lastRun = lastRun +function lastRun (cache) { + return fs.readFileAsync( + path.join(cache, '_lastverified'), 'utf8' + ).then(data => new Date(+data)) +} diff --git a/test/verify.js b/test/verify.js index 41c992e..9000422 100644 --- a/test/verify.js +++ b/test/verify.js @@ -29,9 +29,11 @@ function mockCache () { [DIGEST]: CONTENT }, ALGO)) fixture.create(CACHE) - return index.insert(CACHE, KEY, DIGEST, { - metadata: METADATA, - hashAlgorithm: ALGO + return fs.mkdirAsync(path.join(CACHE, 'tmp')).then(() => { + return index.insert(CACHE, KEY, DIGEST, { + metadata: METADATA, + hashAlgorithm: ALGO + }) }) } @@ -149,6 +151,63 @@ test('removes corrupted content', t => { }) }) -test('removes content not referenced by any entries') +test('removes content not referenced by any entries', t => { + const fixture = new Tacks(CacheContent({ + [DIGEST]: CONTENT + }, ALGO)) + fixture.create(CACHE) + return verify(CACHE).then(stats => { + delete stats.startTime + delete stats.runTime + delete stats.endTime + t.deepEqual(stats, { + verifiedContent: 0, + reclaimedCount: 1, + reclaimedSize: CONTENT.length, + badContentCount: 0, + keptSize: 0, + missingContent: 0, + rejectedEntries: 0, + totalEntries: 0 + }, 'reported correct collection counts') + }) +}) + +test('cleans up contents of tmp dir', t => { + const tmpFile = path.join(CACHE, 'tmp', 'x') + const misc = path.join(CACHE, 'y') + return mockCache().then(() => { + return BB.join( + fs.writeFileAsync(tmpFile, ''), + fs.writeFileAsync(misc, ''), + () => verify(CACHE) + ) + }).then(() => { + return BB.join( + fs.statAsync(tmpFile).catch({code: 'ENOENT'}, e => e), + fs.statAsync(misc), + (err, stat) => { + t.equal(err.code, 'ENOENT', 'tmp file was blown away') + t.ok(stat, 'misc file was not touched') + } + ) + }) +}) + +test('writes a file with last verification time', t => { + return verify(CACHE).then(() => { + return BB.join( + verify.lastRun(CACHE), + fs.readFileAsync( + path.join(CACHE, '_lastverified'), 'utf8' + ).then(data => { + return new Date(parseInt(data)) + }), + (fromLastRun, fromFile) => { + t.equal(+fromLastRun, +fromFile, 'last verified was writen') + } + ) + }) +}) + test('fixes permissions and users on cache contents') -test('cleans up contents of tmp dir')