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

Fix for ipfs.files.add + added corresponding tests #104

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +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

# IntelliJ IDEA
js-ipfs.iml
.idea
4 changes: 1 addition & 3 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,7 @@ function IPFS (repo) {

this.files = {
add: (path, options, callback) => {
options.path = path
options.dagService = dagS
importer(options, callback)
importer(path, dagS, options, callback)
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions test/core-tests/test-files-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-env mocha */

const expect = require('chai').expect
const IPFS = require('../../src/core')
const bs58 = require('bs58')
const path = require('path')

describe('files', () => {
var ipfs

before((done) => {
ipfs = new IPFS()
ipfs.load(done)
})

it('should add a small file (smaller than a block)', (done) => {
ipfs.files.add(path.join(__dirname, '../test-data/files-test/anotherlevel/Test.txt'), {}, (err, fs) => {
expect(err).to.not.exist
expect(fs).to.have.property('Size', 90)
expect(fs).to.have.property('Name', 'Test.txt')
expect(fs).to.have.property('Hash')
expect(bs58.encode(fs.Hash).toString())
.to.equal('QmfUVDCvgSZuKjovHH7hsebgaQfr75uR9oCsd9AoQ5bryf')
expect(fs.Size).to.equal(90)
done()
})
})

it('should add a small file (multiple blocks)', (done) => {
ipfs.files.add(path.join(__dirname, '../test-data/files-test/ipfs-p2p-file-system.pdf'), {}, (err, fs) => {
expect(err).to.not.exist
expect(fs).to.have.property('Size', 213377)
expect(fs).to.have.property('Name', 'ipfs-p2p-file-system.pdf')
expect(fs).to.have.property('Hash')
expect(bs58.encode(fs.Hash).toString())
.to.equal('QmV9tSDx9UiPeWExXEeH6aoDvmihvx6jD5eLb4jbTaKGps')
expect(fs.Size).to.equal(213377)
done()
})
})

it('should add a directory', (done) => {
ipfs.files.add(path.join(__dirname, '../test-data/files-test'), {recursive: true}, (err, fs) => {
expect(err).to.not.exist
expect(fs).to.have.property('Size', 213648)
expect(fs).to.have.property('Name', 'files-test')
expect(fs).to.have.property('Hash')
expect(bs58.encode(fs.Hash).toString())
.to.equal('QmVCr7Jj7GGLrKJmj331M9NKH7YJKLGwekCG4yVeApfAHC')
expect(fs.Size).to.equal(213648)
done()
})
})

it('should add a buffer', (done) => {
ipfs.files.add(new Buffer('42 is the answer', 'ascii'), {}, (err, fs) => {
expect(err).to.not.exist
console.log('FS', fs)
expect(fs).to.have.property('Size', 24)
expect(fs).to.not.have.property('Name')
expect(fs).to.have.property('Hash')
expect(bs58.encode(fs.Hash).toString())
.to.equal('Qmex7CQFgntjL4SR9tBYYgDyYHg7aoiSG7sGybWd6SVE9W')
expect(fs.Size).to.equal(24)
done()
})
})
})
3 changes: 3 additions & 0 deletions test/test-data/files-test/anotherlevel/Test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

This is a small test file
And that is a second line with accented character: éè
Binary file not shown.