rspec-core 3.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.document +5 -0
  5. data/.yardopts +8 -0
  6. data/Changelog.md +2243 -0
  7. data/LICENSE.md +26 -0
  8. data/README.md +384 -0
  9. data/exe/rspec +4 -0
  10. data/lib/rspec/autorun.rb +3 -0
  11. data/lib/rspec/core.rb +185 -0
  12. data/lib/rspec/core/backtrace_formatter.rb +65 -0
  13. data/lib/rspec/core/bisect/coordinator.rb +62 -0
  14. data/lib/rspec/core/bisect/example_minimizer.rb +173 -0
  15. data/lib/rspec/core/bisect/fork_runner.rb +134 -0
  16. data/lib/rspec/core/bisect/server.rb +61 -0
  17. data/lib/rspec/core/bisect/shell_command.rb +126 -0
  18. data/lib/rspec/core/bisect/shell_runner.rb +73 -0
  19. data/lib/rspec/core/bisect/utilities.rb +58 -0
  20. data/lib/rspec/core/configuration.rb +2308 -0
  21. data/lib/rspec/core/configuration_options.rb +233 -0
  22. data/lib/rspec/core/drb.rb +113 -0
  23. data/lib/rspec/core/dsl.rb +98 -0
  24. data/lib/rspec/core/example.rb +656 -0
  25. data/lib/rspec/core/example_group.rb +889 -0
  26. data/lib/rspec/core/example_status_persister.rb +235 -0
  27. data/lib/rspec/core/filter_manager.rb +231 -0
  28. data/lib/rspec/core/flat_map.rb +20 -0
  29. data/lib/rspec/core/formatters.rb +269 -0
  30. data/lib/rspec/core/formatters/base_bisect_formatter.rb +45 -0
  31. data/lib/rspec/core/formatters/base_formatter.rb +70 -0
  32. data/lib/rspec/core/formatters/base_text_formatter.rb +75 -0
  33. data/lib/rspec/core/formatters/bisect_drb_formatter.rb +29 -0
  34. data/lib/rspec/core/formatters/bisect_progress_formatter.rb +157 -0
  35. data/lib/rspec/core/formatters/console_codes.rb +68 -0
  36. data/lib/rspec/core/formatters/deprecation_formatter.rb +223 -0
  37. data/lib/rspec/core/formatters/documentation_formatter.rb +70 -0
  38. data/lib/rspec/core/formatters/exception_presenter.rb +508 -0
  39. data/lib/rspec/core/formatters/fallback_message_formatter.rb +28 -0
  40. data/lib/rspec/core/formatters/helpers.rb +110 -0
  41. data/lib/rspec/core/formatters/html_formatter.rb +153 -0
  42. data/lib/rspec/core/formatters/html_printer.rb +414 -0
  43. data/lib/rspec/core/formatters/html_snippet_extractor.rb +120 -0
  44. data/lib/rspec/core/formatters/json_formatter.rb +102 -0
  45. data/lib/rspec/core/formatters/profile_formatter.rb +68 -0
  46. data/lib/rspec/core/formatters/progress_formatter.rb +29 -0
  47. data/lib/rspec/core/formatters/protocol.rb +182 -0
  48. data/lib/rspec/core/formatters/snippet_extractor.rb +134 -0
  49. data/lib/rspec/core/formatters/syntax_highlighter.rb +91 -0
  50. data/lib/rspec/core/hooks.rb +624 -0
  51. data/lib/rspec/core/invocations.rb +87 -0
  52. data/lib/rspec/core/memoized_helpers.rb +554 -0
  53. data/lib/rspec/core/metadata.rb +498 -0
  54. data/lib/rspec/core/metadata_filter.rb +255 -0
  55. data/lib/rspec/core/minitest_assertions_adapter.rb +31 -0
  56. data/lib/rspec/core/mocking_adapters/flexmock.rb +31 -0
  57. data/lib/rspec/core/mocking_adapters/mocha.rb +57 -0
  58. data/lib/rspec/core/mocking_adapters/null.rb +14 -0
  59. data/lib/rspec/core/mocking_adapters/rr.rb +31 -0
  60. data/lib/rspec/core/mocking_adapters/rspec.rb +32 -0
  61. data/lib/rspec/core/notifications.rb +521 -0
  62. data/lib/rspec/core/option_parser.rb +309 -0
  63. data/lib/rspec/core/ordering.rb +158 -0
  64. data/lib/rspec/core/output_wrapper.rb +29 -0
  65. data/lib/rspec/core/pending.rb +165 -0
  66. data/lib/rspec/core/profiler.rb +34 -0
  67. data/lib/rspec/core/project_initializer.rb +48 -0
  68. data/lib/rspec/core/project_initializer/.rspec +1 -0
  69. data/lib/rspec/core/project_initializer/spec/spec_helper.rb +100 -0
  70. data/lib/rspec/core/rake_task.rb +168 -0
  71. data/lib/rspec/core/reporter.rb +257 -0
  72. data/lib/rspec/core/ruby_project.rb +53 -0
  73. data/lib/rspec/core/runner.rb +199 -0
  74. data/lib/rspec/core/sandbox.rb +37 -0
  75. data/lib/rspec/core/set.rb +54 -0
  76. data/lib/rspec/core/shared_context.rb +55 -0
  77. data/lib/rspec/core/shared_example_group.rb +269 -0
  78. data/lib/rspec/core/shell_escape.rb +49 -0
  79. data/lib/rspec/core/test_unit_assertions_adapter.rb +30 -0
  80. data/lib/rspec/core/version.rb +9 -0
  81. data/lib/rspec/core/warnings.rb +40 -0
  82. data/lib/rspec/core/world.rb +275 -0
  83. metadata +292 -0
  84. metadata.gz.sig +0 -0
