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

Commit 4730c36

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

File tree

5 files changed

+259
-75
lines changed

5 files changed

+259
-75
lines changed

API/pin/README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
path: 'some-path'
24+
}
25+
```
26+
27+
If no `callback` is passed, a promise is returned.
28+
29+
Example:
30+
31+
```JavaScript
32+
ipfs.pin.add(hash, function (err) {})
33+
```
34+
35+
#### `ls`
36+
37+
> List all the objects pinned to local storage or under a specific hash.
38+
39+
##### `Go` **WIP**
40+
41+
##### `JavaScript` - ipfs.pin.ls([hash, options, callback])
42+
43+
Where:
44+
45+
- `hash` is an IPFS multihash.
46+
- `options` is an object that can contain the following keys:
47+
- 'type' - Return also the type of pin (direct, indirect or recursive)
48+
49+
`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`.
50+
51+
If no `callback` is passed, a promise is returned.
52+
53+
Example:
54+
55+
```JavaScript
56+
ipfs.pin.ls(function (err, pinset) {
57+
if (err) {
58+
throw err
59+
}
60+
console.log(pinset)
61+
})
62+
```
63+
64+
65+
#### `rm`
66+
67+
> Remove a hash from the pinset
68+
69+
##### `Go` **WIP**
70+
71+
##### `JavaScript` - ipfs.pin.rm(hash, [options, callback])
72+
73+
Where:
74+
- `hash` is a multihash.
75+
- `options` is an object that can contain the following keys
76+
- `recursive` - Recursively unpin the object linked. Type: bool. Default: 'false'
77+
78+
`callback` must follow `function (err) {}` signature, where `err` is an error if the operation was not successful.
79+
80+
If no `callback` is passed, a promise is returned.
81+
82+
Example:
83+
84+
```JavaScript
85+
ipfs.pin.rm(hash, function (err, pinset) {
86+
if (err) {
87+
throw err
88+
}
89+
console.log(pinset) prints the hashes that were unpinned
90+
})
91+
```

API/pinning-api/README.md

Lines changed: 0 additions & 73 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "interface-ipfs-core",
33
"version": "0.8.0",
44
"description": "A test suite and interface you can use to implement a IPFS core interface.",
5-
"main": "lib/index.js",
5+
"main": "src/index.js",
66
"jsnext:main": "src/index.js",
77
"scripts": {
88
"test": "exit(0)",
@@ -47,4 +47,4 @@
4747
"greenkeeperio-bot <support@greenkeeper.io>",
4848
"nginnever <ginneversource@gmail.com>"
4949
]
50-
}
50+
}

src/index.js

Lines changed: 1 addition & 0 deletions
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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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, res) => {
55+
expect(err).to.not.exist
56+
expect(res).to.exist
57+
ipfs.pin.ls({ type: 'direct' }, (err, res) => {
58+
expect(err).to.not.exist
59+
expect(res).to.exist
60+
expect(res.Keys).to.be.empty
61+
done()
62+
})
63+
})
64+
})
65+
66+
it('.add', (done) => {
67+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
68+
69+
ipfs.pin.add(hash, { recursive: false }, (err, res) => {
70+
expect(err).to.not.exist
71+
expect(res.Pins[0]).to.be.equal(hash)
72+
done()
73+
})
74+
})
75+
76+
it('.ls', (done) => {
77+
ipfs.pin.ls((err, res) => {
78+
expect(err).to.not.exist
79+
expect(res).to.exist
80+
expect(Object.keys(res.Keys).length > 0).to.equal(true)
81+
done()
82+
})
83+
})
84+
85+
it('.ls type direct', (done) => {
86+
ipfs.pin.ls({ type: 'direct' }, (err, res) => {
87+
expect(err).to.not.exist
88+
expect(res).to.exist
89+
expect(Object.keys(res.Keys).length > 0).to.equal(true)
90+
done()
91+
})
92+
})
93+
94+
it('.ls type indirect', (done) => {
95+
ipfs.pin.ls({ type: 'indirect' }, (err, res) => {
96+
expect(err).to.not.exist
97+
expect(res).to.exist
98+
expect(Object.keys(res.Keys).length > 0).to.equal(true)
99+
done()
100+
})
101+
})
102+
103+
it('.ls type recursive', (done) => {
104+
ipfs.pin.ls({ type: 'recursive' }, (err, res) => {
105+
expect(err).to.not.exist
106+
expect(res).to.exist
107+
expect(Object.keys(res.Keys).length > 0).to.equal(true)
108+
done()
109+
})
110+
})
111+
112+
it('.ls for a specific hash', (done) => {
113+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
114+
115+
ipfs.pin.ls(hash, (err, res) => {
116+
expect(err).to.not.exist
117+
expect(res).to.exist
118+
done()
119+
})
120+
})
121+
})
122+
123+
describe('promise API', () => {
124+
it('.add', () => {
125+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
126+
127+
return ipfs.pin.add(hash, { recursive: false })
128+
.then((res) => {
129+
expect(res.Pins[0]).to.be.equal(hash)
130+
})
131+
})
132+
133+
it('.ls', () => {
134+
return ipfs.pin.ls()
135+
.then((res) => {
136+
expect(res).to.exist
137+
expect(Object.keys(res.Keys).length > 0).to.equal(true)
138+
})
139+
})
140+
141+
it('.ls hash', () => {
142+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
143+
144+
return ipfs.pin.ls(hash)
145+
.then((res) => {
146+
expect(res).to.exist
147+
})
148+
})
149+
150+
it('.rm', () => {
151+
const hash = 'Qma4hjFTnCasJ8PVp3mZbZK5g2vGDT4LByLJ7m8ciyRFZP'
152+
153+
return ipfs.pin.rm(hash, { recursive: false })
154+
.then((res) => {
155+
expect(res).to.exist
156+
return ipfs.pin.ls({ type: 'direct' })
157+
})
158+
.then((res) => {
159+
expect(res).to.exist
160+
expect(res.Keys).to.be.empty
161+
})
162+
})
163+
})
164+
})
165+
}

0 commit comments

Comments
 (0)