From bcf410e71b9ba7de40c4efc34fabd6dea954fb18 Mon Sep 17 00:00:00 2001 From: Homer Painter Date: Fri, 28 Feb 2020 11:30:07 -0500 Subject: [PATCH 1/6] Initial commit --- lib/simplecov-console.rb | 68 ++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/lib/simplecov-console.rb b/lib/simplecov-console.rb index fffeb5d..59fae41 100644 --- a/lib/simplecov-console.rb +++ b/lib/simplecov-console.rb @@ -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 @@ -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 @@ -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 - + 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 @@ -149,4 +134,39 @@ def colorize(s) end end + 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}) + t = Terminal::Table.new(opts) + puts t + end + + def block_output(files, root) + puts "" + files.each do |f| + puts sprintf("%8.8s: %s", 'file', f.filename.gsub(root + "/", '')) + puts sprintf("%8.8s: %s (%d/%d lines)", 'coverage', + colorize(sprintf("%.2f%%", f.covered_percent)), + f.covered_lines.count, f.lines_of_code) + puts sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", ")) + puts "" + end + end + end From f35145ee775a6ef60e652508fd0b1fe7f2d79b1c Mon Sep 17 00:00:00 2001 From: Homer Painter Date: Fri, 28 Feb 2020 14:05:21 -0500 Subject: [PATCH 2/6] return string from output methods --- lib/simplecov-console.rb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/simplecov-console.rb b/lib/simplecov-console.rb index 59fae41..6550ea3 100644 --- a/lib/simplecov-console.rb +++ b/lib/simplecov-console.rb @@ -76,7 +76,7 @@ def format(result) files = files.slice(0, max_rows) end - send(SimpleCov::Formatter::Console.output_style << "_output",files,root) + 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" @@ -134,6 +134,7 @@ def colorize(s) end end + # format per-file results output using Terminal::Table def table_output(files, root) table = files.map do |f| [ @@ -153,20 +154,22 @@ def table_output(files, root) headings = %w{ coverage file lines missed missing } opts = table_options.merge({:headings => headings, :rows => table}) - t = Terminal::Table.new(opts) - puts t + Terminal::Table.new(opts) end + # format per-file results output as plain text blocks def block_output(files, root) - puts "" + blocks = [""] files.each do |f| - puts sprintf("%8.8s: %s", 'file', f.filename.gsub(root + "/", '')) - puts sprintf("%8.8s: %s (%d/%d lines)", 'coverage', + 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) - puts sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", ")) - puts "" + block << sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", ")) + blocks << block.join("\n") end + blocks.join("\n") end end From d381e95058e4a9b93a413466048453598863e4f8 Mon Sep 17 00:00:00 2001 From: Homer Painter Date: Fri, 28 Feb 2020 14:05:38 -0500 Subject: [PATCH 3/6] Add tests for 'table_output', 'block_output' methods --- test/test_simplecov-console.rb | 36 +++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/test/test_simplecov-console.rb b/test/test_simplecov-console.rb index 953b106..359c4c1 100644 --- a/test/test_simplecov-console.rb +++ b/test/test_simplecov-console.rb @@ -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 @@ -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" + assert_equal expected, @console.block_output(files,'/') + end end From e2bffd0a58c3536b9b29886ceea98322dcfb344b Mon Sep 17 00:00:00 2001 From: Homer Painter Date: Fri, 28 Feb 2020 14:06:50 -0500 Subject: [PATCH 4/6] Update README.md with block output option details --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index cd2bcd2..ff271ef 100644 --- a/README.md +++ b/README.md @@ -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) From 941e81037cc90c63c8b1f620cf270d9b40cffff9 Mon Sep 17 00:00:00 2001 From: Homer Painter Date: Fri, 28 Feb 2020 15:18:26 -0500 Subject: [PATCH 5/6] fix to add newline between output blocks --- lib/simplecov-console.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/simplecov-console.rb b/lib/simplecov-console.rb index 6550ea3..0d68d8f 100644 --- a/lib/simplecov-console.rb +++ b/lib/simplecov-console.rb @@ -159,7 +159,7 @@ def table_output(files, root) # format per-file results output as plain text blocks def block_output(files, root) - blocks = [""] + blocks = [] files.each do |f| block = [] block << sprintf("%8.8s: %s", 'file', f.filename.gsub(root + "/", '')) @@ -169,7 +169,7 @@ def block_output(files, root) block << sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", ")) blocks << block.join("\n") end - blocks.join("\n") + "\n" << blocks.join("\n\n") << "\n" end end From 3bd7b20464115bab3a0515077af8c5dbb784b385 Mon Sep 17 00:00:00 2001 From: Homer Painter Date: Fri, 28 Feb 2020 15:32:43 -0500 Subject: [PATCH 6/6] test update to match line spacing --- lib/simplecov-console.rb | 2 +- test/test_simplecov-console.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/simplecov-console.rb b/lib/simplecov-console.rb index 0d68d8f..7432396 100644 --- a/lib/simplecov-console.rb +++ b/lib/simplecov-console.rb @@ -169,7 +169,7 @@ def block_output(files, root) block << sprintf("%8.8s: %s", 'missed', missed(f.missed_lines).join(", ")) blocks << block.join("\n") end - "\n" << blocks.join("\n\n") << "\n" + "\n" << blocks.join("\n\n") << "\n\n" end end diff --git a/test/test_simplecov-console.rb b/test/test_simplecov-console.rb index 359c4c1..b5d8b41 100644 --- a/test/test_simplecov-console.rb +++ b/test/test_simplecov-console.rb @@ -46,7 +46,7 @@ def test_block_output 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" + 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