|
| 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 | +} |
0 commit comments