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

Commit 33d2312

Browse files
committed
feat(pin): modularises pin
License: MIT Signed-off-by: Alan Shaw <alan@tableflip.io>
1 parent 5b0b144 commit 33d2312

File tree

5 files changed

+324
-0
lines changed

5 files changed

+324
-0
lines changed

js/src/pin/add.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const each = require('async/each')
7+
const { fixtures } = require('./utils')
8+
const { getDescribe, getIt } = require('../utils/mocha')
9+
10+
const expect = chai.expect
11+
chai.use(dirtyChai)
12+
13+
module.exports = (createCommon, options) => {
14+
const describe = getDescribe(options)
15+
const it = getIt(options)
16+
const common = createCommon()
17+
18+
describe('.pin.add', function () {
19+
this.timeout(50 * 1000)
20+
21+
let ipfs
22+
23+
before(function (done) {
24+
// CI takes longer to instantiate the daemon, so we need to increase the
25+
// timeout for the before step
26+
this.timeout(60 * 1000)
27+
28+
common.setup((err, factory) => {
29+
expect(err).to.not.exist()
30+
factory.spawnNode((err, node) => {
31+
expect(err).to.not.exist()
32+
ipfs = node
33+
populate()
34+
})
35+
})
36+
37+
function populate () {
38+
each(fixtures.files, (file, cb) => {
39+
ipfs.files.add(file.data, { pin: false }, cb)
40+
}, done)
41+
}
42+
})
43+
44+
after((done) => common.teardown(done))
45+
46+
it('should add a pin', (done) => {
47+
ipfs.pin.add(fixtures.files[0].cid, { recursive: false }, (err, pinset) => {
48+
expect(err).to.not.exist()
49+
expect(pinset).to.deep.include({
50+
hash: fixtures.files[0].cid
51+
})
52+
done()
53+
})
54+
})
55+
56+
it('should add a pin (promised)', () => {
57+
return ipfs.pin.add(fixtures.files[1].cid, { recursive: false })
58+
.then((pinset) => {
59+
expect(pinset).to.deep.include({
60+
hash: fixtures.files[1].cid
61+
})
62+
})
63+
})
64+
})
65+
}

js/src/pin/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
const { createSuite } = require('../utils/suite')
3+
4+
const tests = {
5+
ls: require('./ls'),
6+
rm: require('./rm'),
7+
add: require('./add')
8+
}
9+
10+
module.exports = createSuite(tests)

js/src/pin/ls.js

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const parallel = require('async/parallel')
7+
const { fixtures } = require('./utils')
8+
const { getDescribe, getIt } = require('../utils/mocha')
9+
10+
const expect = chai.expect
11+
chai.use(dirtyChai)
12+
13+
module.exports = (createCommon, options) => {
14+
const describe = getDescribe(options)
15+
const it = getIt(options)
16+
const common = createCommon()
17+
18+
describe('.pin.ls', function () {
19+
this.timeout(50 * 1000)
20+
21+
let ipfs
22+
23+
before(function (done) {
24+
// CI takes longer to instantiate the daemon, so we need to increase the
25+
// timeout for the before step
26+
this.timeout(60 * 1000)
27+
28+
common.setup((err, factory) => {
29+
expect(err).to.not.exist()
30+
factory.spawnNode((err, node) => {
31+
expect(err).to.not.exist()
32+
ipfs = node
33+
populate()
34+
})
35+
})
36+
37+
function populate () {
38+
parallel([
39+
(cb) => {
40+
ipfs.files.add(fixtures.files[0].data, { pin: false }, (err, res) => {
41+
if (err) return cb(err)
42+
ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb)
43+
})
44+
},
45+
(cb) => {
46+
ipfs.files.add(fixtures.files[1].data, { pin: false }, (err, res) => {
47+
if (err) return cb(err)
48+
ipfs.pin.add(fixtures.files[1].cid, { recursive: false }, cb)
49+
})
50+
}
51+
], done)
52+
}
53+
})
54+
55+
after((done) => common.teardown(done))
56+
57+
// 1st, because ipfs.files.add pins automatically
58+
it('should list recursive pins', (done) => {
59+
ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => {
60+
expect(err).to.not.exist()
61+
expect(pinset).to.deep.include({
62+
type: 'recursive',
63+
hash: fixtures.files[0].cid
64+
})
65+
done()
66+
})
67+
})
68+
69+
it('should list indirect pins', (done) => {
70+
ipfs.pin.ls({ type: 'indirect' }, (err, pinset) => {
71+
expect(err).to.not.exist()
72+
// because the pinned files have no links
73+
expect(pinset).to.not.deep.include({
74+
type: 'recursive',
75+
hash: fixtures.files[0].cid
76+
})
77+
expect(pinset).to.not.deep.include({
78+
type: 'direct',
79+
hash: fixtures.files[1].cid
80+
})
81+
done()
82+
})
83+
})
84+
85+
it('should list pins', (done) => {
86+
ipfs.pin.ls((err, pinset) => {
87+
expect(err).to.not.exist()
88+
expect(pinset).to.not.be.empty()
89+
expect(pinset).to.deep.include({
90+
type: 'recursive',
91+
hash: fixtures.files[0].cid
92+
})
93+
expect(pinset).to.deep.include({
94+
type: 'direct',
95+
hash: fixtures.files[1].cid
96+
})
97+
done()
98+
})
99+
})
100+
101+
it('should list pins (promised)', () => {
102+
return ipfs.pin.ls()
103+
.then((pinset) => {
104+
expect(pinset).to.deep.include({
105+
type: 'recursive',
106+
hash: fixtures.files[0].cid
107+
})
108+
expect(pinset).to.deep.include({
109+
type: 'direct',
110+
hash: fixtures.files[1].cid
111+
})
112+
})
113+
})
114+
115+
it('should list direct pins', (done) => {
116+
ipfs.pin.ls({ type: 'direct' }, (err, pinset) => {
117+
expect(err).to.not.exist()
118+
expect(pinset).to.deep.include({
119+
type: 'direct',
120+
hash: fixtures.files[1].cid
121+
})
122+
done()
123+
})
124+
})
125+
126+
it('should list pins for a specific hash', (done) => {
127+
ipfs.pin.ls(fixtures.files[0].cid, (err, pinset) => {
128+
expect(err).to.not.exist()
129+
expect(pinset).to.deep.equal([{
130+
type: 'recursive',
131+
hash: fixtures.files[0].cid
132+
}])
133+
done()
134+
})
135+
})
136+
137+
it('should list pins for a specific hash (promised)', () => {
138+
return ipfs.pin.ls(fixtures.files[0].cid)
139+
.then((pinset) => {
140+
expect(pinset).to.deep.equal([{
141+
type: 'recursive',
142+
hash: fixtures.files[0].cid
143+
}])
144+
})
145+
})
146+
})
147+
}

