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

Commit 157e20f

Browse files
committed
Fail fast when asked to config.include something that is not a module.
Previously, if you did `config.include :not_a_module, :some_meta`, and defined an example with `:some_meta` metadata, the ruby type error would be raised when the example was being prepared to run, and would lead to other errors due to RSpec’s example setup and teardown not completing. It was quite confusing. Instead, we fail fast at the point of the mistake. Also, we apply this to `config.extend` and `config.prepend` for consistency.
1 parent cc869c8 commit 157e20f

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/rspec/core/configuration.rb

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,9 +1175,9 @@ def exclusion_filter
11751175
# @see #extend
11761176
# @see #prepend
11771177
def include(mod, *filters)
1178-
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
1179-
@include_modules.append(mod, meta)
1180-
on_existing_matching_groups(meta) { |group| safe_include(mod, group) }
1178+
define_mixed_in_module(mod, filters, @include_modules, :include) do |group|
1179+
safe_include(mod, group)
1180+
end
11811181
end
11821182

11831183
# Tells RSpec to extend example groups with `mod`. Methods defined in
@@ -1211,9 +1211,9 @@ def include(mod, *filters)
12111211
# @see #include
12121212
# @see #prepend
12131213
def extend(mod, *filters)
1214-
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
1215-
@extend_modules.append(mod, meta)
1216-
on_existing_matching_groups(meta) { |group| safe_extend(mod, group) }
1214+
define_mixed_in_module(mod, filters, @extend_modules, :extend) do |group|
1215+
safe_extend(mod, group)
1216+
end
12171217
end
12181218

12191219
if RSpec::Support::RubyFeatures.module_prepends_supported?
@@ -1250,9 +1250,9 @@ def extend(mod, *filters)
12501250
# @see #include
12511251
# @see #extend
12521252
def prepend(mod, *filters)
1253-
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
1254-
@prepend_modules.append(mod, meta)
1255-
on_existing_matching_groups(meta) { |group| safe_prepend(mod, group) }
1253+
define_mixed_in_module(mod, filters, @prepend_modules, :prepend) do |group|
1254+
safe_prepend(mod, group)
1255+
end
12561256
end
12571257
end
12581258

@@ -1956,6 +1956,16 @@ def safe_extend(mod, host)
19561956
end
19571957
# :nocov:
19581958
end
1959+
1960+
def define_mixed_in_module(mod, filters, mod_list, config_method, &block)
1961+
unless Module === mod
1962+
raise TypeError, "`RSpec.configuration.#{config_method}` expects a module but got: #{mod.inspect}"
1963+
end
1964+
1965+
meta = Metadata.build_hash_from(filters, :warn_about_example_group_filtering)
1966+
mod_list.append(mod, meta)
1967+
on_existing_matching_groups(meta, &block)
1968+
end
19591969
end
19601970
# rubocop:enable Metrics/ClassLength
19611971
end

spec/rspec/core/configuration_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,19 @@ def file_at(relative_path)
901901
end
902902
end
903903

904+
config_methods = %w[ include extend ]
905+
config_methods << "prepend" if RSpec::Support::RubyFeatures.module_prepends_supported?
906+
config_methods.each do |config_method|
907+
it "raises an immediate `TypeError` when you attempt to `config.#{config_method}` with something besides a module" do
908+
expect {
909+
config.send(config_method, :not_a_module)
910+
}.to raise_error(TypeError, a_string_including(
911+
"configuration.#{config_method}",
912+
"expects a module but got", "not_a_module"
913+
))
914+
end
915+
end
916+
904917
describe "#include" do
905918
include_examples "warning of deprecated `:example_group` during filtering configuration", :include, Enumerable
906919

0 commit comments

Comments
 (0)