Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed Apr 23, 2016
1 parent 2bb65f6 commit e768675
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"hapi": "^13.3.0",
"ipfs-api": "^3.0.1",
"ipfs-blocks": "^0.2.0",
"ipfs-data-importing": "^0.3.3",
"ipfs-unixfs-engine": "^0.4.2",
"ipfs-merkle-dag": "^0.4.0",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.6.1",
Expand Down
31 changes: 29 additions & 2 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ const DAGService = mDAG.DAGService
const peerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const importer = require('ipfs-data-importing').import
const importer = require('ipfs-unixfs-engine').importer
const exporter = require('ipfs-unixfs-engine').exporter
const libp2p = require('libp2p-ipfs')
const init = require('./init')
const IPFSRepo = require('ipfs-repo')
const UnixFS = require('ipfs-unixfs')

exports = module.exports = IPFS

Expand Down Expand Up @@ -390,7 +392,32 @@ function IPFS (repo) {
add: (path, options, callback) => {
options.path = path
options.dagService = dagS
importer(options, callback)
options.recursive = options

importer.import(path, options.dagService, options, function (err, stat) {
if (err) {
callback(err, null)
}
callback(null, stat)
})
},
cat: (hash, callback) => {
dagS.get(hash, (err, fetchedNode) => {
if (err) {
return callback(err, null)
}
const data = UnixFS.unmarshal(fetchedNode.data)
if (data.type === 'directory') {
callback('This dag node is a directory', null)
} else {
const exportEvent = exporter(hash, dagS)
callback(null, exportEvent)
}
})
},
get: (hash, callback) => {
var exportFile = exporter(hash, dagS)
callback(null, exportFile)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module.exports = (repo, opts, callback) => {
return doneImport(null)
}

const importer = require('ipfs-data-importing')
const importer = require('ipfs-unixfs-engine').importer
const blocks = new IpfsBlocks(repo)
const dag = new IpfsDagService(blocks)

Expand Down
2 changes: 1 addition & 1 deletion test/cli-tests/test-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('commands', () => {
.run((err, stdout, exitcode) => {
expect(err).to.not.exist
expect(exitcode).to.equal(0)
expect(stdout.length).to.equal(45)
expect(stdout.length).to.equal(47)
done()
})
})
Expand Down
44 changes: 44 additions & 0 deletions test/core-tests/test-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* eslint-env mocha */

const bl = require('bl')
const expect = require('chai').expect

const IPFS = require('../../src/core')

describe('files', () => {
var ipfs

before((done) => {
ipfs = new IPFS()
ipfs.load(done)
})

it('cat', (done) => {
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
ipfs.files.cat(hash, (err, res) => {
expect(err).to.not.exist
res.on('file', (data) => {
data.stream.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.equal('hello world\n')
done()
}))
})
})
})

it('get', (done) => {
// TODO create non-trival get test
const hash = 'QmT78zSuBmuS4z925WZfrqQ1qHaJ56DQaTfyMUF7F8ff5o'
ipfs.files.get(hash, (err, res) => {
expect(err).to.not.exist
res.on('file', (data) => {
data.stream.pipe(bl((err, bldata) => {
expect(err).to.not.exist
expect(bldata.toString()).to.equal('hello world\n')
done()
}))
})
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

 hello world


0 comments on commit e768675

Please sign in to comment.