Skip to content
This repository was archived by the owner on Feb 1, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ rdoc
spec/reports
tmp
bundler_bin
.idea
9 changes: 7 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
colorful
execjs
json_pure
nokogiri
rake
slop

Expand All @@ -15,14 +16,17 @@ GEM
coffee-script-source (1.6.3)
colorful (0.0.3)
diff-lcs (1.2.1)
execjs (2.0.1)
execjs (2.0.2)
ffi (1.0.11)
guard (1.0.1)
ffi (>= 0.5.0)
thor (~> 0.14.6)
guard-rspec (0.6.0)
guard (>= 0.10.0)
json_pure (1.8.0)
json_pure (1.8.1)
mini_portile (0.5.1)
nokogiri (1.6.0)
mini_portile (~> 0.5.0)
rake (10.1.0)
rspec (2.13.0)
rspec-core (~> 2.13.0)
Expand All @@ -41,4 +45,5 @@ PLATFORMS
DEPENDENCIES
bwoken!
guard-rspec
nokogiri
rspec
2 changes: 2 additions & 0 deletions bwoken.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ Gem::Specification.new do |gem|
gem.add_dependency 'json_pure'
gem.add_dependency 'rake'
gem.add_dependency 'slop'
gem.add_dependency 'nokogiri'

gem.add_development_dependency 'rspec'
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency 'nokogiri'
end
2 changes: 2 additions & 0 deletions lib/bwoken/cli/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require 'bwoken/formatter'
require 'bwoken/formatters/passthru_formatter'
require 'bwoken/formatters/colorful_formatter'
require 'bwoken/formatters/junit_formatter'
require 'bwoken/script_runner'

module Bwoken
Expand Down Expand Up @@ -115,6 +116,7 @@ def clean
def select_formatter formatter_name
case formatter_name
when 'passthru' then Bwoken::PassthruFormatter.new
when 'junit' then Bwoken::JUnitFormatter.new
else Bwoken::ColorfulFormatter.new
end
end
Expand Down
22 changes: 11 additions & 11 deletions lib/bwoken/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def format_build stdout
new.format_build stdout
end

def on name, &block
def on(name, formatter=nil, &block)
define_method "_on_#{name}_callback" do |*line|
block.call(*line)
block.call(*line, formatter)
end
end

Expand All @@ -28,26 +28,26 @@ def method_missing(method_name, *args, &block)
def line_demuxer line, exit_status
if line =~ /Instruments Trace Error/
exit_status = 1
_on_fail_callback(line)
_on_fail_callback(line,self)
elsif line =~ /^\d{4}/
tokens = line.split(' ')

if tokens[3] =~ /Pass/
_on_pass_callback(line)
_on_pass_callback(line,self)
elsif tokens[3] =~ /Start/
_on_start_callback(line)
_on_start_callback(line,self)
elsif tokens[3] =~ /Fail/ || line =~ /Script threw an uncaught JavaScript error/
exit_status = 1
_on_fail_callback(line)
_on_fail_callback(line,self)
elsif tokens[3] =~ /Error/
_on_error_callback(line)
_on_error_callback(line,self)
else
_on_debug_callback(line)
_on_debug_callback(line, self)
end
elsif line =~ /Instruments Trace Complete/
_on_complete_callback(line)
_on_complete_callback(line,self)
else
_on_other_callback(line)
_on_other_callback(line,self)
end
exit_status
end
Expand All @@ -73,7 +73,7 @@ def format_build stdout
stdout.each_line do |line|
out_string << line
if line.length > 1
_on_build_line_callback(line)
_on_build_line_callback(line,self)
end
end
out_string
Expand Down
247 changes: 247 additions & 0 deletions lib/bwoken/formatters/junit_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
require 'nokogiri'
require 'bwoken/formatter'

module Bwoken

class JUnitTestSuite
attr_accessor :id
attr_accessor :package
attr_accessor :host_name
attr_accessor :name
attr_accessor :tests
attr_accessor :failures
attr_accessor :errors
attr_accessor :time
attr_accessor :timestamp

attr_accessor :test_cases

def initialize
self.test_cases = []
self.tests = 0
self.failures = 0
self.errors = 0
end

def complete
self.time = Time.now - self.timestamp
end

end

class JUnitTestCase
attr_accessor :name
attr_accessor :classname
attr_accessor :time
attr_accessor :error
attr_accessor :logs

attr_accessor :start_time

def initialize
self.logs = String.new
self.error = nil
end

def complete
self.time = Time.now - self.start_time
end

end

class JUnitFormatter < Formatter
attr_accessor :test_suites

def initialize
self.test_suites = []
end

def format stdout
exit_status = super stdout
generate_report
exit_status
end


