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

Commit

Permalink
feat: refs endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc committed Apr 24, 2019
1 parent fa21abb commit ea98e4c
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/files-regular/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ const tests = {
getPullStream: require('./get-pull-stream'),
ls: require('./ls'),
lsReadableStream: require('./ls-readable-stream'),
lsPullStream: require('./ls-pull-stream')
lsPullStream: require('./ls-pull-stream'),
refs: require('./refs'),
refsPullStream: require('./refs-pull-stream')
}

module.exports = createSuite(tests)
53 changes: 53 additions & 0 deletions src/files-regular/refs-pull-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 6] */
'use strict'

const pull = require('pull-stream')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const { expectedResults, addTestFolder } = require('./refs')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.refsPullStream', function () {
this.timeout(40 * 1000)

let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('should pull stream refs', function (done) {
addTestFolder(ipfs, (err, cid) => {
expect(err).to.not.exist()

const stream = ipfs.refsPullStream(cid, { format: expectedResults.format })
pull(
stream,
pull.collect((err, refs) => {
expect(err).to.not.exist()

expect(refs.map(r => r.Ref)).to.eql(expectedResults.results)
done()
})
)
})
})
})
}
92 changes: 92 additions & 0 deletions src/files-regular/refs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 6] */
'use strict'

const { fixtures } = require('./utils')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.refs', function () {
this.timeout(40 * 1000)

let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('refs', function (done) {
module.exports.addTestFolder(ipfs, (err, cid) => {
expect(err).to.not.exist()

ipfs.refs(cid, { format: module.exports.expectedResults.format }, (err, refs) => {
expect(err).to.not.exist()

expect(refs.map(r => r.Ref)).to.eql(module.exports.expectedResults.results)
done()
})
})
})
})
}

module.exports.expectedResults = {
format: '<src> <dst> <linkname>',
results: [
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmZyUEQVuRK3XV7L9Dk26pg6RVSgaYkiSTEdnT2kZZdwoi alice.txt',
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn empty-folder',
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmZ25UfTqXGz9RsEJFg7HUAuBcmfx5dQZDXQd2QEZ8Kj74 files',
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmR4nFjTu18TyANgC65ArNWp5Yaab1gPzQ4D8zp7Kx3vhr holmes.txt',
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmT6orWioMiSqXXPGsUi71CKRRUmJ8YkuueV2DPV34E9y9 jungle.txt',
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP QmVwdDCY4SPGVFnNCiZnX5CtzwWDn6kAM98JXzKxE3kCmn pp.txt'
]
}

module.exports.addTestFolder = (ipfs, callback) => {
const content = (name) => ({
path: `test-folder/${name}`,
content: fixtures.directory.files[name]
})

const emptyDir = (name) => ({ path: `test-folder/${name}` })

const dirs = [
content('pp.txt'),
content('holmes.txt'),
content('jungle.txt'),
content('alice.txt'),
emptyDir('empty-folder'),
content('files/hello.txt'),
content('files/ipfs.txt'),
emptyDir('files/empty')
]

ipfs.add(dirs, (err, res) => {
expect(err).to.not.exist()
const root = res[res.length - 1]

expect(root.path).to.equal('test-folder')
expect(root.hash).to.equal(fixtures.directory.cid)

const cid = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'

callback(null, cid)
})
}

0 comments on commit ea98e4c

Please sign in to comment.