Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(rm): stop crashing if content is missing on rm
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed May 24, 2017
1 parent 950b19a commit ac90bc0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/content/rm.js
Expand Up @@ -9,9 +9,13 @@ const rimraf = BB.promisify(require('rimraf'))
module.exports = rm
function rm (cache, integrity) {
return hasContent(cache, integrity).then(content => {
const sri = content.sri
if (sri) {
return rimraf(contentPath(cache, sri))
if (content) {
const sri = content.sri
if (sri) {
return rimraf(contentPath(cache, sri)).then(() => true)
}
} else {
return false
}
})
}
4 changes: 3 additions & 1 deletion lib/entry-index.js
Expand Up @@ -215,7 +215,9 @@ function formatEntry (cache, entry) {
}

function readdirOrEmpty (dir) {
return readdirAsync(dir).catch({code: 'ENOENT'}, () => [])
return readdirAsync(dir)
.catch({code: 'ENOENT'}, () => [])
.catch({code: 'ENOTDIR'}, () => [])
}

function nop () {
Expand Down
22 changes: 21 additions & 1 deletion test/ls.js
Expand Up @@ -11,6 +11,7 @@ const testDir = require('./util/test-dir')(__filename)

const CACHE = path.join(testDir, 'cache')
const contentPath = require('../lib/content/path')
const File = Tacks.File

const ls = require('..').ls

Expand Down Expand Up @@ -88,6 +89,25 @@ test('separate keys in conflicting buckets', function (t) {

test('works fine on an empty/missing cache', function (t) {
return ls(CACHE).then(listing => {
t.deepEqual(listing, {})
t.deepEqual(listing, {}, 'returned an empty listing')
})
})

test('ignores non-dir files', function (t) {
const index = CacheIndex({
'whatever': {
key: 'whatever',
integrity: 'sha512-deadbeef',
time: 12345,
metadata: 'omgsometa',
size: 234234
}
})
index.contents['garbage'] = File('hello world')
const fixture = new Tacks(index)
fixture.create(CACHE)
return ls(CACHE).then(listing => {
t.equal(Object.keys(listing).length, 1, 'only 1 item in listing')
t.equal(listing.whatever.key, 'whatever', 'only the correct entry listed')
})
})

0 comments on commit ac90bc0

Please sign in to comment.