Skip to content

Commit

Permalink
Integrated optional junit xml output
Browse files Browse the repository at this point in the history
Adds 3 new configurations to CLI and guard config:
* junit - set to true to save output to xml files (false by default)
* junit_consolidate - rolls nested describes up into their parent describe blocks (true by default)
* junit_save_path - optional path to save the xml files to

Uses the junit reporter created by larrymyers from github/com/larrymyers/jasmine-reporters
  • Loading branch information
Ed Spencer committed Jul 25, 2013
1 parent 4861c31 commit f213c1d
Show file tree
Hide file tree
Showing 9 changed files with 394 additions and 36 deletions.
5 changes: 4 additions & 1 deletion lib/guard/jasmine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class Jasmine < Guard
statements_threshold: 0,
functions_threshold: 0,
branches_threshold: 0,
lines_threshold: 0
lines_threshold: 0,
junit: false,
junit_consolidate: true,
junit_save_path: ''
}

# Initialize Guard::Jasmine.
Expand Down
18 changes: 18 additions & 0 deletions lib/guard/jasmine/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,21 @@ class CLI < Thor
default: 0,
desc: 'Lines coverage threshold'

method_option :junit,
type: :boolean,
default: false
desc: 'Whether to save jasmine test results in JUnit-compatible xml files'

method_option :junit_consolidate,
type: :boolean,
default: false
desc: 'Whether to save nested describes within the same xml file as their parent'

method_option :junit_save_path,
type: :string,
default: ''
desc: 'The directory to save junit xml files into'

# Run the Guard::Jasmine::Runner with options from
# the command line.
#
Expand Down Expand Up @@ -169,6 +184,9 @@ def spec(*paths)
runner_options[:notification] = false
runner_options[:hide_success] = true
runner_options[:max_error_notify] = 0
runner_options[:junit] = options.junit
runner_options[:junit_consolidate] = options.junit_consolidate
runner_options[:junit_save_path] = options.junit_save_path

::Guard::UI.options = ::Guard::UI.options.merge({ :template => ':message' })

Expand Down
53 changes: 51 additions & 2 deletions lib/guard/jasmine/phantomjs/guard-jasmine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ options =
focus: /true/i.test phantom.args[3]
console: phantom.args[4] || 'failure'
errors: phantom.args[5] || 'failure'
junit: /true/i.test phantom.args[6]
junit_consolidate: /true/i.test phantom.args[7]
junit_save_path: phantom.args[8] || ''

# Create the web page.
#
Expand All @@ -20,6 +23,8 @@ page = require('webpage').create()
currentSpecId = -1
logs = {}
errors = {}
resultsKey = "__jr" + Math.ceil(Math.random() * 1000000)
fs = require("fs")

# Catch JavaScript errors
#
Expand Down Expand Up @@ -47,18 +52,57 @@ page.onConsoleMessage = (msg, line, source) ->
# Initialize the page before the JavaScript is run.
#
page.onInitialized = ->
overloadPageEvaluate(page)
setupWriteFileFunction(page, resultsKey, fs.separator)

page.injectJs 'lib/console.js'
page.injectJs 'lib/reporter.js'
page.injectJs 'lib/junit_reporter.js'

page.evaluate ->
setupReporters = ->
# Attach the console reporter when the document is ready.
window.onload = ->
window.onload = null
window.resultReceived = false
window.reporter = new ConsoleReporter()
if window.jasmine
jasmine.getEnv().addReporter(new JUnitXmlReporter("%save_path%", "%consolidate%"))
jasmine.getEnv().addReporter(window.reporter)

page.evaluate(setupReporters, {save_path: options.junit_save_path, consolidate: options.junit_consolidate})


getXmlResults = (page, key) ->
getWindowObj = ->
window["%resultsObj%"] || {}
page.evaluate getWindowObj, {resultsObj: key}

replaceFunctionPlaceholders= (fn, replacements) ->
if replacements && typeof replacements == 'object'
fn = fn.toString()
for p of replacements
if replacements.hasOwnProperty(p)
match = new RegExp("%" + p + "%", "g")
loop
fn = fn.replace(match, replacements[p])
break unless fn.indexOf(match) != -1
fn

overloadPageEvaluate = (page) ->
page._evaluate = page.evaluate
page.evaluate = (fn, replacements) ->
page._evaluate(replaceFunctionPlaceholders(fn, replacements))
page

setupWriteFileFunction= (page,key, path_separator) ->
saveData = () ->
window["%resultsObj%"] = {}
window.fs_path_separator = "%fs_path_separator%"
window.__phantom_writeFile = (filename, text) ->
window["%resultsObj%"][filename] = text;

page.evaluate saveData, {resultsObj: key, fs_path_separator: path_separator}

# Open web page and run the Jasmine test runner
#
page.open options.url, (status) ->
Expand All @@ -70,7 +114,6 @@ page.open options.url, (status) ->
else
waitFor jasmineReady, jasmineAvailable, options.timeout, jasmineMissing


# Test if the jasmine has been loaded
#
jasmineReady = ->
Expand Down Expand Up @@ -116,6 +159,12 @@ specsTimedout = ->
console.log JSON.stringify({ error: 'Timeout for the Jasmine test results!' })

specsDone = ->
if options.junit == true
xml_results = getXmlResults(page, resultsKey)
for filename of xml_results
if xml_results.hasOwnProperty(filename) && (output = xml_results[filename]) && typeof(output) == 'string'
fs.write(filename, output, 'w')

phantom.exit()

# Wait until the test condition is true or a timeout occurs.
Expand Down
90 changes: 81 additions & 9 deletions lib/guard/jasmine/phantomjs/guard-jasmine.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 1 addition & 9 deletions lib/guard/jasmine/phantomjs/lib/console.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f213c1d

Please sign in to comment.