Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring and rejiggering.

  • Loading branch information...
commit 9948eaa081dd5cb4c2ed95147388d6185d7770df 1 parent 188d09d
@airportyh authored
View
0  lib/getipaddr.js → attic/getipaddr.js
File renamed without changes
View
3  lib/appviewcharm.js → lib/appview.js
@@ -7,7 +7,6 @@ var charm = require('charm')(process)
, EventEmitter = require('events').EventEmitter
function AppView(app){
- EventEmitter.call(this)
this.app = app
this._currentTab = -1
this.scrollOffset = 0
@@ -155,7 +154,7 @@ AppView.prototype = {
this.writeLine(1, 'Open the URL below in a browser to connect.')
charm.display('underscore')
var url = 'http://localhost:' +
- this.app.config.port
+ this.app.config.get('port')
charm
.position(0, 3)
.write(url)
View
6 lib/browser_launcher.js
@@ -112,7 +112,7 @@ function browsersForPlatform(){
name: 'PhantomJS',
exe: 'phantomjs',
args: function(app){
- return [path.dirname(__dirname) + '/assets/phantom.js', app.config.port]
+ return [path.dirname(__dirname) + '/assets/phantom.js', app.config.get('port')]
},
supported: findableByWhere
}
@@ -156,7 +156,7 @@ function browsersForPlatform(){
name: 'PhantomJS',
exe: 'phantomjs',
args: function(app){
- return [path.dirname(__dirname) + '/assets/phantom.js', app.config.port]
+ return [path.dirname(__dirname) + '/assets/phantom.js', app.config.get('port')]
},
supported: findableByWhich
}
@@ -192,7 +192,7 @@ function browsersForPlatform(){
name: 'PhantomJS',
exe: 'phantomjs',
args: function(app){
- return [path.dirname(__dirname) + '/assets/phantom.js', app.config.port]
+ return [path.dirname(__dirname) + '/assets/phantom.js', app.config.get('port')]
},
supported: findableByWhich
}
View
41 lib/ci_mode_app.js
@@ -6,23 +6,26 @@ var yaml = require('js-yaml')
, path = require('path')
, async = require('async')
, browser_launcher = require('./browser_launcher')
+ , Config = require('./config')
-function App(config){
- this.config = config
+function App(progOptions){
+ this.config = new Config(progOptions)
this.browsers = browser_launcher.browsersForPlatform()
- if (config.browsers){
- var wantedBrowsers = config.browsers.toLowerCase().split(',')
+ var browsers = this.config.get('browsers')
+ , skip = this.config.get('skip')
+ if (browsers){
+ var wantedBrowsers = browsers.toLowerCase().split(',')
this.browsers = this.browsers.filter(function(browser){
return wantedBrowsers.indexOf(browser.name.toLowerCase()) !== -1
})
}
- if (config.skip){
- var unwantedBrowsers = config.skip.toLowerCase().split(',')
+ if (skip){
+ var unwantedBrowsers = skip.toLowerCase().split(',')
this.browsers = this.browsers.filter(function(browser){
return unwantedBrowsers.indexOf(browser.name.toLowerCase()) === -1
})
}
- if (config.list){
+ if (this.config.get('list')){
this.printAvailableBrowsers()
}else{
this.initialize()
@@ -30,7 +33,6 @@ function App(config){
}
App.prototype = {
- configFile: 'testem.yml',
initialize: function(){
var config = this.config
this.tapProducer = new tap.Producer(true)
@@ -38,26 +40,13 @@ App.prototype = {
this.testId = 1
this.failed = false
this.testsStarted = false
- this.configure(function(){
+ this.config.read(function(){
this.server = new Server(this)
this.server.on('browsers-changed', this.onBrowsersChanged.bind(this))
this.server.on('test-result', this.onTestResult.bind(this))
this.server.on('all-test-results', this.onAllTestResults.bind(this))
this.server.on('server-start', this.onServerStart.bind(this))
- })
- },
- configure: function(callback){
- var self = this
- var config = this.config
- if (config.f)
- this.configFile = config.f
- fs.readFile(this.configFile, function(err, data){
- if (err) return
- var cfg = yaml.load(String(data))
- for (var key in cfg)
- config[key] = cfg[key]
- if (callback) callback.call(self, config)
- })
+ }.bind(this))
},
onBrowsersChanged: function(){
if (!this.testsStarted){
@@ -72,7 +61,7 @@ App.prototype = {
process.stdout.write('.')
},
launchNextBrowser: function(){
- var url = 'http://localhost:' + this.config.port
+ var url = 'http://localhost:' + this.config.get('port')
var browser = this.currentBrowser = this.browsers.shift()
var self = this
if (!browser){
@@ -119,9 +108,7 @@ App.prototype = {
},
outputTap: function(results, browser){
- var config = this.config
- , dir = config.output
- , producer = this.tapProducer
+ var producer = this.tapProducer
console.log()
View
31 lib/config.js
@@ -0,0 +1,31 @@
+var fs = require('fs')
+ , yaml = require('js-yaml')
+
+function Config(progOptions){
+ this.progOptions = progOptions
+ this.config = null
+}
+
+Config.prototype = {
+ read: function(callback){
+ var configFile = this.progOptions.file
+ , self = this
+ fs.readFile(configFile, function(err, data){
+ if (!err){
+ var cfg = yaml.load(String(data))
+ self.config = cfg
+ }
+ if (callback) callback.call(self)
+ })
+ },
+ get: function(key){
+ if (key in this.progOptions)
+ return this.progOptions[key]
+ else if (this.config && key in this.config)
+ return this.config[key]
+ else
+ return null
+ }
+}
+
+module.exports = Config
View
22 lib/debounce.js
@@ -1,22 +0,0 @@
-// <http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/>
-module.exports = function (func, threshold, execAsap) {
-
- var timeout;
-
- return function debounced () {
- var obj = this, args = arguments;
- function delayed () {
- if (!execAsap)
- func.apply(obj, args);
- timeout = null;
- };
-
- if (timeout)
- clearTimeout(timeout);
- else if (execAsap)
- func.apply(obj, args);
-
- timeout = setTimeout(delayed, threshold || 100);
- };
-
-}
View
37 lib/dev_mode_app.js
@@ -1,61 +1,46 @@
var Server = require('./server').Server
- , debounce = require('./debounce')
, fs = require('fs')
, log = require('winston')
, child_process = require('child_process')
- , AppView = require('./appviewcharm')
+ , AppView = require('./appview')
, Path = require('path')
, yaml = require('js-yaml')
, FileWatcher = require('./filewatcher')
+ , Config = require('./config')
-function App(config){
- this.config = config
- if (config.config)
- this.configFile = config.config
+function App(progOptions){
+ this.config = new Config(progOptions)
this.fileWatcher = new FileWatcher
this.fileWatcher.on('change', this.onFileChanged.bind(this))
- this.fileWatcher.add(this.configFile)
+ this.fileWatcher.add(this.config.get('file'))
- this.configure(function(){
+ this.config.read(function(){
this.server = new Server(this)
this.server.on('browsers-changed', this.onBrowsersChanged.bind(this))
this.server.on('test-result', this.onTestResult.bind(this))
this.server.on('all-test-results', this.onAllTestResults.bind(this))
this.server.on('server-start', this.initView.bind(this))
this.server.on('file-requested', this.onFileRequested.bind(this))
- })
+ }.bind(this))
}
App.prototype = {
- configFile: 'testem.yml',
- configure: function(callback){
- var config = this.config
- , self = this
- fs.readFile(self.configFile, function(err, data){
- if (!err){
- var cfg = yaml.load(String(data))
- for (var key in cfg)
- config[key] = cfg[key]
- }
- if (callback) callback.call(self)
- })
- },
onFileRequested: function(filepath){
this.fileWatcher.add(filepath)
},
onFileChanged: function(filepath){
- if (filepath === this.configFile){
+ if (filepath === Path.resolve(this.config.get('file'))){
// config changed
- this.configure(this.startTests.bind(this))
+ this.config.read(this.startTests.bind(this))
}else{
this.startTests()
}
},
startPhantomJS: function(){
var path = Path.dirname(__dirname) + '/assets/phantom.js'
- log.info('phantomjs path: ' + path)
- this.phantomProcess = child_process.spawn('phantomjs', [path, this.config.port])
+ this.phantomProcess = child_process.spawn('phantomjs', [path,
+ this.config.get('port')])
log.info('Spawning PhantomJS')
},
initView: function(){
View
1  lib/filewatcher.js
@@ -5,7 +5,6 @@ var fs = require('fs')
, EventEmitter = require('events').EventEmitter
function FileWatcher(){
- EventEmitter.call(this)
this.fileInfo = {} // a map of file info, key by filepath
}
View
25 lib/server.js
@@ -14,7 +14,6 @@ var Express = require('express')
require('./socket.io.patch')
function Server(app){
- EventEmitter.call(this)
this.app = app
// Build the server
@@ -46,14 +45,17 @@ Server.prototype = {
exp.use(Express.static(__dirname + '/../public'))
})
exp.get('/runner/', function(req, res){
+ var framework = config.get('framework')
+ , test_page = config.get('test_page')
+ , src_files = config.get('src_files')
res.header('Cache-Control', 'No-cache')
res.header('Pragma', 'No-cache')
- function render(files){
- var scripts = config.framework === 'qunit' ?
+ function render(err, files){
+ var scripts = framework === 'qunit' ?
['/qunit.js'] :
['/jasmine.js',
'/jasmine-html.js']
- var runnerPage = config.framework === 'qunit' ?
+ var runnerPage = framework === 'qunit' ?
__dirname + '/../views/qunitrunner.html' :
__dirname + '/../views/jasminerunner.html'
scripts = scripts.concat(files)
@@ -62,14 +64,15 @@ Server.prototype = {
})
}
- if (config.test_page){
- var url = '/runner/' + config.test_page
+ if (test_page){
+ var url = '/runner/' + test_page
res.redirect(url + '#testem')
- }else if (isa(config.src_files, Array)){
- async.reduce(config.src_files, [], function(curr, pattern, next){
+ }else if (isa(src_files, Array)){
+ // TODO: dedup
+ async.reduce(src_files, [], function(curr, pattern, next){
glob(pattern, function(err, files){
- if (err) next(curr)
- else next(curr.concat(files))
+ if (err) next(null, curr)
+ else next(null, curr.concat(files))
})
}, render)
}
@@ -92,7 +95,7 @@ Server.prototype = {
this.io.sockets.on('connection', this.onClientConnected.bind(this))
// Start the server!
- exp.listen(config.port)
+ exp.listen(config.get('port'))
},
cleanUpConnections: function(){
var count = this.browsers.length
View
20 testem.js
@@ -2,12 +2,13 @@
var log = require('winston')
, program = require('commander')
- , config = program
+ , progOptions = program
+ , ci = false
program
.version(require(__dirname + '/package').version)
.usage('[options]')
- .option('-c, --config [file]', 'Config file')
+ .option('-f, --file [file]', 'Config file', 'testem.yml')
.option('-p, --port [num]', 'Server port - Defaults to 7357', 7357)
.option('-d, --debug', 'Output debug to debug log')
.option('--debuglog [log]', 'Name of debug log file. Defaults to testem.log', 'testem.log')
@@ -18,21 +19,20 @@ program
.option('-b, --browsers [list]', 'List of browsers to test(comma separated).')
.option('-s, --skip [list]', 'List of browsers to skip(comma separated).')
.option('-l, --list', 'Print the list of available browsers.')
- .option('-p, --port [num]', 'Server port - Defaults to 7357', 7357)
.action(function(env){
env.__proto__ = program
- config = env
- config.ci = true
+ progOptions = env
+ ci = true
})
program.parse(process.argv)
-
-App = config.ci ?
+App = ci ?
require('./lib/ci_mode_app') :
require('./lib/dev_mode_app')
log.remove(log.transports.Console)
-if (config.debug){
- log.add(log.transports.File, {filename: config.debuglog})
+if (progOptions.debug){
+ log.add(log.transports.File, {filename: progOptions.debuglog})
}
-new App(config)
+
+new App(progOptions)
View
8 testem.yml
@@ -1,9 +1,9 @@
framework: jasmine
#test_page: satellite/test/tests.html
-test_page: "examples/test.html"
-#src_files:
-#- examples/hello.js
-#- examples/hello_spec.js
+#test_page: "examples/test.html"
+src_files:
+- examples/hello.js
+- examples/hello_spec.js
#- examples/qunit/validate.js
#- examples/qunit/tests.js
#- examples/hello.js
Please sign in to comment.
Something went wrong with that request. Please try again.