This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathcp.js
134 lines (110 loc) · 4.12 KB
/
cp.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
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const all = require('it-all')
const concat = require('it-concat')
const { fixtures } = require('../utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
/** @typedef { import("ipfsd-ctl/src/factory") } Factory */
/**
* @param {Factory} common
* @param {Object} options
*/
module.exports = (common, options) => {
const describe = getDescribe(options)
const it = getIt(options)
describe('.files.cp', function () {
this.timeout(40 * 1000)
let ipfs
before(async () => { ipfs = (await common.spawn()).api })
after(() => common.clean())
it('should copy file, expect error', () => {
const testDir = `/test-${hat()}`
return expect(ipfs.files.cp(`${testDir}/c`, `${testDir}/b`)).to.eventually.be.rejected()
})
it('should copy file, expect no error', async () => {
const testDir = `/test-${hat()}`
await ipfs.files.mkdir(testDir, { parents: true })
await ipfs.files.write(`${testDir}/a`, Buffer.from('TEST'), { create: true })
await ipfs.files.cp(`${testDir}/a`, `${testDir}/b`)
})
it('should copy dir, expect error', () => {
const testDir = `/test-${hat()}`
return expect(ipfs.files.cp(`${testDir}/lv1/lv3`, `${testDir}/lv1/lv4`)).to.eventually.be.rejected()
})
it('should copy dir, expect no error', async () => {
const testDir = `/test-${hat()}`
await ipfs.files.mkdir(`${testDir}/lv1/lv2`, { parents: true })
await ipfs.files.cp(`${testDir}/lv1/lv2`, `${testDir}/lv1/lv3`)
})
it('should copy from outside of mfs', async () => {
const [{ cid }] = await all(ipfs.add(fixtures.smallFile.data))
const testFilePath = `/${hat()}`
await ipfs.files.cp(`/ipfs/${cid}`, testFilePath)
const testFileData = await concat(ipfs.files.read(testFilePath))
expect(testFileData.slice()).to.eql(fixtures.smallFile.data)
})
it('should respect metadata when copying files', async function () {
const testSrcPath = `/test-${hat()}`
const testDestPath = `/test-${hat()}`
const mode = parseInt('0321', 8)
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
const expectedMtime = {
secs: seconds,
nsecs: (mtime - (seconds * 1000)) * 1000
}
await ipfs.files.write(testSrcPath, Buffer.from('TEST'), {
create: true,
mode,
mtime
})
await ipfs.files.cp(testSrcPath, testDestPath)
const stats = await ipfs.files.stat(testDestPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
expect(stats).to.have.property('mode', mode)
})
it('should respect metadata when copying directories', async function () {
const testSrcPath = `/test-${hat()}`
const testDestPath = `/test-${hat()}`
const mode = parseInt('0321', 8)
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
const expectedMtime = {
secs: seconds,
nsecs: (mtime - (seconds * 1000)) * 1000
}
await ipfs.files.mkdir(testSrcPath, {
mode,
mtime
})
await ipfs.files.cp(testSrcPath, testDestPath, {
recursive: true
})
const stats = await ipfs.files.stat(testDestPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
expect(stats).to.have.property('mode', mode)
})
it('should respect metadata when copying from outside of mfs', async function () {
const testDestPath = `/test-${hat()}`
const mode = parseInt('0321', 8)
const mtime = new Date()
const seconds = Math.floor(mtime.getTime() / 1000)
const expectedMtime = {
secs: seconds,
nsecs: (mtime - (seconds * 1000)) * 1000
}
const [{
cid
}] = await all(ipfs.add({
content: fixtures.smallFile.data,
mode,
mtime
}))
await ipfs.files.cp(`/ipfs/${cid}`, testDestPath)
const stats = await ipfs.files.stat(testDestPath)
expect(stats).to.have.deep.property('mtime', expectedMtime)
expect(stats).to.have.property('mode', mode)
})
})
}