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

Commit 1a42903

Browse files
committed
feat(pin): tests
1 parent 59a45d0 commit 1a42903

File tree

5 files changed

+250
-74
lines changed

5 files changed

+250
-74
lines changed

API/pin/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Pin API
2+
=======
3+
4+
#### `add`
5+
6+
> Adds an IPFS object to the pinset and also stores it to the IPFS repo. pinset is the set of hashes currently pinned (not gc'able).
7+
8+
##### `Go` **WIP**
9+
10+
##### `JavaScript` - ipfs.pin.add(hash, [options, callback])
11+
12+
Where:
13+
14+
- `hash` is an IPFS multihash.
15+
- `options` is an object that can contain the following keys
16+
- 'recursive' - Recursively pin the object linked. Type: bool. Default: `false`
17+
18+
`callback` must follow `function (err, res) {}` signature, where `err` is an error if the operation was not successful. `res` is an array of objects that represent the files that were pinned. Example:
19+
20+
```JavaScript
21+
{
22+
hash: 'QmHash'
23+
}
24+
```
25+
26+
If no `callback` is passed, a promise is returned.
27+
28+
Example:
29+
30+
```JavaScript
31+
ipfs.pin.add(hash, function (err) {})
32+
```
33+
34+
#### `ls`
35+
36+
> List all the objects pinned to local storage or under a specific hash.
37+
38+
##### `Go` **WIP**
39+
40+
##### `JavaScript` - ipfs.pin.ls([hash, options, callback])
41+
42+
Where:
43+
44+
- `hash` is an IPFS multihash.
45+
- `options` is an object that can contain the following keys:
46+
- 'type' - Return also the type of pin (direct, indirect or recursive)
47+
48+
`callback` must follow `function (err, pinset) {}` signature, where `err` is an error if the operation was not successful. `pinset` is an array of objects with keys `hash` and `type`.
49+
50+
If no `callback` is passed, a promise is returned.
51+
52+
Example:
53+
54+
```JavaScript
55+
ipfs.pin.ls(function (err, pinset) {
56+
if (err) {
57+
throw err
58+
}
59+
console.log(pinset)
60+
})
61+
```
62+
63+
64+
#### `rm`
65+
66+
> Remove a hash from the pinset
67+
68+
##### `Go` **WIP**
69+
70+
##### `JavaScript` - ipfs.pin.rm(hash, [options, callback])
71+
72+
Where:
73+
- `hash` is a multihash.
74+
- `options` is an object that can contain the following keys
75+
- 'recursive' - Recursively unpin the object linked. Type: bool. Default: `false`
76+
77+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
78+
79+
If no `callback` is passed, a promise is returned.
80+
81+
Example:
82+
83+
```JavaScript
84+
ipfs.pin.rm(hash, function (err, pinset) {
85+
if (err) {
86+
throw err
87+
}
88+
console.log(pinset) prints the hashes that were unpinned
89+
})
90+
```

API/pinning-api/README.md

-73
This file was deleted.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
"greenkeeperio-bot <[email protected]>",
4848
"nginnever <[email protected]>"
4949
]
50-
}
50+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
exports.object = require('./object')
44
exports.files = require('./files')
55
exports.config = require('./config')
6+
exports.pin = require('./pin')

src/pin.js

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

0 commit comments

Comments
 (0)