Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit e656e7c

Browse files
committed
Add ipfs.files.get tests.
1 parent a86753a commit e656e7c

File tree

1 file changed

+179
-25
lines changed

1 file changed

+179
-25
lines changed

src/files.js

+179-25
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,27 @@ const bs58 = require('bs58')
66
const Readable = require('readable-stream')
77
const path = require('path')
88
const fs = require('fs')
9-
const isNode = require('detect-node')
109
const bl = require('bl')
10+
const through = require('through2')
1111

1212
module.exports = (common) => {
13-
describe('.files', () => {
13+
describe.only('.files', () => {
1414
let smallFile
1515
let bigFile
16+
let directoryContent
1617
let ipfs
1718

1819
before((done) => {
19-
smallFile = fs.readFileSync(path.join(__dirname, './data/testfile.txt')
20-
)
21-
bigFile = fs.readFileSync(path.join(__dirname, './data/15mb.random')
22-
)
20+
smallFile = fs.readFileSync(path.join(__dirname, './data/testfile.txt'))
21+
bigFile = fs.readFileSync(path.join(__dirname, './data/15mb.random'))
22+
directoryContent = {
23+
'pp.txt': fs.readFileSync(path.join(__dirname, './data/test-folder/pp.txt')),
24+
'holmes.txt': fs.readFileSync(path.join(__dirname, './data/test-folder/holmes.txt')),
25+
'jungle.txt': fs.readFileSync(path.join(__dirname, './data/test-folder/jungle.txt')),
26+
'alice.txt': fs.readFileSync(path.join(__dirname, './data/test-folder/alice.txt')),
27+
'files/hello.txt': fs.readFileSync(path.join(__dirname, './data/test-folder/files/hello.txt')),
28+
'files/ipfs.txt': fs.readFileSync(path.join(__dirname, './data/test-folder/files/ipfs.txt'))
29+
}
2330

2431
common.setup((err, _ipfs) => {
2532
expect(err).to.not.exist
@@ -100,15 +107,9 @@ module.exports = (common) => {
100107
})
101108

102109
it('add a nested dir as array', (done) => {
103-
if (!isNode) {
104-
return done()
105-
// can't run this test cause browserify
106-
// can't shim readFileSync in runtime
107-
}
108-
const base = path.join(__dirname, 'data/test-folder')
109110
const content = (name) => ({
110111
path: `test-folder/${name}`,
111-
content: fs.readFileSync(path.join(base, name))
112+
content: directoryContent[name]
112113
})
113114
const emptyDir = (name) => ({
114115
path: `test-folder/${name}`
@@ -138,21 +139,13 @@ module.exports = (common) => {
138139

139140
describe('.createAddStream', () => {
140141
it('stream of valid files and dirs', (done) => {
141-
if (!isNode) {
142-
return done()
143-
// can't run this test cause browserify
144-
// can't shim readFileSync in runtime
145-
}
146-
147-
const base = path.join(__dirname, 'data/test-folder')
148142
const content = (name) => ({
149143
path: `test-folder/${name}`,
150-
content: fs.readFileSync(path.join(base, name))
144+
content: directoryContent[name]
151145
})
152146
const emptyDir = (name) => ({
153147
path: `test-folder/${name}`
154148
})
155-
156149
const files = [
157150
content('pp.txt'),
158151
content('holmes.txt'),
@@ -241,7 +234,7 @@ module.exports = (common) => {
241234
})
242235

243236
describe('.cat', () => {
244-
it('with a bas58 multihash encoded string', () => {
237+
it('with a base58 multihash encoded string', () => {
245238
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
246239

247240
return ipfs.cat(hash)
@@ -273,11 +266,172 @@ module.exports = (common) => {
273266
const hash = new Buffer(bs58.decode('QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'))
274267
return ipfs.cat(hash)
275268
.then((stream) => {
276-
stream.pipe(bl((err, bldata) => {
269+
stream.pipe(bl((err, data) => {
270+
expect(err).to.not.exist
271+
expect(data.toString()).to.contain('Check out some of the other files in this directory:')
272+
}))
273+
})
274+
})
275+
})
276+
})
277+
278+
describe('.get', () => {
279+
it('with a base58 encoded multihash', (done) => {
280+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
281+
ipfs.files.get(hash, (err, stream) => {
282+
expect(err).to.not.exist
283+
stream.pipe(bl((err, files) => {
284+
expect(err).to.not.exist
285+
expect(files).to.be.length(1)
286+
expect(files[0].path).to.equal(hash)
287+
files[0].content.pipe(bl((err, content) => {
288+
expect(err).to.not.exist
289+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
290+
done()
291+
}))
292+
}))
293+
})
294+
})
295+
296+
it('with a multihash', (done) => {
297+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
298+
const mhBuf = new Buffer(bs58.decode(hash))
299+
ipfs.files.get(mhBuf, (err, stream) => {
300+
expect(err).to.not.exist
301+
stream.pipe(bl((err, files) => {
302+
expect(err).to.not.exist
303+
expect(files).to.be.length(1)
304+
expect(files[0].path).to.deep.equal(hash)
305+
files[0].content.pipe(bl((err, content) => {
306+
expect(err).to.not.exist
307+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
308+
done()
309+
}))
310+
}))
311+
})
312+
})
313+
314+
it('large file', (done) => {
315+
const hash = 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq'
316+
ipfs.files.get(hash, (err, stream) => {
317+
expect(err).to.not.exist
318+
319+
// accumulate the files and their content
320+
var files = []
321+
stream.pipe(through.obj((file, enc, next) => {
322+
file.content.pipe(bl((err, content) => {
323+
expect(err).to.not.exist
324+
files.push({
325+
path: file.path,
326+
content: content
327+
})
328+
next()
329+
}))
330+
}, () => {
331+
expect(files.length).to.equal(1)
332+
expect(files[0].path).to.equal(hash)
333+
expect(files[0].content).to.deep.equal(bigFile)
334+
done()
335+
}))
336+
})
337+
})
338+
339+
it('directory', (done) => {
340+
const hash = 'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP'
341+
ipfs.files.get(hash, (err, stream) => {
342+
expect(err).to.not.exist
343+
344+
// accumulate the files and their content
345+
var files = []
346+
stream.pipe(through.obj((file, enc, next) => {
347+
if (file.content) {
348+
file.content.pipe(bl((err, content) => {
349+
expect(err).to.not.exist
350+
files.push({
351+
path: file.path,
352+
content: content
353+
})
354+
next()
355+
}))
356+
} else {
357+
files.push(file)
358+
next()
359+
}
360+
}, () => {
361+
// Check paths
362+
var paths = files.map((file) => {
363+
return file.path
364+
})
365+
expect(paths).to.deep.equal([
366+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP',
367+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/alice.txt',
368+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/empty-folder',
369+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/files',
370+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/files/empty',
371+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/files/hello.txt',
372+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/files/ipfs.txt',
373+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/holmes.txt',
374+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/jungle.txt',
375+
'QmVvjDy7yF7hdnqE8Hrf4MHo5ABDtb5AbX6hWbD3Y42bXP/pp.txt'
376+
])
377+
378+
// Check contents
379+
var contents = files.map((file) => {
380+
return file.content ? file.content : null
381+
})
382+
expect(contents).to.deep.equal([
383+
null,
384+
directoryContent['alice.txt'],
385+
null,
386+
null,
387+
null,
388+
directoryContent['files/hello.txt'],
389+
directoryContent['files/ipfs.txt'],
390+
directoryContent['holmes.txt'],
391+
directoryContent['jungle.txt'],
392+
directoryContent['pp.txt']
393+
])
394+
done()
395+
}))
396+
})
397+
})
398+
399+
describe('promise', () => {
400+
it('with a base58 encoded string', (done) => {
401+
const hash = 'QmPZ9gcCEpqKTo6aq61g2nXGUhM4iCL3ewB6LDXZCtioEB'
402+
ipfs.files.get(hash)
403+
.then((stream) => {
404+
stream.pipe(bl((err, files) => {
277405
expect(err).to.not.exist
278-
expect(bldata.toString()).to.contain('Check out some of the other files in this directory:')
406+
expect(files).to.be.length(1)
407+
expect(files[0].path).to.equal(hash)
408+
files[0].content.pipe(bl((err, content) => {
409+
expect(err).to.not.exist
410+
expect(content.toString()).to.contain('Check out some of the other files in this directory:')
411+
done()
412+
}))
279413
}))
280414
})
415+
.catch((err) => {
416+
expect(err).to.not.exist
417+
})
418+
})
419+
420+
it('errors on invalid key', (done) => {
421+
const hash = 'somethingNotMultihash'
422+
ipfs.files.get(hash)
423+
.then((stream) => {})
424+
.catch((err) => {
425+
expect(err).to.exist
426+
const errString = err.toString()
427+
if (errString === 'Error: invalid ipfs ref path') {
428+
expect(err.toString()).to.contain('Error: invalid ipfs ref path')
429+
}
430+
if (errString === 'Error: Invalid Key') {
431+
expect(err.toString()).to.contain('Error: Invalid Key')
432+
}
433+
done()
434+
})
281435
})
282436
})
283437
})

0 commit comments

Comments
 (0)