Skip to content

Commit 6e6f1a3

Browse files
Fix 1595: make sure to extend the world with a valid module (#1603)
* Add a reproducible example for #1595 * Make sure module is valid before extending the world with it * Update CHANGELOG.md * Simplify world.feature * Extend the world only if modules.any?
1 parent af21b30 commit 6e6f1a3

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Please visit [cucumber/CONTRIBUTING.md](https://github.com/cucumber/cucumber/blo
1616

1717
### Fixed
1818

19+
- Fix usage of namespaced modules across multiple scenarios
20+
([PR#1603](https://github.com/cucumber/cucumber-ruby/pull/1603)
21+
[Issue#1595](https://github.com/cucumber/cucumber-ruby/issues/1595))
22+
1923
- Do not serialize Messages::Hook#tag_expression if it is empty.
2024
([PR#1579](https://github.com/cucumber/cucumber-ruby/pull/1579))
2125

features/docs/writing_support_code/world.feature

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,43 @@ Feature: World
9595
When I run `cucumber features/f.feature`
9696
Then it should pass
9797

98+
Scenario: A world is existing over multiple scenarios
99+
Given a file named "features/support/helpers.rb" with:
100+
"""
101+
module Helpers
102+
def helper_method
103+
42
104+
end
105+
end
106+
107+
World(my_namespace: Helpers)
108+
"""
109+
And a file named "features/step_definitions/step.rb" with:
110+
"""
111+
Then /^my_namespace is not nil$/ do
112+
expect(my_namespace).not_to be_nil
113+
end
114+
115+
Then /^the helper method is called$/ do
116+
expect(my_namespace.helper_method).to eql(42)
117+
end
118+
"""
119+
And a file named "features/f.feature" with:
120+
"""
121+
Feature: Calling a method
122+
Scenario: The namespace exists
123+
Then my_namespace is not nil
124+
And the helper method is called
125+
Scenario: The namespace still exists
126+
Then my_namespace is not nil
127+
And the helper method is called
128+
"""
129+
When I run `cucumber features/f.feature`
130+
Then it should pass with:
131+
"""
132+
2 scenarios (2 passed)
133+
"""
134+
98135
Scenario: A world is extended using multiple modules with different namespaces
99136
Given a file named "features/support/helpers.rb" with:
100137
"""

lib/cucumber/glue/proto_world.rb

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def self.extended(object)
138138
# TODO: pass these in when building the module, instead of mutating them later
139139
# Extend the World with user-defined modules
140140
def add_modules!(world_modules, namespaced_world_modules)
141-
add_world_modules!(world_modules)
142-
add_namespaced_modules!(namespaced_world_modules)
141+
add_world_modules!(world_modules) if world_modules.any?
142+
add_namespaced_modules!(namespaced_world_modules) if namespaced_world_modules.any?
143143
end
144144

145145
define_method(:step) do |name, raw_multiline_arg = nil|
@@ -185,14 +185,13 @@ def add_namespaced_modules!(modules)
185185
modules.each do |namespace, world_modules|
186186
world_modules.each do |world_module|
187187
variable_name = "@__#{namespace}_world"
188+
inner_world = instance_variable_get(variable_name) || Object.new
189+
190+
instance_variable_set(
191+
variable_name,
192+
inner_world.extend(world_module)
193+
)
188194

189-
inner_world = if self.class.respond_to?(namespace)
190-
instance_variable_get(variable_name)
191-
else
192-
Object.new
193-
end
194-
instance_variable_set(variable_name,
195-
inner_world.extend(world_module))
196195
self.class.send(:define_method, namespace) do
197196
instance_variable_get(variable_name)
198197
end
@@ -202,6 +201,8 @@ def add_namespaced_modules!(modules)
202201

203202
# @private
204203
def stringify_namespaced_modules
204+
return '' if @__namespaced_modules.nil?
205+
205206
@__namespaced_modules.map { |k, v| "#{v.join(',')} (as #{k})" }.join('+')
206207
end
207208
end

0 commit comments

Comments
 (0)