@@ -10,158 +10,147 @@ const {
10
10
createShardedDirectory
11
11
} = require ( './helpers' )
12
12
13
- describe ( 'cp' , function ( ) {
13
+ describe ( 'cp' , ( ) => {
14
14
let mfs
15
15
16
- before ( ( ) => {
17
- return createMfs ( )
18
- . then ( instance => {
19
- mfs = instance
20
- } )
16
+ before ( async ( ) => {
17
+ mfs = await createMfs ( )
21
18
} )
22
19
23
- it ( 'refuses to copy files without arguments' , ( ) => {
24
- return mfs . cp ( )
25
- . then ( ( ) => {
26
- throw new Error ( 'No error was thrown for missing files' )
27
- } )
28
- . catch ( error => {
29
- expect ( error . message ) . to . contain ( 'Please supply at least one source' )
30
- } )
20
+ it ( 'refuses to copy files without arguments' , async ( ) => {
21
+ try {
22
+ await mfs . cp ( )
23
+ throw new Error ( 'No error was thrown for missing files' )
24
+ } catch ( err ) {
25
+ expect ( err . message ) . to . contain ( 'Please supply at least one source' )
26
+ }
31
27
} )
32
28
33
- it ( 'refuses to copy files without files' , ( ) => {
34
- return mfs . cp ( '/destination' )
35
- . then ( ( ) => {
36
- throw new Error ( 'No error was thrown for missing files' )
37
- } )
38
- . catch ( error => {
39
- expect ( error . message ) . to . contain ( 'Please supply at least one source' )
40
- } )
29
+ it ( 'refuses to copy files without files' , async ( ) => {
30
+ try {
31
+ await mfs . cp ( '/destination' )
32
+ throw new Error ( 'No error was thrown for missing files' )
33
+ } catch ( err ) {
34
+ expect ( err . message ) . to . contain ( 'Please supply at least one source' )
35
+ }
41
36
} )
42
37
43
- it ( 'refuses to copy files without files even with options' , ( ) => {
44
- return mfs . cp ( '/destination' , { } )
45
- . then ( ( ) => {
46
- throw new Error ( 'No error was thrown for missing files' )
47
- } )
48
- . catch ( error => {
49
- expect ( error . message ) . to . contain ( 'Please supply at least one source' )
50
- } )
38
+ it ( 'refuses to copy files without files even with options' , async ( ) => {
39
+ try {
40
+ await mfs . cp ( '/destination' , { } )
41
+ throw new Error ( 'No error was thrown for missing files' )
42
+ } catch ( err ) {
43
+ expect ( err . message ) . to . contain ( 'Please supply at least one source' )
44
+ }
51
45
} )
52
46
53
- it ( 'refuses to copy a file to a non-existent directory' , ( ) => {
54
- return mfs . cp ( '/i-do-not-exist' , '/output' )
55
- . then ( ( ) => {
56
- throw new Error ( 'No error was thrown for a non-existent file' )
57
- } )
58
- . catch ( error => {
59
- expect ( error . message ) . to . contain ( 'does not exist' )
60
- } )
47
+ it ( 'refuses to copy a file to a non-existent directory' , async ( ) => {
48
+ try {
49
+ await mfs . cp ( '/i-do-not-exist' , '/output' )
50
+ throw new Error ( 'No error was thrown for a non-existent file' )
51
+ } catch ( err ) {
52
+ expect ( err . message ) . to . contain ( 'does not exist' )
53
+ }
61
54
} )
62
55
63
- it ( 'refuses to copy files to an exsting file' , ( ) => {
56
+ it ( 'refuses to copy files to an exsting file' , async ( ) => {
64
57
const source = `/source-file-${ Math . random ( ) } .txt`
65
58
const destination = `/dest-file-${ Math . random ( ) } .txt`
66
59
67
- return mfs . write ( source , bufferStream ( 100 ) , {
60
+ await mfs . write ( source , bufferStream ( 100 ) , {
68
61
create : true
69
62
} )
70
- . then ( ( ) => mfs . write ( destination , bufferStream ( 100 ) , {
71
- create : true
72
- } ) )
73
- . then ( ( ) => mfs . cp ( source , destination ) )
74
- . then ( ( ) => {
75
- throw new Error ( 'No error was thrown for a non-existent file' )
76
- } )
77
- . catch ( error => {
78
- expect ( error . message ) . to . contain ( 'directory already has entry by that name' )
79
- } )
63
+ await mfs . write ( destination , bufferStream ( 100 ) , {
64
+ create : true
65
+ } )
66
+
67
+ try {
68
+ await mfs . cp ( source , destination )
69
+ throw new Error ( 'No error was thrown for a non-existent file' )
70
+ } catch ( err ) {
71
+ expect ( err . message ) . to . contain ( 'directory already has entry by that name' )
72
+ }
80
73
} )
81
74
82
- it ( 'refuses to copy a file to itself' , ( ) => {
75
+ it ( 'refuses to copy a file to itself' , async ( ) => {
83
76
const source = `/source-file-${ Math . random ( ) } .txt`
84
77
85
- return mfs . write ( source , bufferStream ( 100 ) , {
78
+ await mfs . write ( source , bufferStream ( 100 ) , {
86
79
create : true
87
80
} )
88
- . then ( ( ) => mfs . cp ( source , source ) )
89
- . then ( ( ) => {
90
- throw new Error ( 'No error was thrown for a non-existent file' )
91
- } )
92
- . catch ( error => {
93
- expect ( error . message ) . to . contain ( 'directory already has entry by that name' )
94
- } )
81
+
82
+ try {
83
+ await mfs . cp ( source , source )
84
+ throw new Error ( 'No error was thrown for a non-existent file' )
85
+ } catch ( err ) {
86
+ expect ( err . message ) . to . contain ( 'directory already has entry by that name' )
87
+ }
95
88
} )
96
89
97
- it ( 'copies a file to new location' , ( ) => {
90
+ it ( 'copies a file to new location' , async ( ) => {
98
91
const source = `/source-file-${ Math . random ( ) } .txt`
99
92
const destination = `/dest-file-${ Math . random ( ) } .txt`
100
93
let data = Buffer . alloc ( 0 )
101
94
102
- return mfs . write ( source , bufferStream ( 500 , {
95
+ await mfs . write ( source , bufferStream ( 500 , {
103
96
collector : ( bytes ) => {
104
97
data = Buffer . concat ( [ data , bytes ] )
105
98
}
106
99
} ) , {
107
100
create : true
108
101
} )
109
- . then ( ( ) => mfs . cp ( source , destination ) )
110
- . then ( ( ) => mfs . read ( destination ) )
111
- . then ( ( buffer ) => {
112
- expect ( buffer ) . to . deep . equal ( data )
113
- } )
102
+
103
+ await mfs . cp ( source , destination )
104
+ const buffer = await mfs . read ( destination )
105
+
106
+ expect ( buffer ) . to . deep . equal ( data )
114
107
} )
115
108
116
- it ( 'copies a file to a pre-existing directory' , ( ) => {
109
+ it ( 'copies a file to a pre-existing directory' , async ( ) => {
117
110
const source = `/source-file-${ Math . random ( ) } .txt`
118
111
const directory = `/dest-directory-${ Math . random ( ) } `
119
112
const destination = `${ directory } ${ source } `
120
113
121
- return mfs . write ( source , bufferStream ( 500 ) , {
114
+ await mfs . write ( source , bufferStream ( 500 ) , {
122
115
create : true
123
116
} )
124
- . then ( ( ) => mfs . mkdir ( directory ) )
125
- . then ( ( ) => mfs . cp ( source , directory ) )
126
- . then ( ( ) => mfs . stat ( destination ) )
127
- . then ( ( stats ) => {
128
- expect ( stats . size ) . to . equal ( 500 )
129
- } )
117
+ await mfs . mkdir ( directory )
118
+ await mfs . cp ( source , directory )
119
+
120
+ const stats = await mfs . stat ( destination )
121
+ expect ( stats . size ) . to . equal ( 500 )
130
122
} )
131
123
132
- it ( 'copies directories' , ( ) => {
124
+ it ( 'copies directories' , async ( ) => {
133
125
const source = `/source-directory-${ Math . random ( ) } `
134
126
const destination = `/dest-directory-${ Math . random ( ) } `
135
127
136
- return mfs . mkdir ( source )
137
- . then ( ( ) => mfs . cp ( source , destination ) )
138
- . then ( ( ) => mfs . stat ( destination ) )
139
- . then ( ( stats ) => {
140
- expect ( stats . type ) . to . equal ( 'directory' )
141
- } )
128
+ await mfs . mkdir ( source )
129
+ await mfs . cp ( source , destination )
130
+
131
+ const stats = await mfs . stat ( destination )
132
+ expect ( stats . type ) . to . equal ( 'directory' )
142
133
} )
143
134
144
- it ( 'copies directories recursively' , ( ) => {
135
+ it ( 'copies directories recursively' , async ( ) => {
145
136
const directory = `/source-directory-${ Math . random ( ) } `
146
137
const subDirectory = `/source-directory-${ Math . random ( ) } `
147
138
const source = `${ directory } ${ subDirectory } `
148
139
const destination = `/dest-directory-${ Math . random ( ) } `
149
140
150
- return mfs . mkdir ( source , {
141
+ await mfs . mkdir ( source , {
151
142
parents : true
152
143
} )
153
- . then ( ( ) => mfs . cp ( directory , destination ) )
154
- . then ( ( ) => mfs . stat ( destination ) )
155
- . then ( ( stats ) => {
156
- expect ( stats . type ) . to . equal ( 'directory' )
157
- } )
158
- . then ( ( ) => mfs . stat ( `${ destination } /${ subDirectory } ` ) )
159
- . then ( ( stats ) => {
160
- expect ( stats . type ) . to . equal ( 'directory' )
161
- } )
144
+ await mfs . cp ( directory , destination )
145
+
146
+ const stats = await mfs . stat ( destination )
147
+ expect ( stats . type ) . to . equal ( 'directory' )
148
+
149
+ const subDirStats = await mfs . stat ( `${ destination } /${ subDirectory } ` )
150
+ expect ( subDirStats . type ) . to . equal ( 'directory' )
162
151
} )
163
152
164
- it ( 'copies multiple files to new location' , ( ) => {
153
+ it ( 'copies multiple files to new location' , async ( ) => {
165
154
const sources = [ {
166
155
path : `/source-file-${ Math . random ( ) } .txt` ,
167
156
data : Buffer . alloc ( 0 )
@@ -171,42 +160,40 @@ describe('cp', function () {
171
160
} ]
172
161
const destination = `/dest-dir-${ Math . random ( ) } `
173
162
174
- return Promise . all (
175
- sources . map ( source => mfs . write ( source . path , bufferStream ( 500 , {
163
+ for ( const source of sources ) {
164
+ await mfs . write ( source . path , bufferStream ( 500 , {
176
165
collector : ( bytes ) => {
177
166
source . data = Buffer . concat ( [ source . data , bytes ] )
178
167
}
179
168
} ) , {
180
169
create : true
181
- } ) )
182
- )
183
- . then ( ( ) => mfs . cp ( sources [ 0 ] . path , sources [ 1 ] . path , destination , {
184
- parents : true
185
- } ) )
186
- . then ( ( ) => Promise . all (
187
- sources . map ( ( source , index ) => mfs . read ( ` ${ destination } ${ source . path } ` )
188
- . then ( ( buffer ) => {
189
- expect ( buffer ) . to . deep . equal ( sources [ index ] . data )
190
- } )
191
- )
192
- ) )
170
+ } )
171
+ }
172
+
173
+ await mfs . cp ( sources [ 0 ] . path , sources [ 1 ] . path , destination , {
174
+ parents : true
175
+ } )
176
+
177
+ for ( const source of sources ) {
178
+ const buffer = await mfs . read ( ` ${ destination } ${ source . path } ` )
179
+
180
+ expect ( buffer ) . to . deep . equal ( source . data )
181
+ }
193
182
} )
194
183
195
- it ( 'copies files from ipfs paths' , ( ) => {
184
+ it ( 'copies files from ipfs paths' , async ( ) => {
196
185
const source = `/source-file-${ Math . random ( ) } .txt`
197
186
const destination = `/dest-file-${ Math . random ( ) } .txt`
198
187
199
- return mfs . write ( source , bufferStream ( 100 ) , {
188
+ await mfs . write ( source , bufferStream ( 100 ) , {
200
189
create : true
201
190
} )
202
- . then ( ( ) => mfs . stat ( source ) )
203
- . then ( ( stats ) => {
204
- return mfs . cp ( `/ipfs/${ stats . hash } ` , destination )
205
- } )
206
- . then ( ( ) => mfs . stat ( destination ) )
207
- . then ( ( stats ) => {
208
- expect ( stats . size ) . to . equal ( 100 )
209
- } )
191
+
192
+ const stats = await mfs . stat ( source )
193
+ await mfs . cp ( `/ipfs/${ stats . hash } ` , destination )
194
+
195
+ const destinationStats = await mfs . stat ( destination )
196
+ expect ( destinationStats . size ) . to . equal ( 100 )
210
197
} )
211
198
212
199
it ( 'copies a sharded directory to a normal directory' , async ( ) => {
0 commit comments