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

Fix the welcome message and throw error when trying to cat a non-exis… #1032

Merged
merged 4 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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