Skip to content

Commit ef4f70a

Browse files
authored
fix(coverage): more coverage for content reads (#70)
1 parent 03e947e commit ef4f70a

File tree

2 files changed

+66
-30
lines changed

2 files changed

+66
-30
lines changed

Diff for: lib/content/read.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ function hasContent (cache, address, algorithm) {
4646
if (!address) { return BB.resolve(false) }
4747
return fs.lstatAsync(
4848
contentPath(cache, address, algorithm || 'sha512')
49-
).then(() => true).catch(err => {
50-
if (err && err.code === 'ENOENT') {
51-
return BB.resolve(false)
52-
} else if (err && process.platform === 'win32' && err.code === 'EPERM') {
53-
return BB.resolve(false)
54-
} else {
49+
).then(() => true)
50+
.catch({code: 'ENOENT'}, () => false)
51+
.catch({code: 'EPERM'}, err => {
52+
if (process.platform !== 'win32') {
5553
throw err
5654
}
5755
})

Diff for: test/content.read.js

+62-24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const Buffer = require('safe-buffer').Buffer
44
const BB = require('bluebird')
55

66
const crypto = require('crypto')
7+
const finished = BB.promisify(require('mississippi').finished)
78
const path = require('path')
89
const Tacks = require('tacks')
910
const test = require('tap').test
@@ -37,15 +38,18 @@ test('read.stream: returns a stream with cache content data', function (t) {
3738
stream.on('error', function (e) { throw e })
3839
let buf = ''
3940
stream.on('data', function (data) { buf += data })
40-
stream.on('end', function () {
41-
t.ok(true, 'stream completed successfully')
42-
t.deepEqual(Buffer.from(buf), CONTENT, 'cache contents read correctly')
43-
t.end()
44-
})
41+
return BB.join(
42+
finished(stream).then(() => Buffer.from(buf)),
43+
read(CACHE, DIGEST),
44+
(fromStream, fromBulk) => {
45+
t.deepEqual(fromStream, CONTENT, 'stream data checks out')
46+
t.deepEqual(fromBulk, CONTENT, 'promise data checks out')
47+
}
48+
)
4549
})
4650

47-
test('read.stream: allows hashAlgorithm configuration', function (t) {
48-
const CONTENT = 'foobarbaz'
51+
test('read: allows hashAlgorithm configuration', function (t) {
52+
const CONTENT = Buffer.from('foobarbaz')
4953
const HASH = 'whirlpool'
5054
const DIGEST = crypto.createHash(HASH).update(CONTENT).digest('hex')
5155
const fixture = new Tacks(CacheContent({
@@ -56,44 +60,78 @@ test('read.stream: allows hashAlgorithm configuration', function (t) {
5660
stream.on('error', function (e) { throw e })
5761
let buf = ''
5862
stream.on('data', function (data) { buf += data })
59-
stream.on('end', function () {
60-
t.ok(true, 'stream completed successfully, off a sha512')
61-
t.deepEqual(buf, CONTENT, 'cache contents read correctly')
62-
t.end()
63-
})
63+
return BB.join(
64+
finished(stream).then(() => Buffer.from(buf)),
65+
read(CACHE, DIGEST, {
66+
hashAlgorithm: HASH
67+
}),
68+
(fromStream, fromBulk) => {
69+
t.deepEqual(fromStream, CONTENT, 'stream used algorithm')
70+
t.deepEqual(fromBulk, CONTENT, 'promise used algorithm')
71+
}
72+
)
6473
})
6574

66-
test('read.stream: errors if content missing', function (t) {
75+
test('read: errors if content missing', function (t) {
6776
const stream = read.stream(CACHE, 'whatnot')
68-
stream.on('error', function (e) {
69-
t.ok(e, 'got an error!')
70-
t.equal(e.code, 'ENOENT', 'error uses ENOENT error code')
71-
t.end()
72-
})
7377
stream.on('data', function (data) {
7478
throw new Error('unexpected data: ' + JSON.stringify(data))
7579
})
7680
stream.on('end', function () {
7781
throw new Error('end was called even though stream errored')
7882
})
83+
return BB.join(
84+
finished(stream).catch({code: 'ENOENT'}, err => err),
85+
read(CACHE, 'whatnot').catch({code: 'ENOENT'}, err => err),
86+
(streamErr, bulkErr) => {
87+
t.equal(streamErr.code, 'ENOENT', 'stream got the right error')
88+
t.equal(bulkErr.code, 'ENOENT', 'bulk got the right error')
89+
}
90+
)
7991
})
8092

81-
test('read.stream: errors if content fails checksum', function (t) {
82-
const CONTENT = 'foobarbaz'
93+
test('read: errors if content fails checksum', function (t) {
94+
const CONTENT = Buffer.from('foobarbaz')
8395
const DIGEST = crypto.createHash('sha512').update(CONTENT).digest('hex')
8496
const fixture = new Tacks(CacheContent({
8597
[DIGEST]: CONTENT.slice(3) // invalid contents!
8698
}))
8799
fixture.create(CACHE)
88100
const stream = read.readStream(CACHE, DIGEST)
89-
stream.on('error', function (e) {
90-
t.ok(e, 'got an error!')
91-
t.equal(e.code, 'EBADCHECKSUM', 'error uses EBADCHECKSUM error code')
92-
t.end()
101+
stream.on('end', function () {
102+
throw new Error('end was called even though stream errored')
93103
})
104+
return BB.join(
105+
finished(stream).catch({code: 'EBADCHECKSUM'}, err => err),
106+
read(CACHE, DIGEST).catch({code: 'EBADCHECKSUM'}, err => err),
107+
(streamErr, bulkErr) => {
108+
t.equal(streamErr.code, 'EBADCHECKSUM', 'stream got the right error')
109+
t.equal(bulkErr.code, 'EBADCHECKSUM', 'bulk got the right error')
110+
}
111+
)
112+
})
113+
114+
test('read: errors if content size does not match size option', function (t) {
115+
const CONTENT = Buffer.from('foobarbaz')
116+
const DIGEST = crypto.createHash('sha512').update(CONTENT).digest('hex')
117+
const fixture = new Tacks(CacheContent({
118+
[DIGEST]: CONTENT.slice(3) // bad size!
119+
}))
120+
fixture.create(CACHE)
121+
const stream = read.readStream(CACHE, DIGEST, { size: CONTENT.length })
94122
stream.on('end', function () {
95123
throw new Error('end was called even though stream errored')
96124
})
125+
return BB.join(
126+
finished(stream).catch({code: 'EBADSIZE'}, err => err),
127+
read(CACHE, DIGEST, {
128+
size: CONTENT.length
129+
}).catch({code: 'EBADSIZE'}, err => err),
130+
(streamErr, bulkErr) => {
131+
t.equal(streamErr.code, 'EBADSIZE', 'stream got the right error')
132+
t.equal(bulkErr.code, 'EBADSIZE', 'bulk got the right error')
133+
}
134+
)
97135
})
98136

99137
test('hasContent: returns true when a cache file exists', function (t) {

0 commit comments

Comments
 (0)