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

Commit e8955b1

Browse files
committed
fix: pin.ls ignored opts when hash was present
Adds tests to guard against issue described in ipfs-inactive/js-ipfs-http-client#875 License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
1 parent 666ccba commit e8955b1

File tree

2 files changed

+97
-10
lines changed

2 files changed

+97
-10
lines changed

src/pin/ls.js

+85-10
Original file line numberDiff line numberDiff line change
@@ -31,32 +31,50 @@ module.exports = (createCommon, options) => {
3131

3232
function populate () {
3333
series([
34-
cb => ipfs.add(fixtures.files[0].data, { pin: false }, cb),
35-
cb => ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb),
36-
cb => ipfs.add(fixtures.files[1].data, { pin: false }, cb),
37-
cb => ipfs.pin.add(fixtures.files[1].cid, { recursive: false }, cb)
34+
cb => { // two files wrapped in a directory, root CID pinned recursively
35+
const dir = fixtures.directory.files.map((file) => ({ path: file.path, content: file.data }))
36+
ipfs.files.add(dir, { pin: false }, (err, res) => {
37+
if (err) return cb(err)
38+
ipfs.pin.add(fixtures.directory.cid, { recursive: true }, cb)
39+
})
40+
},
41+
cb => { // a file (CID pinned recursively)
42+
ipfs.files.add(fixtures.files[0].data, { pin: false }, (err, res) => {
43+
if (err) return cb(err)
44+
ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb)
45+
})
46+
},
47+
cb => { // a single CID (pinned directly)
48+
ipfs.files.add(fixtures.files[1].data, { pin: false }, (err, res) => {
49+
if (err) return cb(err)
50+
ipfs.pin.add(fixtures.files[1].cid, { recursive: false }, cb)
51+
})
52+
}
3853
], done)
3954
}
4055
})
4156

4257
after((done) => common.teardown(done))
4358

4459
// 1st, because ipfs.add pins automatically
45-
it('should list recursive pins', (done) => {
60+
it('should list all recursive pins', (done) => {
4661
ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => {
4762
expect(err).to.not.exist()
4863
expect(pinset).to.deep.include({
4964
type: 'recursive',
5065
hash: fixtures.files[0].cid
5166
})
67+
expect(pinset).to.deep.include({
68+
type: 'recursive',
69+
hash: fixtures.directory.cid
70+
})
5271
done()
5372
})
5473
})
5574

56-
it('should list indirect pins', (done) => {
75+
it('should list all indirect pins', (done) => {
5776
ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => {
5877
expect(err).to.not.exist()
59-
// because the pinned files have no links
6078
expect(pinset).to.not.deep.include({
6179
type: 'recursive',
6280
hash: fixtures.files[0].cid
@@ -65,14 +83,24 @@ module.exports = (createCommon, options) => {
6583
type: 'direct',
6684
hash: fixtures.files[1].cid
6785
})
86+
expect(pinset).to.not.deep.include({
87+
type: 'recursive',
88+
hash: fixtures.directory.cid
89+
})
6890
done()
6991
})
7092
})
7193

72-
it('should list pins', (done) => {
94+
it('should list all types of pins', (done) => {
7395
ipfs.pin.ls((err, pinset) => {
7496
expect(err).to.not.exist()
7597
expect(pinset).to.not.be.empty()
98+
expect(pinset).to.have.lengthOf(15)
99+
// check the three "roots"
100+
expect(pinset).to.deep.include({
101+
type: 'recursive',
102+
hash: fixtures.directory.cid
103+
})
76104
expect(pinset).to.deep.include({
77105
type: 'recursive',
78106
hash: fixtures.files[0].cid
@@ -85,9 +113,16 @@ module.exports = (createCommon, options) => {
85113
})
86114
})
87115

88-
it('should list pins (promised)', () => {
116+
it('should list all types of pins (promised)', () => {
89117
return ipfs.pin.ls()
90118
.then((pinset) => {
119+
expect(pinset).to.not.be.empty()
120+
expect(pinset).to.have.lengthOf(15)
121+
// check our three "roots"
122+
expect(pinset).to.deep.include({
123+
type: 'recursive',
124+
hash: fixtures.directory.cid
125+
})
91126
expect(pinset).to.deep.include({
92127
type: 'recursive',
93128
hash: fixtures.files[0].cid
@@ -99,7 +134,7 @@ module.exports = (createCommon, options) => {
99134
})
100135
})
101136

102-
it('should list direct pins', (done) => {
137+
it('should list all direct pins', (done) => {
103138
ipfs.pin.ls({ type: 'direct' }, (err, pinset) => {
104139
expect(err).to.not.exist()
105140
expect(pinset).to.deep.include({
@@ -130,5 +165,45 @@ module.exports = (createCommon, options) => {
130165
}])
131166
})
132167
})
168+
169+
it('should throw an error on missing direct pins for a specific path', (done) => {
170+
// alice.txt is an indirect pin, so lookup for direct one should throw an error
171+
ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/files/ipfs.txt`, { type: 'direct' }, (err, pinset) => {
172+
expect(pinset).to.not.exist()
173+
expect(err).to.not.be.empty()
174+
expect(err.message).to.be.equal(`path '/ipfs/${fixtures.directory.cid}/files/ipfs.txt' is not pinned`)
175+
done()
176+
})
177+
})
178+
179+
it('should throw an error on missing link for a specific path', (done) => {
180+
ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/I-DONT-EXIST.txt`, { type: 'direct' }, (err, pinset) => {
181+
expect(pinset).to.not.exist()
182+
expect(err).to.not.be.empty()
183+
expect(err.message).to.be.equal('no link by that name')
184+
done()
185+
})
186+
})
187+
188+
it('should list indirect pins for a specific path', (done) => {
189+
ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/files/ipfs.txt`, { type: 'indirect' }, (err, pinset) => {
190+
expect(err).to.not.exist()
191+
expect(pinset).to.deep.include({
192+
type: `indirect through ${fixtures.directory.cid}`,
193+
hash: fixtures.directory.files[1].cid
194+
})
195+
done()
196+
})
197+
})
198+
199+
it('should list recursive pins for a specific hash (promised)', () => {
200+
return ipfs.pin.ls(fixtures.files[0].cid, { type: 'recursive' })
201+
.then((pinset) => {
202+
expect(pinset).to.deep.equal([{
203+
type: 'recursive',
204+
hash: fixtures.files[0].cid
205+
}])
206+
})
207+
})
133208
})
134209
}

src/pin/utils.js

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
const loadFixture = require('aegir/fixtures')
44

55
exports.fixtures = Object.freeze({
6+
directory: Object.freeze({
7+
cid: 'QmVJV2VF9Qf7rJUFdimhpZDhkyyumM1i4CjjGcss5rqJPa',
8+
files: Object.freeze([Object.freeze({
9+
path: 'test-folder/ipfs-add.js',
10+
data: loadFixture('js/test/fixtures/test-folder/ipfs-add.js', 'interface-ipfs-core'),
11+
cid: 'QmU7wetVaAqc3Meurif9hcYBHGvQmL5QdpPJYBoZizyTNL'
12+
}), Object.freeze({
13+
path: 'test-folder/files/ipfs.txt',
14+
data: loadFixture('js/test/fixtures/test-folder/files/ipfs.txt', 'interface-ipfs-core'),
15+
cid: 'QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w'
16+
})])
17+
}),
618
files: Object.freeze([Object.freeze({
719
data: loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core'),
820
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

0 commit comments

Comments
 (0)