module MobileMetrics # Adapted from: https://github.com/orta/danger-junit/blob/master/lib/junit/plugin.rb class JunitParser # All the tests for introspection # # @return [Array] attr_accessor :tests # An array of XML elements that represent passed tests. # # @return [Array] attr_accessor :passes # An array of XML elements that represent failed tests. # # @return [Array] attr_accessor :failures # An array of XML elements that represent passed tests. # # @return [Array] attr_accessor :errors # An array of XML elements that represent skipped tests. # # @return [Array] attr_accessor :skipped # An attribute to make the plugin show a warning on skipped tests. # # @return [Bool] attr_accessor :show_skipped_tests # An array of symbols that become the columns of your tests, # if `nil`, the default, it will be all of the attributes. # # @return [Array] attr_accessor :headers # 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) suite_root = @doc.nodes.first.value == 'testsuites' ? @doc.nodes.first : @doc @tests = suite_root.nodes.map(&:nodes).flatten.select { |node| node.kind_of?(Ox::Element) && 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.kind_of?(Ox::Element) && node.value == 'testcase' } @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 end end