ruby/test/test_bundled_gems.rb
Chris Hasiński 7688628684 Fix incorrect bundled gems warning for hyphenated gem names
When requiring a file like "benchmark/ips", the warning system would
incorrectly warn about the "benchmark" gem not being a default gem,
even when the user has "benchmark-ips" (a separate third-party gem)
in their Gemfile.

The fix checks if a hyphenated version of the require path exists in
the bundle specs before issuing a warning. For example, requiring
"benchmark/ips" now checks for both "benchmark" and "benchmark-ips"
in the Gemfile.

[Bug #21828]
2026-01-08 13:15:29 +09:00

49 lines
1.5 KiB
Ruby

require_relative "rubygems/helper"
require "rubygems"
require "bundled_gems"
class TestBundlerGem < Gem::TestCase
def setup
Gem::BUNDLED_GEMS::WARNED.clear
end
def teardown
Gem::BUNDLED_GEMS::WARNED.clear
end
def test_warning
assert Gem::BUNDLED_GEMS.warning?("csv", specs: {})
assert_nil Gem::BUNDLED_GEMS.warning?("csv", specs: {})
end
def test_no_warning_warning
assert_nil Gem::BUNDLED_GEMS.warning?("some_gem", specs: {})
assert_nil Gem::BUNDLED_GEMS.warning?("/path/to/some_gem.rb", specs: {})
end
def test_warning_libdir
path = File.join(::RbConfig::CONFIG.fetch("rubylibdir"), "csv.rb")
assert Gem::BUNDLED_GEMS.warning?(path, specs: {})
assert_nil Gem::BUNDLED_GEMS.warning?(path, specs: {})
end
def test_warning_archdir
path = File.join(::RbConfig::CONFIG.fetch("rubyarchdir"), "syslog.so")
assert Gem::BUNDLED_GEMS.warning?(path, specs: {})
assert_nil Gem::BUNDLED_GEMS.warning?(path, specs: {})
end
def test_no_warning_for_hyphenated_gem
# When benchmark-ips gem is in specs, requiring "benchmark/ips" should not warn
# about the benchmark gem (Bug #21828)
assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {"benchmark-ips" => true})
end
def test_warning_without_hyphenated_gem
# When benchmark-ips is NOT in specs, requiring "benchmark/ips" should warn
warning = Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {})
assert warning
assert_match(/benchmark/, warning)
end
end