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

Commit

Permalink
fix: fix the welcome message and throw error when trying to cat a non…
Browse files Browse the repository at this point in the history
…-exis… (#1032)

* Fix the welcome message and throw error when trying to cat a non-existent file [Fixes #1031]

* Add tests

* Use CID instead of multihash, fix lint

* Increase cli init basic test timeout
  • Loading branch information
atvanguard authored and daviddias committed Oct 16, 2017
1 parent 629d5a7 commit 25fb390
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/core/components/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
})
)
Expand Down
18 changes: 7 additions & 11 deletions src/core/components/init-assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 CID = require('cids')

// 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]),
Expand All @@ -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
}
Expand All @@ -34,14 +34,10 @@ 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') {
const cid = new CID(el.multihash)
log('to get started, enter:\n')
log(`\t jsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`)
}
}),
pull.collect((err) => {
Expand Down
8 changes: 8 additions & 0 deletions test/cli/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
14 changes: 11 additions & 3 deletions test/cli/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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(8000)

it('bits', () => {
return ipfs('init --bits 1024').then(() => {
Expand Down

0 comments on commit 25fb390

Please sign in to comment.