Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added command-line switch to force/deny user-level installations.

By default it falls back to user installation only if it can't write to gem home.
You can force it to use ~/.gem with --user-install

Also added stdout/stderr redirecting to test setup/teardown.


git-svn-id: http://rubygems.rubyforge.org/svn/trunk@1810 3d4018f9-ac1a-0410-99e9-8a154d859a19
  • Loading branch information...
commit fb9dbec9ba9a1f739f70dba94eeaf245898957fe 1 parent dc51246
technomancy authored
View
7 lib/rubygems/install_update_options.rb
@@ -90,6 +90,13 @@ def add_install_update_options
options[:format_executable] = value
end
+ add_option(:"Install/Update", '--[no-]user-install',
+ 'Install in user\'s home directory instead',
+ 'of GEM_HOME. Defaults to using home directory',
+ 'only if GEM_HOME is not writable.') do |value, options|
+ options[:user_install] = value
+ end
+
add_option(:"Install/Update", "--development",
"Install any additional development",
"dependencies") do |value, options|
View
23 lib/rubygems/installer.rb
@@ -85,15 +85,29 @@ def initialize(gem, options={})
raise Gem::InstallError, "invalid gem format for #{@gem}"
end
- if ! File.writable? @gem_home # or ! File.writable? RbConfig::CONFIG['bindir']
- STDERR.puts "Warning: #{@gem_home} and #{@bin_dir} are not both writable."
+ if !File.writable? @gem_home # TODO: or !File.writable? RbConfig::CONFIG['bindir']
+ if options[:user_install] == false # You explicitly don't want to use ~
+ raise Gem::FilePermissionError, @gem_home
+ elsif options[:user_install].nil?
+ $stderr.puts "Warning: falling back to user-level install since #{@gem_home} and #{@bin_dir} aren't both writable."
+ end
+ options[:user_install] = true
+ end
+
+ if options[:user_install]
@gem_home = File.join(ENV['HOME'], '.gem')
- STDERR.puts " Falling back to #{@gem_home}."
+
+ user_bin_dir = File.join(@gem_home, 'gems', 'bin')
+ if !ENV['PATH'].split(':').include?(user_bin_dir)
+ $stderr.puts "You don't have #{user_bin_dir} in your PATH."
+ $stderr.puts "You won't be able to run gem-installed executables until you add it."
+ end
+
Dir.mkdir @gem_home if ! File.directory? @gem_home
# If it's still not writable, you've got issues.
raise Gem::FilePermissionError, @gem_home if ! File.writable? @gem_home
end
-
+
@spec = @format.spec
@gem_dir = File.join(@gem_home, "gems", @spec.full_name).untaint
@@ -214,6 +228,7 @@ def write_spec
file_name = File.join @gem_home, 'specifications',
"#{@spec.full_name}.gemspec"
+
file_name.untaint
File.open(file_name, "w") do |file|
View
4 test/gemutilities.rb
@@ -83,6 +83,8 @@ def setup
'private_key.pem')
@public_cert = File.expand_path File.join(File.dirname(__FILE__),
'public_cert.pem')
+ $stdout = File.new('/dev/null','w')
+ $stderr = File.new('/dev/null','w')
end
def teardown
@@ -100,6 +102,8 @@ def teardown
ENV.delete 'GEM_PATH'
Gem.clear_paths
+ $stdout = STDOUT
+ $stderr = STDERR
end
def install_gem gem
View
31 test/test_gem_install_update_options.rb
@@ -1,20 +1,21 @@
require 'test/unit'
require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
+require File.join(File.expand_path(File.dirname(__FILE__)),
+ 'gem_installer_test_case')
require 'rubygems/install_update_options'
require 'rubygems/command'
-class TestGemInstallUpdateOptions < RubyGemTestCase
+class TestGemInstallUpdateOptions < GemInstallerTestCase
def setup
super
@cmd = Gem::Command.new 'dummy', 'dummy'
@cmd.extend Gem::InstallUpdateOptions
+ @cmd.add_install_update_options
end
def test_add_install_update_options
- @cmd.add_install_update_options
-
args = %w[-i /install_to --rdoc --ri -E -f -t -w -P HighSecurity
--ignore-dependencies --format-exec --include-dependencies]
@@ -22,8 +23,6 @@ def test_add_install_update_options
end
def test_security_policy
- @cmd.add_install_update_options
-
@cmd.handle_options %w[-P HighSecurity]
assert_equal Gem::Security::HighSecurity, @cmd.options[:security_policy]
@@ -37,4 +36,26 @@ def test_security_policy_unknown
end
end
+ def test_user_install_enabled
+ @cmd.handle_options %w[--user-install]
+
+ @installer = Gem::Installer.new @gem, @cmd.options
+ @installer.install
+ assert File.exist?(File.join(@userhome, '.gem', 'gems'))
+ assert File.exist?(File.join(@userhome, '.gem', 'gems',
+ @spec.full_name))
+ end
+
+ def test_user_install_disabled_read_only
+ @cmd.handle_options %w[--no-user-install]
+
+ File.chmod 0755, @userhome
+ FileUtils.chmod 0000, @gemhome
+
+ assert_raises(Gem::FilePermissionError) do
+ @installer = Gem::Installer.new @gem, @cmd.options
+ end
+ ensure
+ FileUtils.chmod 0755, @gemhome
+ end
end
View
2  test/test_gem_installer.rb
@@ -635,7 +635,7 @@ def test_install_missing_dirs
"#{@spec.full_name}.gemspec"))
end
- def test_install_user_local
+ def test_install_user_local_fallback
Dir.mkdir util_inst_bindir
File.chmod 0755, @userhome
File.chmod 0000, util_inst_bindir
Please sign in to comment.
Something went wrong with that request. Please try again.