From 83d11dda4449871d41abd926970f4a328a3e63e7 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal <93arpit@gmail.com> Date: Mon, 2 Oct 2017 02:09:59 +0530 Subject: [PATCH 1/4] Fix the welcome message and throw error when trying to cat a non-existent file [Fixes #1031] --- src/core/components/files.js | 3 ++- src/core/components/init-assets.js | 17 ++++++----------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/core/components/files.js b/src/core/components/files.js index 6a9f5fbced..e0f07f9215 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -80,7 +80,7 @@ module.exports = function files (self) { cat: promisify((ipfsPath, callback) => { if (typeof ipfsPath === 'function') { - return callback(new Error('You must supply a ipfsPath')) + return callback(new Error('You must supply an ipfsPath')) } pull( @@ -89,6 +89,7 @@ module.exports = function files (self) { if (err) { return callback(err) } + if (!files || !files.length) return callback(new Error('No such file')) callback(null, toStream.source(files[files.length - 1].content)) }) ) diff --git a/src/core/components/init-assets.js b/src/core/components/init-assets.js index 33778a50ef..eb723263ff 100644 --- a/src/core/components/init-assets.js +++ b/src/core/components/init-assets.js @@ -6,12 +6,12 @@ const glob = require('glob') const importer = require('ipfs-unixfs-engine').importer const pull = require('pull-stream') const file = require('pull-file') -// const mh = require('multihashes') +const mh = require('multihashes') // Add the default assets to the repo. module.exports = function addDefaultAssets (self, log, callback) { const initDocsPath = path.join(__dirname, '../../init-files/init-docs') - const index = __dirname.lastIndexOf('/') + const index = initDocsPath.lastIndexOf('/') pull( pull.values([initDocsPath]), @@ -20,7 +20,7 @@ module.exports = function addDefaultAssets (self, log, callback) { }), pull.flatten(), pull.map((element) => { - const addPath = element.substring(index + 1, element.length) + const addPath = element.substring(index + 1) if (fs.statSync(element).isDirectory()) { return } @@ -34,14 +34,9 @@ module.exports = function addDefaultAssets (self, log, callback) { pull.filter(Boolean), importer(self._ipldResolver), pull.through((el) => { - if (el.path === 'files/init-docs/docs') { - log('to get started, enter:') - log() - log(`\t jsipfs files cat /ipfs/QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB`) - // TODO when we support pathing in unixfs-engine - // const hash = mh.toB58String(el.multihash) - // log(`\t jsipfs files cat /ipfs/${hash}/readme`) - log() + if (el.path === 'init-docs') { + log('to get started, enter:\n') + log(`\t jsipfs files cat /ipfs/${mh.toB58String(el.multihash)}/readme\n`) } }), pull.collect((err) => { From 20604f6907bb4f1a7e4601e2f452f79cf30f9803 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal <93arpit@gmail.com> Date: Fri, 6 Oct 2017 01:40:30 +0530 Subject: [PATCH 2/4] Add tests --- test/cli/files.js | 8 ++++++++ test/cli/init.js | 14 +++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/test/cli/files.js b/test/cli/files.js index afcf581112..f1db1b8cef 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -238,6 +238,14 @@ describe('files', () => runOnAndOff((thing) => { }) }) + it('cat non-existent file', () => { + return ipfs('cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB/dummy') + .then(() => expect.fail(0, 1, 'Should have thrown an error')) + .catch((err) => { + expect(err).to.exist(); + }) + }) + it('get', () => { return ipfs('files get QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB') .then((out) => { diff --git a/test/cli/init.js b/test/cli/init.js index 919a74f3af..8d44717ec3 100644 --- a/test/cli/init.js +++ b/test/cli/init.js @@ -12,6 +12,9 @@ describe('init', () => { let repoPath let ipfs + const readme = fs.readFileSync(path.join(process.cwd(), '/src/init-files/init-docs/readme')) + .toString('utf-8') + const repoExistsSync = (p) => fs.existsSync(path.join(repoPath, p)) const repoDirSync = (p) => { @@ -27,12 +30,17 @@ describe('init', () => { afterEach(() => clean(repoPath)) it('basic', () => { - return ipfs('init').then(() => { + return ipfs('init').then((out) => { expect(repoDirSync('blocks')).to.have.length.above(2) expect(repoExistsSync('config')).to.equal(true) expect(repoExistsSync('version')).to.equal(true) - }) - }) + + // Test that the following was written when init-ing the repo + // jsipfs files cat /ipfs/QmfGBRT6BbWJd7yUc2uYdaUZJBbnEFvTqehPFoSMQ6wgdr/readme + let command = out.substring(out.indexOf('files cat'), out.length - 2 /* omit the newline char */) + return ipfs(command) + }).then((out) => expect(out).to.equal(readme)) + }).timeout(6000) it('bits', () => { return ipfs('init --bits 1024').then(() => { From 5c4f9cf4d366e6def78ef4641dffc66e28d8f9ac Mon Sep 17 00:00:00 2001 From: Arpit Agarwal <93arpit@gmail.com> Date: Sat, 14 Oct 2017 14:16:07 +0530 Subject: [PATCH 3/4] Use CID instead of multihash, fix lint --- .gitignore | 2 +- src/core/components/init-assets.js | 5 +++-- test/cli/files.js | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index e4c5b0e314..b620619d8c 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,7 @@ build # Dependency directory # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git -node_modules +# node_modules lib dist diff --git a/src/core/components/init-assets.js b/src/core/components/init-assets.js index eb723263ff..1d6d46baad 100644 --- a/src/core/components/init-assets.js +++ b/src/core/components/init-assets.js @@ -6,7 +6,7 @@ const glob = require('glob') const importer = require('ipfs-unixfs-engine').importer const pull = require('pull-stream') const file = require('pull-file') -const mh = require('multihashes') +const CID = require('cids') // Add the default assets to the repo. module.exports = function addDefaultAssets (self, log, callback) { @@ -35,8 +35,9 @@ module.exports = function addDefaultAssets (self, log, callback) { importer(self._ipldResolver), pull.through((el) => { if (el.path === 'init-docs') { + const cid = new CID(el.multihash) log('to get started, enter:\n') - log(`\t jsipfs files cat /ipfs/${mh.toB58String(el.multihash)}/readme\n`) + log(`\t jsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`) } }), pull.collect((err) => { diff --git a/test/cli/files.js b/test/cli/files.js index f1db1b8cef..2a71143387 100644 --- a/test/cli/files.js +++ b/test/cli/files.js @@ -242,7 +242,7 @@ describe('files', () => runOnAndOff((thing) => { return ipfs('cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB/dummy') .then(() => expect.fail(0, 1, 'Should have thrown an error')) .catch((err) => { - expect(err).to.exist(); + expect(err).to.exist() }) }) From 8d2631792409e53288ca1b4e14df28acff4bc9f3 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal <93arpit@gmail.com> Date: Sun, 15 Oct 2017 18:18:33 +0530 Subject: [PATCH 4/4] Increase cli init basic test timeout --- test/cli/init.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cli/init.js b/test/cli/init.js index 8d44717ec3..7953f0fcaf 100644 --- a/test/cli/init.js +++ b/test/cli/init.js @@ -40,7 +40,7 @@ describe('init', () => { let command = out.substring(out.indexOf('files cat'), out.length - 2 /* omit the newline char */) return ipfs(command) }).then((out) => expect(out).to.equal(readme)) - }).timeout(6000) + }).timeout(8000) it('bits', () => { return ipfs('init --bits 1024').then(() => {