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

Commit 25fb390

Browse files
atvanguarddaviddias
authored andcommitted
fix: fix the welcome message and throw error when trying to cat a non-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
1 parent 629d5a7 commit 25fb390

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

Diff for: .gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ build
3030

3131
# Dependency directory
3232
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
33-
node_modules
33+
# node_modules
3434

3535
lib
3636
dist

Diff for: src/core/components/files.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ module.exports = function files (self) {
8080

8181
cat: promisify((ipfsPath, callback) => {
8282
if (typeof ipfsPath === 'function') {
83-
return callback(new Error('You must supply a ipfsPath'))
83+
return callback(new Error('You must supply an ipfsPath'))
8484
}
8585

8686
pull(
@@ -89,6 +89,7 @@ module.exports = function files (self) {
8989
if (err) {
9090
return callback(err)
9191
}
92+
if (!files || !files.length) return callback(new Error('No such file'))
9293
callback(null, toStream.source(files[files.length - 1].content))
9394
})
9495
)

Diff for: src/core/components/init-assets.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const glob = require('glob')
66
const importer = require('ipfs-unixfs-engine').importer
77
const pull = require('pull-stream')
88
const file = require('pull-file')
9-
// const mh = require('multihashes')
9+
const CID = require('cids')
1010

1111
// Add the default assets to the repo.
1212
module.exports = function addDefaultAssets (self, log, callback) {
1313
const initDocsPath = path.join(__dirname, '../../init-files/init-docs')
14-
const index = __dirname.lastIndexOf('/')
14+
const index = initDocsPath.lastIndexOf('/')
1515

1616
pull(
1717
pull.values([initDocsPath]),
@@ -20,7 +20,7 @@ module.exports = function addDefaultAssets (self, log, callback) {
2020
}),
2121
pull.flatten(),
2222
pull.map((element) => {
23-
const addPath = element.substring(index + 1, element.length)
23+
const addPath = element.substring(index + 1)
2424
if (fs.statSync(element).isDirectory()) {
2525
return
2626
}
@@ -34,14 +34,10 @@ module.exports = function addDefaultAssets (self, log, callback) {
3434
pull.filter(Boolean),
3535
importer(self._ipldResolver),
3636
pull.through((el) => {
37-
if (el.path === 'files/init-docs/docs') {
38-
log('to get started, enter:')
39-
log()
40-
log(`\t jsipfs files cat /ipfs/QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB`)
41-
// TODO when we support pathing in unixfs-engine
42-
// const hash = mh.toB58String(el.multihash)
43-
// log(`\t jsipfs files cat /ipfs/${hash}/readme`)
44-
log()
37+
if (el.path === 'init-docs') {
38+
const cid = new CID(el.multihash)
39+
log('to get started, enter:\n')
40+
log(`\t jsipfs files cat /ipfs/${cid.toBaseEncodedString()}/readme\n`)
4541
}
4642
}),
4743
pull.collect((err) => {

Diff for: test/cli/files.js

+8
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,14 @@ describe('files', () => runOnAndOff((thing) => {
238238
})
239239
})
240240

241+
it('cat non-existent file', () => {
242+
return ipfs('cat QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB/dummy')
243+
.then(() => expect.fail(0, 1, 'Should have thrown an error'))
244+
.catch((err) => {
245+
expect(err).to.exist()
246+
})
247+
})
248+
241249
it('get', () => {
242250
return ipfs('files get QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB')
243251
.then((out) => {

Diff for: test/cli/init.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ describe('init', () => {
1212
let repoPath
1313
let ipfs
1414

15+
const readme = fs.readFileSync(path.join(process.cwd(), '/src/init-files/init-docs/readme'))
16+
.toString('utf-8')
17+
1518
const repoExistsSync = (p) => fs.existsSync(path.join(repoPath, p))
1619

1720
const repoDirSync = (p) => {
@@ -27,12 +30,17 @@ describe('init', () => {
2730
afterEach(() => clean(repoPath))
2831

2932
it('basic', () => {
30-
return ipfs('init').then(() => {
33+
return ipfs('init').then((out) => {
3134
expect(repoDirSync('blocks')).to.have.length.above(2)
3235
expect(repoExistsSync('config')).to.equal(true)
3336
expect(repoExistsSync('version')).to.equal(true)
34-
})
35-
})
37+
38+
// Test that the following was written when init-ing the repo
39+
// jsipfs files cat /ipfs/QmfGBRT6BbWJd7yUc2uYdaUZJBbnEFvTqehPFoSMQ6wgdr/readme
40+
let command = out.substring(out.indexOf('files cat'), out.length - 2 /* omit the newline char */)
41+
return ipfs(command)
42+
}).then((out) => expect(out).to.equal(readme))
43+
}).timeout(8000)
3644

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

0 commit comments

Comments
 (0)