minitest-cc 0.1.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 848f6762978bb5db1d4bfb03e13ff466a9fc7111ee1525f5825a558863b026ae
4
- data.tar.gz: fa59d3d0887831482fb6ea9ce737f841bd326d42808d3c3b89556f914657ba8d
3
+ metadata.gz: 3057b7420f4eb647e697cfa3e2fb9a0d9586665dfe577b1fc5f5552af8b7b6e8
4
+ data.tar.gz: 57173dbf03435603affd8e078faf958cbe53b227ffbc13e7989e37647e3e17f5
5
5
  SHA512:
6
- metadata.gz: ff8cc42c16e7b0baad675f64f275a3435b8f990d43dff8ccca0288888625c3480b4e20584a2dcbb58f231c996ecefcd98609431a00eeaafdeba863afab8c9b96
7
- data.tar.gz: 469ca5c033d2f4075207cd070758592b2d43248bf212eb0cd395b824b30c67b1a994a13f02d7e0078fb7e4ff12416c9ac8bcb87c1b98231fd37cbb102e844f66
6
+ metadata.gz: 72bb82508786ca90a692c3812d33a78147282fc2667fda08274feb43ba3dbd88759b079e46237aecdc2a572043de207e2a307f5b71e06ed6e6c814f491e62529
7
+ data.tar.gz: '08d491e36ad409f5b0e827b7631ee55d2aa7f1236fee5631a326bb279ffeecea36019517c04b1a669cdbaf3ae48d42314098d6dc9759a5445735256b65505239'
@@ -6,35 +6,47 @@ module Minitest
6
6
  class FileArray < Array
7
7
  ##
8
8
  # Calculate the lines average of the self array
9
- # @return [Integer] percent of lines executed
9
+ # @return [Float] percent of lines executed
10
10
  def lines_average
11
11
  lines = reduce(0) { |sum, v| sum + v.lines.to_i }
12
+
13
+ return 0.0 if lines.zero?
14
+
12
15
  lines_executed = reduce(0) { |sum, v| sum + v.lines_executed.to_i }
13
- lines_executed * 100 / lines
14
- rescue ZeroDivisionError
15
- 0
16
+ lines_executed * 100.0 / lines
16
17
  end
17
18
 
18
19
  ##
19
20
  # Calculate the branches average of the self array
20
- # @return [Integer] percent of branches executed
21
+ # @return [Float] percent of branches executed
21
22
  def branches_average
22
23
  branches = reduce(0) { |sum, v| sum + v.branches.to_i }
24
+
25
+ return 0.0 if branches.zero?
26
+
23
27
  branches_executed = reduce(0) { |sum, v| sum + v.branches_executed.to_i }
24
- branches_executed * 100 / branches
25
- rescue ZeroDivisionError
26
- 0
28
+ branches_executed * 100.0 / branches
27
29
  end
28
30
 
29
31
  ##
30
32
  # Calculate the methods average of the self array
31
- # @return [Integer] percent of methods executed
33
+ # @return [Float] percent of methods executed
32
34
  def methods_average
33
35
  methods = reduce(0) { |sum, v| sum + v.methods.to_i }
36
+
37
+ return 0.0 if methods.zero?
38
+
34
39
  methods_executed = reduce(0) { |sum, v| sum + v.methods_executed.to_i }
35
- methods_executed * 100 / methods
40
+ methods_executed * 100.0 / methods
41
+ end
42
+
43
+ ##
44
+ # Calculate total average of coverage
45
+ # @return [Float] percent of coverage
46
+ def total_coverage_percent
47
+ (lines_average + branches_average + methods_average) / 3
36
48
  rescue ZeroDivisionError
37
- 0
49
+ 0.0
38
50
  end
39
51
  end
40
52
  end
@@ -114,7 +114,7 @@ module Minitest
114
114
  # @return [String] contains the information ready to print
115
115
  #
116
116
  def to_s
117
- return "#{relative_path} : Not executed during the tests." unless tested
117
+ return "#{relative_path} : Not executed or tracked by cov." unless tested
118
118
 
119
119
  "#{relative_path}:\n" + to_s_lines + to_s_branches + to_s_methods
120
120
  end
@@ -126,7 +126,7 @@ module Minitest
126
126
  def to_s_lines
127
127
  return '' unless lines
128
128
 
129
- "\tlines: #{lines_executed}/#{lines} #{lines_percent.to_s_color}%"
129
+ " - l: #{lines_executed}/#{lines} #{lines_percent.to_s_color}%"
130
130
  end
