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"]
- BRANCH_COVERAGE_SUPPORTED =
(RUBY_VERSION >= "2.5.0")
- VERSION =
"1.0.2"
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: BRANCH_COVERAGE_SUPPORTED) ⇒ Object
Class Method Details
.all_covered?(result) ⇒ Boolean
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 47 48 49 50 51 52 53 54 |
# File 'lib/single_cov.rb', line 21 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 = line_coverage.each_with_index.map { |c, i| i + 1 if c == 0 }.compact branch_coverage = (coverage.is_a?(Hash) && coverage[:branches]) if branch_coverage uncovered.concat uncovered_branches(file, branch_coverage, uncovered) end next if uncovered.size == expected_uncovered # branches are unsorted and added to the end, only sort when necessary uncovered.sort! if branch_coverage uncovered.map! do |line_start, char_start, line_end, char_end| char_start ? "#{file}:#{line_start}:#{char_start}-#{line_end}:#{char_end}" : "#{file}:#{line_start}" end 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
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/single_cov.rb', line 65 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
56 57 58 59 60 61 62 63 |
# File 'lib/single_cov.rb', line 56 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
16 17 18 19 |
# File 'lib/single_cov.rb', line 16 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
106 107 108 |
# File 'lib/single_cov.rb', line 106 def disable @disabled = true end |
.not_covered! ⇒ Object
13 14 |
# File 'lib/single_cov.rb', line 13 def not_covered! end |
.rewrite(&block) ⇒ Object
optionally rewrite the file we guessed with a lambda
9 10 11 |
# File 'lib/single_cov.rb', line 9 def rewrite(&block) @rewrite = block end |
.setup(framework, root: nil, branches: BRANCH_COVERAGE_SUPPORTED) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/single_cov.rb', line 77 def setup(framework, root: nil, branches: BRANCH_COVERAGE_SUPPORTED) if defined?(SimpleCov) raise "Load SimpleCov after SingleCov" end if branches && !BRANCH_COVERAGE_SUPPORTED raise "Branch coverage needs ruby >= 2.5.0" end @branches = branches @root = 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 |