js/src/pin/rm.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
const dirtyChai = require('dirty-chai')
6+
const parallel = require('async/parallel')
7+
const { fixtures } = require('./utils')
8+
const { getDescribe, getIt } = require('../utils/mocha')
9+
10+
const expect = chai.expect
11+
chai.use(dirtyChai)
12+
13+
module.exports = (createCommon, options) => {
14+
const describe = getDescribe(options)
15+
const it = getIt(options)
16+
const common = createCommon()
17+
18+
describe('.pin', function () {
19+
this.timeout(50 * 1000)
20+
21+
let ipfs
22+
23+
before(function (done) {
24+
// CI takes longer to instantiate the daemon, so we need to increase the
25+
// timeout for the before step
26+
this.timeout(60 * 1000)
27+
28+
common.setup((err, factory) => {
29+
expect(err).to.not.exist()
30+
factory.spawnNode((err, node) => {
31+
expect(err).to.not.exist()
32+
ipfs = node
33+
populate()
34+
})
35+
})
36+
37+
function populate () {
38+
parallel([
39+
(cb) => {
40+
ipfs.files.add(fixtures.files[0].data, { pin: false }, (err, res) => {
41+
if (err) return cb(err)
42+
ipfs.pin.add(fixtures.files[0].cid, { recursive: true }, cb)
43+
})
44+
},
45+
(cb) => {
46+
ipfs.files.add(fixtures.files[1].data, { pin: false }, (err, res) => {
47+
if (err) return cb(err)
48+
ipfs.pin.add(fixtures.files[1].cid, { recursive: false }, cb)
49+
})
50+
}
51+
], done)
52+
}
53+
})
54+
55+
after((done) => common.teardown(done))
56+
57+
it('should remove a recursive pin', (done) => {
58+
ipfs.pin.rm(fixtures.files[0].cid, { recursive: true }, (err, pinset) => {
59+
expect(err).to.not.exist()
60+
expect(pinset).to.deep.equal([{
61+
hash: fixtures.files[0].cid
62+
}])
63+
ipfs.pin.ls({ type: 'recursive' }, (err, pinset) => {
64+
expect(err).to.not.exist()
65+
expect(pinset).to.not.deep.include({
66+
hash: fixtures.files[0].cid,
67+
type: 'recursive'
68+
})
69+
done()
70+
})
71+
})
72+
})
73+
74+
it('should remove a direct pin (promised)', () => {
75+
return ipfs.pin.rm(fixtures.files[1].cid, { recursive: false })
76+
.then((pinset) => {
77+
expect(pinset).to.deep.equal([{
78+
hash: fixtures.files[1].cid
79+
}])
80+
return ipfs.pin.ls({ type: 'direct' })
81+
})
82+
.then((pinset) => {
83+
expect(pinset).to.not.deep.include({
84+
hash: fixtures.files[1].cid
85+
})
86+
})
87+
})
88+
})
89+
}

js/src/pin/utils.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const loadFixture = require('aegir/fixtures')
4+
5+
exports.fixtures = Object.freeze({
6+
files: Object.freeze([Object.freeze({
7+
data: loadFixture('js/test/fixtures/testfile.txt', 'interface-ipfs-core'),
8+
cid: 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
9+
}), Object.freeze({
10+
data: loadFixture('js/test/fixtures/test-folder/files/hello.txt', 'interface-ipfs-core'),
11+
cid: 'QmY9cxiHqTFoWamkQVkpmmqzBrY3hCBEL2XNu3NtX74Fuu'
12+
})])
13+
})

0 commit comments

Comments
 (0)