131
131
 
132
132
  ##
@@ -136,7 +136,7 @@ module Minitest
136
136
  def to_s_branches
137
137
  return '' unless branches
138
138
 
139
- "\tbranches: #{branches_executed}/#{branches} #{branches_percent.to_s_color}%"
139
+ " - b: #{branches_executed}/#{branches} #{branches_percent.to_s_color}%"
140
140
  end
141
141
 
142
142
  ##
@@ -146,7 +146,7 @@ module Minitest
146
146
  def to_s_methods
147
147
  return '' unless methods
148
148
 
149
- "\tmethods: #{methods_executed}/#{methods} #{methods_percent.to_s_color}%"
149
+ " - m: #{methods_executed}/#{methods} #{methods_percent.to_s_color}%"
150
150
  end
151
151
 
152
152
  ##
@@ -154,9 +154,9 @@ module Minitest
154
154
  # @return [Integer] percent of coverage. executed lines / total lines
155
155
  #
156
156
  def lines_percent
157
- lines_executed * 100 / lines
158
- rescue ZeroDivisionError
159
- 0
157
+ return 0.0 if lines.zero?
158
+
159
+ lines_executed * 100.0 / lines
160
160
  end
161
161
 
162
162
  ##
@@ -164,9 +164,9 @@ module Minitest
164
164
  # @return [Integer] percent of coverage. executed branches / total branches
165
165
  #
166
166
  def branches_percent
167
- branches_executed * 100 / branches
168
- rescue ZeroDivisionError
169
- 0
167
+ return 0.0 if branches.zero?
168
+
169
+ branches_executed * 100.0 / branches
170
170
  end
171
171
 
172
172
  ##
@@ -174,9 +174,9 @@ module Minitest
174
174
  # @return [Integer] percent of coverage. executed methods / total methods
175
175
  #
176
176
  def methods_percent
177
- methods_executed * 100 / methods
178
- rescue ZeroDivisionError
179
- 0
177
+ return 0.0 if methods.zero?
178
+
179
+ methods_executed * 100.0 / methods
180
180
  end
181
181
  end
182
182
  end
@@ -7,7 +7,10 @@ module Minitest
7
7
  ##
8
8
  # Executed in the final of the minitest run
9
9
  def report
10
+ Cc.peek_result
10
11
  Cc.summary
12
+ rescue StandardError => e
13
+ puts "\n\n[!] cc extension has problem for generate the report: #{e}"
11
14
  end
12
15
  end
13
16
  end
@@ -3,6 +3,6 @@
3
3
  module Minitest
4
4
  module Cc
5
5
  # plugin version
6
- VERSION = '0.1.2'
6
+ VERSION = '1.0.0'
7
7
  end
8
8
  end
data/lib/minitest/cc.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'coverage'
3
4
  require 'minitest'
4
5
 
5
6
  require_relative 'cc/version'
@@ -11,6 +12,8 @@ module Minitest
11
12
  # Main module of the plugin
12
13
  # @author Andres
13
14
  module Cc
15
+ MODES = %i[lines branches methods].freeze
16
+
14
17
  @results = []
15
18
  @files = FileArray.new
16
19
 
@@ -32,38 +35,47 @@ module Minitest
32
35
  # @return [Array] mode of the coverage, this array could contain:
33
36
  # :lines, :branches, :methods
34
37
  cattr_accessor :coverage_mode
35
- self.coverage_mode = %i[
36
- lines
37
- branches
38
- methods
39
- ]
38
+ self.coverage_mode = MODES
40
39
 
41
40
  class << self
42
41
  ##
43
42
  # Start the coverage process
44
43
  # @see https://runebook.dev/en/docs/ruby/coverage Coverage blog post explaining the use
45
44
  # @see https://ruby-doc.org/stdlib-2.7.6/libdoc/coverage/rdoc/Coverage.html Documentation of the module
45
+ # @deprecated Use {#self.start(*args)} instead.
46
46
  def start_coverage
47
- require 'coverage'
48
- Coverage.start(coverage_mode.collect { |m| [m, true] }.to_h)
47
+ Coverage.start(@coverage_mode.collect { |m| [m, true] }.to_h)
48
+ end
49
+
50
+ ##
51
+ # start coverage process
52
+ # this method recive the arguments direct and not depend on set coverage mode before start
53
+ # @see https://runebook.dev/en/docs/ruby/coverage Coverage blog post explaining the use
54
+ # @see https://ruby-doc.org/stdlib-2.7.6/libdoc/coverage/rdoc/Coverage.html Documentation of the module
55
+ #
56
+ def start(*args)
57
+ @coverage_mode = args.collect(&:to_sym).select { |a| MODES.include? a } unless args.empty?
58
+ Coverage.start(@coverage_mode.collect { |m| [m, true] }.to_h)
49
59
  end
