Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 0d87bc9

Browse files
authored
test: add tests for mfs stat with metadata (#580)
Also asserts that `.add` output has metadata
1 parent 3e84d1c commit 0d87bc9

File tree

3 files changed

+121
-4
lines changed

3 files changed

+121
-4
lines changed

src/files-mfs/stat.js

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ module.exports = (common, options) => {
1919

2020
let ipfs
2121

22-
before(async () => { ipfs = (await common.spawn()).api })
22+
before(async () => {
23+
ipfs = (await common.spawn({
24+
args: common.opts.type === 'go' ? [] : ['--enable-sharding-experiment']
25+
})).api
26+
})
2327
before(async () => { await ipfs.add(fixtures.smallFile.data) })
2428

2529
after(() => common.clean())
@@ -50,6 +54,41 @@ module.exports = (common, options) => {
5054
expect(stat.sizeLocal).to.be.undefined()
5155
})
5256

57+
it('should stat file with mode', async function () {
58+
const testDir = `/test-${hat()}`
59+
60+
await ipfs.files.mkdir(testDir, { parents: true })
61+
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true })
62+
63+
const stat = await ipfs.files.stat(`${testDir}/b`)
64+
65+
expect(stat).to.include({
66+
mode: 0o644
67+
})
68+
})
69+
70+
it('should stat file with mtime', async function () {
71+
const testDir = `/test-${hat()}`
72+
73+
await ipfs.files.mkdir(testDir, { parents: true })
74+
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), {
75+
create: true,
76+
mtime: {
77+
secs: 5,
78+
nsecs: 0
79+
}
80+
})
81+
82+
const stat = await ipfs.files.stat(`${testDir}/b`)
83+
84+
expect(stat).to.deep.include({
85+
mtime: {
86+
secs: 5,
87+
nsecs: 0
88+
}
89+
})
90+
})
91+
5392
it('should stat dir', async function () {
5493
const testDir = `/test-${hat()}`
5594

@@ -68,6 +107,81 @@ module.exports = (common, options) => {
68107
expect(stat.sizeLocal).to.be.undefined()
69108
})
70109

110+
it('should stat dir with mode', async function () {
111+
const testDir = `/test-${hat()}`
112+
113+
await ipfs.files.mkdir(testDir, { parents: true })
114+
const stat = await ipfs.files.stat(testDir)
115+
116+
expect(stat).to.include({
117+
mode: 0o755
118+
})
119+
})
120+
121+
it('should stat dir with mtime', async function () {
122+
const testDir = `/test-${hat()}`
123+
124+
await ipfs.files.mkdir(testDir, {
125+
parents: true,
126+
mtime: {
127+
secs: 5,
128+
nsecs: 0
129+
}
130+
})
131+
132+
const stat = await ipfs.files.stat(testDir)
133+
134+
expect(stat).to.deep.include({
135+
mtime: {
136+
secs: 5,
137+
nsecs: 0
138+
}
139+
})
140+
})
141+
142+
it('should stat sharded dir with mode', async function () {
143+
const testDir = `/test-${hat()}`
144+
145+
await ipfs.files.mkdir(testDir, { parents: true })
146+
await ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), {
147+
create: true,
148+
shardSplitThreshold: 0
149+
})
150+
151+
const stat = await ipfs.files.stat(testDir)
152+
153+
expect(stat).to.have.property('type', 'hamt-sharded-directory')
154+
expect(stat).to.include({
155+
mode: 0o755
156+
})
157+
})
158+
159+
it('should stat sharded dir with mtime', async function () {
160+
const testDir = `/test-${hat()}`
161+
162+
await ipfs.files.mkdir(testDir, {
163+
parents: true,
164+
mtime: {
165+
secs: 5,
166+
nsecs: 0
167+
}
168+
})
169+
await ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), {
170+
create: true,
171+
shardSplitThreshold: 0
172+
})
173+
174+
const stat = await ipfs.files.stat(testDir)
175+
176+
expect(stat).to.have.property('type', 'hamt-sharded-directory')
177+
expect(stat).to.deep.include({
178+
mtime: {
179+
secs: 5,
180+
nsecs: 0
181+
}
182+
})
183+
})
184+
71185
// TODO enable this test when this feature gets released on go-ipfs
72186
it.skip('should stat withLocal file', async function () {
73187
const stat = await ipfs.files.stat('/test/b', { withLocal: true })

src/files-regular/add-pull-stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports = (common, options) => {
5959

6060
const res = await pullToPromise.any(pull(pull.values(data), stream))
6161
expect(res).to.have.property('length', 1)
62-
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
62+
expect(res[0]).to.include({ path: expectedCid, hash: expectedCid, size: 12 })
6363
})
6464
})
6565
}

src/files-regular/add.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module.exports = (common, options) => {
2828
content: Buffer.from(content),
2929
mode
3030
})
31+
3132
expect(files).to.have.length(1)
33+
expect(files).to.have.nested.property('[0].mode', expectedMode)
3234

3335
const stats = await ipfs.files.stat(`/ipfs/${files[0].hash}`)
3436
expect(stats).to.have.property('mode', expectedMode)
@@ -41,6 +43,7 @@ module.exports = (common, options) => {
4143
mtime
4244
})
4345
expect(files).to.have.length(1)
46+
expect(files).to.have.deep.nested.property('[0].mtime', expectedMtime)
4447

4548
const stats = await ipfs.files.stat(`/ipfs/${files[0].hash}`)
4649
expect(stats).to.have.deep.property('mtime', expectedMtime)
@@ -197,15 +200,15 @@ module.exports = (common, options) => {
197200

198201
const res = await ipfs.add(pull.values([Buffer.from('test')]))
199202
expect(res).to.have.length(1)
200-
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
203+
expect(res[0]).to.include({ path: expectedCid, hash: expectedCid, size: 12 })
201204
})
202205

203206
it('should add array of objects with pull stream content', async () => {
204207
const expectedCid = 'QmRf22bZar3WKmojipms22PkXH1MZGmvsqzQtuSvQE3uhm'
205208

206209
const res = await ipfs.add([{ content: pull.values([Buffer.from('test')]) }])
207210
expect(res).to.have.length(1)
208-
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
211+
expect(res[0]).to.include({ path: expectedCid, hash: expectedCid, size: 12 })
209212
})
210213

211214
it('should add a nested directory as array of tupples', async function () {

0 commit comments

Comments
 (0)