Giter Club home page Giter Club logo

simplecov-small-badge's Introduction

SimpleCovSmallBadge

Gem Version Build Status Depfu Coverage RubyCritic

SimpleCovBadge is a gem that can be added to the Gemfile and will produce a file called coverage_badge.png in the coverage directory. It could be looking as follows dependent on how it is configured.

Badge

The idea is to created a badge for SimpleCov to create a persistable image that shows the coverage in percent as a badge.

Installation

The badge creation is dependent on the Repo-small-badge gem which creates and SVG badge. It can be installed in your Ruby library or rails app as part of the Gemfile as follows.

# In your gemfile
gem 'simplecov-small-badge', :require => false

This gem is an alternative and inspired by the great work in the other gem simplecov-badge which does a similar badge but looks different and cannot easily made small. So it's mostly an optical alternative.

Usage

Whereever you are integrating SimpleCov you can configure the SimpleCovSmallBadge gem as any formater can be configured. The default integration could looks as follows:

require 'simplecov_small_badge'

# Wherever your SimpleCov.start block is (spec_helper.rb, test_helper.rb, or .simplecov)
SimpleCov.start do
  # add your normal SimpleCov configs
  add_filter "/app/model"
  # call SimpleCov::Formatter::BadgeFormatter after the normal HTMLFormatter
  SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
    SimpleCov::Formatter::HTMLFormatter,
    SimpleCovSmallBadge::Formatter
  ])
end

# configure any options you want for SimpleCov::Formatter::BadgeFormatter
SimpleCovSmallBadge.configure do |config|
  # does not created rounded borders
  config.rounded_border = true
  # set the background for the title to darkgrey
  config.background = '#ffffcc'
end

Integration into Travis-CI via github-pages

This process is split into two steps.

  1. You need to create an access token for you github repository that can be configured to travis to allow password-less pushing. This is described in Github Help - Authenticating to GitHub / Creating a personal access token for the command line

  2. Configure travis to push the result to github-pages inspired from the Travis description and configuration to be found in .travis.yml

  3. Integrate your badge into the README.md as follows ![Coverage](https://marcgrimme.github.io/simplecov-small-badge/badges/coverage_badge_total.png)

Configuration Options

The behaviour of SimpleCovSmallBadge can be influenced by configuration options as defined in the configuration class.

Development

After checking out the repo, run bundle update to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/marcgrimme/simplecov-small-badge/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

simplecov-small-badge's People

Contributors

dennyabraham avatar depfu[bot] avatar joeyates avatar marcgrimme avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

simplecov-small-badge's Issues

Formatter SimpleCovSmallBadge::Formatter failed with NoMethodError: undefined method `positive?' for {}:Hash

While using:

  • ruby 3.x (I tested with 3.0.4 or 3.1.2)
  • rails 6.1.7
  • simplecov 0.21.2

And running rspec, at the end I'm seeing this error:

Formatter SimpleCovSmallBadge::Formatter failed with NoMethodError: undefined method `positive?' for {}:Hash

  if SimpleCov.minimum_coverage&.positive?
                               ^^^^^^^^^^^ (/home/rael/.rvm/gems/ruby-3.1.2@output-set/gems/simplecov-small-badge-0.2.4/lib/simplecov_small_badge/formatter.rb:36:in `state')

Setting config.background doesn't change the colour

Following the instructions in the readme, setting the config.background to a hex value doesn't actually change the appearance of the generated SVG. I can see that a rectangle with the colour I've indicated is drawn, but it must be layered underneath the others because the appearance of the badge does not change.

This, for example, is the line that is added:

<rect height="20" width="120" rx="3" fill="#FF3399"/>

This is the badge image that is generated:

image

The total badge output is as follows (formatted for readability):

<svg contentScriptType="text/ecmascript" contentStyleType="text/css" preserveAspectRatio="xMidYMid meet" version="1.0"
  height="20" width="120" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
  <linearGradient id="smooth" x2="0" y2="120">
    <stop offset="0" stop-color="#bbb" stop-opacity=".1" />
    <stop offset="1" stop-opacity=".1" />
  </linearGradient>
  <clipPath id="round">
    <rect height="20" width="120" rx="3" fill="#FF3399" />
  </clipPath>
  <g clip-path="url(#round)">
    <rect height="20" width="60" fill="#555" />
    <rect x="60" height="20" width="60" fill="#cccc00" />
    <rect height="20" width="120" fill="url(#smooth)" />
  </g>
  <g fill="#fff" text-anchor="middle" font-family="Verdana,sans-serif" font-size="11">
    <text x="30" y="15" fill="#010101" fill-opacity="0.3">
      scov total
    </text>
    <text x="30" y="14">
      scov total
    </text>
  </g>
  <g fill="#fff" text-anchor="middle" font-family="Verdana,sans-serif" font-size="11">
    <text x="90" y="15" fill="#010101" fill-opacity="0.3">
      95%
    </text>
    <text x="90" y="14">
      95%
    </text>
  </g>
</svg>

Badge not generating when tests are run using rspec parallel gem

I'm using the following rake task to merge results of different test runs, and generate HTML report of simplecov coverage -

namespace :coverage do
  desc "Collates all result sets generated by the different test runners"
  task :report do
    require 'simplecov'
    require 'simplecov_small_badge'

    # Collate coverage results using SimpleCov
    SimpleCov.collate Dir["test/simplecov-resultset-*/.resultset.json"], 'rails' do
      formatter SimpleCov::Formatter::MultiFormatter.new(
        [
          SimpleCov::Formatter::SimpleFormatter,
          SimpleCov::Formatter::HTMLFormatter,
          SimpleCovSmallBadge::Formatter,
        ]
      )
    end
  end
end

The combined coverage report is generated fine, but the badge is not being generated.

Formatter errors with undefined method 'positive?'

Versions Used:

simplecov-small-badge version: 0.2.4
simplecov version: 0.21.2
ruby version: 2.7.1p83
rails version: 6.0.3.4

Issue:

When running rails test, SimpleCovSmallBadge::Formatter throws the following error

Formatter SimpleCovSmallBadge::Formatter failed with NoMethodError: undefined method `positive?' for [:line]:Array (gems/simplecov-small-badge-0.2.4/lib/simplecov_small_badge/formatter.rb:36:in `state')

with unnecessary parts of the path omitted.

It appears as though at some point simplecov started using a hash for the SimpleCov.minimum_coverage variable and this is causing it to fail. By placing the following code into my config/initializers path in the rails app so that it overwrote the problem method I was able to make the code work. It simply accesses the hash and does the rest as normal

require 'simplecov'
require 'simplecov_small_badge'
SimpleCovSmallBadge::Formatter.class_eval do
  private
  def state(covered_percent)
    if SimpleCov.minimum_coverage[:line]&.positive?
      if covered_percent >= SimpleCov.minimum_coverage[:line]
        'good'
      else
        'bad'
      end
    else
      'unknown'
    end
  end
end

However this is the first time I have ever used this gem, or the simplecov gem so perhaps there is just an issue with my configuration. I would appreciate some input. If this is a proper fix just let me know and I can make a pull request for it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.