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

Commit 60c3c12

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 <[email protected]>
1 parent 332cefb commit 60c3c12

File tree

2 files changed

+88
-6
lines changed

2 files changed

+88
-6
lines changed

js/src/pin/ls.js

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ module.exports = (createCommon, options) => {
3131

3232
function populate () {
3333
parallel([
34+
// two files wrapped in a directory, root CID pinned recursively
35+
(cb) => {
36+
const dir = fixtures.directory.files.map((file) => ({path: file.path, content: file.data}))
37+
ipfs.files.add(dir, { pin: false }, (err, res) => {
38+
if (err) return cb(err)
39+
ipfs.pin.add(fixtures.directory.cid, { recursive: true }, cb)
40+
})
41+
},
42+
// a file (CID pinned recursively)
3443
(cb) => {
3544
ipfs.files.add(fixtures.files[0].data, { pin: false }, (err, res) => {
3645
if (err) return cb(err)
3746
ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb)
3847
})
3948
},
49+
// a single CID (pinned directly)
4050
(cb) => {
4151
ipfs.files.add(fixtures.files[1].data, { pin: false }, (err, res) => {
4252
if (err) return cb(err)
@@ -50,21 +60,24 @@ module.exports = (createCommon, options) => {
5060
after((done) => common.teardown(done))
5161

5262
// 1st, because ipfs.files.add pins automatically
53-
it('should list recursive pins', (done) => {
63+
it('should list all recursive pins', (done) => {
5464
ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => {
5565
expect(err).to.not.exist()
5666
expect(pinset).to.deep.include({
5767
type: 'recursive',
5868
hash: fixtures.files[0].cid
5969
})
70+
expect(pinset).to.deep.include({
71+
type: 'recursive',
72+
hash: fixtures.directory.cid
73+
})
6074
done()
6175
})
6276
})
6377

64-
it('should list indirect pins', (done) => {
78+
it('should list all indirect pins', (done) => {
6579
ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => {
6680
expect(err).to.not.exist()
67-
// because the pinned files have no links
6881
expect(pinset).to.not.deep.include({
6982
type: 'recursive',
7083
hash: fixtures.files[0].cid
@@ -73,14 +86,24 @@ module.exports = (createCommon, options) => {
7386
type: 'direct',
7487
hash: fixtures.files[1].cid
7588
})
89+
expect(pinset).to.not.deep.include({
90+
type: 'recursive',
91+
hash: fixtures.directory.cid
92+
})
7693
done()
7794
})
7895
})
7996

80-
it('should list pins', (done) => {
97+
it('should list all types of pins', (done) => {
8198
ipfs.pin.ls((err, pinset) => {
8299
expect(err).to.not.exist()
83100
expect(pinset).to.not.be.empty()
101+
expect(pinset).to.have.lengthOf(15)
102+
// check the three "roots"
103+
expect(pinset).to.deep.include({
104+
type: 'recursive',
105+
hash: fixtures.directory.cid
106+
})
84107
expect(pinset).to.deep.include({
85108
type: 'recursive',
86109
hash: fixtures.files[0].cid
@@ -93,9 +116,16 @@ module.exports = (createCommon, options) => {
93116
})
94117
})
95118

96-
it('should list pins (promised)', () => {
119+
it('should list all types of pins (promised)', () => {
97120
return ipfs.pin.ls()
98121
.then((pinset) => {
122+
expect(pinset).to.not.be.empty()
123+
expect(pinset).to.have.lengthOf(15)
124+
// check our three "roots"
125+
expect(pinset).to.deep.include({
126+
type: 'recursive',
127+
hash: fixtures.directory.cid
128+
})
99129
expect(pinset).to.deep.include({
100130
type: 'recursive',
101131
hash: fixtures.files[0].cid
@@ -107,7 +137,7 @@ module.exports = (createCommon, options) => {
107137
})
108138
})
109139

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

js/src/pin/utils.js

Lines changed: 12 additions & 0 deletions
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('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
820
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'

0 commit comments

Comments
 (0)