lib/jacoco/plugin.rb in danger-jacoco-0.1.5 vs lib/jacoco/plugin.rb in danger-jacoco-0.1.6

- old
+ new

@@ -23,10 +23,11 @@ attr_accessor :minimum_project_coverage_percentage attr_accessor :minimum_class_coverage_percentage attr_accessor :files_extension attr_accessor :minimum_package_coverage_map attr_accessor :minimum_class_coverage_map + attr_accessor :fail_no_coverage_data_found # Initialize the plugin with configured parameters or defaults def setup @minimum_project_coverage_percentage = 0 unless minimum_project_coverage_percentage @minimum_class_coverage_percentage = 0 unless minimum_class_coverage_percentage @@ -59,11 +60,13 @@ # that is your path to source files is something like # # Java => blah/blah/java/slashed_package/Source.java # Kotlin => blah/blah/kotlin/slashed_package/Source.kt # - def report(path, report_url = '', delimiter = %r{\/java\/|\/kotlin\/}) + def report(path, report_url = '', delimiter = %r{\/java\/|\/kotlin\/}, fail_no_coverage_data_found: true) + @fail_no_coverage_data_found = fail_no_coverage_data_found + setup classes = classes(delimiter) parser = Jacoco::SAXParser.new(classes) Nokogiri::XML::SAX::Parser.new(parser).parse(File.open(path)) @@ -87,22 +90,32 @@ .map { |file| file.split('.').first.split(delimiter)[1] } end # It returns a specific class code coverage and an emoji status as well def report_class(jacoco_class) + report_result = { + covered: 'No coverage data found : -', + status: ':black_joker:', + required_coverage_percentage: 'No coverage data found : -' + } + counter = coverage_counter(jacoco_class) - coverage = (counter.covered.fdiv(counter.covered + counter.missed) * 100).floor - required_coverage = minimum_class_coverage_map[jacoco_class.name] - required_coverage = package_coverage(jacoco_class.name) if required_coverage.nil? - required_coverage = minimum_class_coverage_percentage if required_coverage.nil? - status = coverage_status(coverage, required_coverage) + unless counter.nil? + coverage = (counter.covered.fdiv(counter.covered + counter.missed) * 100).floor + required_coverage = minimum_class_coverage_map[jacoco_class.name] + required_coverage = package_coverage(jacoco_class.name) if required_coverage.nil? + required_coverage = minimum_class_coverage_percentage if required_coverage.nil? + status = coverage_status(coverage, required_coverage) - { - covered: coverage, - status: status, - required_coverage_percentage: required_coverage - } + report_result = { + covered: coverage, + status: status, + required_coverage_percentage: required_coverage + } + end + + report_result end # it returns the most suitable coverage by package name to class or nil def package_coverage(class_name) path = class_name @@ -147,10 +160,17 @@ def coverage_counter(jacoco_class) counters = jacoco_class.counters branch_counter = counters.detect { |e| e.type.eql? 'BRANCH' } line_counter = counters.detect { |e| e.type.eql? 'LINE' } counter = branch_counter.nil? ? line_counter : branch_counter - raise "No coverage data found for #{jacoco_class.name}" if counter.nil? + + if counter.nil? + no_coverage_data_found_message = "No coverage data found for #{jacoco_class.name}" + + raise no_coverage_data_found_message if @fail_no_coverage_data_found.class == TrueClass + + warn no_coverage_data_found_message + end counter end # rubocop:disable Style/SignalException