Skip to content

Commit

Permalink
Print only uncovered lines by default (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidrunger authored Nov 20, 2023
1 parent 13bed5a commit 054fd9d
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 63 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased
[no unreleased changes yet]

- Print only uncovered lines by default

## v0.2.4 (2023-05-30)
### Fixed
Expand Down
9 changes: 6 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PATH
memo_wise (>= 1.7.0, < 2)
rouge (>= 4.0.0, < 5)
rspec-core (>= 3.11.0, < 4)
runger_config (>= 3.0.0)
simplecov (>= 0.21.2, < 1)

GEM
Expand Down Expand Up @@ -51,7 +52,7 @@ GEM
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
racc (1.7.1)
racc (1.7.3)
rainbow (3.1.1)
rake (13.1.0)
regexp_parser (2.8.2)
Expand All @@ -66,10 +67,10 @@ GEM
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-support (3.12.0)
rspec-support (3.12.1)
rubocop (1.57.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
Expand All @@ -96,6 +97,8 @@ GEM
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
runger_config (3.0.1)
activesupport (>= 7.1.2)
runger_release_assistant (0.5.0)
activesupport (>= 6, < 8)
memo_wise (>= 1.7, < 2)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ SimpleCov.start
spec file (e.g. `bin/rspec spec/models/user_spec.rb`) and not when multiple specs are executed (e.g.
when simply running `bin/rspec` without any argument).

## Which lines to print

By default, only uncovered lines will be printed.

If you would like to print all lines, add this to your `spec_helper.rb` file:

```rb
SimpleCov::Formatter::Terminal.config.lines_to_print = :all
```

### Modifying the `spec_to_app_file_map`

`SimpleCov::Formatter::Terminal` has a default hash that is used to map spec files to their
Expand Down
12 changes: 7 additions & 5 deletions lib/simple_cov/formatter/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

class SimpleCov::Formatter::Terminal ; end # rubocop:disable Lint/EmptyClass

require_relative 'terminal/config'
require_relative 'terminal/file_determiner'
require_relative 'terminal/r_spec_integration'
require_relative 'terminal/result_printer'
require_relative 'terminal/version'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/string/filters'
require 'anyway_config'
require 'memo_wise'
require 'rouge'
require 'rspec/core'
require 'simplecov'

require_relative 'terminal/config'
require_relative 'terminal/file_determiner'
require_relative 'terminal/r_spec_integration'
require_relative 'terminal/result_printer'
require_relative 'terminal/version'

class SimpleCov::Formatter::Terminal
extend Forwardable
prepend MemoWise
Expand Down
8 changes: 8 additions & 0 deletions lib/simple_cov/formatter/terminal/branch_coverage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ def missed_branch_info(line, sourcefile)
join(', ').
presence
end

memo_wise \
def line_numbers_with_missing_branches(sourcefile)
uncovered_branches(sourcefile).
map(&:start_line).
uniq.
sort
end
end
20 changes: 12 additions & 8 deletions lib/simple_cov/formatter/terminal/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@
require_relative 'spec_to_app_mapping'
require 'memo_wise'

class SimpleCov::Formatter::Terminal::Config
class SimpleCov::Formatter::Terminal::Config < Anyway::Config
prepend MemoWise
include SimpleCov::Formatter::Terminal::SpecToAppMapping

attr_accessor :spec_to_app_file_map, :unmappable_spec_regexes

def initialize
@spec_to_app_file_map =
SimpleCov::Formatter::Terminal::SpecToAppMapping.default_spec_to_app_map
@unmappable_spec_regexes =
SimpleCov::Formatter::Terminal::SpecToAppMapping::DEFAULT_UNMAPPABLE_SPEC_REGEXES
module LinesToPrint
ALL = :all
UNCOVERED = :uncovered
end

attr_config(
lines_to_print: LinesToPrint::UNCOVERED,
spec_to_app_file_map:
SimpleCov::Formatter::Terminal::SpecToAppMapping.default_spec_to_app_map,
unmappable_spec_regexes:
SimpleCov::Formatter::Terminal::SpecToAppMapping::DEFAULT_UNMAPPABLE_SPEC_REGEXES,
)

memo_wise \
def write_target_info_file?
ENV.fetch('SIMPLECOV_WRITE_TARGET_TO_FILE', nil) == '1'
Expand Down
19 changes: 14 additions & 5 deletions lib/simple_cov/formatter/terminal/line_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SimpleCov::Formatter::Terminal::LinePrinter
include SimpleCov::Formatter::Terminal::BranchCoverage
include SimpleCov::Formatter::Terminal::ColorPrinting

def initialize(targeted_application_file)
def initialize(targeted_application_file = nil)
@targeted_application_file = targeted_application_file
end

Expand All @@ -30,18 +30,27 @@ def colored_line(line, sourcefile)
end

# rubocop:disable Style/StringConcatenation
def numbered_line_output(line_number, color, source_code, missed_branch_info = nil)
def numbered_line_output(line_number, color, source_code = '', missed_branch_info = nil)
colored_space =
case color
when :red_on_yellow, :white_on_red then color(' ', color)
else color(' ', :white_on_green)
end

line_number_string =
line_number_width =
if SimpleCov::Formatter::Terminal.config.write_target_info_file?
":::#{line_number}".rjust(6, ' ')
6
else
3
end

line_number_string =
if line_number.blank?
' ' * line_number_width
elsif SimpleCov::Formatter::Terminal.config.write_target_info_file?
":::#{line_number}".rjust(line_number_width, ' ')
else
line_number.to_s.rjust(3, ' ')
line_number.to_s.rjust(line_number_width, ' ')
end

output =
Expand Down
80 changes: 79 additions & 1 deletion lib/simple_cov/formatter/terminal/result_printer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,32 @@ def print_coverage_summary(sourcefile, log_addendum = nil)
end

def print_coverage_details(sourcefile)
@sourcefile = sourcefile

if SimpleCov::Formatter::Terminal.config.write_target_info_file?
target_file_writer.write_target_info_file
end

puts("---- Coverage for #{targeted_application_file} ".ljust(80, '-').rstrip)

skipped_lines = []
sourcefile.lines.each do |line|
puts(line_printer.colored_line(line, sourcefile))
if print_line?(line.line_number)
if skipped_lines.any?
print_skipped_lines(skipped_lines)
end

puts(line_printer.colored_line(line, sourcefile))
skipped_lines = []
else
skipped_lines << line.line_number
end
end

if skipped_lines.any?
print_skipped_lines(skipped_lines)
end

puts(<<~LOG.squish)
----
Line coverage: #{colorized_coverage(sourcefile.covered_percent)}
Expand All @@ -65,6 +83,20 @@ def print_coverage_details(sourcefile)
LOG
end

def print_skipped_lines(skipped_lines)
divider = ' -' * 40

puts(line_printer.numbered_line_output(nil, :white, divider))
puts(
line_printer.numbered_line_output(
nil,
:white,
"#{skipped_lines.size} covered line(s) omitted".center(80, ' '),
),
)
puts(line_printer.numbered_line_output(nil, :white, divider))
end

def print_no_coverage_info_found
puts(<<~LOG.squish)
No code coverage info was found for "#{targeted_application_file}". Try stopping and
Expand Down Expand Up @@ -120,6 +152,52 @@ def colorized_uncovered_branches(num_uncovered_branches)
end
end

def print_line?(line_number)
line_numbers_to_print.include?(line_number)
end

def sourcefile
@sourcefile ||= @result.files.find { _1.filename.end_with?(targeted_application_file) }
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
memo_wise \
def line_numbers_to_print
max_line_number = sourcefile.lines.map(&:line_number).max

begin
case SimpleCov::Formatter::Terminal.config.lines_to_print.to_sym
in SimpleCov::Formatter::Terminal::Config::LinesToPrint::ALL
(1..max_line_number).to_a
in SimpleCov::Formatter::Terminal::Config::LinesToPrint::UNCOVERED
line_numbers_to_print = []

sourcefile.lines.each do |line|
if (
line.coverage.nil? || (
(line.coverage > 0) &&
!line_numbers_with_missing_branches(sourcefile).include?(line.line_number)
)
)
next
end

line_number = line.line_number
contextualized_line_numbers =
((line_number - 2)..(line_number + 2)).
to_a.
select do |context_line_number|
context_line_number.positive? && context_line_number <= max_line_number
end
line_numbers_to_print += contextualized_line_numbers
end

line_numbers_to_print
end
end.to_set
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

memo_wise \
def line_printer
SimpleCov::Formatter::Terminal::LinePrinter.new(targeted_application_file)
Expand Down
1 change: 1 addition & 0 deletions simple_cov-formatter-terminal.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Gem::Specification.new do |spec|
spec.add_dependency('memo_wise', '>= 1.7.0', '< 2')
spec.add_dependency('rouge', '>= 4.0.0', '< 5')
spec.add_dependency('rspec-core', '>= 3.11.0', '< 4')
spec.add_dependency('runger_config', '>= 3.0.0')
spec.add_dependency('simplecov', '>= 0.21.2', '< 1')

# For more information and examples about making a new gem, check out our
Expand Down
Loading

0 comments on commit 054fd9d

Please sign in to comment.