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

test: add tests for mfs stat with metadata #580

Merged
merged 1 commit into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
116 changes: 115 additions & 1 deletion src/files-mfs/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ module.exports = (common, options) => {

let ipfs

before(async () => { ipfs = (await common.spawn()).api })
before(async () => {
ipfs = (await common.spawn({
args: common.opts.type === 'go' ? [] : ['--enable-sharding-experiment']
})).api
})
before(async () => { await ipfs.add(fixtures.smallFile.data) })

after(() => common.clean())
Expand Down Expand Up @@ -50,6 +54,41 @@ module.exports = (common, options) => {
expect(stat.sizeLocal).to.be.undefined()
})

it('should stat file with mode', async function () {
const testDir = `/test-${hat()}`

await ipfs.files.mkdir(testDir, { parents: true })
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true })

const stat = await ipfs.files.stat(`${testDir}/b`)

expect(stat).to.include({
mode: 0o644
})
})

it('should stat file with mtime', async function () {
const testDir = `/test-${hat()}`

await ipfs.files.mkdir(testDir, { parents: true })
await ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), {
create: true,
mtime: {
secs: 5,
nsecs: 0
}
})

const stat = await ipfs.files.stat(`${testDir}/b`)

expect(stat).to.deep.include({
mtime: {
secs: 5,
nsecs: 0
}
})
})

it('should stat dir', async function () {
const testDir = `/test-${hat()}`

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

it('should stat dir with mode', async function () {
const testDir = `/test-${hat()}`

await ipfs.files.mkdir(testDir, { parents: true })
const stat = await ipfs.files.stat(testDir)

expect(stat).to.include({
mode: 0o755
})
})

it('should stat dir with mtime', async function () {
const testDir = `/test-${hat()}`

await ipfs.files.mkdir(testDir, {
parents: true,
mtime: {
secs: 5,
nsecs: 0
}
})

const stat = await ipfs.files.stat(testDir)

expect(stat).to.deep.include({
mtime: {
secs: 5,
nsecs: 0
}
})
})

it('should stat sharded dir with mode', async function () {
const testDir = `/test-${hat()}`

await ipfs.files.mkdir(testDir, { parents: true })
await ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), {
create: true,
shardSplitThreshold: 0
})

const stat = await ipfs.files.stat(testDir)

expect(stat).to.have.property('type', 'hamt-sharded-directory')
expect(stat).to.include({
mode: 0o755
})
})

it('should stat sharded dir with mtime', async function () {
const testDir = `/test-${hat()}`

await ipfs.files.mkdir(testDir, {
parents: true,
mtime: {
secs: 5,
nsecs: 0
}
})
await ipfs.files.write(`${testDir}/a`, Buffer.from('Hello, world!'), {
create: true,
shardSplitThreshold: 0
})

const stat = await ipfs.files.stat(testDir)

expect(stat).to.have.property('type', 'hamt-sharded-directory')
expect(stat).to.deep.include({
mtime: {
secs: 5,
nsecs: 0
}
})
})

// TODO enable this test when this feature gets released on go-ipfs
it.skip('should stat withLocal file', async function () {
const stat = await ipfs.files.stat('/test/b', { withLocal: true })
Expand Down
2 changes: 1 addition & 1 deletion src/files-regular/add-pull-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports = (common, options) => {

const res = await pullToPromise.any(pull(pull.values(data), stream))
expect(res).to.have.property('length', 1)
expect(res[0]).to.deep.equal({ path: expectedCid, hash: expectedCid, size: 12 })
expect(res[0]).to.include({ path: expectedCid, hash: expectedCid, size: 12 })
})
})
}
7 changes: 5 additions & 2 deletions src/files-regular/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ module.exports = (common, options) => {
content: Buffer.from(content),
mode
})

expect(files).to.have.length(1)
expect(files).to.have.nested.property('[0].mode', expectedMode)

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

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

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

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

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

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