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

Commit 8773c80

Browse files
achingbrainalanshaw
authored andcommitted
test: adds tests for mfs files.read*Stream (#341)
1 parent 3f7a775 commit 8773c80

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

js/src/files/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const tests = {
1818
rm: require('./rm'),
1919
stat: require('./stat'),
2020
read: require('./read'),
21+
readReadableStream: require('./read-readable-stream'),
22+
readPullStream: require('./read-pull-stream'),
2123
ls: require('./ls'),
2224
flush: require('./flush')
2325
}

js/src/files/read-pull-stream.js

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const series = require('async/series')
5+
const hat = require('hat')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const pull = require('pull-stream/pull')
8+
const collect = require('pull-stream/sinks/collect')
9+
10+
module.exports = (createCommon, options) => {
11+
const describe = getDescribe(options)
12+
const it = getIt(options)
13+
const common = createCommon()
14+
15+
describe('.files.readPullStream', function () {
16+
this.timeout(40 * 1000)
17+
18+
let ipfs
19+
20+
before(function (done) {
21+
// CI takes longer to instantiate the daemon, so we need to increase the
22+
// timeout for the before step
23+
this.timeout(60 * 1000)
24+
25+
common.setup((err, factory) => {
26+
expect(err).to.not.exist()
27+
factory.spawnNode((err, node) => {
28+
expect(err).to.not.exist()
29+
ipfs = node
30+
done()
31+
})
32+
})
33+
})
34+
35+
after((done) => common.teardown(done))
36+
37+
it('should not read not found, expect error', (done) => {
38+
const testDir = `/test-${hat()}`
39+
40+
pull(
41+
ipfs.files.readPullStream(`${testDir}/404`),
42+
collect((err) => {
43+
expect(err).to.exist()
44+
expect(err.message).to.contain('does not exist')
45+
done()
46+
})
47+
)
48+
})
49+
50+
it('should read file', (done) => {
51+
const testDir = `/test-${hat()}`
52+
53+
series([
54+
(cb) => ipfs.files.mkdir(testDir, cb),
55+
(cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb)
56+
], (err) => {
57+
expect(err).to.not.exist()
58+
59+
pull(
60+
ipfs.files.readPullStream(`${testDir}/a`),
61+
collect((err, bufs) => {
62+
expect(err).to.not.exist()
63+
expect(bufs).to.eql([Buffer.from('Hello, world!')])
64+
done()
65+
})
66+
)
67+
})
68+
})
69+
})
70+
}

js/src/files/read-readable-stream.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const series = require('async/series')
5+
const hat = require('hat')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const bl = require('bl')
8+
9+
module.exports = (createCommon, options) => {
10+
const describe = getDescribe(options)
11+
const it = getIt(options)
12+
const common = createCommon()
13+
14+
describe('.files.readReadableStream', function () {
15+
this.timeout(40 * 1000)
16+
17+
let ipfs
18+
19+
before(function (done) {
20+
// CI takes longer to instantiate the daemon, so we need to increase the
21+
// timeout for the before step
22+
this.timeout(60 * 1000)
23+
24+
common.setup((err, factory) => {
25+
expect(err).to.not.exist()
26+
factory.spawnNode((err, node) => {
27+
expect(err).to.not.exist()
28+
ipfs = node
29+
done()
30+
})
31+
})
32+
})
33+
34+
after((done) => common.teardown(done))
35+
36+
it('should not read not found, expect error', (done) => {
37+
const testDir = `/test-${hat()}`
38+
39+
const stream = ipfs.files.readReadableStream(`${testDir}/404`)
40+
41+
stream.on('error', (err) => {
42+
expect(err).to.exist()
43+
expect(err.message).to.contain('does not exist')
44+
done()
45+
})
46+
})
47+
48+
it('should read file', (done) => {
49+
const testDir = `/test-${hat()}`
50+
51+
series([
52+
(cb) => ipfs.files.mkdir(testDir, cb),
53+
(cb) => ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), { create: true }, cb)
54+
], (err) => {
55+
expect(err).to.not.exist()
56+
57+
const stream = ipfs.files.readReadableStream(`${testDir}/a`)
58+
59+
stream.pipe(bl((err, buf) => {
60+
expect(err).to.not.exist()
61+
expect(buf).to.eql(Buffer.from('Hello, world!'))
62+
done()
63+
}))
64+
})
65+
})
66+
})
67+
}

0 commit comments

Comments
 (0)