50
60
 
51
61
  ##
52
- # Print the results after the runs
62
+ # Print results after the runs
63
+ #
53
64
  def summary
54
- @results = Coverage.peek_result
55
- list_files
56
65
  puts "\n\n# Coverage:\n\n"
57
- @files.each { |f| puts f.to_s } if cc_mode == :per_file
58
- puts resume if cc_mode == :resume
59
- puts
60
- rescue NameError
61
- puts "\n\n[!] cc extension is installed but was not started."
66
+ case cc_mode
67
+ when :per_file
68
+ @files.each { |f| puts f.to_s }
69
+ else
70
+ puts resume
71
+ end
72
+ puts "\nAverage: #{@files.total_coverage_percent.round(2).to_s_color}%"
62
73
  end
63
74
 
64
75
  ##
65
76
  # This method populate the FileArray variable with results
66
- def list_files
77
+ def peek_result
78
+ @results = Coverage.result
67
79
  Dir.glob(tracked_files.each { |f| File.expand_path(f) }).each do |d|
68
80
  full_path = File.expand_path(d)
69
81
  @files << FileCoverage.new(full_path, d, result_for_file(full_path))
@@ -82,13 +94,13 @@ module Minitest
82
94
  end
83
95
 
84
96
  ##
85
- # compose the string with resume of averages
97
+ # compose the string with resume of cc
86
98
  # @return [String] String with averages
87
99
  def resume
88
100
  str = ''
89
- str += "lines: #{@files.lines_average.to_s_color}%\t" if coverage_mode.include? :lines
90
- str += "branches: #{@files.branches_average.to_s_color}%\t" if coverage_mode.include? :branches
91
- str += "methods: #{@files.methods_average.to_s_color}%\t" if coverage_mode.include? :methods
101
+ str += "Lines: #{@files.lines_average.round(2).to_s_color}%\t" if coverage_mode.include? :lines
102
+ str += "Branches: #{@files.branches_average.round(2).to_s_color}%\t" if coverage_mode.include? :branches
103
+ str += "Methods: #{@files.methods_average.round(2).to_s_color}%\t" if coverage_mode.include? :methods
92
104
  str
93
105
  end
94
106
  end
@@ -109,14 +121,14 @@ class String # :nodoc:
109
121
  end
110
122
  end
111
123
 
112
- class Integer # :nodoc:
124
+ class Float # :nodoc:
113
125
  def to_s_color
114
126
  case self
115
- when 1..33
127
+ when 1.0..33.0
116
128
  to_s.red
117
- when 34..66
129
+ when 33.1..66.0
118
130
  to_s.yellow
119
- when 67..100
131
+ when 66.1..100.0
120
132
  to_s.green
121
133
  else
122
134
  to_s
@@ -5,9 +5,7 @@ require 'minitest/cc'
5
5
  # Minitest module
6
6
  module Minitest
7
7
  # Init method called from minitest
8
- # it start the plugin
9
8
  def self.plugin_cc_init(_options)
10
- # insert ower reporter to minitest reporters
11
- reporter << Minitest::Cc::Reporter.new
9
+ reporter << Minitest::Cc::Reporter.new if Coverage.running?
12
10
  end
13
11
  end
metadata CHANGED
@@ -1,17 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-cc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - a-chacon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-01 00:00:00.000000000 Z
11
+ date: 2023-04-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: 'Plugin for minitest, provide a minimal information of code coverage
14
- to your test output. '
13
+ description: 'Plugin for minitest. It provides minimal information about code coverage. '
15
14
  email:
16
15
  - andres.ch@protonmail.com
17
16
  executables: []
@@ -41,14 +40,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
40
  requirements:
42
41
  - - ">="
43
42
  - !ruby/object:Gem::Version
44
- version: 2.5.3
43
+ version: 2.5.0
45
44
  required_rubygems_version: !ruby/object:Gem::Requirement
46
45
  requirements:
47
46
  - - ">="
48
47
  - !ruby/object:Gem::Version
49
48
  version: '0'
50
49
  requirements: []
51
- rubygems_version: 3.2.22
50
+ rubygems_version: 3.2.33
52
51
  signing_key:
53
52
  specification_version: 4
54
53
  summary: Minitest plugin, add code coverage metrics.