diff options
| author | Anthony Panozzo <panozzaj@gmail.com> | 2026-01-01 19:21:42 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2026-02-12 17:06:36 +0000 |
| commit | 2101c1bf091c30379daf96d4426a59e84002cccf (patch) | |
| tree | 1ca4bb8e2e70a2cab45e3d973050a6f799867382 /test | |
| parent | fd9dd36dfc38fd818dba0558bacb7c2df9bcdb79 (diff) | |
[ruby/rubygems] Add global gem cache shared by RubyGems and Bundler
Add opt-in support for a global .gem file cache at ~/.cache/gem/gems
(respects XDG_CACHE_HOME). This allows sharing cached gems across all
Ruby installations and between RubyGems and Bundler.
Enable via:
- Environment: RUBYGEMS_GLOBAL_GEM_CACHE=true
- gemrc: global_gem_cache: true
- Bundler: bundle config set global_gem_cache true
When enabled, RubyGems checks the global cache before downloading and
copies downloaded gems to the cache. Bundler's existing global_gem_cache
setting now uses the same unified cache location.
https://github.com/ruby/rubygems/commit/417e10d6a1
Diffstat (limited to 'test')
| -rw-r--r-- | test/rubygems/test_gem_config_file.rb | 27 | ||||
| -rw-r--r-- | test/rubygems/test_gem_remote_fetcher.rb | 70 |
2 files changed, 97 insertions, 0 deletions
diff --git a/test/rubygems/test_gem_config_file.rb b/test/rubygems/test_gem_config_file.rb index 4230eda4d3..79bf5f582c 100644 --- a/test/rubygems/test_gem_config_file.rb +++ b/test/rubygems/test_gem_config_file.rb @@ -83,6 +83,33 @@ class TestGemConfigFile < Gem::TestCase util_config_file %W[--config-file #{@temp_conf}] assert_equal true, @cfg.ipv4_fallback_enabled + ensure + ENV.delete("IPV4_FALLBACK_ENABLED") + end + + def test_initialize_global_gem_cache_default + util_config_file %W[--config-file #{@temp_conf}] + + assert_equal false, @cfg.global_gem_cache + end + + def test_initialize_global_gem_cache_env + ENV["RUBYGEMS_GLOBAL_GEM_CACHE"] = "true" + util_config_file %W[--config-file #{@temp_conf}] + + assert_equal true, @cfg.global_gem_cache + ensure + ENV.delete("RUBYGEMS_GLOBAL_GEM_CACHE") + end + + def test_initialize_global_gem_cache_gemrc + File.open @temp_conf, "w" do |fp| + fp.puts "global_gem_cache: true" + end + + util_config_file %W[--config-file #{@temp_conf}] + + assert_equal true, @cfg.global_gem_cache end def test_initialize_handle_arguments_config_file diff --git a/test/rubygems/test_gem_remote_fetcher.rb b/test/rubygems/test_gem_remote_fetcher.rb index 9badd75b42..e914e91677 100644 --- a/test/rubygems/test_gem_remote_fetcher.rb +++ b/test/rubygems/test_gem_remote_fetcher.rb @@ -575,6 +575,76 @@ class TestGemRemoteFetcher < Gem::TestCase end end + def test_download_with_global_gem_cache + # Use a temp directory to safely test global cache behavior + test_cache_dir = File.join(@tempdir, "global_gem_cache_test") + + Gem.stub :global_gem_cache_path, test_cache_dir do + Gem.configuration.global_gem_cache = true + + # Use the real RemoteFetcher with stubbed fetch_path + fetcher = Gem::RemoteFetcher.fetcher + def fetcher.fetch_path(uri, *rest) + File.binread File.join(@test_gem_dir, "a-1.gem") + end + fetcher.instance_variable_set(:@test_gem_dir, File.dirname(@a1_gem)) + + a1_cache_gem = @a1.cache_file + assert_equal a1_cache_gem, fetcher.download(@a1, "http://gems.example.com") + + # Verify gem was also copied to global cache + global_cache_gem = File.join(test_cache_dir, @a1.file_name) + assert File.exist?(global_cache_gem), "Gem should be copied to global cache" + end + ensure + Gem.configuration.global_gem_cache = false + end + + def test_download_uses_global_gem_cache + # Use a temp directory to safely test global cache behavior + test_cache_dir = File.join(@tempdir, "global_gem_cache_test") + + Gem.stub :global_gem_cache_path, test_cache_dir do + Gem.configuration.global_gem_cache = true + + # Pre-populate global cache + FileUtils.mkdir_p test_cache_dir + global_cache_gem = File.join(test_cache_dir, @a1.file_name) + FileUtils.cp @a1_gem, global_cache_gem + + fetcher = Gem::RemoteFetcher.fetcher + + # Should return global cache path without downloading + result = fetcher.download(@a1, "http://gems.example.com") + assert_equal global_cache_gem, result + end + ensure + Gem.configuration.global_gem_cache = false + end + + def test_download_without_global_gem_cache + # Use a temp directory to safely test global cache behavior + test_cache_dir = File.join(@tempdir, "global_gem_cache_test") + + Gem.stub :global_gem_cache_path, test_cache_dir do + Gem.configuration.global_gem_cache = false + + # Use the real RemoteFetcher with stubbed fetch_path + fetcher = Gem::RemoteFetcher.fetcher + def fetcher.fetch_path(uri, *rest) + File.binread File.join(@test_gem_dir, "a-1.gem") + end + fetcher.instance_variable_set(:@test_gem_dir, File.dirname(@a1_gem)) + + a1_cache_gem = @a1.cache_file + assert_equal a1_cache_gem, fetcher.download(@a1, "http://gems.example.com") + + # Verify gem was NOT copied to global cache + global_cache_gem = File.join(test_cache_dir, @a1.file_name) + refute File.exist?(global_cache_gem), "Gem should not be copied to global cache when disabled" + end + end + private def assert_error(exception_class = Exception) |
