diff --git a/package.json b/package.json index dcde760ba4..fa2f578af8 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "form-data": "^1.0.0-rc4", "gulp": "^3.9.1", "idb-plus-blob-store": "^1.1.2", - "interface-ipfs-core": "^0.6.0", + "interface-ipfs-core": "^0.7.2", "left-pad": "^1.1.1", "lodash": "^4.14.1", "ncp": "^2.0.0", @@ -121,4 +121,4 @@ "kumavis ", "nginnever " ] -} \ No newline at end of file +} diff --git a/test/cli/test-version.js b/test/cli/test-version.js index dc0186f3f0..2378bd2df5 100644 --- a/test/cli/test-version.js +++ b/test/cli/test-version.js @@ -6,6 +6,7 @@ const nexpect = require('nexpect') const HttpAPI = require('../../src/http-api') const repoPath = require('./index').repoPath const _ = require('lodash') +const pkgversion = require('../../package.json').version describe('version', () => { const env = _.clone(process.env) @@ -16,7 +17,7 @@ describe('version', () => { nexpect.spawn('node', [process.cwd() + '/src/cli/bin.js', 'version'], {env}) .run((err, stdout, exitcode) => { expect(err).to.not.exist - expect(stdout[0]).to.equal('js-ipfs version: 0.14.1') + expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion) expect(exitcode).to.equal(0) done() }) @@ -46,7 +47,7 @@ describe('version', () => { .run((err, stdout, exitcode) => { expect(err).to.not.exist expect(exitcode).to.equal(0) - expect(stdout[0]).to.equal('js-ipfs version: 0.14.1') + expect(stdout[0]).to.equal('js-ipfs version: ' + pkgversion) done() }) }) diff --git a/test/core/both/test-files.js b/test/core/both/test-files.js index 18f7c88b34..e6772ce706 100644 --- a/test/core/both/test-files.js +++ b/test/core/both/test-files.js @@ -2,17 +2,17 @@ 'use strict' const test = require('interface-ipfs-core') -const IPFS = require('../../../src/core') +const IPFSFactory = require('../../utils/factory') + +let factory const common = { setup: function (cb) { - const ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(() => { - cb(null, ipfs) - }) + factory = new IPFSFactory() + cb(null, factory) }, teardown: function (cb) { - cb() + factory.dismantle(cb) } } diff --git a/test/core/both/test-object.js b/test/core/both/test-object.js index f5daa46cc6..4ce9e4fd0e 100644 --- a/test/core/both/test-object.js +++ b/test/core/both/test-object.js @@ -3,18 +3,17 @@ 'use strict' const test = require('interface-ipfs-core') +const IPFSFactory = require('../../utils/factory') -const IPFS = require('../../../src/core') +let factory const common = { setup: function (cb) { - const ipfs = new IPFS(require('../../utils/repo-path')) - ipfs.load(() => { - cb(null, ipfs) - }) + factory = new IPFSFactory() + cb(null, factory) }, teardown: function (cb) { - cb() + factory.dismantle(cb) } } diff --git a/test/core/both/test-version.js b/test/core/both/test-version.js index c15fcf2469..4cbfa91e92 100644 --- a/test/core/both/test-version.js +++ b/test/core/both/test-version.js @@ -2,6 +2,7 @@ 'use strict' const expect = require('chai').expect +const pkgversion = require('../../../package.json').version const IPFS = require('../../../src/core') @@ -16,7 +17,7 @@ describe('version', () => { it('get version', (done) => { ipfs.version((err, version) => { expect(err).to.not.exist - expect(version).to.equal('0.14.1') + expect(version).to.equal(pkgversion) done() }) }) diff --git a/test/http-api/test-version.js b/test/http-api/test-version.js index f9887359c2..c425110e79 100644 --- a/test/http-api/test-version.js +++ b/test/http-api/test-version.js @@ -3,6 +3,7 @@ const expect = require('chai').expect const APIctl = require('ipfs-api') +const pkgversion = require('../../package.json').version module.exports = (httpAPI) => { describe('version', () => { @@ -18,7 +19,7 @@ module.exports = (httpAPI) => { method: 'GET', url: '/api/v0/version' }, (res) => { - expect(res.result.Version).to.equal('0.14.1') + expect(res.result.Version).to.equal(pkgversion) expect(res.result).to.have.a.property('Commit') expect(res.result).to.have.a.property('Repo') done() diff --git a/test/utils/factory/default-config.json b/test/utils/factory/default-config.json new file mode 100644 index 0000000000..20e1d1b753 --- /dev/null +++ b/test/utils/factory/default-config.json @@ -0,0 +1,68 @@ +{ + "Identity": { + "PeerID": "", + "PrivKey": "" + }, + "Datastore": { + "Type": "", + "Path": "", + "StorageMax": "", + "StorageGCWatermark": 0, + "GCPeriod": "", + "Params": null, + "NoSync": false + }, + "Addresses": { + "Swarm": [ + "/ip4/127.0.0.1/tcp/0" + ], + "API": "/ip4/127.0.0.1/tcp/0", + "Gateway": "/ip4/127.0.0.1/tcp/0" + }, + "Mounts": { + "IPFS": "/ipfs", + "IPNS": "/ipns", + "FuseAllowOther": false + }, + "Version": { + "Current": "jsipfs-dev", + "Check": "error", + "CheckDate": "0001-01-01T00:00:00Z", + "CheckPeriod": "172800000000000", + "AutoUpdate": "minor" + }, + "Discovery": { + "MDNS": { + "Enabled": true, + "Interval": 10 + } + }, + "Ipns": { + "RepublishPeriod": "", + "RecordLifetime": "", + "ResolveCacheSize": 128 + }, + "Bootstrap": [], + "Tour": { + "Last": "" + }, + "Gateway": { + "HTTPHeaders": null, + "RootRedirect": "", + "Writable": false + }, + "SupernodeRouting": { + "Servers": [] + }, + "API": { + "HTTPHeaders": null + }, + "Swarm": { + "AddrFilters": null + }, + "Log": { + "MaxSizeMB": 250, + "MaxBackups": 1, + "MaxAgeDays": 0 + } +} diff --git a/test/utils/factory/index.js b/test/utils/factory/index.js new file mode 100644 index 0000000000..463c36b8a2 --- /dev/null +++ b/test/utils/factory/index.js @@ -0,0 +1,118 @@ +'use strict' + +const PeerId = require('peer-id') +const isNode = require('detect-node') +const IPFSRepo = require('ipfs-repo') +const cleanRepo = require('../clean') +const IPFS = require('../../../src/core') +const series = require('run-series') +const defaultConfig = require('./default-config.json') + +module.exports = Factory + +function Factory () { + if (!(this instanceof Factory)) { + return new Factory() + } + + const nodes = [] + + /* yields a new started node */ + this.spawnNode = (repoPath, config, callback) => { + if (typeof repoPath === 'function') { + callback = repoPath + repoPath = undefined + } + if (typeof config === 'function') { + callback = config + config = undefined + } + + if (!repoPath) { + repoPath = '/tmp/.ipfs-' + Math.random() + .toString() + .substring(2, 8) + } + + if (!config) { + config = JSON.parse(JSON.stringify(defaultConfig)) + const pId = PeerId.create({ bits: 32 }).toJSON() + config.Identity.PeerID = pId.id + config.Identity.PrivKey = pId.privKey + } + + // set up the repo + let store + let teardown + + if (isNode) { + store = require('fs-blob-store') + teardown = (done) => { + cleanRepo(repoPath) + done() + } + } else { + const idb = window.indexedDB || + window.mozIndexedDB || + window.webkitIndexedDB || + window.msIndexedDB + store = require('idb-plus-blob-store') + teardown = (done) => { + idb.deleteDatabase(repoPath) + idb.deleteDatabase(repoPath + '/blocks') + done() + } + } + + const repo = new IPFSRepo(repoPath, { stores: store }) + repo.teardown = teardown + + // create the IPFS node + const ipfs = new IPFS(repo) + ipfs.init({ emptyRepo: true }, (err) => { + if (err) { + return callback(err) + } + repo.config.set(config, launchNode) + }) + + function launchNode () { + ipfs.load((err) => { + if (err) { + return callback(err) + } + + ipfs.goOnline((err) => { + if (err) { + return callback(err) + } + + nodes.push({ + repo: repo, + ipfs: ipfs + }) + + callback(null, ipfs) + }) + }) + } + } + + this.dismantle = function (callback) { + series(nodes.map((node) => { + return node.ipfs.goOffline + }), clean) + + function clean (err) { + if (err) { + return callback(err) + } + series( + nodes.map((node) => { + return node.repo.teardown + }), + callback + ) + } + } +}