Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

test: adds tests for mfs files.read*Stream #341

Merged
merged 1 commit into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions js/src/files/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const tests = {
rm: require('./rm'),
stat: require('./stat'),
read: require('./read'),
readReadableStream: require('./read-readable-stream'),
readPullStream: require('./read-pull-stream'),
ls: require('./ls'),
flush: require('./flush')
}
Expand Down
70 changes: 70 additions & 0 deletions js/src/files/read-pull-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-env mocha */
'use strict'

const series = require('async/series')
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const pull = require('pull-stream/pull')
const collect = require('pull-stream/sinks/collect')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.files.readPullStream', function () {
this.timeout(40 * 1000)

let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('should not read not found, expect error', (done) => {
const testDir = `/test-${hat()}`

pull(
ipfs.files.readPullStream(`${testDir}/404`),
collect((err) => {
expect(err).to.exist()
expect(err.message).to.contain('does not exist')
done()
})
)
})

it('should read file', (done) => {
const testDir = `/test-${hat()}`

series([
(cb) => ipfs.files.mkdir(testDir, cb),
(cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb)
], (err) => {
expect(err).to.not.exist()

pull(
ipfs.files.readPullStream(`${testDir}/a`),
collect((err, bufs) => {
expect(err).to.not.exist()
expect(bufs).to.eql([Buffer.from('Hello, world!')])
done()
})
)
})
})
})
}
67 changes: 67 additions & 0 deletions js/src/files/read-readable-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* eslint-env mocha */
'use strict'

const series = require('async/series')
const hat = require('hat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const bl = require('bl')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.files.readReadableStream', function () {
this.timeout(40 * 1000)

let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('should not read not found, expect error', (done) => {
const testDir = `/test-${hat()}`

const stream = ipfs.files.readReadableStream(`${testDir}/404`)

stream.on('error', (err) => {
expect(err).to.exist()
expect(err.message).to.contain('does not exist')
done()
})
})

it('should read file', (done) => {
const testDir = `/test-${hat()}`

series([
(cb) => ipfs.files.mkdir(testDir, cb),
(cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb)
], (err) => {
expect(err).to.not.exist()

const stream = ipfs.files.readReadableStream(`${testDir}/a`)

stream.pipe(bl((err, buf) => {
expect(err).to.not.exist()
expect(buf).to.eql(Buffer.from('Hello, world!'))
done()
}))
})
})
})
}