-
Notifications
You must be signed in to change notification settings - Fork 22
/
verify.rb
175 lines (155 loc) · 4.18 KB
/
verify.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require 'rake'
require 'rake/tasklib'
require 'yardstick'
module Yardstick
module Rake
# A rake task for verifying the doc thresholds
class Verify < ::Rake::TaskLib
# Set the threshold
#
# @return [undefined]
#
# @api public
attr_writer :threshold
# Specify if the threshold should match the coverage
#
# @return [undefined]
#
# @api public
attr_writer :require_exact_threshold
# List of paths to measure
#
# @return [undefined]
#
# @api public
attr_writer :path
# Specify if the coverage summary should be displayed
#
# @return [undefined]
#
# @api public
attr_writer :verbose
# Initialize a Verify task
#
# @example
# task = Yardstick::Rake::Verify.new do |task|
# task.threshold = 100
# end
#
# @param [Symbol] name
# optional task name
#
# @yield [task]
# yield to self
#
# @yieldparam [Yardstick::Rake::Verify] task
# the verification task
#
# @return [Yardstick::Rake::Verify] task
# the verification task instance
#
# @api public
def initialize(name = :verify_measurements)
@name = name
@require_exact_threshold = true
@path = 'lib/**/*.rb'
@verbose = true
yield self
assert_threshold
define
end
# Verify the YARD coverage measurements
#
# @example
# task.verify_measurements # output coverage and threshold
#
# @return [undefined]
#
# @raise [RuntimeError]
# raised if threshold is not met
# @raise [RuntimeError]
# raised if threshold is not equal to the coverage
#
# @api public
def verify_measurements
puts "Coverage: #{total_coverage}% (threshold: #{@threshold}%)" if verbose
assert_meets_threshold
assert_matches_threshold
end
protected
# The total YARD coverage
#
# @return [Float]
# the total coverage
#
# @api private
def total_coverage
measurements = Yardstick.measure(@path)
self.class.round_percentage(measurements.coverage * 100)
end
private
# Define the task
#
# @return [undefined]
#
# @api private
def define
desc "Verify that yardstick coverage is at least #{@threshold}%"
task(@name) { verify_measurements }
end
# Raise an exception if threshold is not set
#
# @return [undefined]
#
# @raise [RuntimeError]
# raised if threshold is not set
#
# @api private
def assert_threshold
if @threshold.nil?
raise 'threshold must be set'
end
end
# Raise an exception if the threshold is not met
#
# @return [undefined]
#
# @raise [RuntimeError]
# raised if threshold is not met
#
# @api private
def assert_meets_threshold
total_coverage = self.total_coverage
if total_coverage < @threshold
raise "Coverage must be at least #{@threshold}% but was #{total_coverage}%"
end
end
# Raise an exception if the threshold is not equal to the coverage
#
# @return [undefined]
#
# @raise [RuntimeError]
# raised if threshold is not equal to the coverage
#
# @api private
def assert_matches_threshold
total_coverage = self.total_coverage
if @require_exact_threshold && total_coverage > @threshold
raise "Coverage has increased above the threshold of #{@threshold}% to #{total_coverage}%. You should update your threshold value."
end
end
# Round percentage to 1/10th of a percent
#
# @param [Float] percentage
# the percentage to round
#
# @return [Float]
# the rounded percentage
#
# @api private
def self.round_percentage(percentage)
(percentage * 10).ceil / 10.0
end
end # class Verify
end # module Rake
end # module Yardstick