bootbox
Version:
Wrappers for JavaScript alert(), confirm() and other flexible dialogs using Twitter's bootstrap framework
226 lines (174 loc) • 6.13 kB
text/coffeescript
system = require 'system'
webpage = require 'webpage'
USAGE = """
Usage: phantomjs mocha-phantomjs.coffee URL REPORTER
"""
class Reporter
constructor: () ->
= system.args[1]
= parseInt(system.env.COLUMNS or 75) * .75 | 0
= false
= 6000
unless
run: ->
# Subclass Hooks
customizeRunner: (options) ->
undefined
customizeProcessStdout: (options) ->
undefined
customizeConsole: (options) ->
undefined
customizeOptions: ->
columns:
# Private
fail: (msg) ->
console.log msg if msg
phantom.exit 1
finish: ->
phantom.exit .evaluate -> mochaPhantomJS.failures
initPage: ->
= webpage.create()
.onConsoleMessage = (msg) -> console.log msg
.onInitialized = =>
.evaluate ->
window.mochaPhantomJS =
failures: 0
ended: false
started: false
run: ->
mochaPhantomJS.started = true
loadPage: ->
.open
.onLoadFinished = (status) =>
if status isnt 'success' then else
onLoadSuccess: ->
onLoadFailed: ->
"Failed to load the page. Check the url: #{@url}"
injectJS: ->
if .evaluate(-> window.mocha?)
.injectJs 'mocha-phantomjs/core_extensions.js'
.evaluate ,
.evaluate ,
else
"Failed to find mocha on the page."
runMocha: ->
.evaluate ,
= .evaluate -> mochaPhantomJS.runner or false
if
= new Date().getTime()
.evaluate ,
else
"Failed to start mocha."
waitForMocha: =>
ended = .evaluate -> mochaPhantomJS.ended
if ended then else setTimeout , 100
waitForRunMocha: =>
started = .evaluate -> mochaPhantomJS.started
if started then else setTimeout , 100
runner: (reporter) ->
try
mocha.setup reporter: reporter
mochaPhantomJS.runner = mocha.run()
if mochaPhantomJS.runner
mochaPhantomJS.runner.on 'end', ->
mochaPhantomJS.failures =
mochaPhantomJS.ended = true
catch error
false
class Spec extends Reporter
constructor: ->
super 'spec'
customizeProcessStdout: (options) ->
process.stdout.write = (string) ->
return if string is process.cursor.deleteLine or string is process.cursor.beginningOfLine
console.log string
customizeConsole: (options) ->
process.cursor.CRMatcher = /\s+◦\s\w/
process.cursor.CRCleaner = process.cursor.up + process.cursor.deleteLine
origLog = console.log
console.log = ->
string = console.format.apply(console, arguments)
if string.match(process.cursor.CRMatcher)
process.cursor.CRCleanup = true
else if process.cursor.CRCleanup
string = process.cursor.CRCleaner + string
process.cursor.CRCleanup = false
origLog.call console, string
class Dot extends Reporter
constructor: ->
super 'dot'
customizeProcessStdout: (options) ->
process.cursor.margin = 2
process.cursor.CRMatcher = /\u001b\[\d\dm\․\u001b\[0m/
process.stdout.columns = options.columns
process.stdout.allowedFirstNewLine = false
process.stdout.write = (string) ->
if string is '\n '
unless process.stdout.allowedFirstNewLine
process.stdout.allowedFirstNewLine = true
else
return
else if string.match(process.cursor.CRMatcher)
if process.cursor.count is process.stdout.columns
process.cursor.count = 0
forward = process.cursor.margin
string = process.cursor.forwardN(forward) + string
else
forward = process.cursor.margin + process.cursor.count
string = process.cursor.up + process.cursor.forwardN(forward) + string
++process.cursor.count
console.log string
class Tap extends Reporter
constructor: ->
super 'tap'
class List extends Reporter
constructor: ->
super 'list'
customizeProcessStdout: (options) ->
process.stdout.write = (string) ->
return if string is process.cursor.deleteLine or string is process.cursor.beginningOfLine
console.log string
customizeProcessStdout: (options) ->
process.cursor.CRMatcher = /\u001b\[90m.*:\s\u001b\[0m/
process.cursor.CRCleaner = (string) -> process.cursor.up + process.cursor.deleteLine + string + process.cursor.up + process.cursor.up
origLog = console.log
console.log = ->
string = console.format.apply(console, arguments)
if string.match /\u001b\[32m\s\s-\u001b\[0m/
string = string
process.cursor.CRCleanup = false
if string.match(process.cursor.CRMatcher)
process.cursor.CRCleanup = true
else if process.cursor.CRCleanup
string = process.cursor.CRCleaner(string)
process.cursor.CRCleanup = false
origLog.call console, string
class Min extends Reporter
constructor: -> super 'min'
class Doc extends Reporter
constructor: -> super 'doc'
class Teamcity extends Reporter
constructor: -> super 'teamcity'
class Json extends Reporter
constructor: -> super 'json'
class JsonCov extends Reporter
constructor: -> super 'json-cov'
class HtmlCov extends Reporter
constructor: -> super 'html-cov'
reporterString = system.args[2] || 'spec'
reporterString = ("#{s.charAt(0).toUpperCase()}#{s.slice(1)}" for s in reporterString.split('-')).join('')
reporterKlass = try
eval(reporterString)
catch error
undefined
if reporterKlass
reporter = new reporterKlass
reporter.run()
else
console.log "Reporter class not implemented: #{reporterString}"
phantom.exit 1