loading
Generated 2020-10-27T17:30:33+00:00

All Files ( 100.0% covered at 38.13 hits/line )

5 files in total.
168 relevant lines, 168 lines covered and 0 lines missed. ( 100.0% )
File % covered Lines Relevant Lines Lines covered Lines missed Avg. Hits / Line
lib/simplecov_small_badge.rb 100.00 % 27 10 10 0 2.30
lib/simplecov_small_badge/configuration.rb 100.00 % 55 14 14 0 140.57
lib/simplecov_small_badge/formatter.rb 100.00 % 75 40 40 0 66.98
spec/simplecov_small_badge_formatter_spec.rb 100.00 % 133 79 79 0 1.00
spec/support/mocks.rb 100.00 % 53 25 25 0 66.24

lib/simplecov_small_badge.rb

100.0% lines covered

10 relevant lines. 10 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. # Ensure we are using a compatible version of SimpleCov
  3. skipped # :nocov:
  4. skipped if defined?(SimpleCov::VERSION) &&
  5. skipped Gem::Version.new(SimpleCov::VERSION) < Gem::Version.new('0.7.1')
  6. skipped raise 'The version of SimpleCov you are using is too old. '\
  7. skipped 'Please update with `gem install simplecov` or `bundle update simplecov`'
  8. skipped end
  9. skipped # :nocov:
  10. 1 require 'simplecov_small_badge/configuration'
  11. # :nodoc:
  12. 1 module SimpleCovSmallBadge
  13. 1 @configuration = Configuration.new
  14. 1 def self.configure
  15. 1 yield config
  16. end
  17. 1 def self.config
  18. 13 @configuration
  19. end
  20. end
  21. 1 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__)))
  22. 1 require 'simplecov_small_badge/version'
  23. 1 require 'simplecov_small_badge/formatter'

lib/simplecov_small_badge/configuration.rb

100.0% lines covered

14 relevant lines. 14 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 module SimpleCovSmallBadge
  3. # Class to keep all the valid documentations that are required to build the
  4. # badge
  5. 1 class Configuration
  6. # Set up config variables.
  7. # rubocop:disable Metrics/MethodLength
  8. 1 def self.options
  9. 48 {
  10. with_groups: false,
  11. background: '#fff',
  12. title_prefix: 'scov',
  13. title_background: '#555',
  14. title_font: 'Verdana,sans-serif',
  15. font_size: 11,
  16. title_color: '#fff',
  17. coverage_background_bad: '#ff0000',
  18. coverage_background_unknown: '#cccc00',
  19. coverage_background_good: '#4dc71f',
  20. coverage_font: 'Verdana,sans-serif',
  21. coverage_font_color: '#fff',
  22. coverage_font_size: 11,
  23. badge_height: 20,
  24. badge_width: 120,
  25. filename_prefix: 'coverage_badge',
  26. output_path: SimpleCov.coverage_path,
  27. log_level: 'info',
  28. rounded_border: true,
  29. rounded_edge_radius: 3,
  30. percent_sign: '%'
  31. }
  32. end
  33. # rubocop:enable Metrics/MethodLength
  34. # set up class variables and getters/setters
  35. 1 options.each_key do |opt|
  36. 64 define_method(opt) { instance_variable_get "@#{opt}" }
  37. 504 define_method("#{opt}=") { |val| instance_variable_set("@#{opt}", val) }
  38. end
  39. 1 def initialize(**opts)
  40. SimpleCovSmallBadge::Configuration
  41. 506 .options.merge(opts).each { |opt, v| send(:"#{opt}=", v) }
  42. end
  43. 1 def to_hash
  44. 35 hash = {}
  45. 35 instance_variables.each do |var|
  46. 735 hash[var.to_s.delete('@').to_sym] = instance_variable_get(var)
  47. end
  48. 35 hash
  49. end
  50. end
  51. end

lib/simplecov_small_badge/formatter.rb

100.0% lines covered

