From 7b0a1569654cc2798a737411fefdbfab9979324f Mon Sep 17 00:00:00 2001 From: Alexander Bulancov <6594487+trinistr@users.noreply.github.com> Date: Sat, 15 Nov 2025 00:46:02 +0300 Subject: [PATCH] Protect mkspec against accessing SortedSet without the gem --- lib/mspec/utils/name_map.rb | 13 ++++++++++--- spec/utils/fixtures/this_file_raises.rb | 1 + spec/utils/name_map_spec.rb | 7 +++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 spec/utils/fixtures/this_file_raises.rb diff --git a/lib/mspec/utils/name_map.rb b/lib/mspec/utils/name_map.rb index bf70e65..9b04112 100644 --- a/lib/mspec/utils/name_map.rb +++ b/lib/mspec/utils/name_map.rb @@ -66,10 +66,17 @@ def exception?(name) end def class_or_module(c) - const = Object.const_get(c, false) + begin + const = Object.const_get(c, false) + rescue NameError, RuntimeError + # Either the constant doesn't exist or it is + # explicitly raising an error, like `SortedSet`. + return nil + end + return nil unless Module === const + filtered = @filter && EXCLUDED.include?(const.name) - return const if Module === const and !filtered - rescue NameError + return const unless filtered end def namespace(mod, const) diff --git a/spec/utils/fixtures/this_file_raises.rb b/spec/utils/fixtures/this_file_raises.rb new file mode 100644 index 0000000..8e37a58 --- /dev/null +++ b/spec/utils/fixtures/this_file_raises.rb @@ -0,0 +1 @@ +raise "This is a BAD file" diff --git a/spec/utils/name_map_spec.rb b/spec/utils/name_map_spec.rb index a18a481..23bf95d 100644 --- a/spec/utils/name_map_spec.rb +++ b/spec/utils/name_map_spec.rb @@ -21,6 +21,8 @@ class Fixnum def f; end end + autoload :BadFile, "#{__dir__}/fixtures/this_file_raises.rb" + def self.n; end def n; end end @@ -84,6 +86,11 @@ def n; end expect(@map.class_or_module("Hell")).to eq(nil) expect(@map.class_or_module("Bush::Brain")).to eq(nil) end + + it "returns nil if accessing the constant raises RuntimeError" do + expect { NameMapSpecs::BadFile }.to raise_error(RuntimeError) + expect(@map.class_or_module("NameMapSpecs::BadFile")).to eq(nil) + end end RSpec.describe NameMap, "#dir_name" do