Permalink
Browse files

Add more sanity to the cases where bundler runs commands with sudo

  • Loading branch information...
1 parent 39adb2d commit 3f350e0e0e6dee22a46eaf3a95e6e15c1083c2c8 Carlhuda committed Jul 1, 2010
Showing with 54 additions and 11 deletions.
  1. +17 −4 lib/bundler.rb
  2. +1 −1 lib/bundler/installer.rb
  3. +4 −4 lib/bundler/source.rb
  4. +29 −0 spec/install/gems/sudo_spec.rb
  5. +3 −2 spec/support/sudo.rb
View
21 lib/bundler.rb
@@ -177,16 +177,29 @@ def default_gemfile
NULL = WINDOWS ? "NUL" : "/dev/null"
def requires_sudo?
+ path = bundle_path
+ path = path.parent until path.exist?
+
case
- when File.writable?(bundle_path) ||
+ when File.writable?(path) ||
`which sudo 2>#{NULL}`.empty? ||
- File.owned?(bundle_path)
+ File.owned?(path)
false
else
true
end
- rescue Errno::ENOENT
- false
+ end
+
+ def mkdir_p(path)
+ if requires_sudo?
+ sudo "mkdir -p '#{path}'"
+ else
+ FileUtils.mkdir_p(path)
+ end
+ end
+
+ def sudo(str)
+ `sudo -p 'Enter your password to install the bundled RubyGems to your system: ' -E #{str}`
end
private
View
2 lib/bundler/installer.rb
@@ -21,7 +21,7 @@ def run(options)
@definition.resolve_remotely!
# Ensure that BUNDLE_PATH exists
- FileUtils.mkdir_p(Bundler.bundle_path)
+ Bundler.mkdir_p(Bundler.bundle_path)
# Must install gems in the order that the resolver provides
# as dependencies might actually affect the installation of
View
8 lib/bundler/source.rb
@@ -97,15 +97,15 @@ def install(spec)
# SUDO HAX
if Bundler.requires_sudo?
sudo "mkdir -p #{Gem.dir}/gems #{Gem.dir}/specifications"
- sudo "mv #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/"
- sudo "mv #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/"
+ sudo "cp -R #{Bundler.tmp}/gems/#{spec.full_name} #{Gem.dir}/gems/"
+ sudo "cp -R #{Bundler.tmp}/specifications/#{spec.full_name}.gemspec #{Gem.dir}/specifications/"
end
spec.loaded_from = "#{Gem.dir}/specifications/#{spec.full_name}.gemspec"
end
def sudo(str)
- `sudo -p 'Enter your password to install the bundled RubyGems to your system: ' -E #{str}`
+ Bundler.sudo(str)
end
def cache(spec)
@@ -568,7 +568,7 @@ def cache
in_cache { git %|fetch --force --quiet "#{uri}" refs/heads/*:refs/heads/*| }
else
Bundler.ui.info "Fetching #{uri}"
- FileUtils.mkdir_p(cache_path.dirname)
+ Bundler.mkdir_p(cache_path.dirname)
git %|clone "#{uri}" "#{cache_path}" --bare --no-hardlinks|
end
end
View
29 spec/install/gems/sudo_spec.rb
@@ -0,0 +1,29 @@
+require "spec_helper"
+
+describe 'omg' do
+ describe_sudo "bundle install with GEM_HOME owned by root" do
+ it "works" do
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack", '1.0'
+ G
+
+ system_gem_path("gems/rack-1.0.0").should exist
+ File.stat(system_gem_path("gems/rack-1.0.0")).uid.should == 0
+ should_be_installed "rack 1.0"
+ end
+
+ it "works during the first time when BUNDLE_PATH does not exist" do
+ bundle_path = "#{tmp}/owned_by_root"
+ ENV['BUNDLE_PATH'] = bundle_path
+
+ install_gemfile <<-G
+ source "file://#{gem_repo1}"
+ gem "rack", '1.0'
+ G
+
+ tmp("owned_by_root/gems/rack-1.0.0").should exist
+ should_be_installed "rack 1.0"
+ end
+ end
+end
View
5 spec/support/sudo.rb
@@ -2,15 +2,14 @@ module Spec
module Sudo
def self.sudo?
@which_sudo ||= `which sudo`.strip
- !@which_sudo.empty?
+ !@which_sudo.empty? && ENV['BUNDLER_SUDO_TESTS']
end
module Describe
def describe_sudo(*args, &blk)
return unless Sudo.sudo?
describe(*args) do
before :each do
- pending "sudo tests are broken"
chown_system_gems
end
@@ -33,6 +32,8 @@ def sudo(cmd)
end
def chown_system_gems
+ FileUtils.mkdir_p tmp('owned_by_root')
+ sudo "chown -R root #{tmp('owned_by_root')}"
sudo "chown -R root #{system_gem_path}"
end
end

0 comments on commit 3f350e0

Please sign in to comment.