40 relevant lines. 40 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require 'repo_small_badge/image'
  3. 1 require 'simplecov_small_badge/configuration'
  4. 1 module SimpleCovSmallBadge
  5. # Basic Badge Formater Class that creates the badges.
  6. 1 class Formatter
  7. 1 def initialize(output = nil)
  8. 12 @output = output || $stdout
  9. 12 @config = SimpleCovSmallBadge.config
  10. end
  11. 1 def format(result)
  12. 12 percent = result.source_files.covered_percent.round(0)
  13. 12 @image = RepoSmallBadge::Image.new(map_image_config(state(percent)))
  14. 12 badge('total', 'total', percent)
  15. 12 group_percent_from_result(result) do |name, title, cov_percent|
  16. 10 badge(name, title, cov_percent.round(0))
  17. end
  18. end
  19. 1 private
  20. 1 def badge(name, title, percent)
  21. 22 percent_txt = percent_text(percent)
  22. 22 @image.config_merge(map_image_config(state(percent)))
  23. 22 @image.badge(name, title, percent_txt)
  24. end
  25. 1 def percent_text(percent)
  26. 22 "#{percent}#{@config.percent_sign}"
  27. end
  28. 1 def state(covered_percent)
  29. 34 if line_coverage_minimum&.positive?
  30. 28 if covered_percent >= line_coverage_minimum
  31. 14 'good'
  32. else
  33. 14 'bad'
  34. end
  35. else
  36. 6 'unknown'
  37. end
  38. end
  39. 1 def line_coverage_minimum
  40. 62 minimums = SimpleCov.minimum_coverage
  41. 62 minimums.is_a?(Hash) ? minimums[:line] : minimums
  42. end
  43. 1 def map_image_config(state)
  44. 34 hash = {}
  45. 34 @config.to_hash.map do |key, value|
  46. 714 key = key
  47. .to_s.sub(/^coverage_background_#{state}/, 'value_background')
  48. .to_sym
  49. 714 key = key.to_s.sub(/^coverage_/, 'value_').to_sym
  50. 714 hash[key] = value
  51. end
  52. 34 hash
  53. end
  54. # converts the result to a hash consisting of the groupname
  55. # array of percentage (integer in percent), strength float
  56. # and the state [ 'good', 'bad', 'unknown' ]
  57. # consolidated for each group.
  58. 1 def group_percent_from_result(result)
  59. 12 result.groups.each do |name, files|
  60. 10 covered = files.covered_percent.round(0)
  61. 10 yield name, name, covered, covered
  62. end
  63. end
  64. end
  65. end

spec/simplecov_small_badge_formatter_spec.rb

100.0% lines covered

79 relevant lines. 79 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 require 'spec_helper'
  3. 1 describe SimpleCovSmallBadge::Formatter do
  4. 1 include TestSimpleCovSmallBadge::Mocks
  5. 1 describe '#format' do
  6. 1 context 'bad result' do
  7. 1 it do
  8. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(90)
  9. 1 mock_repo_badge_image(cov: '50%', state: 'bad')
  10. 1 result = mock_result(50)
  11. 1 expect(subject.format(result)).to be_truthy
  12. end
  13. 1 it do
  14. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(line: 90)
  15. 1 mock_repo_badge_image(cov: '50%', state: 'bad')
  16. 1 result = mock_result(50)
  17. 1 expect(subject.format(result)).to be_truthy
  18. end
  19. end
  20. 1 context 'good result' do
  21. 1 it do
  22. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(100)
  23. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  24. cov: '100%')
  25. 1 mock_repo_badge_image(title: 'library', name: 'library',
  26. cov: '100%', mock: image_mock)
  27. 1 result = mock_result(100, 'library' => mock_result_group(100))
  28. 1 expect(subject.format(result)).to be_truthy
  29. end
  30. 1 it do
  31. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(90)
  32. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  33. cov: '90%')
  34. 1 mock_repo_badge_image(title: 'library', name: 'library',
  35. cov: '90%', mock: image_mock)
  36. 1 result = mock_result(90, 'library' => mock_result_group(90))
  37. 1 expect(subject.format(result)).to be_truthy
  38. end
  39. 1 it do
  40. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(line: 100)
  41. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  42. cov: '100%')
  43. 1 mock_repo_badge_image(title: 'library', name: 'library',
  44. cov: '100%', mock: image_mock)
  45. 1 result = mock_result(100, 'library' => mock_result_group(100))
  46. 1 expect(subject.format(result)).to be_truthy
  47. end
  48. 1 it do
  49. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(line: 90)
  50. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  51. cov: '90%')
  52. 1 mock_repo_badge_image(title: 'library', name: 'library',
  53. cov: '90%', mock: image_mock)
  54. 1 result = mock_result(90, 'library' => mock_result_group(90))
  55. 1 expect(subject.format(result)).to be_truthy
  56. end
  57. end
  58. 1 context 'bad result' do
  59. 1 it do
  60. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(91)
  61. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  62. cov: '90%', state: 'bad')
  63. 1 mock_repo_badge_image(title: 'library', name: 'library',
  64. cov: '90%', state: 'bad', mock: image_mock)
  65. 1 result = mock_result(90, 'library' => mock_result_group(90))
  66. 1 expect(subject.format(result)).to be_truthy
  67. end
  68. 1 it do
  69. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(line: 91)
  70. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  71. cov: '90%', state: 'bad')
  72. 1 mock_repo_badge_image(title: 'library', name: 'library',
  73. cov: '90%', state: 'bad', mock: image_mock)
  74. 1 result = mock_result(90, 'library' => mock_result_group(90))
  75. 1 expect(subject.format(result)).to be_truthy
  76. end
  77. end
  78. 1 context 'unknown result' do
  79. 1 it do
  80. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(nil)
  81. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  82. cov: '90%', state: 'unknown')
  83. 1 mock_repo_badge_image(title: 'library', name: 'library',
  84. cov: '90%', state: 'unknown', mock: image_mock)
  85. 1 result = mock_result(90, 'library' => mock_result_group(90))
  86. 1 expect(subject.format(result)).to be_truthy
  87. end
  88. 1 it do
  89. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return({})
  90. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  91. cov: '90%', state: 'unknown')
  92. 1 mock_repo_badge_image(title: 'library', name: 'library',
  93. cov: '90%', state: 'unknown', mock: image_mock)
  94. 1 result = mock_result(90, 'library' => mock_result_group(90))
  95. 1 expect(subject.format(result)).to be_truthy
  96. end
  97. end
  98. 1 context 'mixed result' do
  99. 1 it do
  100. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(90)
  101. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  102. cov: '89%', state: 'bad')
  103. 1 mock_repo_badge_image(title: 'library', name: 'library',
  104. cov: '90%', state: 'good', mock: image_mock)
  105. 1 result = mock_result(89, 'library' => mock_result_group(90))
  106. 1 expect(subject.format(result)).to be_truthy
  107. end
  108. 1 it do
  109. 1 allow(SimpleCov).to receive(:minimum_coverage).and_return(line: 90)
  110. 1 image_mock = mock_repo_badge_image(title: 'total', name: 'total',
  111. cov: '89%', state: 'bad')
  112. 1 mock_repo_badge_image(title: 'library', name: 'library',
  113. cov: '90%', state: 'good', mock: image_mock)
  114. 1 result = mock_result(89, 'library' => mock_result_group(90))
  115. 1 expect(subject.format(result)).to be_truthy
  116. end
  117. end
  118. end
  119. end

