Skip to content

Commit

Permalink
fix: assure .hoodie/data folders created when using leveldb (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
minrwhite authored and gr2m committed Jan 9, 2017
1 parent 6cff8eb commit ec3c293
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 119 deletions.
9 changes: 9 additions & 0 deletions lib/config/configure-pouchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = configurePouchDb

function configurePouchDb (state, callback) {
state.db.config = new state.PouchDB('hoodie-config').doc('hoodie')

process.nextTick(function () {
callback()
})
}
4 changes: 2 additions & 2 deletions lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var series = require('async').series

var accountConfig = require('./account')
var assureFolders = require('./assure-folders')
var configurePouchDb = require('./configure-pouchdb')
var getAdmins = require('./admins')
var getDefaults = require('./defaults')
var getSecret = require('./secret')
Expand All @@ -31,8 +32,6 @@ function getConfig (server, config, callback) {
server: server,
PouchDB: config.PouchDB,
db: {
config: new config.PouchDB('hoodie-config').doc('hoodie'),

// TODO: remove hack once .options is exposed:
// https://github.com/pouchdb/pouchdb/issues/5548
options: new config.PouchDB('hack', {skip_setup: true}).__opts
Expand All @@ -57,6 +56,7 @@ function getConfig (server, config, callback) {

series([
assureFolders.bind(null, state),
configurePouchDb.bind(null, state),
getSecret.bind(null, state),
getAdmins.bind(null, state),
accountConfig.bind(null, state),
Expand Down
191 changes: 74 additions & 117 deletions test/unit/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,128 +6,85 @@ var serverMock = {
on: function () {}
}

test('config', function (group) {
group.test('defaults', function (t) {
var accountConfigMock = simple.stub().callbackWith(null)
var adminsConfigMock = simple.stub().callbackWith(null)
var assureFolders = simple.stub().callbackWith(null)
var couchDbConfigMock = simple.stub().callbackWith(null)
var appOptionsMock = simple.stub().returnWith('app options')
var secretConfigMock = simple.stub().callbackWith(null)
var storeConfigMock = simple.stub().callbackWith(null)

var getConfig = proxyquire('../../lib/config', {
'./account': accountConfigMock,
'./assure-folders': assureFolders,
'./admins': adminsConfigMock,
'./app-options': appOptionsMock,
'./secret': secretConfigMock,
'./store': storeConfigMock,
'fs': {
statSync: simple.stub().returnWith({
isDirectory: simple.stub()
})
}
})

var docApiStub = simple.stub()
var PouchDBMock = simple.stub().returnWith({
__opts: {},
doc: docApiStub
})
PouchDBMock.preferredAdapters = ['test']
PouchDBMock.plugin = simple.stub().returnWith(PouchDBMock)

getConfig(serverMock, {
PouchDB: PouchDBMock
}, function (error, config) {
t.error(error)

var state = {
server: serverMock,
config: config,
inMemory: false,
PouchDB: PouchDBMock,
db: {
config: docApiStub(),
options: {}
;[
{
name: 'config',
preferredAdapters: ['test'],
output: {inMemory: false}
},
{
name: 'config with http adapter',
preferredAdapters: [],
output: {inMemory: false}
},
{
name: 'config with memory adapter',
preferredAdapters: ['memory'],
output: {inMemory: true}
}
].forEach(function (testConfig) {
test(testConfig.name, function (group) {
group.test('defaults', function (t) {
var accountConfigMock = simple.stub().callbackWith(null)
var adminsConfigMock = simple.stub().callbackWith(null)
var assureFolders = simple.stub().callbackWith(null)
var couchDbConfigMock = simple.stub().callbackWith(null)
var configPouchDbMock = simple.stub().callbackWith(null)
var appOptionsMock = simple.stub().returnWith('app options')
var secretConfigMock = simple.stub().callbackWith(null)
var storeConfigMock = simple.stub().callbackWith(null)

var getConfig = proxyquire('../../lib/config', {
'./account': accountConfigMock,
'./assure-folders': assureFolders,
'./admins': adminsConfigMock,
'./app-options': appOptionsMock,
'./configure-pouchdb': configPouchDbMock,
'./secret': secretConfigMock,
'./store': storeConfigMock,
'fs': {
statSync: simple.stub().returnWith({
isDirectory: simple.stub()
})
}
}

t.is(couchDbConfigMock.callCount, 0, 'couchdb config not called')
t.same(accountConfigMock.lastCall.arg, state, 'called account config')
t.same(storeConfigMock.lastCall.arg, state, 'called store config')

t.ok(secretConfigMock.lastCall.k < adminsConfigMock.lastCall.k, 'secret config called before admins config')
t.ok(adminsConfigMock.lastCall.k < accountConfigMock.lastCall.k, 'admin config called before account config')
t.ok(adminsConfigMock.lastCall.k < storeConfigMock.lastCall.k, 'admin config called before store config')

t.end()
})
})

group.end()
})

test('config with http adapter', function (group) {
group.test('defaults', function (t) {
var accountConfigMock = simple.stub().callbackWith(null)
var adminsConfigMock = simple.stub().callbackWith(null)
var assureFolders = simple.stub().callbackWith(null)
var couchDbConfigMock = simple.stub().callbackWith(null)
var appOptionsMock = simple.stub().returnWith('app options')
var secretConfigMock = simple.stub().callbackWith(null)
var storeConfigMock = simple.stub().callbackWith(null)

var getConfig = proxyquire('../../lib/config', {
'./account': accountConfigMock,
'./assure-folders': assureFolders,
'./admins': adminsConfigMock,
'./app-options': appOptionsMock,
'./secret': secretConfigMock,
'./store': storeConfigMock,
'fs': {
statSync: simple.stub().returnWith({
isDirectory: simple.stub()
})
}
})

var docApiStub = simple.stub()
var PouchDBMock = simple.stub().returnWith({
__opts: {},
doc: docApiStub
})
PouchDBMock.preferredAdapters = []
PouchDBMock.plugin = simple.stub().returnWith(PouchDBMock)

getConfig(serverMock, {
PouchDB: PouchDBMock
}, function (error, config) {
t.error(error)

var state = {
server: serverMock,
config: config,
inMemory: false,
PouchDB: PouchDBMock,
db: {
config: docApiStub(),
options: {}
})

var PouchDBMock = simple.stub().returnWith({
__opts: {}
})
PouchDBMock.preferredAdapters = testConfig.preferredAdapters
PouchDBMock.plugin = simple.stub().returnWith(PouchDBMock)

getConfig(serverMock, {
PouchDB: PouchDBMock
}, function (error, config) {
t.error(error)

var state = {
server: serverMock,
config: config,
inMemory: testConfig.output.inMemory,
PouchDB: PouchDBMock,
db: {
options: {}
}
}
}

t.is(couchDbConfigMock.callCount, 0, 'couchdb config not called')
t.same(accountConfigMock.lastCall.arg, state, 'called account config')
t.same(storeConfigMock.lastCall.arg, state, 'called store config')
t.is(couchDbConfigMock.callCount, 0, 'couchdb config not called')
t.same(configPouchDbMock.lastCall.arg, state, 'called config PouchDB')
t.same(accountConfigMock.lastCall.arg, state, 'called account config')
t.same(storeConfigMock.lastCall.arg, state, 'called store config')

t.ok(secretConfigMock.lastCall.k < adminsConfigMock.lastCall.k, 'secret config called before admins config')
t.ok(adminsConfigMock.lastCall.k < accountConfigMock.lastCall.k, 'admin config called before account config')
t.ok(adminsConfigMock.lastCall.k < storeConfigMock.lastCall.k, 'admin config called before store config')
t.ok(assureFolders.lastCall.k < configPouchDbMock.lastCall.k, 'assure folders called before config PouchDB')
t.ok(configPouchDbMock.lastCall.k < secretConfigMock.lastCall.k, 'config PouchDB called before secret config')
t.ok(secretConfigMock.lastCall.k < adminsConfigMock.lastCall.k, 'secret config called before admins config')
t.ok(adminsConfigMock.lastCall.k < accountConfigMock.lastCall.k, 'admin config called before account config')
t.ok(adminsConfigMock.lastCall.k < storeConfigMock.lastCall.k, 'admin config called before store config')

t.end()
t.end()
})
})
})

group.end()
group.end()
})
})
28 changes: 28 additions & 0 deletions test/unit/config/configure-pouchdb-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var simple = require('simple-mock')
var test = require('tap').test

var configurePouchDB = require('../../../lib/config/configure-pouchdb')

test('pouchdb config', function (t) {
var docApiStub = simple.stub().returnWith('hoodie-config-db')
var PouchDBMock = simple.stub().returnWith({
doc: docApiStub
})
var state = {
db: {},
PouchDB: PouchDBMock
}

configurePouchDB(state, function (error) {
t.error(error)

t.is(PouchDBMock.calls.length, 1, 'PouchDB constructor called once')
t.is(PouchDBMock.lastCall.arg, 'hoodie-config', 'PouchDB constructor called with \'hoodie-config\'')
t.is(docApiStub.calls.length, 1, 'PouchDB\'s doc method called once')
t.is(docApiStub.lastCall.arg, 'hoodie', 'doc method called with \'hoodie\'')

t.is(state.db.config, 'hoodie-config-db', 'config key in state.db set')

t.end()
})
})

0 comments on commit ec3c293

Please sign in to comment.