Skip to content

Commit

Permalink
[rubygems/rubygems] Instead of checking writability, try to write
Browse files Browse the repository at this point in the history
Checking writability is prone to errors. For example:

$ mkdir -p foo/bar
$ chmod -w foo
$ touch foo/bar/baz # succeeds even if foo is not writable

rubygems/rubygems@6056138b6a
  • Loading branch information
deivid-rodriguez authored and hsbt committed Dec 5, 2023
1 parent 4a71852 commit 7ab8777
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/rubygems/installer.rb
Expand Up @@ -351,6 +351,9 @@ def install
run_post_install_hooks

spec
rescue Errno::EACCES => e
# Permission denied - /path/to/foo
raise Gem::FilePermissionError, e.message.split(" - ").last
end

def run_pre_install_hooks # :nodoc:
Expand Down Expand Up @@ -716,7 +719,6 @@ def check_that_user_bin_dir_is_in_path # :nodoc:

def verify_gem_home # :nodoc:
FileUtils.mkdir_p gem_home, :mode => options[:dir_mode] && 0o755
raise Gem::FilePermissionError, gem_home unless File.writable?(gem_home)
end

def verify_spec
Expand Down
6 changes: 4 additions & 2 deletions test/rubygems/test_gem_commands_install_command.rb
Expand Up @@ -193,7 +193,7 @@ def test_execute_no_user_install
pend "skipped in root privilege" if Process.uid.zero?

spec_fetcher do |fetcher|
fetcher.gem "a", 2
fetcher.download "a", 2
end

@cmd.options[:user_install] = false
Expand All @@ -204,12 +204,14 @@ def test_execute_no_user_install
FileUtils.chmod 0o755, @userhome
FileUtils.chmod 0o555, @gemhome

assert_raise Gem::FilePermissionError do
assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
@cmd.execute
end
ensure
FileUtils.chmod 0o755, @gemhome
end

assert_equal %w[a-2], @cmd.installed_specs.map(&:full_name).sort
end

def test_execute_local_missing
Expand Down

0 comments on commit 7ab8777

Please sign in to comment.