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

Commit f25ff62

Browse files
committed
test: add tests for addFromFs, addFromUrl, addFromStream
1 parent c6fb17e commit f25ff62

25 files changed

+14103
-0
lines changed

js/src/files-regular/add-from-fs.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const path = require('path')
5+
const expectTimeout = require('../utils/expect-timeout')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
const fs = require('fs')
8+
const os = require('os')
9+
10+
module.exports = (createCommon, options) => {
11+
const describe = getDescribe(options)
12+
const it = getIt(options)
13+
const common = createCommon()
14+
15+
describe('.addFromFs', function () {
16+
this.timeout(40 * 1000)
17+
18+
const fixturesPath = path.join(__dirname, '../../test/fixtures')
19+
let ipfs
20+
21+
before(function (done) {
22+
// CI takes longer to instantiate the daemon, so we need to increase the
23+
// timeout for the before step
24+
this.timeout(60 * 1000)
25+
26+
common.setup((err, factory) => {
27+
expect(err).to.not.exist()
28+
factory.spawnNode((err, node) => {
29+
expect(err).to.not.exist()
30+
ipfs = node
31+
done()
32+
})
33+
})
34+
})
35+
36+
after((done) => common.teardown(done))
37+
38+
it('a directory', (done) => {
39+
const filesPath = path.join(fixturesPath, 'test-folder')
40+
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
41+
expect(err).to.not.exist()
42+
expect(result.length).to.be.above(8)
43+
done()
44+
})
45+
})
46+
47+
it('a directory with an odd name', (done) => {
48+
const filesPath = path.join(fixturesPath, 'weird name folder [v0]')
49+
ipfs.addFromFs(filesPath, { recursive: true }, (err, result) => {
50+
expect(err).to.not.exist()
51+
expect(result.length).to.be.above(8)
52+
done()
53+
})
54+
})
55+
56+
it('add and ignore a directory', (done) => {
57+
const filesPath = path.join(fixturesPath, 'test-folder')
58+
ipfs.addFromFs(filesPath, { recursive: true, ignore: ['files/**'] }, (err, result) => {
59+
expect(err).to.not.exist()
60+
expect(result.length).to.be.below(9)
61+
done()
62+
})
63+
})
64+
65+
it('a file', (done) => {
66+
const filePath = path.join(fixturesPath, 'testfile.txt')
67+
ipfs.addFromFs(filePath, (err, result) => {
68+
expect(err).to.not.exist()
69+
expect(result.length).to.equal(1)
70+
expect(result[0].path).to.equal('testfile.txt')
71+
done()
72+
})
73+
})
74+
75+
it('a hidden file in a directory', (done) => {
76+
const filesPath = path.join(fixturesPath, 'hidden-files-folder')
77+
ipfs.addFromFs(filesPath, { recursive: true, hidden: true }, (err, result) => {
78+
expect(err).to.not.exist()
79+
expect(result.length).to.be.above(10)
80+
expect(result.map(object => object.path)).to.include('hidden-files-folder/.hiddenTest.txt')
81+
expect(result.map(object => object.hash)).to.include('QmdbAjVmLRdpFyi8FFvjPfhTGB2cVXvWLuK7Sbt38HXrtt')
82+
done()
83+
})
84+
})
85+
86+
it('with only-hash=true', function () {
87+
this.slow(10 * 1000)
88+
89+
const content = String(Math.random() + Date.now())
90+
const filepath = path.join(os.tmpdir(), `${content}.txt`)
91+
fs.writeFileSync(filepath, content)
92+
93+
return ipfs.addFromFs(filepath, { onlyHash: true })
94+
.then(out => {
95+
fs.unlinkSync(filepath)
96+
return expectTimeout(ipfs.object.get(out[0].hash), 4000)
97+
})
98+
})
99+
})
100+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const loadFixture = require('aegir/fixtures')
5+
const into = require('into-stream')
6+
const { getDescribe, getIt, expect } = require('../utils/mocha')
7+
8+
module.exports = (createCommon, options) => {
9+
const describe = getDescribe(options)
10+
const it = getIt(options)
11+
const common = createCommon()
12+
13+
describe('.addFromStream', function () {
14+
this.timeout(40 * 1000)
15+
16+
let ipfs
17+
18+
before(function (done) {
19+
// CI takes longer to instantiate the daemon, so we need to increase the
20+
// timeout for the before step
21+
this.timeout(60 * 1000)
22+
23+
common.setup((err, factory) => {
24+
expect(err).to.not.exist()
25+
factory.spawnNode((err, node) => {
26+
expect(err).to.not.exist()
27+
ipfs = node
28+
done()
29+
})
30+
})
31+
})
32+
33+
after((done) => common.teardown(done))
34+
35+
it('same as .add', (done) => {
36+
const testData = loadFixture('js/test/fixtures/15mb.random', 'interface-ipfs-core')
37+
38+
ipfs.addFromStream(into(testData), (err, result) => {
39+
expect(err).to.not.exist()
40+
expect(result.length).to.equal(1)
41+
done()
42+
})
43+
})
44+
})
45+
}

0 commit comments

Comments
 (0)