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

Commit feb479b

Browse files
committed
fix: isolated block tests
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent e4245db commit feb479b

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

js/src/block/get.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,37 @@ chai.use(dirtyChai)
1010
const multihash = require('multihashes')
1111
const CID = require('cids')
1212
const Buffer = require('safe-buffer').Buffer
13+
const auto = require('async/auto')
1314
const { getDescribe } = require('../utils/mocha')
1415

15-
module.exports = (common, options) => {
16+
module.exports = (createCommon, options) => {
1617
const describe = getDescribe(options)
18+
const common = createCommon()
1719

1820
describe('.block.get', function () {
19-
let ipfs
21+
const data = Buffer.from('blorb')
22+
let ipfs, hash
2023

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

26-
common.setup((err, factory) => {
27-
expect(err).to.not.exist()
28-
factory.spawnNode((err, node) => {
29-
expect(err).to.not.exist()
30-
ipfs = node
31-
done()
32-
})
29+
auto({
30+
factory: (cb) => common.setup(cb),
31+
ipfs: ['factory', (res, cb) => res.factory.spawnNode(cb)],
32+
block: ['ipfs', (res, cb) => res.ipfs.block.put(data, cb)]
33+
}, (err, res) => {
34+
if (err) return done(err)
35+
ipfs = res.ipfs
36+
hash = res.block.cid.multihash
37+
done()
3338
})
3439
})
3540

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

3843
it('should get by CID object', (done) => {
39-
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
4044
const cid = new CID(hash)
4145

4246
ipfs.block.get(cid, (err, block) => {
@@ -48,12 +52,10 @@ module.exports = (common, options) => {
4852
})
4953

5054
it('should get by CID in string', (done) => {
51-
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
52-
53-
ipfs.block.get(hash, (err, block) => {
55+
ipfs.block.get(multihash.toB58String(hash), (err, block) => {
5456
expect(err).to.not.exist()
5557
expect(block.data).to.eql(new Buffer('blorb'))
56-
expect(block.cid.multihash).to.eql(multihash.fromB58String(hash))
58+
expect(block.cid.multihash).to.eql(hash)
5759
done()
5860
})
5961
})

js/src/block/put.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ const CID = require('cids')
1313
const Buffer = require('safe-buffer').Buffer
1414
const { getDescribe } = require('../utils/mocha')
1515

16-
module.exports = (common, options) => {
16+
module.exports = (createCommon, options) => {
1717
const describe = getDescribe(options)
18+
const common = createCommon()
1819

1920
describe('.block.put', () => {
2021
let ipfs
@@ -38,7 +39,7 @@ module.exports = (common, options) => {
3839

3940
it('should put a buffer, using defaults', (done) => {
4041
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
41-
const blob = new Buffer('blorb')
42+
const blob = Buffer.from('blorb')
4243

4344
ipfs.block.put(blob, (err, block) => {
4445
expect(err).to.not.exist()
@@ -51,7 +52,7 @@ module.exports = (common, options) => {
5152
it('should put a buffer, using CID', (done) => {
5253
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
5354
const cid = new CID(expectedHash)
54-
const blob = new Buffer('blorb')
55+
const blob = Buffer.from('blorb')
5556

5657
ipfs.block.put(blob, { cid: cid }, (err, block) => {
5758
expect(err).to.not.exist()
@@ -63,7 +64,7 @@ module.exports = (common, options) => {
6364

6465
it('should put a buffer, using options', (done) => {
6566
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
66-
const blob = new Buffer('blorb')
67+
const blob = Buffer.from('blorb')
6768

6869
ipfs.block.put(blob, {
6970
format: 'dag-pb',
@@ -80,7 +81,7 @@ module.exports = (common, options) => {
8081
it('should put a Block instance', (done) => {
8182
const expectedHash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
8283
const cid = new CID(expectedHash)
83-
const b = new Block(new Buffer('blorb'), cid)
84+
const b = new Block(Buffer.from('blorb'), cid)
8485

8586
ipfs.block.put(b, (err, block) => {
8687
expect(err).to.not.exist()
@@ -91,7 +92,7 @@ module.exports = (common, options) => {
9192
})
9293

9394
it('should error with array of blocks', (done) => {
94-
const blob = Buffer('blorb')
95+
const blob = Buffer.from('blorb')
9596

9697
ipfs.block.put([blob, blob], (err) => {
9798
expect(err).to.be.an.instanceof(Error)

js/src/block/stat.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@ const dirtyChai = require('dirty-chai')
88
const expect = chai.expect
99
chai.use(dirtyChai)
1010
const CID = require('cids')
11+
const auto = require('async/auto')
1112
const { getDescribe } = require('../utils/mocha')
1213

13-
module.exports = (common, options) => {
14+
module.exports = (createCommon, options) => {
1415
const describe = getDescribe(options)
16+
const common = createCommon()
1517

1618
describe('.block.stat', () => {
17-
let ipfs
19+
const data = Buffer.from('blorb')
20+
let ipfs, hash
1821

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

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-
})
27+
auto({
28+
factory: (cb) => common.setup(cb),
29+
ipfs: ['factory', (res, cb) => res.factory.spawnNode(cb)],
30+
block: ['ipfs', (res, cb) => res.ipfs.block.put(data, cb)]
31+
}, (err, res) => {
32+
if (err) return done(err)
33+
ipfs = res.ipfs
34+
hash = res.block.cid.multihash
35+
done()
3136
})
3237
})
3338

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

3641
it('should stat by CID', (done) => {
37-
const hash = 'QmPv52ekjS75L4JmHpXVeuJ5uX2ecSfSZo88NSyxwA3rAQ'
3842
const cid = new CID(hash)
3943

4044
ipfs.block.stat(cid, (err, stats) => {

js/src/utils/suite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
function createSuite (tests) {
2-
const suite = (common, options) => {
2+
const suite = (createCommon, options) => {
33
Object.keys(tests).forEach(t => {
44
const opts = Object.assign({}, options)
55
opts.skip = Array.isArray(opts.skip) ? opts.skip.includes(t) : opts.skip
66
opts.only = Array.isArray(opts.only) ? opts.only.includes(t) : opts.only
7-
tests[t](common, opts)
7+
tests[t](createCommon, opts)
88
})
99
}
1010

0 commit comments

Comments
 (0)