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

Commit

Permalink
fix: pin.ls ignored opts when hash was present (#375)
Browse files Browse the repository at this point in the history
This PR adds more detailed tests for `pin.ls`, including one that guards against issue described in ipfs-inactive/js-ipfs-http-client#875

refs ipfs/js-ipfs#2228

License: MIT
Signed-off-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
lidel authored and alanshaw committed Jul 16, 2019
1 parent c649e18 commit be72ed6
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 8 deletions.
107 changes: 99 additions & 8 deletions src/pin/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,17 @@ module.exports = (createCommon, options) => {

function populate () {
series([
cb => ipfs.add(fixtures.files[0].data, { pin: false }, cb),
// two files wrapped in directories, only root CID pinned recursively
cb => {
const dir = fixtures.directory.files.map((file) => ({ path: file.path, content: file.data }))
ipfs.add(dir, { pin: false, cidVersion: 0 }, cb)
},
cb => ipfs.pin.add(fixtures.directory.cid, { recursive: true }, cb),
// a file (CID pinned recursively)
cb => ipfs.add(fixtures.files[0].data, { pin: false, cidVersion: 0 }, cb),
cb => ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb),
cb => ipfs.add(fixtures.files[1].data, { pin: false }, cb),
// a single CID (pinned directly)
cb => ipfs.add(fixtures.files[1].data, { pin: false, cidVersion: 0 }, cb),
cb => ipfs.pin.add(fixtures.files[1].cid, { recursive: false }, cb)
], done)
}
Expand All @@ -42,21 +50,24 @@ module.exports = (createCommon, options) => {
after((done) => common.teardown(done))

// 1st, because ipfs.add pins automatically
it('should list recursive pins', (done) => {
it('should list all recursive pins', (done) => {
ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => {
expect(err).to.not.exist()
expect(pinset).to.deep.include({
type: 'recursive',
hash: fixtures.files[0].cid
})
expect(pinset).to.deep.include({
type: 'recursive',
hash: fixtures.directory.cid
})
done()
})
})

it('should list indirect pins', (done) => {
it('should list all indirect pins', (done) => {
ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => {
expect(err).to.not.exist()
// because the pinned files have no links
expect(pinset).to.not.deep.include({
type: 'recursive',
hash: fixtures.files[0].cid
Expand All @@ -65,14 +76,31 @@ module.exports = (createCommon, options) => {
type: 'direct',
hash: fixtures.files[1].cid
})
expect(pinset).to.not.deep.include({
type: 'recursive',
hash: fixtures.directory.cid
})
expect(pinset).to.deep.include({
type: 'indirect',
hash: fixtures.directory.files[0].cid
})
expect(pinset).to.deep.include({
type: 'indirect',
hash: fixtures.directory.files[1].cid
})
done()
})
})

it('should list pins', (done) => {
it('should list all types of pins', (done) => {
ipfs.pin.ls((err, pinset) => {
expect(err).to.not.exist()
expect(pinset).to.not.be.empty()
// check the three "roots"
expect(pinset).to.deep.include({
type: 'recursive',
hash: fixtures.directory.cid
})
expect(pinset).to.deep.include({
type: 'recursive',
hash: fixtures.files[0].cid
Expand All @@ -81,13 +109,27 @@ module.exports = (createCommon, options) => {
type: 'direct',
hash: fixtures.files[1].cid
})
expect(pinset).to.deep.include({
type: 'indirect',
hash: fixtures.directory.files[0].cid
})
expect(pinset).to.deep.include({
type: 'indirect',
hash: fixtures.directory.files[1].cid
})
done()
})
})

it('should list pins (promised)', () => {
it('should list all types of pins (promised)', () => {
return ipfs.pin.ls()
.then((pinset) => {
expect(pinset).to.not.be.empty()
// check our three "roots"
expect(pinset).to.deep.include({
type: 'recursive',
hash: fixtures.directory.cid
})
expect(pinset).to.deep.include({
type: 'recursive',
hash: fixtures.files[0].cid
Expand All @@ -96,12 +138,21 @@ module.exports = (createCommon, options) => {
type: 'direct',
hash: fixtures.files[1].cid
})
expect(pinset).to.deep.include({
type: 'indirect',
hash: fixtures.directory.files[0].cid
})
expect(pinset).to.deep.include({
type: 'indirect',
hash: fixtures.directory.files[1].cid
})
})
})

it('should list direct pins', (done) => {
it('should list all direct pins', (done) => {
ipfs.pin.ls({ type: 'direct' }, (err, pinset) => {
expect(err).to.not.exist()
expect(pinset).to.have.lengthOf(1)
expect(pinset).to.deep.include({
type: 'direct',
hash: fixtures.files[1].cid
Expand Down Expand Up @@ -130,5 +181,45 @@ module.exports = (createCommon, options) => {
}])
})
})

it('should throw an error on missing direct pins for existing path', (done) => {
// ipfs.txt is an indirect pin, so lookup for direct one should throw an error
ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/files/ipfs.txt`, { type: 'direct' }, (err, pinset) => {
expect(err).to.exist()
expect(pinset).to.not.exist()
expect(err.message).to.be.equal(`path '/ipfs/${fixtures.directory.cid}/files/ipfs.txt' is not pinned`)
done()
})
})

it('should throw an error on missing link for a specific path', (done) => {
ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/I-DONT-EXIST.txt`, { type: 'direct' }, (err, pinset) => {
expect(err).to.exist()
expect(pinset).to.not.exist()
expect(err.message).to.be.equal(`no link named "I-DONT-EXIST.txt" under ${fixtures.directory.cid}`)
done()
})
})

it('should list indirect pins for a specific path', (done) => {
ipfs.pin.ls(`/ipfs/${fixtures.directory.cid}/files/ipfs.txt`, { type: 'indirect' }, (err, pinset) => {
expect(err).to.not.exist()
expect(pinset).to.deep.include({
type: `indirect through ${fixtures.directory.cid}`,
hash: fixtures.directory.files[1].cid
})
done()
})
})

it('should list recursive pins for a specific hash (promised)', () => {
return ipfs.pin.ls(fixtures.files[0].cid, { type: 'recursive' })
.then((pinset) => {
expect(pinset).to.deep.equal([{
type: 'recursive',
hash: fixtures.files[0].cid
}])
})
})
})
}
13 changes: 13 additions & 0 deletions src/pin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
const loadFixture = require('aegir/fixtures')

exports.fixtures = Object.freeze({
// NOTE: files under 'directory' need to be different than standalone ones in 'files'
directory: Object.freeze({
cid: 'QmY8KdYQSYKFU5hM7F5ioZ5yYSgV5VZ1kDEdqfRL3rFgcd',
files: Object.freeze([Object.freeze({
path: 'test-folder/ipfs-add.js',
data: loadFixture('test/fixtures/test-folder/ipfs-add.js', 'interface-ipfs-core'),
cid: 'QmbKtKBrmeRHjNCwR4zAfCJdMVu6dgmwk9M9AE9pUM9RgG'
}), Object.freeze({
path: 'test-folder/files/ipfs.txt',
data: loadFixture('test/fixtures/test-folder/files/ipfs.txt', 'interface-ipfs-core'),
cid: 'QmdFyxZXsFiP4csgfM5uPu99AvFiKH62CSPDw5TP92nr7w'
})])
}),
files: Object.freeze([Object.freeze({
data: loadFixture('test/fixtures/testfile.txt', 'interface-ipfs-core'),
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
Expand Down

0 comments on commit be72ed6

Please sign in to comment.