summaryrefslogtreecommitdiff
path: root/test/test_bundled_gems.rb
blob: 0889584185cb0754d344f15814287d5bdab375c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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_no_warning_for_subfeatures_of_hyphenated_gem
    # When benchmark-ips gem is in specs, requiring any "benchmark/*" subfeature
    # should not warn, since hyphenated gems may provide multiple files
    # (e.g., benchmark-ips provides benchmark/ips, benchmark/timing, benchmark/compare)
    assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/timing", specs: {"benchmark-ips" => true})
    assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/compare", 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

  def test_no_warning_for_subfeature_found_outside_stdlib
    # When a subfeature like "benchmark/ips" is found on $LOAD_PATH
    # from a non-standard-library location (e.g., benchmark-ips gem's lib dir),
    # don't warn even if the gem is not in specs (Bug #21828)
    Dir.mktmpdir do |dir|
      FileUtils.mkdir_p(File.join(dir, "benchmark"))
      File.write(File.join(dir, "benchmark", "ips.rb"), "")
      original_load_path = $LOAD_PATH.dup
      $LOAD_PATH.unshift(dir)
      begin
        assert_nil Gem::BUNDLED_GEMS.warning?("benchmark/ips", specs: {})
      ensure
        $LOAD_PATH.replace(original_load_path)
      end
    end
  end
end