Skip to content

Commit

Permalink
Merge branch 'cache-normalization' of https://github.com/erikh/rubygems
Browse files Browse the repository at this point in the history
… into erikh-cache-normalization

* 'cache-normalization' of https://github.com/erikh/rubygems:
  * Fix more extraneous File.join calls
  * Silly wabbit.
  remote fetcher tests
  More tests
  most commands out of the way.
  install command fetch command
  test_gem.rb
  moar
  gem cache handling in base test case
  Checkpointing; cache api slightly modified and redocumented; applied to most non-test places.
  ! Gem.cache_dir always references the proper cache dir. Pass true to support a user path. ! Gem.cache_gem, given a filename always references the cache gem. Pass true to support a user path.
  • Loading branch information
drbrain committed Feb 16, 2011
2 parents 0d1b5d5 + bd6c3c4 commit 4ab6caa
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 97 deletions.
21 changes: 21 additions & 0 deletions lib/rubygems.rb
Expand Up @@ -736,6 +736,27 @@ def self.path
@gem_path
end

##
# Get the appropriate cache path.
#
# Pass a string to use a different base path, or nil/false (default) for
# Gem.dir.
#

def self.cache_dir(custom_dir=false)
File.join(custom_dir ? custom_dir : Gem.dir, 'cache')
end

##
# Given a gem path, find the gem in cache.
#
# Pass a string as the second argument to use a different base path, or
# nil/false (default) for Gem.dir.

def self.cache_gem(filename, user_dir=false)
File.join(cache_dir(user_dir), filename)
end

##
# Set array of platforms this RubyGems supports (primarily for testing).

Expand Down
5 changes: 3 additions & 2 deletions lib/rubygems/commands/unpack_command.rb
Expand Up @@ -63,8 +63,9 @@ def execute
# TODO: see comments in get_path() about general service.

def find_in_cache(filename)
Gem.path.each do |gem_dir|
this_path = File.join gem_dir, 'cache', filename

Gem.path.each do |path|
this_path = Gem.cache_gem(filename, path)
return this_path if File.exist? this_path
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/dependency_installer.rb
Expand Up @@ -68,7 +68,7 @@ def initialize(options = {})
@installed_gems = []

@install_dir = options[:install_dir] || Gem.dir
@cache_dir = options[:cache_dir] || @install_dir
@cache_dir = options[:cache_dir] || Gem.cache_dir(@install_dir)

# Set with any errors that SpecFetcher finds while search through
# gemspecs for a dep
Expand Down
5 changes: 2 additions & 3 deletions lib/rubygems/installer.rb
Expand Up @@ -173,10 +173,9 @@ def install

write_require_paths_file_if_needed if Gem::QUICKLOADER_SUCKAGE

