Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
adds (ugly) init test teardown
Browse files Browse the repository at this point in the history
  • Loading branch information
hackergrrl committed Mar 19, 2016
1 parent 04d345c commit 2241be6
Showing 1 changed file with 60 additions and 29 deletions.
89 changes: 60 additions & 29 deletions tests/test-core/test-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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) => {
Expand All @@ -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
})

0 comments on commit 2241be6

Please sign in to comment.