@@ -0,0 +1,134 @@
1
+ module RSpec
2
+ module Core
3
+ module Formatters
4
+ # @private
5
+ class SnippetExtractor
6
+ NoSuchFileError = Class.new(StandardError)
7
+ NoSuchLineError = Class.new(StandardError)
8
+
9
+ def self.extract_line_at(file_path, line_number)
10
+ source = source_from_file(file_path)
11
+ line = source.lines[line_number - 1]
12
+ raise NoSuchLineError unless line
13
+ line
14
+ end
15
+
16
+ def self.source_from_file(path)
17
+ raise NoSuchFileError unless File.exist?(path)
18
+ RSpec.world.source_from_file(path)
19
+ end
20
+
21
+ if RSpec::Support::RubyFeatures.ripper_supported?
22
+ NoExpressionAtLineError = Class.new(StandardError)
23
+
24
+ attr_reader :source, :beginning_line_number, :max_line_count
25
+
26
+ def self.extract_expression_lines_at(file_path, beginning_line_number, max_line_count=nil)
27
+ if max_line_count == 1
28
+ [extract_line_at(file_path, beginning_line_number)]
29
+ else
30
+ source = source_from_file(file_path)
31
+ new(source, beginning_line_number, max_line_count).expression_lines
32
+ end
33
+ end
34
+
35
+ def initialize(source, beginning_line_number, max_line_count=nil)
36
+ @source = source
37
+ @beginning_line_number = beginning_line_number
38
+ @max_line_count = max_line_count
39
+ end
40
+
41
+ def expression_lines
42
+ line_range = line_range_of_expression
43
+
44
+ if max_line_count && line_range.count > max_line_count
45
+ line_range = (line_range.begin)..(line_range.begin + max_line_count - 1)
46
+ end
47
+
48
+ source.lines[(line_range.begin - 1)..(line_range.end - 1)]
49
+ rescue SyntaxError, NoExpressionAtLineError
50
+ [self.class.extract_line_at(source.path, beginning_line_number)]
51
+ end
52
+
53
+ private
54
+
55
+ def line_range_of_expression
56
+ @line_range_of_expression ||= begin
57
+ line_range = line_range_of_location_nodes_in_expression
58
+ initial_unclosed_tokens = unclosed_tokens_in_line_range(line_range)
59
+ unclosed_tokens = initial_unclosed_tokens
60
+
61
+ until (initial_unclosed_tokens & unclosed_tokens).empty?
62
+ line_range = (line_range.begin)..(line_range.end + 1)
63
+ unclosed_tokens = unclosed_tokens_in_line_range(line_range)
64
+ end
65
+
66
+ line_range
67
+ end
68
+ end
69
+
70
+ def unclosed_tokens_in_line_range(line_range)
71
+ tokens = FlatMap.flat_map(line_range) do |line_number|
72
+ source.tokens_by_line_number[line_number]
73
+ end
74
+
75
+ tokens.each_with_object([]) do |token, unclosed_tokens|
76
+ if token.opening?
77
+ unclosed_tokens << token
78
+ else
79
+ index = unclosed_tokens.rindex do |unclosed_token|
80
+ unclosed_token.closed_by?(token)
81
+ end
82
+ unclosed_tokens.delete_at(index) if index
83
+ end
84
+ end
85
+ end
86
+
87
+ def line_range_of_location_nodes_in_expression
88
+ line_numbers = expression_node.each_with_object(Set.new) do |node, set|
89
+ set << node.location.line if node.location
90
+ end
91
+
92
+ line_numbers.min..line_numbers.max
93
+ end
94
+
95
+ def expression_node
96
+ raise NoExpressionAtLineError if location_nodes_at_beginning_line.empty?
97
+
98
+ @expression_node ||= begin
99
+ common_ancestor_nodes = location_nodes_at_beginning_line.map do |node|
100
+ node.each_ancestor.to_a
101
+ end.reduce(:&)
102
+
103
+ common_ancestor_nodes.find { |node| expression_outmost_node?(node) }
104
+ end
105
+ end
106
+
107
+ def expression_outmost_node?(node)
108
+ return true unless node.parent
109
+ return false if node.type.to_s.start_with?('@')
110
+ ![node, node.parent].all? do |n|
111
+ # See `Ripper::PARSER_EVENTS` for the complete list of sexp types.
112
+ type = n.type.to_s
113
+ type.end_with?('call') || type.start_with?('method_add_')
114
+ end
115
+ end
116
+
117
+ def location_nodes_at_beginning_line
118
+ source.nodes_by_line_number[beginning_line_number]
119
+ end
120
+ else
121
+ # :nocov:
122
+ def self.extract_expression_lines_at(file_path, beginning_line_number, *)
123
+ [extract_line_at(file_path, beginning_line_number)]
124
+ end
125
+ # :nocov:
126
+ end
127
+
128
+ def self.least_indentation_from(lines)
129
+ lines.map { |line| line[/^[ \t]*/] }.min
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,91 @@
1
+ module RSpec
2
+ module Core
3
+ module Formatters
4
+ # @private
5
+ # Provides terminal syntax highlighting of code snippets
6
+ # when coderay is available.
7
+ class SyntaxHighlighter
8
+ def initialize(configuration)
9
+ @configuration = configuration
10
+ end
11
+
12
+ def highlight(lines)
13
+ implementation.highlight_syntax(lines)
14
+ end
15
+
16
+ # rubocop:disable Lint/RescueException
17
+ # rubocop:disable Lint/HandleExceptions
18
+ def self.attempt_to_add_rspec_terms_to_coderay_keywords
19
+ CodeRay::Scanners::Ruby::Patterns::IDENT_KIND.add(%w[
20
+ describe context
21
+ it specify
22
+ before after around
23
+ let subject
24
+ expect allow
25
+ ], :keyword)
26
+ rescue Exception
27
+ # Mutating CodeRay's contants like this is not a public API
28
+ # and might not always work. If we cannot add our keywords
29
+ # to CodeRay it is not a big deal and not worth raising an
30
+ # error over, so we ignore it.
31
+ end
32
+ # rubocop:enable Lint/HandleExceptions
33
+ # rubocop:enable Lint/RescueException
34
+
35
+ private
36
+
37
+ if RSpec::Support::OS.windows?
38
+ # :nocov:
39
+ def implementation
40
+ WindowsImplementation
41
+ end
42
+ # :nocov:
43
+ else
44
+ def implementation
45
+ return color_enabled_implementation if @configuration.color_enabled?
46
+ NoSyntaxHighlightingImplementation
47
+ end
48
+ end
49
+
50
+ def color_enabled_implementation
51
+ @color_enabled_implementation ||= begin
52
+ require 'coderay'
53
+ self.class.attempt_to_add_rspec_terms_to_coderay_keywords
54
+ CodeRayImplementation
55
+ rescue LoadError
56
+ NoSyntaxHighlightingImplementation
57
+ end
58
+ end
59
+
60
+ # @private
61
+ module CodeRayImplementation
62
+ RESET_CODE = "\e[0m"
63
+
64
+ def self.highlight_syntax(lines)
65
+ highlighted = begin
66
+ CodeRay.encode(lines.join("\n"), :ruby, :terminal)
67
+ rescue Support::AllExceptionsExceptOnesWeMustNotRescue
68
+ return lines
69
+ end
70
+
71
+ highlighted.split("\n").map do |line|
72
+ line.sub(/\S/) { |char| char.insert(0, RESET_CODE) }
73
+ end
74
+ end
75
+ end
76
+
77
+ # @private
78
+ module NoSyntaxHighlightingImplementation
79
+ def self.highlight_syntax(lines)
80
+ lines
81
+ end
82
+ end
83
+
84
+ # @private
85
+ # Not sure why, but our code above (and/or coderay itself) does not work
86
+ # on Windows, so we disable the feature on Windows.
87
+ WindowsImplementation = NoSyntaxHighlightingImplementation
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,624 @@
1
+ module RSpec
2
+ module Core
3
+ # Provides `before`, `after` and `around` hooks as a means of
4
+ # supporting common setup and teardown. This module is extended
5
+ # onto {ExampleGroup}, making the methods available from any `describe`
6
+ # or `context` block and included in {Configuration}, making them
7
+ # available off of the configuration object to define global setup
8
+ # or teardown logic.
9
+ module Hooks
10
+ # @api public
11
+ #
12
+ # @overload before(&block)
13
+ # @overload before(scope, &block)
14
+ # @param scope [Symbol] `:example`, `:context`, or `:suite`
15
+ # (defaults to `:example`)
16
+ # @overload before(scope, conditions, &block)
17
+ # @param scope [Symbol] `:example`, `:context`, or `:suite`
18
+ # (defaults to `:example`)
19
+ # @param conditions [Hash]
20
+ # constrains this hook to examples matching these conditions e.g.
21
+ # `before(:example, :ui => true) { ... }` will only run with examples
22
+ # or groups declared with `:ui => true`.
23
+ # @overload before(conditions, &block)
24
+ # @param conditions [Hash]
25
+ # constrains this hook to examples matching these conditions e.g.
26
+ # `before(:example, :ui => true) { ... }` will only run with examples
27
+ # or groups declared with `:ui => true`.
28
+ #
29
+ # @see #after
30
+ # @see #around
31
+ # @see ExampleGroup
32
+ # @see SharedContext
33
+ # @see SharedExampleGroup
34
+ # @see Configuration
35
+ #
36
+ # Declare a block of code to be run before each example (using `:example`)
37
+ # or once before any example (using `:context`). These are usually
38
+ # declared directly in the {ExampleGroup} to which they apply, but they
39
+ # can also be shared across multiple groups.
40
+ #
41
+ # You can also use `before(:suite)` to run a block of code before any
42
+ # example groups are run. This should be declared in {RSpec.configure}.
43
+ #
44
+ # Instance variables declared in `before(:example)` or `before(:context)`
45
+ # are accessible within each example.
46
+ #
47
+ # ### Order
48
+ #
49
+ # `before` hooks are stored in three scopes, which are run in order:
50
+ # `:suite`, `:context`, and `:example`. They can also be declared in
51
+ # several different places: `RSpec.configure`, a parent group, the current
52
+ # group. They are run in the following order:
53
+ #
54
+ # before(:suite) # Declared in RSpec.configure.
55
+ # before(:context) # Declared in RSpec.configure.
56
+ # before(:context) # Declared in a parent group.
57
+ # before(:context) # Declared in the current group.
58
+ # before(:example) # Declared in RSpec.configure.
59
+ # before(:example) # Declared in a parent group.
60
+ # before(:example) # Declared in the current group.
61
+ #
62
+ # If more than one `before` is declared within any one scope, they are run
63
+ # in the order in which they are declared.
64
+ #
65
+ # ### Conditions
66
+ #
67
+ # When you add a conditions hash to `before(:example)` or
68
+ # `before(:context)`, RSpec will only apply that hook to groups or
69
+ # examples that match the conditions. e.g.
70
+ #
71
+ # RSpec.configure do |config|
72
+ # config.before(:example, :authorized => true) do
73
+ # log_in_as :authorized_user
74
+ # end
75
+ # end
76
+ #
77
+ # describe Something, :authorized => true do
78
+ # # The before hook will run in before each example in this group.
79
+ # end
80
+ #
81
+ # describe SomethingElse do
82
+ # it "does something", :authorized => true do
83
+ # # The before hook will run before this example.
84
+ # end
85
+ #
86
+ # it "does something else" do
87
+ # # The hook will not run before this example.
88
+ # end
89
+ # end
90
+ #
91
+ # Note that filtered config `:context` hooks can still be applied
92
+ # to individual examples that have matching metadata. Just like
93
+ # Ruby's object model is that every object has a singleton class
94
+ # which has only a single instance, RSpec's model is that every
95
+ # example has a singleton example group containing just the one
96
+ # example.
97
+ #
98
+ # ### Warning: `before(:suite, :with => :conditions)`
99
+ #
100
+ # The conditions hash is used to match against specific examples. Since
101
+ # `before(:suite)` is not run in relation to any specific example or
102
+ # group, conditions passed along with `:suite` are effectively ignored.
103
+ #
104
+ # ### Exceptions
105
+ #
106
+ # When an exception is raised in a `before` block, RSpec skips any
107
+ # subsequent `before` blocks and the example, but runs all of the
108
+ # `after(:example)` and `after(:context)` hooks.
109
+ #
110
+ # ### Warning: implicit before blocks
111
+ #
112
+ # `before` hooks can also be declared in shared contexts which get
113
+ # included implicitly either by you or by extension libraries. Since
114
+ # RSpec runs these in the order in which they are declared within each
115
+ # scope, load order matters, and can lead to confusing results when one
116
+ # before block depends on state that is prepared in another before block
117
+ # that gets run later.
118
+ #
119
+ # ### Warning: `before(:context)`
120
+ #
121
+ # It is very tempting to use `before(:context)` to speed things up, but we
122
+ # recommend that you avoid this as there are a number of gotchas, as well
123
+ # as things that simply don't work.
124
+ #
125
+ # #### Context
126
+ #
127
+ # `before(:context)` is run in an example that is generated to provide
128
+ # group context for the block.
129
+ #
130
+ # #### Instance variables
131
+ #
132
+ # Instance variables declared in `before(:context)` are shared across all
133
+ # the examples in the group. This means that each example can change the
134
+ # state of a shared object, resulting in an ordering dependency that can
135
+ # make it difficult to reason about failures.
136
+ #
137
+ # #### Unsupported RSpec constructs
138
+ #
139
+ # RSpec has several constructs that reset state between each example
140
+ # automatically. These are not intended for use from within
141
+ # `before(:context)`:
142
+ #
143
+ # * `let` declarations
144
+ # * `subject` declarations
145
+ # * Any mocking, stubbing or test double declaration
146
+ #
147
+ # ### other frameworks
148
+ #
149
+ # Mock object frameworks and database transaction managers (like
150
+ # ActiveRecord) are typically designed around the idea of setting up
151
+ # before an example, running that one example, and then tearing down. This
152
+ # means that mocks and stubs can (sometimes) be declared in
153
+ # `before(:context)`, but get torn down before the first real example is
154
+ # ever run.
155
+ #
156
+ # You _can_ create database-backed model objects in a `before(:context)`
157
+ # in rspec-rails, but it will not be wrapped in a transaction for you, so
158
+ # you are on your own to clean up in an `after(:context)` block.
159
+ #
160
+ # @example before(:example) declared in an {ExampleGroup}
161
+ #
162
+ # describe Thing do
163
+ # before(:example) do
164
+ # @thing = Thing.new
165
+ # end
166
+ #
167
+ # it "does something" do
168
+ # # Here you can access @thing.
169
+ # end
170
+ # end
171
+ #
172
+ # @example before(:context) declared in an {ExampleGroup}
173
+ #
174
+ # describe Parser do
175
+ # before(:context) do
176
+ # File.open(file_to_parse, 'w') do |f|
177
+ # f.write <<-CONTENT
178
+ # stuff in the file
179
+ # CONTENT
180
+ # end
181
+ # end
182
+ #
183
+ # it "parses the file" do
184
+ # Parser.parse(file_to_parse)
185
+ # end
186
+ #
187
+ # after(:context) do
188
+ # File.delete(file_to_parse)
189
+ # end
190
+ # end
191
+ #
192
+ # @note The `:example` and `:context` scopes are also available as
193
+ # `:each` and `:all`, respectively. Use whichever you prefer.
194
+ # @note The `:suite` scope is only supported for hooks registered on
195
+ # `RSpec.configuration` since they exist independently of any
196
+ # example or example group.
197
+ def before(*args, &block)
198
+ hooks.register :append, :before, *args, &block
199
+ end
200
+
201
+ alias_method :append_before, :before
202
+
203
+ # Adds `block` to the front of the list of `before` blocks in the same
204
+ # scope (`:example`, `:context`, or `:suite`).
205
+ #
206
+ # See {#before} for scoping semantics.
207
+ def prepend_before(*args, &block)
208
+ hooks.register :prepend, :before, *args, &block
209
+ end
210
+
211
+ # @api public
212
+ # @overload after(&block)
213
+ # @overload after(scope, &block)
214
+ # @param scope [Symbol] `:example`, `:context`, or `:suite` (defaults to
215
+ # `:example`)
216
+ # @overload after(scope, conditions, &block)
217
+ # @param scope [Symbol] `:example`, `:context`, or `:suite` (defaults to
218
+ # `:example`)
219
+ # @param conditions [Hash]
220
+ # constrains this hook to examples matching these conditions e.g.
221
+ # `after(:example, :ui => true) { ... }` will only run with examples
222
+ # or groups declared with `:ui => true`.
223
+ # @overload after(conditions, &block)
224
+ # @param conditions [Hash]
225
+ # constrains this hook to examples matching these conditions e.g.
226
+ # `after(:example, :ui => true) { ... }` will only run with examples
227
+ # or groups declared with `:ui => true`.
228
+ #
229
+ # @see #before
230
+ # @see #around
231
+ # @see ExampleGroup
232
+ # @see SharedContext
233
+ # @see SharedExampleGroup
234
+ # @see Configuration
235
+ #
236
+ # Declare a block of code to be run after each example (using `:example`)
237
+ # or once after all examples n the context (using `:context`). See
238
+ # {#before} for more information about ordering.
239
+ #
240
+ # ### Exceptions
241
+ #
242
+ # `after` hooks are guaranteed to run even when there are exceptions in
243
+ # `before` hooks or examples. When an exception is raised in an after
244
+ # block, the exception is captured for later reporting, and subsequent
245
+ # `after` blocks are run.
246
+ #
247
+ # ### Order
248
+ #
249
+ # `after` hooks are stored in three scopes, which are run in order:
250
+ # `:example`, `:context`, and `:suite`. They can also be declared in
251
+ # several different places: `RSpec.configure`, a parent group, the current
252
+ # group. They are run in the following order:
253
+ #
254
+ # after(:example) # Declared in the current group.
255
+ # after(:example) # Declared in a parent group.
256
+ # after(:example) # Declared in RSpec.configure.
257
+ # after(:context) # Declared in the current group.
258
+ # after(:context) # Declared in a parent group.
259
+ # after(:context) # Declared in RSpec.configure.
260
+ # after(:suite) # Declared in RSpec.configure.
261
+ #
262
+ # This is the reverse of the order in which `before` hooks are run.
263
+ # Similarly, if more than one `after` is declared within any one scope,
264
+ # they are run in reverse order of that in which they are declared.
265
+ #
266
+ # @note The `:example` and `:context` scopes are also available as
267
+ # `:each` and `:all`, respectively. Use whichever you prefer.
268
+ # @note The `:suite` scope is only supported for hooks registered on
269
+ # `RSpec.configuration` since they exist independently of any
270
+ # example or example group.
271
+ def after(*args, &block)
272
+ hooks.register :prepend, :after, *args, &block
273
+ end
274
+
275
+ alias_method :prepend_after, :after
276
+
277
+ # Adds `block` to the back of the list of `after` blocks in the same
278
+ # scope (`:example`, `:context`, or `:suite`).
279
+ #
280
+ # See {#after} for scoping semantics.
281
+ def append_after(*args, &block)
282
+ hooks.register :append, :after, *args, &block
283
+ end
284
+
285
+ # @api public
286
+ # @overload around(&block)
287
+ # @overload around(scope, &block)
288
+ # @param scope [Symbol] `:example` (defaults to `:example`)
289
+ # present for syntax parity with `before` and `after`, but
290
+ # `:example`/`:each` is the only supported value.
291
+ # @overload around(scope, conditions, &block)
292
+ # @param scope [Symbol] `:example` (defaults to `:example`)
293
+ # present for syntax parity with `before` and `after`, but
294
+ # `:example`/`:each` is the only supported value.
295
+ # @param conditions [Hash] constrains this hook to examples matching
296
+ # these conditions e.g. `around(:example, :ui => true) { ... }` will
297
+ # only run with examples or groups declared with `:ui => true`.
298
+ # @overload around(conditions, &block)
299
+ # @param conditions [Hash] constrains this hook to examples matching
300
+ # these conditions e.g. `around(:example, :ui => true) { ... }` will
301
+ # only run with examples or groups declared with `:ui => true`.
302
+ #
303
+ # @yield [Example] the example to run
304
+ #
305
+ # @note the syntax of `around` is similar to that of `before` and `after`
306
+ # but the semantics are quite different. `before` and `after` hooks are
307
+ # run in the context of of the examples with which they are associated,
308
+ # whereas `around` hooks are actually responsible for running the
309
+ # examples. Consequently, `around` hooks do not have direct access to
310
+ # resources that are made available within the examples and their
311
+ # associated `before` and `after` hooks.
312
+ #
313
+ # @note `:example`/`:each` is the only supported scope.
314
+ #
315
+ # Declare a block of code, parts of which will be run before and parts
316
+ # after the example. It is your responsibility to run the example:
317
+ #
318
+ # around(:example) do |ex|
319
+ # # Do some stuff before.
320
+ # ex.run
321
+ # # Do some stuff after.
322
+ # end
323
+ #
324
+ # The yielded example aliases `run` with `call`, which lets you treat it
325
+ # like a `Proc`. This is especially handy when working with libraries
326
+ # that manage their own setup and teardown using a block or proc syntax,
327
+ # e.g.
328
+ #
329
+ # around(:example) {|ex| Database.transaction(&ex)}
330
+ # around(:example) {|ex| FakeFS(&ex)}
331
+ #
332
+ def around(*args, &block)
333
+ hooks.register :prepend, :around, *args, &block
334
+ end
335
+
336
+ # @private
337
+ # Holds the various registered hooks.
338
+ def hooks
339
+ @hooks ||= HookCollections.new(self, FilterableItemRepository::UpdateOptimized)
340
+ end
341
+
342
+ # @private
343
+ Hook = Struct.new(:block, :options)
344
+
345
+ # @private
346
+ class BeforeHook < Hook
347
+ def run(example)
348
+ example.instance_exec(example, &block)
349
+ end
350
+ end
351
+
352
+ # @private
353
+ class AfterHook < Hook
354
+ def run(example)
355
+ example.instance_exec(example, &block)
356
+ rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
357
+ example.set_exception(ex)
358
+ end
359
+ end
360
+
361
+ # @private
362
+ class AfterContextHook < Hook
363
+ def run(example)
364
+ example.instance_exec(example, &block)
365
+ rescue Support::AllExceptionsExceptOnesWeMustNotRescue => e
366
+ RSpec.configuration.reporter.notify_non_example_exception(e, "An error occurred in an `after(:context)` hook.")
367
+ end
368
+ end
369
+
370
+ # @private
371
+ class AroundHook < Hook
372
+ def execute_with(example, procsy)
373
+ example.instance_exec(procsy, &block)
374
+ return if procsy.executed?
375
+ Pending.mark_skipped!(example,
376
+ "#{hook_description} did not execute the example")
377
+ end
378
+
379
+ if Proc.method_defined?(:source_location)
380
+ def hook_description
381
+ "around hook at #{Metadata.relative_path(block.source_location.join(':'))}"
382
+ end
383
+ else # for 1.8.7
384
+ # :nocov:
385
+ def hook_description
386
+ "around hook"
387
+ end
388
+ # :nocov:
389
+ end
390
+ end
391
+
392
+ # @private
393
+ #
394
+ # This provides the primary API used by other parts of rspec-core. By hiding all
395
+ # implementation details behind this facade, it's allowed us to heavily optimize
396
+ # this, so that, for example, hook collection objects are only instantiated when
397
+ # a hook is added. This allows us to avoid many object allocations for the common
398
+ # case of a group having no hooks.
399
+ #
400
+ # This is only possible because this interface provides a "tell, don't ask"-style
401
+ # API, so that callers _tell_ this class what to do with the hooks, rather than
402
+ # asking this class for a list of hooks, and then doing something with them.
403
+ class HookCollections
404
+ def initialize(owner, filterable_item_repo_class)
405
+ @owner = owner
406
+ @filterable_item_repo_class = filterable_item_repo_class
407
+ @before_example_hooks = nil
408
+ @after_example_hooks = nil
409
+ @before_context_hooks = nil
410
+ @after_context_hooks = nil
411
+ @around_example_hooks = nil
412
+ end
413
+
414
+ def register_globals(host, globals)
415
+ parent_groups = host.parent_groups
416
+
417
+ process(host, parent_groups, globals, :before, :example, &:options)
418
+ process(host, parent_groups, globals, :after, :example, &:options)
419
+ process(host, parent_groups, globals, :around, :example, &:options)
420
+
421
+ process(host, parent_groups, globals, :before, :context, &:options)
422
+ process(host, parent_groups, globals, :after, :context, &:options)
423
+ end
424
+
425
+ def register_global_singleton_context_hooks(example, globals)
426
+ parent_groups = example.example_group.parent_groups
427
+
428
+ process(example, parent_groups, globals, :before, :context) { {} }
429
+ process(example, parent_groups, globals, :after, :context) { {} }
430
+ end
431
+
432
+ def register(prepend_or_append, position, *args, &block)
433
+ scope, options = scope_and_options_from(*args)
434
+
435
+ if scope == :suite
436
+ # TODO: consider making this an error in RSpec 4. For SemVer reasons,
437
+ # we are only warning in RSpec 3.
438
+ RSpec.warn_with "WARNING: `#{position}(:suite)` hooks are only supported on " \
439
+ "the RSpec configuration object. This " \
440
+ "`#{position}(:suite)` hook, registered on an example " \
441
+ "group, will be ignored."
442
+ return
443
+ end
444
+
445
+ hook = HOOK_TYPES[position][scope].new(block, options)
446
+ ensure_hooks_initialized_for(position, scope).__send__(prepend_or_append, hook, options)
447
+ end
448
+
449
+ # @private
450
+ #
451
+ # Runs all of the blocks stored with the hook in the context of the
452
+ # example. If no example is provided, just calls the hook directly.
453
+ def run(position, scope, example_or_group)
454
+ return if RSpec.configuration.dry_run?
455
+
456
+ if scope == :context
457
+ unless example_or_group.class.metadata[:skip]
458
+ run_owned_hooks_for(position, :context, example_or_group)
459
+ end
460
+ else
461
+ case position
462
+ when :before then run_example_hooks_for(example_or_group, :before, :reverse_each)
463
+ when :after then run_example_hooks_for(example_or_group, :after, :each)
464
+ when :around then run_around_example_hooks_for(example_or_group) { yield }
465
+ end
466
+ end
467
+ end
468
+
469
+ SCOPES = [:example, :context]
470
+
471
+ SCOPE_ALIASES = { :each => :example, :all => :context }
472
+
473
+ HOOK_TYPES = {
474
+ :before => Hash.new { BeforeHook },
475
+ :after => Hash.new { AfterHook },
476
+ :around => Hash.new { AroundHook }
477
+ }
478
+
479
+ HOOK_TYPES[:after][:context] = AfterContextHook
480
+
481
+ protected
482
+
483
+ EMPTY_HOOK_ARRAY = [].freeze
484
+
485
+ def matching_hooks_for(position, scope, example_or_group)
486
+ repository = hooks_for(position, scope) { return EMPTY_HOOK_ARRAY }
487
+
488
+ # It would be nice to not have to switch on type here, but
489
+ # we don't want to define `ExampleGroup#metadata` because then
490
+ # `metadata` from within an individual example would return the
491
+ # group's metadata but the user would probably expect it to be
492
+ # the example's metadata.
493
+ metadata = case example_or_group
494
+ when ExampleGroup then example_or_group.class.metadata
495
+ else example_or_group.metadata
496
+ end
497
+
498
+ repository.items_for(metadata)
499
+ end
500
+
501
+ def all_hooks_for(position, scope)
502
+ hooks_for(position, scope) { return EMPTY_HOOK_ARRAY }.items_and_filters.map(&:first)
503
+ end
504
+
505
+ def run_owned_hooks_for(position, scope, example_or_group)
506
+ matching_hooks_for(position, scope, example_or_group).each do |hook|
507
+ hook.run(example_or_group)
508
+ end
509
+ end
510
+
511
+ def processable_hooks_for(position, scope, host)
512
+ if scope == :example
513
+ all_hooks_for(position, scope)
514
+ else
515
+ matching_hooks_for(position, scope, host)
516
+ end
517
+ end
518
+
519
+ private
520
+
521
+ def hooks_for(position, scope)
522
+ if position == :before
523
+ scope == :example ? @before_example_hooks : @before_context_hooks
524
+ elsif position == :after
525
+ scope == :example ? @after_example_hooks : @after_context_hooks
526
+ else # around
527
+ @around_example_hooks
528
+ end || yield
529
+ end
530
+
531
+ def ensure_hooks_initialized_for(position, scope)
532
+ if position == :before
533
+ if scope == :example
534
+ @before_example_hooks ||= @filterable_item_repo_class.new(:all?)
535
+ else
536
+ @before_context_hooks ||= @filterable_item_repo_class.new(:all?)
537
+ end
538
+ elsif position == :after
539
+ if scope == :example
540
+ @after_example_hooks ||= @filterable_item_repo_class.new(:all?)
541
+ else
542
+ @after_context_hooks ||= @filterable_item_repo_class.new(:all?)
543
+ end
544
+ else # around
545
+ @around_example_hooks ||= @filterable_item_repo_class.new(:all?)
546
+ end
547
+ end
548
+
549
+ def process(host, parent_groups, globals, position, scope)
550
+ hooks_to_process = globals.processable_hooks_for(position, scope, host)
551
+ return if hooks_to_process.empty?
552
+
553
+ hooks_to_process -= FlatMap.flat_map(parent_groups) do |group|
554
+ group.hooks.all_hooks_for(position, scope)
555
+ end
556
+ return if hooks_to_process.empty?
557
+
558
+ repository = ensure_hooks_initialized_for(position, scope)
559
+ hooks_to_process.each { |hook| repository.append hook, (yield hook) }
560
+ end
561
+
562
+ def scope_and_options_from(*args)
563
+ return :suite if args.first == :suite
564
+ scope = extract_scope_from(args)
565
+ meta = Metadata.build_hash_from(args, :warn_about_example_group_filtering)
566
+ return scope, meta
567
+ end
568
+
569
+ def extract_scope_from(args)
570
+ if known_scope?(args.first)
571
+ normalized_scope_for(args.shift)
572
+ elsif args.any? { |a| a.is_a?(Symbol) }
573
+ error_message = "You must explicitly give a scope " \
574
+ "(#{SCOPES.join(", ")}) or scope alias " \
575
+ "(#{SCOPE_ALIASES.keys.join(", ")}) when using symbols as " \
576
+ "metadata for a hook."
577
+ raise ArgumentError.new error_message
578
+ else
579
+ :example
580
+ end
581
+ end
582
+
583
+ def known_scope?(scope)
584
+ SCOPES.include?(scope) || SCOPE_ALIASES.keys.include?(scope)
585
+ end
586
+
587
+ def normalized_scope_for(scope)
588
+ SCOPE_ALIASES[scope] || scope
589
+ end
590
+
591
+ def run_example_hooks_for(example, position, each_method)
592
+ owner_parent_groups.__send__(each_method) do |group|
593
+ group.hooks.run_owned_hooks_for(position, :example, example)
594
+ end
595
+ end
596
+
597
+ def run_around_example_hooks_for(example)
598
+ hooks = FlatMap.flat_map(owner_parent_groups) do |group|
599
+ group.hooks.matching_hooks_for(:around, :example, example)
600
+ end
601
+
602
+ return yield if hooks.empty? # exit early to avoid the extra allocation cost of `Example::Procsy`
603
+
604
+ initial_procsy = Example::Procsy.new(example) { yield }
605
+ hooks.inject(initial_procsy) do |procsy, around_hook|
606
+ procsy.wrap { around_hook.execute_with(example, procsy) }
607
+ end.call
608
+ end
609
+
610
+ if respond_to?(:singleton_class) && singleton_class.ancestors.include?(singleton_class)
611
+ def owner_parent_groups
612
+ @owner.parent_groups
613
+ end
614
+ else # Ruby < 2.1 (see https://bugs.ruby-lang.org/issues/8035)
615
+ # :nocov:
616
+ def owner_parent_groups
617
+ @owner_parent_groups ||= [@owner] + @owner.parent_groups
618
+ end
619
+ # :nocov:
620
+ end
621
+ end
622
+ end
623
+ end
624
+ end