Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plain-text block output style option #15

Merged
merged 6 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,46 @@ SimpleCov::Formatter::Console.table_options = {:style => {:width => 200}}
SimpleCov.formatter = SimpleCov::Formatter::Console
```

### Block output style

As an alternative to the default table output format, results can be printed as plain text blocks instead by setting
the formatter `output_style` to 'block':

```ruby
SimpleCov::Formatter::Console.output_style = 'block'
SimpleCov.formatter = SimpleCov::Formatter::Console
```

Example output:

```text
COVERAGE: 82.34% -- 2345/2848 lines in 111 files

showing bottom (worst) 5 of 69 files

file: lib/bixby/api/websocket_server.rb
coverage: 22.73% (17/22 lines)
missed: 11, 14, 17-18, 20-22, 24, 28-30, 32, 36-...

file: app/models/role.rb
coverage: 30.77% (9/13 lines)
missed: 28-34, 36-37

file: lib/bixby/modules/metrics/rescan.rb
coverage: 32.14% (19/28 lines)
missed: 19-23, 27-31, 33-37, 39-41, 43

file: lib/archie/mail.rb
coverage: 42.86% (8/14 lines)
missed: 6-8, 12-15, 22

file: lib/archie/controller.rb
coverage: 44.00% (28/50 lines)
missed: 18-21, 23, 27-30, 32, 38-40, 44-45, 48-4...

42 file(s) with 100% coverage not shown
```

## History

### 0.6 (2019.11.08)
Expand Down
71 changes: 47 additions & 24 deletions lib/simplecov-console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class SimpleCov::Formatter::Console

VERSION = IO.read(File.expand_path("../../VERSION", __FILE__)).strip

ATTRIBUTES = [:table_options, :use_colors, :max_rows, :show_covered, :sort]
ATTRIBUTES = [:table_options, :use_colors, :max_rows, :show_covered, :sort, :output_style]
class << self
attr_accessor(*ATTRIBUTES)
end
Expand All @@ -23,6 +23,9 @@ class << self
# configure sort from SORT env var
SimpleCov::Formatter::Console.sort = ENV.fetch('SORT', 'coverage')

# configure output format ('table', 'block')
SimpleCov::Formatter::Console.output_style = ENV.fetch('OUTPUT_STYLE', 'table')

def format(result)

root = nil
Expand Down Expand Up @@ -66,33 +69,15 @@ def format(result)
end
end

table = files.map do |f|
[
colorize(pct(f)),
f.filename.gsub(root + "/", ''),
f.lines_of_code,
f.missed_lines.count,
missed(f.missed_lines).join(", ")
]
end

max_rows = SimpleCov::Formatter::Console.max_rows

if ![-1, nil].include?(max_rows) && table.size > max_rows then
puts "showing bottom (worst) #{max_rows} of #{table.size} files"
table = table.slice(0, max_rows)
if ![-1, nil].include?(max_rows) && files.size > max_rows then
puts "showing bottom (worst) #{max_rows} of #{files.size} files"
files = files.slice(0, max_rows)
end

table_options = SimpleCov::Formatter::Console.table_options || {}
if !table_options.kind_of?(Hash) then
raise ArgumentError.new("SimpleCov::Formatter::Console.table_options must be a Hash")
end
headings = %w{ coverage file lines missed missing }

opts = table_options.merge({:headings => headings, :rows => table})
t = Terminal::Table.new(opts)
puts t

puts send(SimpleCov::Formatter::Console.output_style << "_output",files,root)

if covered_files > 0 then
puts "#{covered_files} file(s) with 100% coverage not shown"
end
Expand Down Expand Up @@ -149,4 +134,42 @@ def colorize(s)
end
end

# format per-file results output using Terminal::Table
def table_output(files, root)
table = files.map do |f|
[
colorize(pct(f)),
f.filename.gsub(root + "/", ''),
f.lines_of_code,
f.missed_lines.count,
missed(f.missed_lines).join(", ")
]
end

table_options = SimpleCov::Formatter::Console.table_options || {}
if !table_options.kind_of?(Hash) then
raise ArgumentError.new("SimpleCov::Formatter::Console.table_options must be a Hash")
end

headings = %w{ coverage file lines missed missing }

opts = table_options.merge({:headings => headings, :rows => table})
Terminal::Table.new(opts)
end

# format per-file results output as plain text blocks
def block_output(files, root)
blocks = []
files.each do |f|
block = []
block << sprintf("%8.8s: %s", 'file', f.filename.gsub(root + "/", ''))
block << sprintf("%8.8s: %s (%d/%d lines)", 'coverage',
colorize(sprintf("%.2f%%", f.covered_percent)),
f.covered_lines.count, f.lines_of_code)
block << sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", "))
blocks << block.join("\n")
end
"\n" << blocks.join("\n\n") << "\n\n"
end

end
36 changes: 33 additions & 3 deletions test/test_simplecov-console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

class TestSimplecovConsole < MiniTest::Test

Source = Struct.new(:line_number)
# mock for SimpleCov::SourceFile::Line
Line = Struct.new(:line_number)

# mock for SimpleCov::SourceFile
SourceFile = Struct.new(
:filename,
:lines_of_code,
:covered_lines,
:missed_lines,
:covered_percent
)

def setup
@console = SimpleCov::Formatter::Console.new
Expand All @@ -14,9 +24,29 @@ def test_defined
end

def test_missed
missed_lines = [Source.new(1), Source.new(2),
Source.new(3), Source.new(5)]
missed_lines = [Line.new(1), Line.new(2),
Line.new(3), Line.new(5)]
expected_result = ["1-3", "5"]
assert_equal @console.missed(missed_lines), expected_result
end

def test_table_output
SimpleCov::Formatter::Console.output_style = 'table'
files = [
SourceFile.new('foo.rb',5,[2,3],[Line.new(1), Line.new(4), Line.new(5)],40.0)
]
actual = @console.table_output(files,'/')
assert actual.is_a? Terminal::Table
assert_equal 1, actual.rows.count
end

def test_block_output
SimpleCov::Formatter::Console.use_colors = false
SimpleCov::Formatter::Console.output_style = 'block'
files = [
SourceFile.new('foo.rb',5,[2,3],[Line.new(1), Line.new(4), Line.new(5)],40.0)
]
expected = "\n file: foo.rb\ncoverage: 40.00% (2/5 lines)\n missed: 1, 4-5\n\n"
assert_equal expected, @console.block_output(files,'/')
end
end