# HACK remove? Isn't this done in multiple places?
cached_gem = File.join @gem_home, "cache", @gem.split(/\//).pop
cached_gem = Gem.cache_gem(File.basename(@gem), @gem_home)
unless File.exist? cached_gem then
FileUtils.cp @gem, File.join(@gem_home, "cache")
FileUtils.cp @gem, Gem.cache_dir(@gem_home)
end

say @spec.post_install_message unless @spec.post_install_message.nil?
Expand Down
4 changes: 1 addition & 3 deletions lib/rubygems/installer_test_case.rb
Expand Up @@ -121,9 +121,7 @@ def util_setup_gem(ui = @ui) # HACK fix use_ui to make this automatic

def util_installer(spec, gem_path, gem_home)
util_build_gem spec
FileUtils.mv File.join(@gemhome, 'cache', spec.file_name),
@tempdir

FileUtils.mv Gem.cache_gem(spec.file_name), @tempdir
installer = Gem::Installer.new gem_path
installer.gem_dir = util_gem_dir
installer.gem_home = gem_home
Expand Down
4 changes: 2 additions & 2 deletions lib/rubygems/remote_fetcher.rb
Expand Up @@ -97,9 +97,9 @@ def download(spec, source_uri, install_dir = Gem.dir)
Gem.ensure_gem_subdirectories(install_dir) rescue nil

if File.writable?(install_dir)
cache_dir = File.join install_dir, 'cache'
cache_dir = Gem.cache_dir(install_dir)
else
cache_dir = File.join(Gem.user_dir, 'cache')
cache_dir = Gem.cache_dir(Gem.user_dir)
end

gem_file_name = spec.file_name
Expand Down
5 changes: 2 additions & 3 deletions lib/rubygems/test_case.rb
Expand Up @@ -347,7 +347,7 @@ def util_build_gem(spec)
end

FileUtils.mv spec.file_name,
File.join(@gemhome, 'cache', "#{spec.original_name}.gem")
Gem.cache_gem("#{spec.original_name}.gem")
end
end

Expand Down Expand Up @@ -389,8 +389,7 @@ def util_gem(name, version, deps = nil, &block)
util_build_gem spec

cache_file = File.join @tempdir, 'gems', "#{spec.original_name}.gem"
FileUtils.mv File.join(@gemhome, 'cache', "#{spec.original_name}.gem"),
cache_file
FileUtils.mv Gem.cache_gem("#{spec.original_name}.gem"), cache_file
FileUtils.rm File.join(@gemhome, 'specifications', spec.spec_name)

spec.loaded_from = nil
Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/test_utilities.rb
Expand Up @@ -98,7 +98,7 @@ def fetch_size(path)

def download spec, source_uri, install_dir = Gem.dir
name = spec.file_name
path = File.join(install_dir, 'cache', name)
path = Gem.cache_gem(name, install_dir)

Gem.ensure_gem_subdirectories install_dir

Expand Down
5 changes: 2 additions & 3 deletions lib/rubygems/uninstaller.rb
Expand Up @@ -209,11 +209,10 @@ def remove(spec, list)

FileUtils.rm_rf gemspec

cache_dir = File.join spec.installation_path, 'cache'
gem = File.join cache_dir, spec.file_name
gem = Gem.cache_gem(spec.file_name, spec.installation_path)

unless File.exist? gem then
gem = File.join cache_dir, "#{original_platform_name}.gem"
gem = Gem.cache_gem("#{original_platform_name}.gem", spec.installation_path)
end

FileUtils.rm_rf gem
Expand Down
2 changes: 1 addition & 1 deletion lib/rubygems/validator.rb
Expand Up @@ -88,7 +88,7 @@ def alien(gems=[])
next unless gems.include? gem_spec.name unless gems.empty?

install_dir = gem_spec.installation_path
gem_path = File.join install_dir, "cache", gem_spec.file_name
gem_path = Gem.cache_gem(gem_spec.file_name, install_dir)
spec_path = File.join install_dir, "specifications", gem_spec.spec_name
gem_directory = gem_spec.full_gem_path

Expand Down
22 changes: 18 additions & 4 deletions test/rubygems/test_gem.rb
Expand Up @@ -401,7 +401,7 @@ def test_self_ensure_gem_directories

Gem.ensure_gem_subdirectories @gemhome

assert File.directory?(File.join(@gemhome, "cache"))
assert File.directory?(Gem.cache_dir(@gemhome))
end

def test_self_ensure_gem_directories_missing_parents
Expand All @@ -413,7 +413,7 @@ def test_self_ensure_gem_directories_missing_parents

Gem.ensure_gem_subdirectories gemdir

assert File.directory?("#{gemdir}/cache")
assert File.directory?(Gem.cache_dir(gemdir))
end

unless win_platform? then # only for FS that support write protection
Expand All @@ -427,7 +427,7 @@ def test_self_ensure_gem_directories_write_protected

Gem.ensure_gem_subdirectories gemdir

refute File.exist?("#{gemdir}/cache")
refute File.exist?(Gem.cache_dir(gemdir))
ensure
FileUtils.chmod 0600, gemdir
end
Expand All @@ -444,7 +444,7 @@ def test_self_ensure_gem_directories_write_protected_parents

Gem.ensure_gem_subdirectories gemdir

refute File.exist?("#{gemdir}/cache")
refute File.exist?(Gem.cache_dir(gemdir))
ensure
FileUtils.chmod 0600, parent
end
Expand Down Expand Up @@ -812,6 +812,20 @@ def test_self_user_home
end
end

def test_self_cache_dir
util_ensure_gem_dirs

assert_equal File.join(@gemhome, 'cache'), Gem.cache_dir
assert_equal File.join(@userhome, '.gem', Gem.ruby_engine, Gem::ConfigMap[:ruby_version], 'cache'), Gem.cache_dir(Gem.user_dir)
end

def test_self_cache_gem
util_ensure_gem_dirs

assert_equal File.join(@gemhome, 'cache', 'test.gem'), Gem.cache_gem('test.gem')
assert_equal File.join(@userhome, '.gem', Gem.ruby_engine, Gem::ConfigMap[:ruby_version], 'cache', 'test.gem'), Gem.cache_gem('test.gem', Gem.user_dir)
end

if Gem.win_platform? then
def test_self_user_home_userprofile
skip 'Ruby 1.9 properly handles ~ path expansion' unless '1.9' > RUBY_VERSION
Expand Down
8 changes: 4 additions & 4 deletions test/rubygems/test_gem_commands_fetch_command.rb
Expand Up @@ -16,7 +16,7 @@ def test_execute
util_setup_spec_fetcher @a2

@fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] =
File.read(File.join(@gemhome, 'cache', @a2.file_name))
File.read(Gem.cache_gem(@a2.file_name, @gemhome))

