22'use strict'
33
44const { getDescribe, getIt, expect } = require ( '../utils/mocha' )
5+ const hat = require ( 'hat' )
56
67module . exports = ( createCommon , options ) => {
78 const describe = getDescribe ( options )
89 const it = getIt ( options )
910 const common = createCommon ( )
1011
1112 describe ( '.block.rm' , function ( ) {
12- let ipfs , cid
13+ let ipfs
1314
1415 before ( function ( done ) {
1516 // CI takes longer to instantiate the daemon, so we need to increase the
@@ -26,60 +27,97 @@ module.exports = (createCommon, options) => {
2627 } )
2728 } )
2829
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 ( )
30+ after ( ( done ) => common . teardown ( done ) )
31+
32+ it ( 'should remove by CID object' , async ( ) => {
33+ const cid = await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
34+
35+ // block should be present in the local store
36+ expect ( await ipfs . refs . local ( ) ) . to . deep . include ( {
37+ ref : cid . toString ( ) ,
38+ err : ''
39+ } )
40+
41+ const result = await ipfs . block . rm ( cid )
42+
43+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 )
44+ expect ( result [ 0 ] ) . to . have . property ( 'hash' , cid . toString ( ) )
45+ expect ( result [ 0 ] ) . to . not . have . property ( 'error' )
46+
47+ // did we actually remove the block?
48+ expect ( await ipfs . refs . local ( ) ) . to . not . deep . include ( {
49+ ref : cid . toString ( ) ,
50+ err : ''
3551 } )
3652 } )
3753
38- afterEach ( function ( ) {
39- cid = undefined
54+ it ( 'should remove by CID in string' , async ( ) => {
55+ const cid = await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
56+ const result = await ipfs . block . rm ( cid . toString ( ) )
57+
58+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 )
59+ expect ( result [ 0 ] ) . to . have . property ( 'hash' , cid . toString ( ) )
60+ expect ( result [ 0 ] ) . to . not . have . property ( 'error' )
4061 } )
4162
42- after ( ( done ) => common . teardown ( done ) )
63+ it ( 'should remove by CID in buffer' , async ( ) => {
64+ const cid = await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
65+ const result = await ipfs . block . rm ( cid . buffer )
4366
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- } )
67+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 )
68+ expect ( result [ 0 ] ) . to . have . property ( 'hash' , cid . toString ( ) )
69+ expect ( result [ 0 ] ) . to . not . have . property ( 'error' )
5170 } )
5271
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 ( )
72+ it ( 'should remove multiple CIDs' , async ( ) => {
73+ const cids = [
74+ await ipfs . dag . put ( Buffer . from ( hat ( ) ) ) ,
75+ await ipfs . dag . put ( Buffer . from ( hat ( ) ) ) ,
76+ await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
77+ ]
78+
79+ const result = await ipfs . block . rm ( cids )
80+
81+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 3 )
82+
83+ result . forEach ( ( res , index ) => {
84+ expect ( res ) . to . have . property ( 'hash' , cids [ index ] . toString ( ) )
85+ expect ( res ) . to . not . have . property ( 'error' )
5986 } )
6087 } )
6188
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- } )
89+ it ( 'should error when removing non-existent blocks' , async ( ) => {
90+ const cid = await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
91+
92+ // remove it
93+ await ipfs . block . rm ( cid )
94+
95+ // remove it again
96+ const result = await ipfs . block . rm ( cid )
97+
98+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 )
99+ expect ( result [ 0 ] ) . to . have . property ( 'error' ) . and . to . include ( 'block not found' )
69100 } )
70101
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- } )
102+ it ( 'should not error when force removing non-existent blocks' , async ( ) => {
103+ const cid = await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
104+
105+ // remove it
106+ await ipfs . block . rm ( cid )
107+
108+ // remove it again
109+ const result = await ipfs . block . rm ( cid , { force : true } )
110+
111+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 1 )
112+ expect ( result [ 0 ] ) . to . have . property ( 'hash' , cid . toString ( ) )
113+ expect ( result [ 0 ] ) . to . not . have . property ( 'error' )
114+ } )
115+
116+ it ( 'should return empty output when removing blocks quietly' , async ( ) => {
117+ const cid = await ipfs . dag . put ( Buffer . from ( hat ( ) ) )
118+ const result = await ipfs . block . rm ( cid , { quiet : true } )
119+
120+ expect ( result ) . to . be . an ( 'array' ) . and . to . have . lengthOf ( 0 )
83121 } )
84122 } )
85123}
0 commit comments