Skip to content
This repository has been archived by the owner on Mar 1, 2021. It is now read-only.

Commit

Permalink
Initial commit for this.
Browse files Browse the repository at this point in the history
Working and looks like tests are passing but need README info and added tests for the relative paths.
  • Loading branch information
Jack Williams committed Jun 21, 2016
1 parent 5309eef commit 9df4267
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 62 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
var up = require('./lib/up')
var dirname = require('path').dirname
var working_dir = dirname(module.parent.filename)

var up = require('./lib/up')(working_dir)
up.up = up

module.exports = up
8 changes: 7 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ module.exports = {
lookup: lookup
}

function load (paths) {
function load (working_dir, paths) {
var callbacks = []

if (typeof working_dir !== 'string') {
throw new Error('Error loading files; invalid working directory', working_dir)
}

if (!(paths instanceof Array)) {
throw new Error('Error loading files; invalid "paths" argument', paths)
}

paths.forEach(function (path) {
path = resolve(working_dir, path)

lookup(path).forEach(function (item) {
var func = require(resolve(item))

Expand Down
11 changes: 9 additions & 2 deletions lib/up.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
var debug = require('debug')('microboot:up')
var load = require('./loader').load
var run = require('./run')
var working_dir

module.exports = function up (phases, callback) {
module.exports = function (root) {
working_dir = root

return up
}

function up (phases, callback) {
this.opts = this.opts || {}

if (!(phases instanceof Array)) {
Expand All @@ -21,7 +28,7 @@ module.exports = function up (phases, callback) {

debug('Loading phases...')

var funcs = load(phases)
var funcs = load(working_dir, phases)

debug('Running phases...')

Expand Down
111 changes: 60 additions & 51 deletions test/lib/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,35 @@ describe('microboot/lib/loader', function () {
})

describe('-> load', function () {
it('should throw if not given an array for "paths"', function () {
var err = 'invalid "paths" argument'
it('should throw if not given a valid working directory', function () {
var err = 'invalid working directory'

expect(loader.load.bind(loader.load, 'test')).to.throw(err)
expect(loader.load.bind(loader.load, {})).to.throw(err)
expect(loader.load.bind(loader.load, null)).to.throw(err)
expect(loader.load.bind(loader.load, 123)).to.throw(err)
expect(loader.load.bind(loader.load, {})).to.throw(err)
expect(loader.load.bind(loader.load, [])).to.throw(err)
expect(loader.load.bind(loader.load)).to.throw(err)
})

it('should throw if not given an array for "paths"', function () {
var err = 'invalid "paths" argument'

expect(loader.load.bind(loader.load, __dirname, 'test')).to.throw(err)
expect(loader.load.bind(loader.load, __dirname, {})).to.throw(err)
expect(loader.load.bind(loader.load, __dirname, null)).to.throw(err)
expect(loader.load.bind(loader.load, __dirname, 123)).to.throw(err)
expect(loader.load.bind(loader.load, __dirname)).to.throw(err)
})

it('should order results by paths then callbacks', function () {
var first = require(resolve('test/data/timings/first/1'))
var second = require(resolve('test/data/timings/first/2'))
var sidejob = require(resolve('test/data/timings/first/3/sidejob'))
var third = require(resolve('test/data/timings/second/3'))
var fourth = require(resolve('test/data/timings/second/4'))

var callbacks = loader.load([
'test/data/timings/second',
'test/data/timings/first'
var first = require('../data/timings/first/1')
var second = require('../data/timings/first/2')
var sidejob = require('../data/timings/first/3/sidejob')
var third = require('../data/timings/second/3')
var fourth = require('../data/timings/second/4')

var callbacks = loader.load(__dirname, [
'../data/timings/second',
'../data/timings/first'
])

expect(callbacks).to.be.an('array')
Expand All @@ -44,20 +53,20 @@ describe('microboot/lib/loader', function () {
})

it('should throw if a found file does not export a function', function () {
expect(loader.load.bind(loader.load, [
'test/data/errors'
expect(loader.load.bind(loader.load, __dirname, [
'../data/errors'
])).to.throw('all phases must be functions')
})

it('should return an empty array if no paths are given', function () {
var callbacks = loader.load([])
var callbacks = loader.load(__dirname, [])

expect(callbacks).to.be.an('array')
expect(callbacks).to.be.empty
})

it('should return an array of callbacks if successful', function () {
var callbacks = loader.load(['test/data/timings'])
var callbacks = loader.load(__dirname, ['../data/timings'])

expect(callbacks).to.be.an('array')
expect(callbacks).to.have.lengthOf(5)
Expand All @@ -76,100 +85,100 @@ describe('microboot/lib/loader', function () {
})

it('should find a single JS file if given an extensionless path', function () {
var files = loader.lookup('test/data/fake/database')
var files = loader.lookup(resolve(__dirname, '../data/fake/database'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(1)
expect(files[0]).to.equal('test/data/fake/database.js')
expect(files[0]).to.equal(resolve(__dirname, '../data/fake/database.js'))
})

it('should not return hidden files', function () {
var files = loader.lookup('test/data/fake')
var files = loader.lookup(resolve(__dirname, '../data/fake'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(2)
expect(files).to.not.contain('test/data/fake/.hidden.js')
expect(files).to.not.contain(resolve(__dirname, '../data/fake/.hidden.js'))
})

it('should not return files that don\'t have the ".js" extension', function () {
var files = loader.lookup('test/data/fake')
var files = loader.lookup(resolve(__dirname, '../data/fake'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(2)
expect(files).to.not.contain('test/data/fake/test.jpg')
expect(files).to.not.contain(resolve(__dirname, '../data/fake/test.jpg'))
})

it('should remove hidden files when searching an extensionless path', function () {
var files = loader.lookup('test/data/fake/**')
var files = loader.lookup(resolve(__dirname, '../data/fake/**'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(2)
expect(files).to.not.contain('test/data/fake/.hidden.js')
expect(files).to.not.contain(resolve(__dirname, '../data/fake/.hidden.js'))
})

it('should remove non-.js files when searching an extensionless path', function () {
var files = loader.lookup('test/data/fake/**')
var files = loader.lookup(resolve(__dirname, '../data/fake/**'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(2)
expect(files).to.not.contain('test/data/fake/test.jpg')
expect(files).to.not.contain(resolve(__dirname, '../data/fake/test.jpg'))
})

it('should return files ordered', function () {
var files = loader.lookup('test/data/timings')
var files = loader.lookup(resolve(__dirname, '../data/timings'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(5)

expect(files[0]).to.equal('test/data/timings/first/1.js')
expect(files[1]).to.equal('test/data/timings/first/2.js')
expect(files[2]).to.equal('test/data/timings/first/3/sidejob.js')
expect(files[3]).to.equal('test/data/timings/second/3.js')
expect(files[4]).to.equal('test/data/timings/second/4.js')
expect(files[0]).to.equal(resolve(__dirname, '../data/timings/first/1.js'))
expect(files[1]).to.equal(resolve(__dirname, '../data/timings/first/2.js'))
expect(files[2]).to.equal(resolve(__dirname, '../data/timings/first/3/sidejob.js'))
expect(files[3]).to.equal(resolve(__dirname, '../data/timings/second/3.js'))
expect(files[4]).to.equal(resolve(__dirname, '../data/timings/second/4.js'))
})

it('should find a single JS file if given a specific file', function () {
var files = loader.lookup('test/data/timings/second/3.js')
var files = loader.lookup(resolve(__dirname, '../data/timings/second/3.js'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(1)
expect(files[0]).to.equal('test/data/timings/second/3.js')
expect(files[0]).to.equal(resolve(__dirname, '../data/timings/second/3.js'))
})

it('should recursively search directories if just a path name given', function () {
var files = loader.lookup('test/data/timings')
var files = loader.lookup(resolve(__dirname, '../data/timings'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(5)

expect(files[0]).to.equal('test/data/timings/first/1.js')
expect(files[1]).to.equal('test/data/timings/first/2.js')
expect(files[2]).to.equal('test/data/timings/first/3/sidejob.js')
expect(files[3]).to.equal('test/data/timings/second/3.js')
expect(files[4]).to.equal('test/data/timings/second/4.js')
expect(files[0]).to.equal(resolve(__dirname, '../data/timings/first/1.js'))
expect(files[1]).to.equal(resolve(__dirname, '../data/timings/first/2.js'))
expect(files[2]).to.equal(resolve(__dirname, '../data/timings/first/3/sidejob.js'))
expect(files[3]).to.equal(resolve(__dirname, '../data/timings/second/3.js'))
expect(files[4]).to.equal(resolve(__dirname, '../data/timings/second/4.js'))
})

it('should recursively search directories if path name and "**" given', function () {
var files = loader.lookup('test/data/timings/**')
var files = loader.lookup(resolve(__dirname, '../data/timings/**'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(5)

expect(files[0]).to.equal('test/data/timings/first/1.js')
expect(files[1]).to.equal('test/data/timings/first/2.js')
expect(files[2]).to.equal('test/data/timings/first/3/sidejob.js')
expect(files[3]).to.equal('test/data/timings/second/3.js')
expect(files[4]).to.equal('test/data/timings/second/4.js')
expect(files[0]).to.equal(resolve(__dirname, '../data/timings/first/1.js'))
expect(files[1]).to.equal(resolve(__dirname, '../data/timings/first/2.js'))
expect(files[2]).to.equal(resolve(__dirname, '../data/timings/first/3/sidejob.js'))
expect(files[3]).to.equal(resolve(__dirname, '../data/timings/second/3.js'))
expect(files[4]).to.equal(resolve(__dirname, '../data/timings/second/4.js'))
})

it('should not recursively search directories if "*" given after path', function () {
var files = loader.lookup('test/data/timings/first/*')
var files = loader.lookup(resolve(__dirname, '../data/timings/first/*'))

expect(files).to.be.an('array')
expect(files).to.have.lengthOf(2)

expect(files[0]).to.equal('test/data/timings/first/1.js')
expect(files[1]).to.equal('test/data/timings/first/2.js')
expect(files[0]).to.equal(resolve(__dirname, '../data/timings/first/1.js'))
expect(files[1]).to.equal(resolve(__dirname, '../data/timings/first/2.js'))
})
})
})
19 changes: 12 additions & 7 deletions test/lib/up.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var up = require('../../lib/up')
var up_init = require('../../lib/up')
var up = up_init(__dirname)

describe('microboot/lib/up', function () {
beforeEach(function () {
Expand All @@ -10,7 +11,11 @@ describe('microboot/lib/up', function () {
global.end4 = false
})

it('should export up function', function () {
it('should export init function', function () {
expect(up_init).to.be.a('function')
})

it('should return up function', function () {
expect(up).to.be.a('function')
})

Expand All @@ -26,7 +31,7 @@ describe('microboot/lib/up', function () {
})

it('should succeed if passed valid phases but no callback', function () {
expect(up.bind(up, ['test/data/fake'])).to.not.throw()
expect(up.bind(up, ['../data/fake'])).to.not.throw()
})

it('should search for phases based on CWD', function () {
Expand All @@ -38,13 +43,13 @@ describe('microboot/lib/up', function () {
})

it('should fail if passed a mixture of valid and invalid phases', function () {
expect(up.bind(up, ['test/data/fake', 'na'])).to.throw('Cannot resolve path')
expect(up.bind(up, ['../data/fake', 'na'])).to.throw('Cannot resolve path')
})

it('should run database and endpoints phase', function (done) {
this.slow(200)

up(['test/data/fake'], function () {
up(['../data/fake'], function () {
expect(global.database).to.equal(true)
expect(global.endpoints).to.equal(true)

Expand All @@ -53,15 +58,15 @@ describe('microboot/lib/up', function () {
})

it('should run items in order', function (done) {
up(['test/data/timings/first'], function () {
up(['../data/timings/first'], function () {
expect(global.end1).to.be.below(global.end2)

done()
})
})

it('should run phases in order', function (done) {
up(['test/data/timings/second', 'test/data/timings/first'], function () {
up(['../data/timings/second', '../data/timings/first'], function () {
expect(global.end3).to.be.below(global.end4)
expect(global.end4).to.be.below(global.end1)
expect(global.end1).to.be.below(global.end2)
Expand Down

0 comments on commit 9df4267

Please sign in to comment.