This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathls.js
130 lines (110 loc) · 3.74 KB
/
ls.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
/* eslint-env mocha */
'use strict'
const series = require('async/series')
const hat = require('hat')
const { fixtures } = require('../files-regular/utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')
module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()
describe('.files.ls', function () {
this.timeout(40 * 1000)
let ipfs
before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)
common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})
after((done) => common.teardown(done))
it('should not ls not found file/dir, expect error', (done) => {
const testDir = `/test-${hat()}`
ipfs.files.ls(`${testDir}/404`, (err, info) => {
expect(err).to.exist()
expect(info).to.not.exist()
done()
})
})
it('should ls directory', (done) => {
const testDir = `/test-${hat()}`
series([
(cb) => ipfs.files.mkdir(`${testDir}/lv1`, { parents: true }, cb),
(cb) => ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true }, cb)
], (err) => {
expect(err).to.not.exist()
ipfs.files.ls(testDir, (err, info) => {
expect(err).to.not.exist()
expect(info.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
{ name: 'b', type: 0, size: 0, hash: '' },
{ name: 'lv1', type: 0, size: 0, hash: '' }
])
done()
})
})
})
it('should ls directory with long option', (done) => {
const testDir = `/test-${hat()}`
series([
(cb) => ipfs.files.mkdir(`${testDir}/lv1`, { parents: true }, cb),
(cb) => ipfs.files.write(`${testDir}/b`, Buffer.from('Hello, world!'), { create: true }, cb)
], (err) => {
expect(err).to.not.exist()
ipfs.files.ls(testDir, { long: true }, (err, info) => {
expect(err).to.not.exist()
expect(info.sort((a, b) => a.name.localeCompare(b.name))).to.eql([
{
name: 'b',
type: 0,
size: 13,
hash: 'QmcZojhwragQr5qhTeFAmELik623Z21e3jBTpJXoQ9si1T'
},
{
name: 'lv1',
type: 1,
size: 0,
hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
}
])
done()
})
})
})
it('should ls from outside of mfs', async () => {
const testFileName = hat()
const [{
hash
}] = await ipfs.add({ path: `/test/${testFileName}`, content: fixtures.smallFile.data })
const listing = await ipfs.files.ls('/ipfs/' + hash)
expect(listing).to.have.length(1)
expect(listing[0].name).to.equal(hash)
})
it('should list an empty directory', async () => {
const testDir = `/test-${hat()}`
await ipfs.files.mkdir(testDir)
const contents = await ipfs.files.ls(testDir)
expect(contents).to.be.an('array').and.to.be.empty()
})
it('should list an file directly', async () => {
const fileName = `single-file-${hat()}.txt`
const filePath = `/${fileName}`
await ipfs.files.write(filePath, Buffer.from('Hello world'), {
create: true
})
const contents = await ipfs.files.ls(filePath)
expect(contents).to.be.an('array').and.have.lengthOf(1).and.to.deep.equal([{
hash: '',
name: fileName,
size: 0,
type: 0
}])
})
})
}