Skip to content

Commit f213c44

Browse files
committed
Protect mkspec against accessing SortedSet without the gem
1 parent 6a7b509 commit f213c44

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

lib/mspec/utils/name_map.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,13 @@ def exception?(name)
6767

6868
def class_or_module(c)
6969
const = Object.const_get(c, false)
70+
return nil unless Module === const
71+
7072
filtered = @filter && EXCLUDED.include?(const.name)
71-
return const if Module === const and !filtered
72-
rescue NameError
73+
return const unless filtered
74+
rescue NameError, RuntimeError
75+
# Either the constant doesn't exist or it is
76+
# explicitly raising an error, like `SortedSet`.
7377
end
7478

7579
def namespace(mod, const)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
raise "This is a BAD file"

spec/utils/name_map_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class Fixnum
2121
def f; end
2222
end
2323

24+
autoload :BadFile, "#{__dir__}/fixtures/this_file_raises.rb"
25+
2426
def self.n; end
2527
def n; end
2628
end
@@ -84,6 +86,10 @@ def n; end
8486
expect(@map.class_or_module("Hell")).to eq(nil)
8587
expect(@map.class_or_module("Bush::Brain")).to eq(nil)
8688
end
89+
90+
it "returns nil if accessing the constant raises RuntimeError" do
91+
expect(@map.class_or_module("NameMapSpecs::BadFile")).to eq(nil)
92+
end
8793
end
8894

8995
RSpec.describe NameMap, "#dir_name" do

0 commit comments

Comments
 (0)