Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit 9ffcc38

Browse files
committed
Fix another bug in metadata backwards compatibility.
In RSpec 2.x, an example's `metadata[:example_group]` did not returned it's example group's `metadata`, but rather it's example group's `metadata[:example_group]`, which is where all the computed values for the example group went. In RSpec 3, we've simplified this nesting structure (it's silly to put an example group's computed properties on a nested subhash keyed by `:example_group`), but our backwards compatibility didn't work properly with this. The problem was that `example.metadata[:example_group][:example_group]` returned the `LegacyExampleGroupHash` available off of `example.example_group.metadata[:example_group]`, which contains the computed properties of the example group, but on 2.99 this returned the computed properties of the parent group of the example's group. Unfortunately, I had to make one spec pending as I can't find a way to keep it passing with the difference in how the metadata is exposed from an example vs a group, but it really only matters when people mutate the metadata hash and that's not a normal, supported use case. I think the backwards compatibility is more important right now. We may revert this at some point after 3.0's been out for awhile and people have had time to adjust to the deprecation warnings issued for the metadata changes.
1 parent c0f9b8a commit 9ffcc38

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lib/rspec/core/metadata.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ def ensure_valid_user_keys
149149
class ExampleHash < HashPopulator
150150
def self.create(group_metadata, user_metadata, description, block)
151151
example_metadata = group_metadata.dup
152+
group_metadata = Hash.new(&ExampleGroupHash.backwards_compatibility_default_proc do |hash|
153+
hash[:parent_example_group]
154+
end)
155+
group_metadata.update(example_metadata)
156+
152157
example_metadata[:example_group] = group_metadata
153158
example_metadata.delete(:parent_example_group)
154159

@@ -184,14 +189,18 @@ def self.create(parent_group_metadata, user_metadata, *args, &block)
184189
end
185190

186191
def self.hash_with_backwards_compatibility_default_proc
187-
Hash.new do |hash, key|
192+
Hash.new(&backwards_compatibility_default_proc { |hash| hash })
193+
end
194+
195+
def self.backwards_compatibility_default_proc(&example_group_selector)
196+
Proc.new do |hash, key|
188197
case key
189198
when :example_group
190199
RSpec.deprecate("The `:example_group` key in an example group's metadata hash",
191200
:replacement => "the example group's hash directly for the " +
192201
"computed keys and `:parent_example_group` to access the parent " +
193202
"example group metadata")
194-
LegacyExampleGroupHash.new(hash)
203+
LegacyExampleGroupHash.new(example_group_selector.call(hash))
195204
when :example_group_block
196205
RSpec.deprecate("`metadata[:example_group_block]`",
197206
:replacement => "`metadata[:block]`")

spec/rspec/core/metadata_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ def metadata_for(*args)
117117
end
118118

119119
a[:description] = "new description"
120+
121+
pending "Cannot maintain this and provide full `:example_group` backwards compatibility (see GH #1490):("
120122
expect(b[:description]).to eq("new description")
121123
end
122124

@@ -473,11 +475,12 @@ def value_for(*args)
473475
RSpec.describe "Level 1" do
474476
describe "Level 2" do
475477
describe "Level 3" do
476-
inner_metadata = metadata
478+
inner_metadata = example("Level 4").metadata
477479
end
478480
end
479481
end
480482

483+
expect(inner_metadata[:description]).to eq("Level 4")
481484
expect(inner_metadata[:example_group][:description]).to eq("Level 3")
482485
expect(inner_metadata[:example_group][:example_group][:description]).to eq("Level 2")
483486
expect(inner_metadata[:example_group][:example_group][:example_group][:description]).to eq("Level 1")

0 commit comments

Comments
 (0)