spec/support/mocks.rb

100.0% lines covered

25 relevant lines. 25 lines covered and 0 lines missed.
    
  1. # frozen_string_literal: true
  2. 1 module TestSimpleCovSmallBadge
  3. 1 module Mocks
  4. # rubocop:disable Metrics/ParameterLists
  5. 1 def mock_repo_badge_image(cov: 100, name: 'total',
  6. title: 'total',
  7. state: 'good',
  8. config: {},
  9. mock: instance_double('Image'))
  10. 22 config = map_config_options(config, state)
  11. 22 allow(RepoSmallBadge::Image).to receive(:new)
  12. .with(config).and_return(mock)
  13. 22 allow(mock).to receive(:config_merge).with(config).and_return(config)
  14. 22 allow(mock).to receive(:badge).with(name, title, cov)
  15. 22 mock
  16. end
  17. # rubocop:enable Metrics/ParameterLists
  18. 1 def map_config_options(config_hash, state)
  19. 22 hash = {}
  20. SimpleCovSmallBadge::Configuration.options.merge(config_hash)
  21. 22 .map do |key, value|
  22. 462 key = key.to_s
  23. .sub(/^coverage_background_#{state}/, 'value_background')
  24. .to_sym
  25. 462 key = key.to_s.sub(/^coverage_/, 'value_').to_sym
  26. 462 hash[key] = value
  27. end
  28. 22 hash
  29. end
  30. 1 def mock_result(total_cov, groups_hash = {})
  31. 12 result_double = instance_double('Result')
  32. 12 allow(result_double)
  33. .to receive_message_chain('source_files.covered_percent')
  34. .and_return(total_cov)
  35. 12 allow(result_double).to receive('groups').and_return(groups_hash)
  36. 12 result_double
  37. end
  38. 1 def mock_result_group(cov, strength = 1)
  39. 10 group_double = instance_double('Group')
  40. 10 allow(group_double)
  41. .to receive('covered_percent')
  42. .and_return(cov)
  43. 10 allow(group_double)
  44. .to receive('covered_strength')
  45. .and_return(strength)
  46. 10 group_double
  47. end
  48. end
  49. end