Skip to content
This repository has been archived by the owner on Aug 12, 2020. It is now read-only.

Commit

Permalink
fix: expect empty stream to not generate any nodes (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgte authored and daviddias committed Jan 30, 2017
1 parent 224ba8d commit 7b054b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/builder/balanced/balanced-reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('assert')
const pull = require('pull-stream')
const pushable = require('pull-pushable')
const pullPair = require('pull-pair')
Expand All @@ -17,9 +16,12 @@ module.exports = function balancedReduceToRoot (reduce, options) {
result.end(err)
return // early
}
assert.equal(roots.length, 1, 'need one root')
result.push(roots[0])
result.end()
if (roots.length === 1) {
result.push(roots[0])
result.end()
} else if (roots.length > 1) {
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
}
})

function reduceToParents (_chunks, callback) {
Expand Down
12 changes: 7 additions & 5 deletions src/builder/trickle/trickle-reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const assert = require('assert')
const pull = require('pull-stream')
const pushable = require('pull-pushable')
const batch = require('pull-batch')
Expand All @@ -21,13 +20,16 @@ module.exports = function trickleReduceToRoot (reduce, options) {
trickle(0, -1),
batch(Infinity),
pull.asyncMap(reduce),
pull.collect((err, nodes) => {
pull.collect((err, roots) => {
if (err) {
result.end(err)
} else {
assert.equal(nodes.length, 1, 'need one root')
result.push(nodes[0])
result.end()
if (roots.length === 1) {
result.push(roots[0])
result.end()
} else if (roots.length > 1) {
result.end(new Error('expected a maximum of 0 roots and got ' + roots.length))
}
}
})
)
Expand Down
11 changes: 11 additions & 0 deletions test/test-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ module.exports = (repo) => {
)
})

it('doesn\'t yield anything on empty source', (done) => {
pull(
pull.empty(),
importer(ipldResolver, options),
pull.collect((err, nodes) => {
expect(err).to.not.exist
expect(nodes.length).to.be.eql(0)
done()
}))
})

it('fails on more than one root', (done) => {
pull(
pull.values([
Expand Down

0 comments on commit 7b054b6

Please sign in to comment.