This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathmv.js
301 lines (245 loc) · 10.8 KB
/
mv.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* eslint-env mocha */
'use strict'
const { fromString: uint8ArrayFromString } = require('uint8arrays/from-string')
const { concat: uint8ArrayConcat } = require('uint8arrays/concat')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const createShardedDirectory = require('../utils/create-sharded-directory')
const { randomBytes } = require('iso-random-stream')
const isShardAtPath = require('../utils/is-shard-at-path')
const all = require('it-all')
/**
* @typedef {import('ipfsd-ctl').Factory} Factory
*/
/**
* @param {Factory} factory
* @param {Object} options
*/
module.exports = (factory, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.mv', function () {
this.timeout(120 * 1000)
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async () => { ipfs = (await factory.spawn()).api })
before(async () => {
await ipfs.files.mkdir('/test/lv1/lv2', { parents: true })
await ipfs.files.write('/test/a', uint8ArrayFromString('Hello, world!'), { create: true })
})
after(() => factory.clean())
it('refuses to move files without arguments', async () => {
// @ts-expect-error invalid args
await expect(ipfs.files.mv()).to.eventually.be.rejected()
})
it('refuses to move files without enough arguments', async () => {
// @ts-expect-error invalid args
await expect(ipfs.files.mv()).to.eventually.be.rejected()
})
it('moves a file', async () => {
const source = `/source-file-${Math.random()}.txt`
const destination = `/dest-file-${Math.random()}.txt`
const data = randomBytes(500)
await ipfs.files.write(source, data, {
create: true
})
await ipfs.files.mv(source, destination)
const bytes = uint8ArrayConcat(await all(ipfs.files.read(destination)))
expect(bytes).to.deep.equal(data)
await expect(ipfs.files.stat(source)).to.eventually.be.rejectedWith(/does not exist/)
})
it('moves a directory', async () => {
const source = `/source-directory-${Math.random()}`
const destination = `/dest-directory-${Math.random()}`
await ipfs.files.mkdir(source)
await ipfs.files.mv(source, destination, {
recursive: true
})
const stats = await ipfs.files.stat(destination)
expect(stats.type).to.equal('directory')
try {
await ipfs.files.stat(source)
throw new Error('Directory was copied but not removed')
} catch (err) {
expect(err.message).to.contain('does not exist')
}
})
it('moves directories recursively', async () => {
const directory = `source-directory-${Math.random()}`
const subDirectory = `/source-directory-${Math.random()}`
const source = `/${directory}${subDirectory}`
const destination = `/dest-directory-${Math.random()}`
await ipfs.files.mkdir(source, {
parents: true
})
await ipfs.files.mv(`/${directory}`, destination, {
recursive: true
})
const stats = await ipfs.files.stat(destination)
expect(stats.type).to.equal('directory')
const subDirectoryStats = await ipfs.files.stat(`${destination}${subDirectory}`)
expect(subDirectoryStats.type).to.equal('directory')
try {
await ipfs.files.stat(source)
throw new Error('Directory was copied but not removed')
} catch (err) {
expect(err.message).to.contain('does not exist')
}
})
describe('with sharding', () => {
/** @type {import('ipfs-core-types').IPFS} */
let ipfs
before(async function () {
const ipfsd = await factory.spawn({
ipfsOptions: {
EXPERIMENTAL: {
// enable sharding for js
sharding: true
},
config: {
// enable sharding for go
Experimental: {
ShardingEnabled: true
}
}
}
})
ipfs = ipfsd.api
})
it('moves a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `/dir-${Math.random()}`
const finalShardedDirPath = `${dirPath}${shardedDirPath}`
await ipfs.files.mkdir(dirPath)
await ipfs.files.mv(shardedDirPath, dirPath)
await expect(isShardAtPath(finalShardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(finalShardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(dirPath)).type).to.equal('directory')
try {
await ipfs.files.stat(shardedDirPath)
throw new Error('Dir was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
it('moves a normal directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `/dir-${Math.random()}`
const finalDirPath = `${shardedDirPath}${dirPath}`
await ipfs.files.mkdir(dirPath)
await ipfs.files.mv(dirPath, shardedDirPath)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalDirPath)).type).to.equal('directory')
try {
await ipfs.files.stat(dirPath)
throw new Error('Dir was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
it('moves a sharded directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const otherShardedDirPath = await createShardedDirectory(ipfs)
const finalShardedDirPath = `${shardedDirPath}${otherShardedDirPath}`
await ipfs.files.mv(otherShardedDirPath, shardedDirPath)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
await expect(isShardAtPath(finalShardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(finalShardedDirPath)).type).to.equal('directory')
try {
await ipfs.files.stat(otherShardedDirPath)
throw new Error('Sharded dir was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
it('moves a file from a normal directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `/dir-${Math.random()}`
const file = `file-${Math.random()}.txt`
const filePath = `${dirPath}/${file}`
const finalFilePath = `${shardedDirPath}/${file}`
await ipfs.files.mkdir(dirPath)
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3, 4]), {
create: true
})
await ipfs.files.mv(filePath, shardedDirPath)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
try {
await ipfs.files.stat(filePath)
throw new Error('File was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
it('moves a file from a sharded directory to a normal directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const dirPath = `/dir-${Math.random()}`
const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${dirPath}/${file}`
await ipfs.files.mkdir(dirPath)
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3, 4]), {
create: true
})
await ipfs.files.mv(filePath, dirPath)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
expect((await ipfs.files.stat(dirPath)).type).to.equal('directory')
try {
await ipfs.files.stat(filePath)
throw new Error('File was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
it('moves a file from a sharded directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const otherShardedDirPath = await createShardedDirectory(ipfs)
const file = `file-${Math.random()}.txt`
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${otherShardedDirPath}/${file}`
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3, 4]), {
create: true
})
await ipfs.files.mv(filePath, otherShardedDirPath)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
await expect(isShardAtPath(otherShardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(otherShardedDirPath)).type).to.equal('directory')
try {
await ipfs.files.stat(filePath)
throw new Error('File was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
it('moves a file from a sub-shard of a sharded directory to a sharded directory', async () => {
const shardedDirPath = await createShardedDirectory(ipfs)
const otherShardedDirPath = await createShardedDirectory(ipfs)
const file = 'file-1a.txt'
const filePath = `${shardedDirPath}/${file}`
const finalFilePath = `${otherShardedDirPath}/${file}`
await ipfs.files.write(filePath, Uint8Array.from([0, 1, 2, 3, 4]), {
create: true
})
await ipfs.files.mv(filePath, otherShardedDirPath)
await expect(isShardAtPath(shardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(shardedDirPath)).type).to.equal('directory')
expect((await ipfs.files.stat(finalFilePath)).type).to.equal('file')
await expect(isShardAtPath(otherShardedDirPath, ipfs)).to.eventually.be.true()
expect((await ipfs.files.stat(otherShardedDirPath)).type).to.equal('directory')
try {
await ipfs.files.stat(filePath)
throw new Error('File was not removed')
} catch (error) {
expect(error.message).to.contain('does not exist')
}
})
})
})
}