diff --git a/tests/test-core/test-init.js b/tests/test-core/test-init.js index 5830781488..2dde342f85 100644 --- a/tests/test-core/test-init.js +++ b/tests/test-core/test-init.js @@ -4,10 +4,35 @@ const expect = require('chai').expect const IPFS = require('../../src/ipfs-core') const IPFSRepo = require('/home/stephen/Projects/Forks/js-ipfs-repo') -function createEmptyTempRepo () { - const isNode = !global.window +function createTestRepo () { + const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/' + // console.log('repo', repoPath) - var store = isNode ? require('fs-blob-store') : require('idb-plus-blob-store') + var store + var teardown + + const isNode = !global.window + if (isNode) { + store = require('fs-blob-store') + teardown = (done) => { + const rimraf = require('rimraf') + rimraf(repoPath, (err) => { + expect(err).to.not.exist + 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 options = { bits: 64, @@ -20,15 +45,19 @@ function createEmptyTempRepo () { version: store } } - const repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + '/' - return new IPFSRepo(repoPath, options) + + var repo = new IPFSRepo(repoPath, options) + + repo.teardown = teardown + + return repo } describe('init', function () { this.timeout(10000) it('basic', (done) => { - var repo = createEmptyTempRepo() + var repo = createTestRepo() const ipfs = new IPFS(repo) ipfs.init({}, (err) => { expect(err).to.not.exist @@ -41,34 +70,15 @@ describe('init', function () { expect(err).to.not.exist expect(config.Identity).to.exist - done() - }) - }) - }) - }) - - it('force init (overwrite)', (done) => { - var repo = createEmptyTempRepo() - const ipfs1 = new IPFS(repo) - const ipfs2 = new IPFS(repo) - ipfs1.init({}, (err) => { - expect(err).to.not.exist - - ipfs2.init({ force: false }, (err) => { - expect(err).to.exist - - ipfs2.init({ force: true }, (err) => { - expect(err).to.not.exist - - done() + repo.teardown(done) }) }) }) }) it('set # of bits in key', (done) => { - var repo1 = createEmptyTempRepo() - var repo2 = createEmptyTempRepo() + var repo1 = createTestRepo() + var repo2 = createTestRepo() const ipfsShort = new IPFS(repo1) const ipfsLong = new IPFS(repo2) ipfsShort.init({ bits: 128 }, (err) => { @@ -84,12 +94,33 @@ describe('init', function () { expect(err).to.not.exist expect(config1.Identity.PrivKey.length).is.below(config2.Identity.PrivKey.length) - done() + repo1.teardown(() => { + repo2.teardown(done) + }) }) }) }) }) }) + it('force init (overwrite)', (done) => { + var repo = createTestRepo() + const ipfs1 = new IPFS(repo) + const ipfs2 = new IPFS(repo) + ipfs1.init({ bits: 128 }, (err) => { + expect(err).to.not.exist + + ipfs2.init({ bits: 128, force: false }, (err) => { + expect(err).to.exist + + ipfs2.init({ force: true }, (err) => { + expect(err).to.not.exist + + repo.teardown(done) + }) + }) + }) + }) + // test --empty-repo })