@cmd.options[:args] = [@a2.name]

Expand All @@ -35,9 +35,9 @@ def test_execute_prerelease
util_setup_spec_fetcher @a2, @a2_pre

@fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] =
File.read(File.join(@gemhome, 'cache', @a2.file_name))
File.read(Gem.cache_gem(@a2.file_name, @gemhome))
@fetcher.data["#{@gem_repo}gems/#{@a2_pre.file_name}"] =
File.read(File.join(@gemhome, 'cache', @a2_pre.file_name))
File.read(Gem.cache_gem(@a2_pre.file_name, @gemhome))

@cmd.options[:args] = [@a2.name]
@cmd.options[:prerelease] = true
Expand All @@ -57,7 +57,7 @@ def test_execute_version
util_setup_spec_fetcher @a1, @a2

@fetcher.data["#{@gem_repo}gems/#{@a1.file_name}"] =
File.read(File.join(@gemhome, 'cache', @a1.file_name))
File.read(Gem.cache_gem(@a1.file_name, @gemhome))

@cmd.options[:args] = [@a2.name]
@cmd.options[:version] = Gem::Requirement.new '1'
Expand Down
28 changes: 12 additions & 16 deletions test/rubygems/test_gem_commands_install_command.rb
Expand Up @@ -16,9 +16,9 @@ def test_execute_exclude_prerelease
util_setup_spec_fetcher @a2, @a2_pre

@fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2.file_name))
read_binary(Gem.cache_gem(@a2.file_name, @gemhome))
@fetcher.data["#{@gem_repo}gems/#{@a2_pre.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2_pre.file_name))
read_binary(Gem.cache_gem(@a2_pre.file_name, @gemhome))

@cmd.options[:args] = [@a2.name]

Expand All @@ -38,9 +38,9 @@ def test_execute_explicit_version_includes_prerelease
util_setup_spec_fetcher @a2, @a2_pre

@fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2.file_name))
read_binary(Gem.cache_gem(@a2.file_name, @gemhome))
@fetcher.data["#{@gem_repo}gems/#{@a2_pre.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2_pre.file_name))
read_binary(Gem.cache_gem(@a2_pre.file_name, @gemhome))

@cmd.handle_options [@a2_pre.name, '--version', @a2_pre.version.to_s]
assert @cmd.options[:prerelease]
Expand Down Expand Up @@ -79,8 +79,7 @@ def test_execute_local
util_setup_fake_fetcher
@cmd.options[:domain] = :local

FileUtils.mv File.join(@gemhome, 'cache', @a2.file_name),
File.join(@tempdir)
FileUtils.mv Gem.cache_gem(@a2.file_name, @gemhome), @tempdir

@cmd.options[:args] = [@a2.name]

Expand Down Expand Up @@ -109,8 +108,7 @@ def test_no_user_install
util_setup_fake_fetcher
@cmd.options[:user_install] = false

FileUtils.mv File.join(@gemhome, 'cache', @a2.file_name),
File.join(@tempdir)
FileUtils.mv Gem.cache_gem(@a2.file_name, @gemhome), @tempdir

@cmd.options[:args] = [@a2.name]

Expand Down Expand Up @@ -201,9 +199,9 @@ def test_execute_prerelease
util_setup_spec_fetcher @a2, @a2_pre

@fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2.file_name))
read_binary(Gem.cache_gem(@a2.file_name, @gemhome))
@fetcher.data["#{@gem_repo}gems/#{@a2_pre.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2_pre.file_name))
read_binary(Gem.cache_gem(@a2_pre.file_name, @gemhome))

@cmd.options[:prerelease] = true
@cmd.options[:args] = [@a2_pre.name]
Expand All @@ -227,7 +225,7 @@ def test_execute_remote
util_setup_spec_fetcher @a2

@fetcher.data["#{@gem_repo}gems/#{@a2.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @a2.file_name))
read_binary(Gem.cache_gem(@a2.file_name, @gemhome))

@cmd.options[:args] = [@a2.name]

Expand All @@ -254,11 +252,9 @@ def test_execute_two
util_setup_fake_fetcher
@cmd.options[:domain] = :local

FileUtils.mv File.join(@gemhome, 'cache', @a2.file_name),
File.join(@tempdir)
FileUtils.mv Gem.cache_gem(@a2.file_name, @gemhome), @tempdir

FileUtils.mv File.join(@gemhome, 'cache', @b2.file_name),
File.join(@tempdir)
FileUtils.mv Gem.cache_gem(@b2.file_name, @gemhome), @tempdir

@cmd.options[:args] = [@a2.name, @b2.name]

Expand Down Expand Up @@ -287,7 +283,7 @@ def test_execute_conservative
util_setup_spec_fetcher @b2

@fetcher.data["#{@gem_repo}gems/#{@b2.file_name}"] =
read_binary(File.join(@gemhome, 'cache', @b2.file_name))
read_binary(Gem.cache_gem(@b2.file_name, @gemhome))

uninstall_gem(@b2)

Expand Down
20 changes: 10 additions & 10 deletions test/rubygems/test_gem_commands_pristine_command.rb
Expand Up @@ -67,29 +67,29 @@ def test_execute_all
end

def test_execute_missing_cache_gem
a = quick_gem 'a' do |s|
s.executables = %w[foo]
a = quick_gem 'a' do |s|
s.executables = %w[foo]
end

FileUtils.mkdir_p File.join(@tempdir, 'bin')

File.open File.join(@tempdir, 'bin', 'foo'), 'w' do |fp|
fp.puts "#!/usr/bin/ruby"
end

install_gem a

a_data = nil
open File.join(@gemhome, 'cache', a.file_name), 'rb' do |fp|
a_data = fp.read
end

util_setup_fake_fetcher
util_setup_spec_fetcher a

Gem::RemoteFetcher.fetcher.data["http://gems.example.com/gems/#{a.file_name}"] = a_data
FileUtils.rm File.join(@gemhome, 'cache', a.file_name)

FileUtils.rm Gem.cache_gem(a.file_name, @gemhome)

@cmd.options[:args] = %w[a]

Expand All @@ -101,9 +101,9 @@ def test_execute_missing_cache_gem

[
"Restoring gem\(s\) to pristine condition...",
"Restored a-1",
"Cached gem for a-2 not found, attempting to fetch...",
"Restored a-2",
"Restored a-1",
"Cached gem for a-2 not found, attempting to fetch...",
"Restored a-2",
"Restored a-3.a"
].each do |line|
assert_equal line, out.shift
Expand Down
2 changes: 1 addition & 1 deletion test/rubygems/test_gem_commands_push_command.rb
Expand Up @@ -7,7 +7,7 @@ def setup
super

@gems_dir = File.join @tempdir, 'gems'
@cache_dir = File.join @gemhome, 'cache'
@cache_dir = Gem.cache_dir(@gemhome)
FileUtils.mkdir @gems_dir
Gem.configuration.rubygems_api_key = "ed244fbf2b1a52e012da8616c512fa47f9aa5250"
@spec, @path = util_gem("freewill", "1.0.0")
Expand Down

0 comments on commit 4ab6caa

Please sign in to comment.