Skip to content

Commit

Permalink
[core] Merge pull request rspec/rspec-core#1081 from rspec/deprecatio…
Browse files Browse the repository at this point in the history
…n_warning_let_subject_before_all

Add a deprecation warning to 2.99 for accessing let/subject from `after(:all)`

---
This commit was imported from rspec/rspec-core@a807624.
  • Loading branch information
JonRowe committed Sep 23, 2013
2 parents 2ee639b + acb8f5c commit b149cb1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
2 changes: 2 additions & 0 deletions rspec-core/Changelog.md
Expand Up @@ -23,6 +23,8 @@ Deprecations
debugger gem) instead (Myron Marston).
* Deprecate `RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values = false`.
RSpec 3 will not support having this option set to `false` (Myron Marston).
* Deprecate accessing a `let` or `subject` declaration in
a `after(:all)` hook. (Myron Marston, Jon Rowe)

### 2.14.5 / 2013-08-13
[full changelog](http://github.com/rspec/rspec-core/compare/v2.14.4...v2.14.5)
Expand Down
6 changes: 4 additions & 2 deletions rspec-core/lib/rspec/core/example_group.rb
Expand Up @@ -380,7 +380,7 @@ def self.run_before_all_hooks(example_group_instance)
begin
assign_before_all_ivars(superclass.before_all_ivars, example_group_instance)

BeforeAllMemoizedHash.isolate_for_before_all(example_group_instance) do
AllHookMemoizedHash::Before.isolate_for_all_hook(example_group_instance) do
run_hook(:before, :all, example_group_instance)
end
ensure
Expand Down Expand Up @@ -408,7 +408,9 @@ def self.run_after_all_hooks(example_group_instance)
return if descendant_filtered_examples.empty?
assign_before_all_ivars(before_all_ivars, example_group_instance)

run_hook(:after, :all, example_group_instance)
AllHookMemoizedHash::After.isolate_for_all_hook(example_group_instance) do
run_hook(:after, :all, example_group_instance)
end
end

# Runs all the examples in this group
Expand Down
57 changes: 43 additions & 14 deletions rspec-core/lib/rspec/core/memoized_helpers.rb
Expand Up @@ -93,15 +93,12 @@ def __memoized
# memoized hash when used in a `before(:all)` hook.
#
# @private
class BeforeAllMemoizedHash
def initialize(example_group_instance)
@example_group_instance = example_group_instance
@hash = {}
end
class AllHookMemoizedHash
def self.isolate_for_all_hook(example_group_instance)
hash_type = self

def self.isolate_for_before_all(example_group_instance)
example_group_instance.instance_eval do
@__memoized = BeforeAllMemoizedHash.new(self)
@__memoized = hash_type.new(example_group_instance)

begin
yield
Expand All @@ -112,6 +109,11 @@ def self.isolate_for_before_all(example_group_instance)
end
end

def initialize(example_group_instance)
@example_group_instance = example_group_instance
@hash = {}
end

def fetch(key, &block)
description = if key == :subject
"subject"
Expand All @@ -120,17 +122,16 @@ def fetch(key, &block)
end

::RSpec.warn_deprecation <<-EOS
WARNING: #{description} accessed in a `before(:all)` hook at:
DEPRECATION: #{description} accessed in #{article} #{hook_expression} hook at:
#{CallerFilter.first_non_rspec_line}
This is deprecated behavior that will not be supported in RSpec 3.
`let` and `subject` declarations are not intended to be called
in a `before(:all)` hook, as they exist to define state that
is reset between each example, while `before(:all)` exists to
define state that is shared across examples in an example group.
EOS
in #{article} #{hook_expression} hook, as they exist to define state that
is reset between each example, while #{hook_expression} exists to
#{hook_intention}.
This is deprecated behavior that will not be supported in RSpec 3.
EOS
@hash.fetch(key, &block)
end

Expand All @@ -148,6 +149,34 @@ def preserve_accessed_lets
end
end
end

class Before < self
def hook_expression
"`before(:all)`"
end

def article
"a"
end

def hook_intention
"define state that is shared across examples in an example group"
end
end

class After < self
def hook_expression
"`after(:all)`"
end

def article
"an"
end

def hook_intention
"cleanup state that is shared across examples in an example group"
end
end
end

def self.included(mod)
Expand Down

0 comments on commit b149cb1

Please sign in to comment.