From e768675d12f1481523a0d871e7ccaf562973afd2 Mon Sep 17 00:00:00 2001 From: nginnever Date: Fri, 22 Apr 2016 17:08:17 -0700 Subject: [PATCH] tests --- package.json | 2 +- src/core/index.js | 31 ++++++++++++- src/core/init.js | 2 +- test/cli-tests/test-commands.js | 2 +- test/core-tests/test-files.js | 44 +++++++++++++++++++ ...dc5e844ead5f91f12858b021eba45768b4c0e.data | 3 ++ 6 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 test/core-tests/test-files.js create mode 100644 test/go-ipfs-repo/blocks/122046d4/122046d44814b9c5af141c3aaab7c05dc5e844ead5f91f12858b021eba45768b4c0e.data diff --git a/package.json b/package.json index d2b4abbdf8..2fa85d0519 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/core/index.js b/src/core/index.js index 8a91870c25..4bc32006f9 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -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 @@ -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) } } } diff --git a/src/core/init.js b/src/core/init.js index 363db8c0a3..1fb78945ca 100644 --- a/src/core/init.js +++ b/src/core/init.js @@ -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) diff --git a/test/cli-tests/test-commands.js b/test/cli-tests/test-commands.js index aa8da00762..efb656d58a 100644 --- a/test/cli-tests/test-commands.js +++ b/test/cli-tests/test-commands.js @@ -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() }) }) diff --git a/test/core-tests/test-files.js b/test/core-tests/test-files.js new file mode 100644 index 0000000000..c6d3896cf4 --- /dev/null +++ b/test/core-tests/test-files.js @@ -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() + })) + }) + }) + }) +}) diff --git a/test/go-ipfs-repo/blocks/122046d4/122046d44814b9c5af141c3aaab7c05dc5e844ead5f91f12858b021eba45768b4c0e.data b/test/go-ipfs-repo/blocks/122046d4/122046d44814b9c5af141c3aaab7c05dc5e844ead5f91f12858b021eba45768b4c0e.data new file mode 100644 index 0000000000..2965d1c457 --- /dev/null +++ b/test/go-ipfs-repo/blocks/122046d4/122046d44814b9c5af141c3aaab7c05dc5e844ead5f91f12858b021eba45768b4c0e.data @@ -0,0 +1,3 @@ + + hello world + \ No newline at end of file