lib/junit/plugin.rb in danger-junit-0.6.4 vs lib/junit/plugin.rb in danger-junit-0.7.0

- old
+ new

@@ -37,15 +37,16 @@ # all_test = junit.tests.map(&:attributes) # slowest_test = sort_by { |attributes| attributes[:time].to_f }.last # message "#{slowest_test[:time]} took #{slowest_test[:time]} seconds" # # - # @see orta/danger-junit, danger/danger, artsy/eigen + # @see orta/danger-junit + # @see danger/danger + # @see artsy/eigen # @tags testing, reporting, junit, rspec, jasmine, jest, xcpretty # class DangerJunit < Plugin - # All the tests for introspection # # @return [Array<Ox::Element>] attr_accessor :tests @@ -78,30 +79,47 @@ # if `nil`, the default, it will be all of the attributes. # # @return [Array<Symbol>] attr_accessor :headers - # Parses an XML file, which fills all the attributes + # Parses an XML file, which fills all the attributes, # will `raise` for errors # @return [void] def parse(file) require 'ox' raise "No Junit file was found at #{file}" unless File.exist? file - xml_string = File.read file - @doc = Ox.parse xml_string + xml_string = File.read(file) + @doc = Ox.parse(xml_string) suite_root = @doc.nodes.first.value == 'testsuites' ? @doc.nodes.first : @doc @tests = suite_root.nodes.map(&:nodes).flatten.select { |node| node.value == 'testcase' } failed_suites = suite_root.nodes.select { |suite| suite[:failures].to_i > 0 || suite[:errors].to_i > 0 } failed_tests = failed_suites.map(&:nodes).flatten.select { |node| node.value == 'testcase' } - @failures = failed_tests.select { |test| test.nodes.count > 0 }.select { |test| test.nodes.first.value == 'failure' } - @errors = failed_tests.select { |test| test.nodes.count > 0 }.select { |test| test.nodes.first.value == 'error' } - @skipped = tests.select { |test| test.nodes.count > 0 }.select { |test| test.nodes.first.value == 'skipped' } + @failures = failed_tests.select do |test| + test.nodes.count > 0 + end.select do |test| + node = test.nodes.first + node.kind_of?(Ox::Element) && node.value == 'failure' + end + @errors = failed_tests.select do |test| + test.nodes.count > 0 + end.select do |test| + node = test.nodes.first + node.kind_of?(Ox::Element) && node.value == 'error' + end + + @skipped = tests.select do |test| + test.nodes.count > 0 + end.select do |test| + node = test.nodes.first + node.kind_of?(Ox::Element) && node.value == 'skipped' + end + @passes = tests - @failures - @errors - @skipped end # Causes a build fail if there are test failures, # and outputs a markdown table of the results. @@ -110,10 +128,10 @@ def report return if failures.nil? # because danger calls `report` before loading a file warn("Skipped #{skipped.count} tests.") if show_skipped_tests && skipped.count > 0 unless failures.empty? && errors.empty? - fail("Tests have failed, see below for more information.", sticky: false) + fail('Tests have failed, see below for more information.', sticky: false) message = "### Tests: \n\n" tests = (failures + errors) keys = headers || tests.first.attributes.keys attributes = keys.map(&:to_s).map(&:capitalize)