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

Commit b691a41

Browse files
committed
Merge pull request #1490 from rspec/metadata-regression
Fix a regression in our metadata backwards compatibility
2 parents a1518a3 + 9ffcc38 commit b691a41

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

lib/rspec/core/metadata.rb

Lines changed: 12 additions & 3 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]`")
@@ -370,7 +379,7 @@ class LegacyExampleGroupHash
370379

371380
def initialize(metadata)
372381
@metadata = metadata
373-
self[:example_group] = metadata[:parent_example_group]
382+
self[:example_group] = metadata[:parent_example_group][:example_group]
374383
end
375384

376385
def to_h

spec/rspec/core/metadata_spec.rb

Lines changed: 20 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

@@ -460,13 +462,30 @@ def value_for(*args)
460462
describe("nested") { child = metadata }
461463
end
462464

463-
expect(child[:example_group][:example_group]).to include(
465+
expect(child[:example_group][:example_group].to_h).to include(
464466
:foo => 3,
465467
:description => "Object group",
466468
:line_number => parent_line
467469
)
468470
end
469471

472+
it "works properly with deep nesting" do
473+
inner_metadata = nil
474+
475+
RSpec.describe "Level 1" do
476+
describe "Level 2" do
477+
describe "Level 3" do
478+
inner_metadata = example("Level 4").metadata
479+
end
480+
end
481+
end
482+
483+
expect(inner_metadata[:description]).to eq("Level 4")
484+
expect(inner_metadata[:example_group][:description]).to eq("Level 3")
485+
expect(inner_metadata[:example_group][:example_group][:description]).to eq("Level 2")
486+
expect(inner_metadata[:example_group][:example_group][:example_group][:description]).to eq("Level 1")
487+
end
488+
470489
it 'can mutate attributes when accessing them via [:example_group]' do
471490
meta = nil
472491

0 commit comments

Comments
 (0)