Module: SingleCov
- Defined in:
- lib/single_cov.rb,
lib/single_cov/version.rb
Constant Summary collapse
- COVERAGES =
[]
- MAX_OUTPUT =
40- APP_FOLDERS =
["models", "serializers", "helpers", "controllers", "mailers", "views", "jobs"]
- VERSION =
"0.8.3"
Class Method Summary collapse
- .all_covered?(result) ⇒ Boolean
- .assert_tested(files: glob('{app,lib}/**/*.rb'), tests: default_tests, untested: []) ⇒ Object
- .assert_used(tests: default_tests) ⇒ Object
- .covered!(file: nil, uncovered: 0) ⇒ Object
-
.disable ⇒ Object
use this in forks when using rspec to silence duplicated output.
- .not_covered! ⇒ Object
-
.rewrite(&block) ⇒ Object
optionally rewrite the file we guessed with a lambda.
- .setup(framework, root: nil, branches: false) ⇒ Object
Class Method Details
.all_covered?(result) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/single_cov.rb', line 20 def all_covered?(result) errors = COVERAGES.map do |file, expected_uncovered| if coverage = result["#{root}/#{file}"] line_coverage = (coverage.is_a?(Hash) ? coverage.fetch(:lines) : coverage) uncovered_lines = line_coverage.each_with_index.map { |c, i| i + 1 if c == 0 }.compact uncovered = uncovered_lines.map { |l| "#{file}:#{l}" } if branch_coverage = (coverage.is_a?(Hash) && coverage[:branches]) uncovered += uncovered_branches(file, branch_coverage, uncovered_lines) end next if uncovered.size == expected_uncovered warn_about_bad_coverage(file, expected_uncovered, uncovered) else warn_about_no_coverage(file) end end.compact return true if errors.empty? errors = errors.join("\n").split("\n") # unify arrays with multiline strings errors[MAX_OUTPUT..-1] = "... coverage output truncated" if errors.size >= MAX_OUTPUT warn errors errors.all? { |l| l.end_with?('?') } # ok if we just have warnings end |
.assert_tested(files: glob('{app,lib}/**/*.rb'), tests: default_tests, untested: []) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/single_cov.rb', line 57 def assert_tested(files: glob('{app,lib}/**/*.rb'), tests: default_tests, untested: []) missing = files - tests.map { |t| file_under_test(t) } fixed = untested - missing missing -= untested if fixed.any? raise "Remove #{fixed.inspect} from untested!" elsif missing.any? raise missing.map { |f| "missing test for #{f}" }.join("\n") end end |
.assert_used(tests: default_tests) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/single_cov.rb', line 48 def assert_used(tests: default_tests) bad = tests.select do |file| File.read(file) !~ /SingleCov.(not_)?covered\!/ end unless bad.empty? raise bad.map { |f| "#{f}: needs to use SingleCov.covered!" }.join("\n") end end |
.covered!(file: nil, uncovered: 0) ⇒ Object
15 16 17 18 |
# File 'lib/single_cov.rb', line 15 def covered!(file: nil, uncovered: 0) file = guess_and_check_covered_file(file) COVERAGES << [file, uncovered] end |
.disable ⇒ Object
use this in forks when using rspec to silence duplicated output
100 101 102 |
# File 'lib/single_cov.rb', line 100 def disable @disabled = true end |
.not_covered! ⇒ Object
12 13 |
# File 'lib/single_cov.rb', line 12 def not_covered! end |
.rewrite(&block) ⇒ Object
optionally rewrite the file we guessed with a lambda
8 9 10 |
# File 'lib/single_cov.rb', line 8 def rewrite(&block) @rewrite = block end |
.setup(framework, root: nil, branches: false) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/single_cov.rb', line 69 def setup(framework, root: nil, branches: false) 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 end @root = root if root case framework when :minitest minitest_should_not_be_running! return if minitest_running_subset_of_tests? when :rspec return if rspec_running_subset_of_tests? else raise "Unsupported framework #{framework.inspect}" end start_coverage_recording override_at_exit do |status, _exception| exit 1 if (!defined?(@disabled) || !@disabled) && status == 0 && !SingleCov.all_covered?(coverage_results) end end |