Skip to content

Commit

Permalink
Added tests; muting appview tests for now until I figure out how to d…
Browse files Browse the repository at this point in the history
…eal with proper initiallization of the charm object.
  • Loading branch information
airportyh committed Aug 15, 2012
1 parent d912782 commit c811a86
Show file tree
Hide file tree
Showing 12 changed files with 482 additions and 214 deletions.
76 changes: 42 additions & 34 deletions lib/appview.js
Expand Up @@ -10,7 +10,7 @@ text scrolling functionality - Substack didn't take my pull request :( )
*/

require('./patchcharm.js')
var charm = require('charm')(process)
var charm = initCharm()
, spawn = require('child_process').spawn
, tty = require('tty')
, log = require('winston')
Expand All @@ -29,42 +29,50 @@ var setRawMode = process.stdin.setRawMode ?
function(bool){ process.stdin.setRawMode(bool) } :
tty.setRawMode

// allow charm.write() to take any object: just convert the passed in object to a string
charm.write = function(charm, write){
return function(obj){
if (!(obj instanceof Buffer) && typeof obj !== 'string'){
obj = String(obj)
}
return write.call(charm, obj)
}
}(charm, charm.write)
// TODO fix tests for this. Make charm object swappable for testing.

// A wrapper around charm (gives the same API) that automatically parks the cursor
// to the bottom right corner when not in use
charm = function(charm){
var timeoutID
function parkCursor(){
getTermSize(function(cols, lines){
charm.position(cols, lines)
})
}
function wrapFunc(func){
return function(){
if (timeoutID) clearTimeout(timeoutID)
var retval = func.apply(charm, arguments)
timeoutID = setTimeout(parkCursor, 150)
return retval
function initCharm(){
// A wrapper around charm (gives the same API) that automatically parks the cursor
// to the bottom right corner when not in use
var charm = function(charm){
var timeoutID
function parkCursor(){
getTermSize(function(cols, lines){
charm.position(cols, lines)
})
}
}
var cursorParker = {}
for (var prop in charm){
var value = charm[prop]
if (typeof value === 'function'){
cursorParker[prop] = wrapFunc(value)
function wrapFunc(func){
return function(){
if (timeoutID) clearTimeout(timeoutID)
var retval = func.apply(charm, arguments)
timeoutID = setTimeout(parkCursor, 150)
return retval
}
}
}
return cursorParker
}(charm)
var cursorParker = {}
for (var prop in charm){
var value = charm[prop]
if (typeof value === 'function'){
cursorParker[prop] = wrapFunc(value)
}
}
return cursorParker
}(require('charm')(process))
// allow charm.write() to take any object: just convert the passed in object to a string
charm.write = function(charm, write){
return function(obj){
if (!(obj instanceof Buffer) && typeof obj !== 'string'){
obj = String(obj)
}
return write.call(charm, obj)
}
}(charm, charm.write)
return charm
}





// ============== Backbone-based View Models ============================

Expand Down
13 changes: 6 additions & 7 deletions lib/config.js
Expand Up @@ -18,8 +18,6 @@ var fs = require('fs')
, Chars = require('./chars')
, pad = require('./strutils').pad

require('colors')

var fileExists = fs.exists || path.exists

function Config(appMode, progOptions){
Expand Down Expand Up @@ -61,7 +59,6 @@ Config.prototype.readConfigFile = function(configFile, callback){
}

Config.prototype.readYAML = function(configFile, callback){
log.info('readYAML')
var self = this
fs.readFile(configFile, function (err, data) {
if (!err){
Expand All @@ -73,7 +70,6 @@ Config.prototype.readYAML = function(configFile, callback){
}

Config.prototype.readJSON = function(configFile, callback){
log.info('readJSON')
var self = this
fs.readFile(configFile, function (err, data) {
if (!err){
Expand Down Expand Up @@ -113,15 +109,14 @@ Config.prototype.getLaunchers = function(app, cb){
var customLaunchers = self.get('launchers')
if (customLaunchers){
for (var name in customLaunchers){
log.info('Installing custom launcher ' + name)
availableLaunchers[name.toLowerCase()] = new Launcher(name, customLaunchers[name], app)
}
}
cb(self.getWantedLaunchers(availableLaunchers))
})
}

Config.prototype.getWantedLaunchers = function(available){
Config.prototype.getWantedLauncherNames = function(available){
var launchers, skip
launchers = (
(launchers = this.get('launch')) ?
Expand All @@ -138,7 +133,11 @@ Config.prototype.getWantedLaunchers = function(available){
return skip.indexOf(name) === -1
})
}
return launchers.map(function(name){
return launchers
}

Config.prototype.getWantedLaunchers = function(available){
return this.getWantedLauncherNames(available).map(function(name){
return available[name.toLowerCase()]
})
}
Expand Down
13 changes: 10 additions & 3 deletions lib/launcher.js
Expand Up @@ -56,10 +56,12 @@ Launcher.prototype = {
var url = app.url
var settings = this.settings
var self = this
var options = {}
if (settings.cwd) options.cwd = settings.cwd
if (settings.exe){

function spawn(exe){
self.process = child_process.spawn(exe, args)
self.process = child_process.spawn(exe, args, options)
self.process.once('exit', self.onExit.bind(self))
self.emit('processStarted', self.process)
if (cb) cb(self.process)
Expand All @@ -80,18 +82,23 @@ Launcher.prototype = {
}

}else if (settings.command){
this.process = child_process.exec(settings.command)
this.process = child_process.exec(settings.command, options)
this.process.once('exit', self.onExit.bind(self))
self.emit('processStarted', self.process)
if (cb) cb(self.process)
}
}
, onExit: function(code){

this.exitCode = code
this.emit('processExit', code)
this.process = null
}
, kill: function(sig, cb){
if (!this.process) return
if (!this.process){
if(cb) cb(this.exitCode)
return
}
sig = sig || 'SIGKILL'
if (cb){
this.process.once('exit', cb)
Expand Down
1 change: 0 additions & 1 deletion lib/runners.js
Expand Up @@ -92,7 +92,6 @@ var BrowserRunner = Backbone.Model.extend({
})
client.on('browser-login', function(browserName){
self.set('name', browserName)
log.info('browser-login ' + browserName)
})
client.on('tests-start', function(){
self.trigger('tests-start')
Expand Down
18 changes: 18 additions & 0 deletions play/bufferstream_tap.js
@@ -0,0 +1,18 @@
var tap = require('tap')
var BufferStream = require('bufferstream')
var stdout = new BufferStream([{encoding:'utf8', size:'none'}])
tapConsumer = new tap.Consumer
tapConsumer.on('data', function(data){
console.log(data)
})
tapConsumer.on('end', function(err, count){
console.log('tap end')
console.log(err)
console.log('count: ' + count)
})
tapConsumer.on('bailout', function(){
console.log('tap bailout')
})
stdout.pipe(tapConsumer)
stdout.end('1..1\nok 1 foobar that')
setInterval(function(){}, 100)
9 changes: 5 additions & 4 deletions testem.yml
@@ -1,9 +1,10 @@
src_files:
- lib/catchem.js
- tests/catchem_tests.js
- lib/server.js
- tests/server_tests.js
launchers:
Mocha:
command: mocha tests/catchem_tests.js -R tap
command: mocha server_tests.js -R tap
protocol: tap
auto_launch_in_dev:
cwd: tests
launch_in_dev:
- Mocha
4 changes: 3 additions & 1 deletion tests/appview_tests.js
@@ -1,3 +1,4 @@
/*
var appview = require('../lib/appview.js')
, test = require('./testutils.js')
, EventEmitter = require('events').EventEmitter
Expand Down Expand Up @@ -92,4 +93,5 @@ describe('LogPanel', function(){
logPanel.set('textLines', ['blah'])
expect(logPanel.render.callCount).to.equal(1)
})
})
})
*/
94 changes: 86 additions & 8 deletions tests/config_tests.js
@@ -1,22 +1,27 @@
var Config = require('../lib/config.js')
, test = require('./testutils.js')
, EventEmitter = require('events').EventEmitter
, expect = test.expect
var test = require('./testutils.js')
var EventEmitter = require('events').EventEmitter
var expect = test.expect
var spy = require('sinon').spy
var stub = require('sinon').stub
var browser_launcher = require('../lib/browser_launcher')

describe('Config', function(){
var config, progOptions
var config, appMode, progOptions
beforeEach(function(){
appMode = 'dev'
progOptions = {
file: __dirname + '/testem.yml'
}
config = new Config(progOptions)
config = new Config(appMode, progOptions)
})
it('can create', function(){
expect(config.progOptions).to.equal(progOptions)
})
it('gives progOptions properties when got', function(){
expect(config.get('file')).to.equal(progOptions.file)
})

describe('read yaml config file', function(){
beforeEach(function(done){
config.read(done)
Expand All @@ -33,7 +38,7 @@ describe('Config', function(){
var progOptions = {
file: __dirname + '/testem.json'
}
config = new Config(progOptions)
config = new Config('dev', progOptions)
config.read(done)
})
it('gets properties from config file', function(){
Expand All @@ -42,10 +47,11 @@ describe('Config', function(){
})
})

it('give precendence to json config file', function(){
var config = new Config({})
it('give precendence to json config file', function(done){
var config = new Config('dev', {})
config.read(function(){
expect(config.get('framework')).to.equal('mocha')
done()
})
})

Expand All @@ -68,4 +74,76 @@ describe('Config', function(){
expect(config.isCwdMode()).to.not.be.ok
config.get.restore()
})

it('should getLaunchers should call getAvailable browsers', function(){
stub(config, 'getWantedLaunchers', function(n){return n})
var getAvailableBrowsers = browser_launcher.getAvailableBrowsers
var availableBrowsers = [
{name: 'Chrome', exe: 'chrome.exe'}
, {name: 'Firefox'}
]
browser_launcher.getAvailableBrowsers = function(cb){
cb(availableBrowsers)
}
var cb = spy()
config.getLaunchers({}, cb)
expect(cb.called).to.be.ok
var launchers = cb.args[0][0]
expect(launchers.chrome.name).to.equal('Chrome')
expect(launchers.chrome.settings.exe).to.equal('chrome.exe')
expect(launchers.firefox.name).to.equal('Firefox')
browser_launcher.getAvailableBrowsers = getAvailableBrowsers
})

it('should install custom launchers', function(){
stub(config, 'getWantedLaunchers', function(n){return n})
var launchers = {
Node: {
command: 'node tests.js'
}
}
config.config = {
launchers: launchers
}
browser_launcher.getAvailableBrowsers = function(cb){
cb([])
}
var cb = spy()
config.getLaunchers({}, cb)
var launchers = cb.args[0][0]
expect(launchers.node.name).to.equal('Node')
expect(launchers.node.settings.command).to.equal('node tests.js')
})

it('getWantedLaunchers uses getWantedLauncherNames', function(){
stub(config, 'getWantedLauncherNames').returns(['Chrome', 'Firefox'])
var results = config.getWantedLaunchers({
chrome: { name: 'Chrome' }
, firefox: { name: 'Firefox' }
})
expect(results).to.deep.equal([{ name: 'Chrome' }, { name: 'Firefox' }])
})

describe('getWantedLauncherNames', function(){
it('adds "launch" param', function(){
config.progOptions.launch = 'Chrome,Firefox'
expect(config.getWantedLauncherNames()).to.deep.equal(['Chrome', 'Firefox'])
config.progOptions.launch = 'IE'
expect(config.getWantedLauncherNames()).to.deep.equal(['IE'])
})
it('adds "launch_in_dev" config', function(){
config.config = {launch_in_dev: ['Chrome', 'Firefox']}
expect(config.getWantedLauncherNames()).to.deep.equal(['Chrome', 'Firefox'])
})
it('adds "launch_in_ci" config', function(){
config.appMode = 'ci'
config.config = {launch_in_ci: ['Chrome', 'Firefox']}
expect(config.getWantedLauncherNames()).to.deep.equal(['Chrome', 'Firefox'])
})
it('removes skip param', function(){
config.progOptions.launch = 'Chrome,Firefox'
config.progOptions.skip = 'Chrome'
expect(config.getWantedLauncherNames()).to.deep.equal(['Firefox'])
})
})
})

0 comments on commit c811a86

Please sign in to comment.