Skip to content

Commit

Permalink
fix: replace tape with tap and add code standard
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes J. Schmidt committed Feb 9, 2016
1 parent fa24919 commit 70bcd02
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.nyc_output
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ var options = minimist(process.argv.slice(2), {
})

if (!options._.length) {
return console.log('Usage: \ncouchdb-bootstrap URL [SOURCE] [OPTIONS]')
console.log('Usage: \ncouchdb-bootstrap URL [SOURCE] [OPTIONS]')
process.exit()
}

var url = options._[0]
var source = options._[1] || process.cwd()

bootstrap(url, source, options, function(error, response) {
bootstrap(url, source, options, function (error, response) {
if (error) return console.error(error)

console.log(JSON.stringify(response, null, ' '))
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var configure = require('./lib/configure')
var secure = require('./lib/secure')
var push = require('./lib/push')

module.exports = function(url, source, options, callback) {
module.exports = function (url, source, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
Expand Down
4 changes: 2 additions & 2 deletions lib/configure.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
var configure = require('couchdb-configure')
var utils = require('./utils')

module.exports = function(url, source, options, callback) {
utils.config(source, function(error, filename) {
module.exports = function (url, source, options, callback) {
utils.config(source, function (error, filename) {
if (error) return callback(error)
if (!filename) return callback()

Expand Down
11 changes: 5 additions & 6 deletions lib/push.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
var push = require('couchdb-push')
var path = require('path')
var async = require('async')
var utils = require('./utils')

module.exports = function(couch, source, options, callback) {
module.exports = function (couch, source, options, callback) {
options.concurrency = 'concurrency' in options ? options.concurrency : 100
options.index = 'index' in options ? options.index : true

utils.dbs(source, function(error, filenames) {
utils.dbs(source, function (error, filenames) {
if (error) return callback(error)

async.map(filenames, function(filename, done) {
async.map(filenames, function (filename, done) {
var dbname = utils.mapDbName(filename, options)
var db = couch.use(dbname)

utils.docs(filename, function(error, filenames) {
utils.docs(filename, function (error, filenames) {
if (error) return done(error)

async.mapLimit(filenames, options.concurrency, function(filename, next) {
async.mapLimit(filenames, options.concurrency, function (filename, next) {
push(db, filename, options, next)
}, utils.groupByDatabase(dbname, done))
})
Expand Down
9 changes: 4 additions & 5 deletions lib/secure.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
var path = require('path')
var async = require('async')
var secure = require('couchdb-secure')
var utils = require('./utils')

module.exports = function(couch, source, options, callback) {
utils.dbs(source, function(error, filenames) {
module.exports = function (couch, source, options, callback) {
utils.dbs(source, function (error, filenames) {
if (error) return callback(error)

async.map(filenames, function(filename, done) {
async.map(filenames, function (filename, done) {
var dbname = utils.mapDbName(filename, options)
var db = couch.use(dbname)

utils.security(filename, function(error, filename) {
utils.security(filename, function (error, filename) {
if (error) return done(error)
if (!filename) return done()

Expand Down
31 changes: 15 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ var DBS = '{_users,_replicator,[^_]*}'
var DOCS = '{_design/*,_local/*,[^_]*}'
var SECURITY = '{_security.json,_security.js,_security}'


function singleFile(callback) {
return function(error, filenames) {
function singleFile (callback) {
return function (error, filenames) {
if (error) return callback(error)

if (filenames.length > 1) {
Expand All @@ -26,30 +25,30 @@ function singleFile(callback) {
}
}

exports.config = function(source, callback) {
exports.config = function (source, callback) {
glob(path.join(source, CONFIG), singleFile(callback))
}

exports.dbs = function(source, callback) {
exports.dbs = function (source, callback) {
glob(path.join(source, DBS), callback)
}

exports.docs = function(source, callback) {
exports.docs = function (source, callback) {
glob(path.join(source, DOCS), callback)
}

exports.security = function(source, callback) {
exports.security = function (source, callback) {
glob(path.join(source, SECURITY), singleFile(callback))
}

exports.ignoreError = function(callback, defaultResponse) {
return function(error, response) {
exports.ignoreError = function (callback, defaultResponse) {
return function (_, response) {
callback(null, response || defaultResponse)
}
}

exports.groupByDatabase = function(dbname, callback) {
return function(error, results) {
exports.groupByDatabase = function (dbname, callback) {
return function (error, results) {
if (error) return callback(error)

var result = {}
Expand All @@ -59,14 +58,14 @@ exports.groupByDatabase = function(dbname, callback) {
}
}

exports.reduceGroupedResult = function(callback) {
return function(error, results) {
exports.reduceGroupedResult = function (callback) {
return function (error, results) {
if (error) return callback(error)

var result = results.reduce(function(memo, res) {
var result = results.reduce(function (memo, res) {
if (typeof res !== 'object') return memo

Object.keys(res).forEach(function(key) {
Object.keys(res).forEach(function (key) {
memo[key] = res[key]
})

Expand All @@ -79,7 +78,7 @@ exports.reduceGroupedResult = function(callback) {

exports.mapDbName = function (filename, options) {
var dbName = path.basename(filename)
if (!options) return dbNames
if (!options) return dbName
if (typeof options.mapDbName === 'object') return options.mapDbName[dbName] || dbName
if (typeof options.mapDbName === 'function') return options.mapDbName(dbName)

Expand Down
14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"cdbb": "cli.js"
},
"scripts": {
"test": "tape test/*.js | tap-spec",
"pretest": "standard",
"test": "tap --coverage test/test-*.js",
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
},
"repository": {
Expand All @@ -29,9 +30,11 @@
"homepage": "https://github.com/eHealthAfrica/couchdb-bootstrap#readme",
"devDependencies": {
"nano": "^6.2.0",
"semantic-release": "^6.0.3",
"standard": "^6.0.4",
"tap": "^5.4.3",
"tap-spec": "^4.1.1",
"tape": "^4.4.0",
"semantic-release": "^6.0.3"
"tape": "^4.4.0"
},
"dependencies": {
"async": "^1.5.2",
Expand All @@ -41,5 +44,10 @@
"glob": "^6.0.4",
"minimist": "^1.2.0",
"nano-option": "^1.2.2"
},
"standard": {
"ignore": [
"test/fixtures/**/*"
]
}
}
14 changes: 6 additions & 8 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
var path = require('path')
var async = require('async')
var nano = require('nano')


exports.docs = {
'_users': [
Expand All @@ -18,7 +16,7 @@ exports.docs = {
exports.configSection = 'couchdb-bootstrap'

exports.dbnames = Object.keys(exports.docs)
.filter(function(dbname) {
.filter(function (dbname) {
return dbname[0] !== '_'
})

Expand All @@ -27,24 +25,24 @@ exports.source = path.join(__dirname, 'fixtures')

exports.couch = require('nano')(exports.url)

exports.setup = function(callback) {
exports.setup = function (callback) {
async.each(exports.dbnames, exports.couch.db.destroy, callback)
}

exports.createDatabases = function(callback) {
exports.createDatabases = function (callback) {
async.each(exports.dbnames, exports.couch.db.create, callback)
}

// There is an issue with section deletion in CouchDB.
// You cannot delete an entire section:
// $ curl -XDELETE http://localhost:5984/_config/couchdb-bootstrap
// {"error":"method_not_allowed","reason":"Only GET,PUT,DELETE allowed"}
exports.clearConfig = function(callback) {
exports.clearConfig = function (callback) {
exports.couch.request({
path: '_config/' + exports.configSection
}, function(error, config) {
}, function (error, config) {
if (error) return callback(error)
async.map(Object.keys(config), function(key, next) {
async.map(Object.keys(config), function (key, next) {
exports.couch.request({
method: 'DELETE',
path: '_config/' + exports.configSection + '/' + encodeURIComponent(key)
Expand Down
28 changes: 15 additions & 13 deletions test/test-configure.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
var test = require('tape')
var test = require('tap').test
var helper = require('./helper')
var configure = require('../lib/configure')

test('configure', function(t) {
helper.clearConfig(function(error) {
t.error(error, 'no error occured')
test('configure', function (t) {
helper.clearConfig(function (error) {
t.error(error)

configure(helper.couch, helper.source, {}, function(error, responses) {
t.error(error, 'no error occured')
configure(helper.couch, helper.source, {}, function (error, responses) {
t.error(error)

helper.couch.request({
path: '_config/couchdb-bootstrap/foo'
}, function(error, config) {
}, function (error, config) {
t.error(error)
t.equal(config, 'bar')
t.end()
})
})
})
})

test('configure with trailing slash', function(t) {
helper.clearConfig(function(error) {
t.error(error, 'no error occured')
test('configure with trailing slash', function (t) {
helper.clearConfig(function (error) {
t.error(error)

configure(helper.url + '/', helper.source, {}, function(error, responses) {
t.error(error, 'no error occured')
configure(helper.url + '/', helper.source, {}, function (error, responses) {
t.error(error)

helper.couch.request({
path: '_config/couchdb-bootstrap/foo'
}, function(error, config) {
}, function (error, config) {
t.error(error)
t.equal(config, 'bar')
t.end()
})
Expand Down
8 changes: 4 additions & 4 deletions test/test-couchdb-bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var test = require('tape')
var test = require('tap').test
var helper = require('./helper')
var bootstrap = require('..')

test('basics', function(t) {
bootstrap(helper.url, helper.source, function(error, response) {
t.error(error, 'no error occured')
test('basics', function (t) {
bootstrap(helper.url, helper.source, function (error, response) {
t.error(error)

t.end()
})
Expand Down
18 changes: 7 additions & 11 deletions test/test-map-db-name-option.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
var test = require('tape')
var test = require('tap').test
var helper = require('./helper')
var bootstrap = require('..')

test('options.mapDbName is object', function(t) {
test('options.mapDbName is object', function (t) {
bootstrap(helper.url, helper.source, {
mapDbName: {
'test-couchdb-bootstrap': 'custom-db-name'
}
}, function(error, response) {
t.error(error, 'no error occured')

}, function (error, response) {
t.error(error)
t.ok(response.secure['custom-db-name'].ok, 'creates security in db with custom name')
t.ok(response.push['custom-db-name'], 'pushes docs to db with custom name')

t.end()
})
})

test('options.mapDbName is function', function(t) {
test('options.mapDbName is function', function (t) {
bootstrap(helper.url, helper.source, {
mapDbName: function (name) { return name.replace('test', 'foobar') }
}, function(error, response) {
t.error(error, 'no error occured')

}, function (error, response) {
t.error(error)
t.ok(response.secure['foobar-couchdb-bootstrap'], 'creates security in db with custom name')
t.ok(response.push['foobar-couchdb-bootstrap'], 'pushes docs to db with custom name')

t.end()
})
})
16 changes: 8 additions & 8 deletions test/test-push.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
var test = require('tape')
var test = require('tap').test
var helper = require('./helper')
var push = require('../lib/push')

test('push docs', function(t) {
helper.setup(function(error) {
t.error(error, 'no error occured')
test('push docs', function (t) {
helper.setup(function (error) {
t.error(error)

push(helper.couch, helper.source, {}, function(error, response) {
t.error(error, 'no error occured')
push(helper.couch, helper.source, {}, function (error, response) {
t.error(error)

Object.keys(helper.docs).forEach(function(db) {
Object.keys(helper.docs).forEach(function (db) {
t.ok(db in response, db + ' included')

var responses = response[db]
var docs = helper.docs[db]

t.equal(responses.length, docs.length, 'correct # of docs pushed')

responses.forEach(function(r) {
responses.forEach(function (r) {
t.ok(r.ok, 'response is ok')
t.ok(docs.indexOf(r.id) > -1, db + '/' + r.id + ' has been pushed')
})
Expand Down
Loading

0 comments on commit 70bcd02

Please sign in to comment.