Skip to content

Commit

Permalink
enable branch coverage by default
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 3, 2018
1 parent 8b0d7f3 commit b70d14e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Actionable code coverage.
- Get actionable feedback on every successful test run
- Only 2-5% runtime overhead on small files compared to 50% for `SimpleCov`
- No more PRs with bad test coverage
- Branch coverage on ruby 2.5+
- Branch coverage on ruby 2.5+ (disable via `branches: false`)

```Ruby
# Gemfile
gem 'single_cov', group: :test

# spec/spec_helper.rb ... load before loading rails / minitest / libraries
require 'single_cov'
SingleCov.setup :rspec, branches: true
SingleCov.setup :rspec

# spec/foobar_spec.rb ... add covered! call to every test file
require 'spec_helper'
Expand All @@ -37,7 +37,7 @@ lib/foobar.rb:23
Call setup before loading minitest.
```Ruby
SingleCov.setup :minitest, branches: true
SingleCov.setup :minitest
require 'minitest/autorun'
```
Expand Down
13 changes: 6 additions & 7 deletions lib/single_cov.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module SingleCov
COVERAGES = []
MAX_OUTPUT = 40
APP_FOLDERS = ["models", "serializers", "helpers", "controllers", "mailers", "views", "jobs"]
BRANCH_COVERAGE_SUPPORTED = (RUBY_VERSION >= "2.5.0")

class << self
# optionally rewrite the file we guessed with a lambda
Expand Down Expand Up @@ -66,18 +67,16 @@ def assert_tested(files: glob('{app,lib}/**/*.rb'), tests: default_tests, untest
end
end

def setup(framework, root: nil, branches: false)
def setup(framework, root: nil, branches: BRANCH_COVERAGE_SUPPORTED)
if defined?(SimpleCov)
raise "Load SimpleCov after SingleCov"
end
if branches && RUBY_VERSION < "2.5.0"
warn "Branch coverage needs ruby 2.5.0"
@branches = false
else
@branches = branches
if branches && !BRANCH_COVERAGE_SUPPORTED
raise "Branch coverage needs ruby >= 2.5.0"
end

@root = root if root
@branches = branches
@root = root

case framework
when :minitest
Expand Down
5 changes: 4 additions & 1 deletion specs/single_cov_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ def self.it_does_not_complain_when_everything_is_covered
end

describe "when SimpleCov was loaded after" do
around { |t| change_file("test/a_test.rb", default_setup, "#{default_setup}\nrequire 'simplecov'\nSimpleCov.start\n", &t) }
# NOTE: SimpleCov also starts coverage and will break when we activated branches
let(:branchless_setup) { default_setup.sub('root: root', 'root: root, branches: false') }

around { |t| change_file("test/a_test.rb", default_setup, "#{branchless_setup}\nrequire 'simplecov'\nSimpleCov.start\n", &t) }

it "works" do
result = sh "ruby test/a_test.rb"
Expand Down

0 comments on commit b70d14e

Please sign in to comment.