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

Commit

Permalink
test: add tests for addFromFs, addFromUrl, addFromStream
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Oct 26, 2018
1 parent 821b95f commit d50220f
Show file tree
Hide file tree
Showing 25 changed files with 14,103 additions and 2 deletions.
100 changes: 100 additions & 0 deletions js/src/files-regular/add-from-fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* eslint-env mocha */
'use strict'

const path = require('path')
const expectTimeout = require('../utils/expect-timeout')
const { getDescribe, getIt, expect } = require('../utils/mocha')
const fs = require('fs')
const os = require('os')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.addFromFs', function () {
this.timeout(40 * 1000)

const fixturesPath = path.join(__dirname, '../../test/fixtures')
let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('a directory', (done) => {
const filesPath = path.join(fixturesPath, 'test-folder')
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(8)
done()
})
})

it('a directory with an odd name', (done) => {
const filesPath = path.join(fixturesPath, 'weird name folder [v0]')
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(8)
done()
})
})

it('add and ignore a directory', (done) => {
const filesPath = path.join(fixturesPath, 'test-folder')
ipfs.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.below(9)
done()
})
})

it('a file', (done) => {
const filePath = path.join(fixturesPath, 'testfile.txt')
ipfs.addFromFs(filePath, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.equal(1)
expect(result[0].path).to.equal('testfile.txt')
done()
})
})

it('a hidden file in a directory', (done) => {
const filesPath = path.join(fixturesPath, 'hidden-files-folder')
ipfs.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.be.above(10)
expect(result.map(object => object.path)).to.include('hidden-files-folder/.hiddenTest.txt')
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
done()
})
})

it('with only-hash=true', function () {
this.slow(10 * 1000)

const content = String(Math.random() + Date.now())
const filepath = path.join(os.tmpdir(), `${content}.txt`)
fs.writeFileSync(filepath, content)

return ipfs.addFromFs(filepath, { onlyHash: true })
.then(out => {
fs.unlinkSync(filepath)
return expectTimeout(ipfs.object.get(out[0].hash), 4000)
})
})
})
}
45 changes: 45 additions & 0 deletions js/src/files-regular/add-from-stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* eslint-env mocha */
'use strict'

const loadFixture = require('aegir/fixtures')
const into = require('into-stream')
const { getDescribe, getIt, expect } = require('../utils/mocha')

module.exports = (createCommon, options) => {
const describe = getDescribe(options)
const it = getIt(options)
const common = createCommon()

describe('.addFromStream', function () {
this.timeout(40 * 1000)

let ipfs

before(function (done) {
// CI takes longer to instantiate the daemon, so we need to increase the
// timeout for the before step
this.timeout(60 * 1000)

common.setup((err, factory) => {
expect(err).to.not.exist()
factory.spawnNode((err, node) => {
expect(err).to.not.exist()
ipfs = node
done()
})
})
})

after((done) => common.teardown(done))

it('same as .add', (done) => {
const testData = loadFixture('js/test/fixtures/15mb.random', 'interface-ipfs-core')

ipfs.addFromStream(into(testData), (err, result) => {
expect(err).to.not.exist()
expect(result.length).to.equal(1)
done()
})
})
})
}
Loading

0 comments on commit d50220f

Please sign in to comment.