Skip to content

Commit 8a07929

Browse files
authored
Merge pull request ipfs#487 from Prabhakar-Poudel/block-remove
test(block.rm): add block remove tests
2 parents 1bd9c06 + a3c4cc3 commit 8a07929

File tree

3 files changed

+128
-3
lines changed

3 files changed

+128
-3
lines changed

SPEC/BLOCK.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* [block.get](#blockget)
44
* [block.put](#blockput)
5+
* [block.rm](#blockrm)
56
* [block.stat](#blockstat)
67

78
#### `block.get`
@@ -106,6 +107,44 @@ ipfs.block.put(blob, cid, (err, block) => {
106107

107108
A great source of [examples][] can be found in the tests for this API.
108109

110+
#### `block.rm`
111+
112+
> Remove one or more IPFS block(s).
113+
114+
##### `ipfs.block.rm(cid, [options], [callback])`
115+
116+
`cid` is a [cid][cid] which can be passed as:
117+
118+
- Buffer, the raw Buffer of the cid
119+
- CID, a CID instance
120+
- String, the base58 encoded version of the multihash
121+
- Array, list of CIDs in any of the above three formats
122+
123+
`options` is an Object that can contain the following properties:
124+
125+
- force (boolean): Ignores nonexistent blocks.
126+
- quiet (boolean): write minimal output
127+
128+
`callback` must follow `function (err, result) {}` signature, where `err` is an error if the operation was not successful and `result` is an object, containing hash and error strings.
129+
130+
If no `callback` is passed, a promise is returned.
131+
132+
NOTE: If the specified block is pinned it won't be removed and no error
133+
will be returned
134+
135+
**Example:**
136+
137+
```JavaScript
138+
ipfs.block.rm(cid, function (err, result) {
139+
if (err) {
140+
throw err
141+
}
142+
console.log(result.hash)
143+
})
144+
```
145+
146+
A great source of [examples][] can be found in the tests for this API.
147+
109148
#### `block.stat`
110149

111150
> Print information of a raw IPFS block.
@@ -150,7 +189,7 @@ ipfs.block.stat(cid, (err, stats) => {
150189

151190
A great source of [examples][] can be found in the tests for this API.
152191

153-
[block]:https://github.com/ipfs/js-ipfs-block
154-
[multihash]:https://github.com/multiformats/multihash
192+
[block]: https://github.com/ipfs/js-ipfs-block
193+
[multihash]: https://github.com/multiformats/multihash
155194
[examples]: https://github.com/ipfs/interface-ipfs-core/blob/master/src/block
156195
[cid]: https://www.npmjs.com/package/cids

src/block/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
const { createSuite } = require('../utils/suite')
33

44
const tests = {
5-
put: require('./put'),
65
get: require('./get'),
6+
put: require('./put'),
7+
rm: require('./rm'),
78
stat: require('./stat')
89
}
910

src/block/rm.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const { getDescribe, getIt, expect } = require('../utils/mocha')
5+
6+
module.exports = (createCommon, options) => {
7+
const describe = getDescribe(options)
8+
const it = getIt(options)
9+
const common = createCommon()
10+
11+
describe('.block.rm', function () {
12+
let ipfs, cid
13+
14+
before(function (done) {
15+
// CI takes longer to instantiate the daemon, so we need to increase the
16+
// timeout for the before step
17+
this.timeout(60 * 1000)
18+
19+
common.setup((err, factory) => {
20+
expect(err).to.not.exist()
21+
factory.spawnNode((err, node) => {
22+
expect(err).to.not.exist()
23+
ipfs = node
24+
done()
25+
})
26+
})
27+
})
28+
29+
beforeEach(function (done) {
30+
const blob = Buffer.from('blorb')
31+
ipfs.block.put(blob, (err, block) => {
32+
if (err) return done(err)
33+
cid = block.cid
34+
done()
35+
})
36+
})
37+
38+
afterEach(function () {
39+
cid = undefined
40+
})
41+
42+
after((done) => common.teardown(done))
43+
44+
it('should remove by CID object', (done) => {
45+
ipfs.block.rm(cid, (err, resp) => {
46+
expect(err).to.not.exist()
47+
expect(resp).to.have.property('hash')
48+
expect(resp.error).to.not.exist()
49+
done()
50+
})
51+
})
52+
53+
it('should remove by CID in string', (done) => {
54+
ipfs.block.rm(cid.toString(), (err, resp) => {
55+
expect(err).to.not.exist()
56+
expect(resp).to.have.property('hash')
57+
expect(resp.error).to.not.exist()
58+
done()
59+
})
60+
})
61+
62+
it('should remove by CID in buffer', (done) => {
63+
ipfs.block.rm(cid.buffer, (err, resp) => {
64+
expect(err).to.not.exist()
65+
expect(resp).to.have.property('hash')
66+
expect(resp.error).to.not.exist()
67+
done()
68+
})
69+
})
70+
71+
it('should remove multiple CIDs', (done) => {
72+
const blob = Buffer.from('more blorb')
73+
ipfs.block.put(blob, (err, block) => {
74+
if (err) return done(err)
75+
const cid1 = block.cid
76+
ipfs.block.rm([cid, cid1], (err, resp) => {
77+
expect(err).to.not.exist()
78+
expect(resp).to.have.property('hash')
79+
expect(resp.error).to.not.exist()
80+
done()
81+
})
82+
})
83+
})
84+
})
85+
}

0 commit comments

Comments
 (0)