/
filter_manager.rb
203 lines (175 loc) · 6.42 KB
/
filter_manager.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
module RSpec
module Core
# Manages the filtering of examples and groups by matching tags declared on
# the command line or options files, or filters declared via
# `RSpec.configure`, with hash key/values submitted within example group
# and/or example declarations. For example, given this declaration:
#
# describe Thing, :awesome => true do
# it "does something" do
# # ...
# end
# end
#
# That group (or any other with `:awesome => true`) would be filtered in
# with any of the following commands:
#
# rspec --tag awesome:true
# rspec --tag awesome
# rspec -t awesome:true
# rspec -t awesome
#
# Prefixing the tag names with `~` negates the tags, thus excluding this group with
# any of:
#
# rspec --tag ~awesome:true
# rspec --tag ~awesome
# rspec -t ~awesome:true
# rspec -t ~awesome
#
# ## Options files and command line overrides
#
# Tag declarations can be stored in `.rspec`, `~/.rspec`, or a custom
# options file. This is useful for storing defaults. For example, let's
# say you've got some slow specs that you want to suppress most of the
# time. You can tag them like this:
#
# describe Something, :slow => true do
#
# And then store this in `.rspec`:
#
# --tag ~slow:true
#
# Now when you run `rspec`, that group will be excluded.
#
# ## Overriding
#
# Of course, you probably want to run them sometimes, so you can override
# this tag on the command line like this:
#
# rspec --tag slow:true
#
# ## RSpec.configure
#
# You can also store default tags with `RSpec.configure`. We use `tag` on
# the command line (and in options files like `.rspec`), but for historical
# reasons we use the term `filter` in `RSpec.configure:
#
# RSpec.configure do |c|
# c.filter_run_including :foo => :bar
# c.filter_run_excluding :foo => :bar
# end
#
# These declarations can also be overridden from the command line.
#
# @see RSpec.configure
# @see Configuration#filter_run_including
# @see Configuration#filter_run_excluding
class FilterManager
DEFAULT_EXCLUSIONS = {
:if => lambda { |value| !value },
:unless => lambda { |value| value }
}
STANDALONE_FILTERS = [:locations, :line_numbers, :full_description]
module Describable
PROC_HEX_NUMBER = /0x[0-9a-f]+@/
PROJECT_DIR = File.expand_path('.')
def description
reject { |k, v| RSpec::Core::FilterManager::DEFAULT_EXCLUSIONS[k] == v }.inspect.gsub(PROC_HEX_NUMBER, '').gsub(PROJECT_DIR, '.').gsub(' (lambda)','')
end
def empty_without_conditional_filters?
reject { |k, v| RSpec::Core::FilterManager::DEFAULT_EXCLUSIONS[k] == v }.empty?
end
end
module BackwardCompatibility
def merge(orig, opposite, *updates)
_warn_deprecated_keys(updates.last)
super
end
def reverse_merge(orig, opposite, *updates)
_warn_deprecated_keys(updates.last)
super
end
# Supports a use case that probably doesn't exist: overriding the
# if/unless procs.
def _warn_deprecated_keys(updates)
_warn_deprecated_key(:unless, updates) if updates.has_key?(:unless)
_warn_deprecated_key(:if, updates) if updates.has_key?(:if)
end
# Emits a deprecation warning for keys that will not be supported in
# the future.
def _warn_deprecated_key(key, updates)
RSpec.warn_deprecation("\nDEPRECATION NOTICE: FilterManager#exclude(#{key.inspect} => #{updates[key].inspect}) is deprecated with no replacement, and will be removed from rspec-3.0.")
@exclusions[key] = updates.delete(key)
end
end
attr_reader :exclusions, :inclusions
def initialize
@exclusions = DEFAULT_EXCLUSIONS.dup.extend(Describable)
@inclusions = {}.extend(Describable)
extend(BackwardCompatibility)
end
def add_location(file_path, line_numbers)
# locations is a hash of expanded paths to arrays of line
# numbers to match against. e.g.
# { "path/to/file.rb" => [37, 42] }
locations = @inclusions.delete(:locations) || Hash.new {|h,k| h[k] = []}
locations[File.expand_path(file_path)].push(*line_numbers)
@inclusions.replace(:locations => locations)
@exclusions.clear
end
def empty?
inclusions.empty? && exclusions.empty_without_conditional_filters?
end
def prune(examples)
examples.select {|e| !exclude?(e) && include?(e)}
end
def exclude(*args)
merge(@exclusions, @inclusions, *args)
end
def exclude!(*args)
replace(@exclusions, @inclusions, *args)
end
def exclude_with_low_priority(*args)
reverse_merge(@exclusions, @inclusions, *args)
end
def exclude?(example)
@exclusions.empty? ? false : example.any_apply?(@exclusions)
end
def include(*args)
unless_standalone(*args) { merge(@inclusions, @exclusions, *args) }
end
def include!(*args)
unless_standalone(*args) { replace(@inclusions, @exclusions, *args) }
end
def include_with_low_priority(*args)
unless_standalone(*args) { reverse_merge(@inclusions, @exclusions, *args) }
end
def include?(example)
@inclusions.empty? ? true : example.any_apply?(@inclusions)
end
private
def unless_standalone(*args)
is_standalone_filter?(args.last) ? @inclusions.replace(args.last) : yield unless already_set_standalone_filter?
end
def merge(orig, opposite, *updates)
orig.merge!(updates.last).each_key {|k| opposite.delete(k)}
end
def replace(orig, opposite, *updates)
updates.last.each_key {|k| opposite.delete(k)}
orig.replace(updates.last)
end
def reverse_merge(orig, opposite, *updates)
updated = updates.last.merge(orig)
opposite.each_pair {|k,v| updated.delete(k) if updated[k] == v}
orig.replace(updated)
end
def already_set_standalone_filter?
is_standalone_filter?(inclusions)
end
def is_standalone_filter?(filter)
STANDALONE_FILTERS.any? {|key| filter.has_key?(key)}
end
end
end
end