Skip to content

Commit

Permalink
Add --decimals flag for HTML and simple output
Browse files Browse the repository at this point in the history
.to_f is used to avoid excessive zeros.
0.0000.to_f => 0.0

Only run coveralls on Travis
  • Loading branch information
bootstraponline committed May 11, 2016
1 parent 53eb530 commit 1923f2b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 7 deletions.
6 changes: 6 additions & 0 deletions lib/slather/command/coverage_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CoverageCommand < Clamp::Command
option ["--binary-file"], "BINARY_FILE", "The binary file against the which the coverage will be run", :multivalued => true
option ["--binary-basename"], "BINARY_BASENAME", "Basename of the file against which the coverage will be run", :multivalued => true
option ["--source-files"], "SOURCE_FILES", "A Dir.glob compatible pattern used to limit the lookup to specific source files. Ignored in gcov mode.", :multivalued => true
option ["--decimals"], "DECIMALS", "The amount of decimals to use for % coverage reporting"

def execute
puts "Slathering..."
Expand All @@ -44,6 +45,7 @@ def execute
setup_binary_file
setup_binary_basename
setup_source_files
setup_decimals

project.configure

Expand Down Expand Up @@ -139,4 +141,8 @@ def setup_binary_basename
def setup_source_files
project.source_files = source_files_list if !source_files_list.empty?
end

def setup_decimals
project.decimals = decimals if decimals
end
end
6 changes: 3 additions & 3 deletions lib/slather/coverage_service/html_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def create_index_html(coverage_files)
cov.h4 {
percentage = (total_tested_lines / total_relevant_lines.to_f) * 100.0
cov.span "Total Coverage : "
cov.span '%.2f%%' % percentage, :class => class_for_coverage_percentage(percentage), :id => "total_coverage"
cov.span decimal_f(percentage) + '%', :class => class_for_coverage_percentage(percentage), :id => "total_coverage"
}

cov.input(:class => "search", :placeholder => "Search")
Expand All @@ -105,7 +105,7 @@ def create_index_html(coverage_files)
cov.tr {
percentage = coverage_file.percentage_lines_tested

cov.td { cov.span '%.2f' % percentage, :class => "percentage #{class_for_coverage_percentage(percentage)} data_percentage" }
cov.td { cov.span decimal_f(percentage), :class => "percentage #{class_for_coverage_percentage(percentage)} data_percentage" }
cov.td(:class => "data_filename") {
cov.a filename, :href => filename_link
}
Expand Down Expand Up @@ -140,7 +140,7 @@ def create_html_from_file(coverage_file)
builder = Nokogiri::HTML::Builder.with(template.at('#reports')) { |cov|
cov.h2(:class => "cov_title") {
cov.span("Coverage for \"#{filename}\"" + (!is_file_empty ? " : " : ""))
cov.span("#{'%.2f' % percentage}%", :class => class_for_coverage_percentage(percentage)) unless is_file_empty
cov.span("#{decimal_f(percentage)}%", :class => class_for_coverage_percentage(percentage)) unless is_file_empty
}

cov.h4("(#{coverage_file.num_lines_tested} of #{coverage_file.num_lines_testable} relevant lines covered)", :class => "cov_subtitle")
Expand Down
4 changes: 2 additions & 2 deletions lib/slather/coverage_service/simple_output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def post

lines_tested = coverage_file.num_lines_tested
total_lines = coverage_file.num_lines_testable
percentage = '%.2f' % [coverage_file.percentage_lines_tested]
percentage = decimal_f([coverage_file.percentage_lines_tested])

total_project_lines_tested += lines_tested
total_project_lines += total_lines
Expand All @@ -42,7 +42,7 @@ def post
puts "##teamcity[buildStatisticValue key='CodeCoverageAbsLTotal' value='%i']" % total_project_lines
end

total_percentage = '%.2f' % [(total_project_lines_tested / total_project_lines.to_f) * 100.0]
total_percentage = decimal_f([(total_project_lines_tested / total_project_lines.to_f) * 100.0])
puts "Test Coverage: #{total_percentage}%"
end

Expand Down
17 changes: 16 additions & 1 deletion lib/slather/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ module Slather
class Project < Xcodeproj::Project

attr_accessor :build_directory, :ignore_list, :ci_service, :coverage_service, :coverage_access_token, :source_directory,
:output_directory, :xcodeproj, :show_html, :verbose_mode, :input_format, :scheme, :workspace, :binary_file, :binary_basename, :source_files
:output_directory, :xcodeproj, :show_html, :verbose_mode, :input_format, :scheme, :workspace, :binary_file, :binary_basename, :source_files,
:decimals

alias_method :setup_for_coverage, :slather_setup_for_coverage

Expand Down Expand Up @@ -238,6 +239,7 @@ def configure
configure_output_directory
configure_input_format
configure_binary_file
configure_decimals
rescue => e
puts e.message
puts failure_help_string
Expand Down Expand Up @@ -295,6 +297,12 @@ def configure_scheme
self.scheme ||= self.class.yml["scheme"] if self.class.yml["scheme"]
end

def configure_decimals
return if self.decimals
self.decimals ||= self.class.yml["decimals"] if self.class.yml["decimals"]
self.decimals = self.decimals ? Integer(self.decimals) : 2
end

def configure_workspace
self.workspace ||= self.class.yml["workspace"] if self.class.yml["workspace"]
end
Expand Down Expand Up @@ -338,6 +346,13 @@ def configure_binary_file
end
end

def decimal_f decimal_arg
configure_decimals unless decimals
decimal = "%.#{decimals}f" % decimal_arg
return decimal if decimals == 2 # special case 2 for backwards compatibility
decimal.to_f.to_s
end

def find_binary_file_in_bundle(bundle_file)
if File.directory? bundle_file
bundle_file_noext = File.basename(bundle_file, File.extname(bundle_file))
Expand Down
16 changes: 16 additions & 0 deletions spec/slather/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -503,4 +503,20 @@ class SpecXcode7CoverageFile < Slather::ProfdataCoverageFile
end
end

def decimal_f *args
fixtures_project.decimal_f *args
end

describe '#decimal_f' do
it 'should preserve length 2 decimals for backwards compatibility' do
expect(decimal_f('100.00')).to eq('100.00')
expect(decimal_f('50.00')).to eq('50.00')
end

it 'should convert length >= 3 decimals to floats' do
fixtures_project.decimals = 3
expect(decimal_f('100.000')).to eq('100.0')
expect(decimal_f('50.00000')).to eq('50.0')
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
if ENV['SIMPLECOV']
require 'simplecov'
SimpleCov.start
else
elsif ENV['TRAVIS']
require 'coveralls'
Coveralls.wear!
end
Expand Down

0 comments on commit 1923f2b

Please sign in to comment.