on :complete do |line,formatter|
tokens = line.split(' ')
test_suite = formatter.test_suites.last
test_suite.time = tokens[5].sub(';', '')
end

on :debug do |line, formatter|
filtered_line = line.sub(/(target\.frontMostApp.+)\.tap\(\)/, "#{'tap'} \\1")
filtered_line = filtered_line.gsub(/\[("[^\]]*")\]/, "[" + '\1' + "]")
filtered_line = filtered_line.gsub('()', '')
filtered_line = filtered_line.sub(/target.frontMostApp.(?:mainWindow.)?/, '')
tokens = filtered_line.split(' ')

test_suite = formatter.test_suites.last
test_case = test_suite.test_cases.last

if test_case
test_case.logs << "\n#{tokens[3].cyan}\t#{tokens[4..-1].join(' ')}"
end
end

on :error do |line,formatter|
@failed = true
tokens = line.split(' ')

test_suite = formatter.test_suites.last
test_case = test_suite.test_cases.last
if test_case
test_case.complete
test_case.error = tokens[4..-1].join(' ')
end

test_suite.errors += 1

end

on :fail do |line,formatter|
@failed = true
tokens = line.split(' ')

test_suite = formatter.test_suites.last
test_case = test_suite.test_cases.last
if test_case
test_case.complete
test_case.error = tokens[4..-1].join(' ')
end

test_suite.failures += 1

end

on :start do |line,formatter|
tokens = line.split(' ')

suite = formatter.test_suites.last
if suite
test_case = Bwoken::JUnitTestCase.new
test_case.name = tokens[4..-1].join(' ')
test_case.classname = test_case.name
test_case.start_time = Time.now

suite.tests+=1
suite.test_cases << test_case
end
end

on :pass do |line,formatter|
tokens = line.split(' ')

test_case = formatter.test_suites.last.test_cases.last
if test_case
test_case.complete
test_case.error = nil
end
end

on :before_script_run do |path, formatter|
tokens = path.split('/')

new_suite = Bwoken::JUnitTestSuite.new
new_suite.timestamp = Time.now
new_suite.host_name = tokens[-2]
new_suite.name = tokens[-1]
new_suite.package = new_suite.name
new_suite.id = formatter.test_suites.count + 1

formatter.test_suites << new_suite

@failed = false
end

on :before_build_start do
print "Building"
end

on :build_line do |line,formatter|
print '.'
end

on :build_successful do |line,formatter|
puts
puts 'Build Successful!'
end

on :build_failed do |build_log, error_log|
puts build_log
puts 'Standard Error:'
puts error_log
puts 'Build failed!'
end

on :other do |line,formatter|
nil
end


def generate_report
doc = Nokogiri::XML::Document.new()
root = Nokogiri::XML::Element.new('testsuites', doc)
doc.add_child(root)

result_name = 'unknown'

self.test_suites.each do |suite|
result_name = suite.name.gsub /\.js$/, ''

suite_elm = Nokogiri::XML::Element.new('testsuite', doc)
suite_elm['id'] = suite.id
suite_elm['package'] = suite.package
suite_elm['hostname'] = suite.host_name
suite_elm['name'] = suite.name
suite_elm['tests'] = suite.tests
suite_elm['failures'] = suite.failures
suite_elm['errors'] = suite.errors
suite_elm['time'] = suite.time
suite_elm['timestamp'] = suite.timestamp.to_s

system_out = ''
system_err = ''

suite.test_cases.each do |test_case|
test_case_elm = Nokogiri::XML::Element.new('testcase', doc)
test_case_elm['name'] = test_case.name
test_case_elm['classname'] = test_case.classname
test_case_elm['time'] = test_case.time

if test_case.error
error = Nokogiri::XML::Element.new('error', doc)
error['type'] = test_case.error
test_case_elm.add_child(error)
system_err << "\n\n#{test_case.logs}"
else
system_out << "\n\n#{test_case.logs}"
end

suite_elm.add_child(test_case_elm)
end

suite_elm.add_child("<system-out>#{doc.create_cdata(system_out)}</system-out>")
suite_elm.add_child("<system-err>#{doc.create_cdata(system_err)}</system-err>")

root.add_child(suite_elm)

end

out_xml = doc.to_xml

write_results(out_xml, result_name)
end

def write_results(xml, suite_name)
output_path = File.join(Bwoken.results_path, "#{suite_name}_results.xml")
File.open(output_path, 'w+') do |io|
io.write(xml)
end

puts "\nJUnit report generated to #{output_path}\n\n"
end


end
end
2 changes: 1 addition & 1 deletion lib/bwoken/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def device_flag
end

def run
formatter.before_script_run path
formatter.before_script_run path, formatter

Open3.popen3(cmd) do |stdin, stdout, stderr, wait_thr|
exit_status = formatter.format stdout
Expand Down
Loading