Skip to content

Commit a1d62a3

Browse files
larouxnhsbt
authored andcommitted
[rubygems/rubygems] Handle RubyGems installing to custom dir with non-existent parent dirs
ruby/rubygems@4701123601
1 parent 8f00960 commit a1d62a3

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

lib/rubygems/installer.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,7 @@ def write_cache_file
953953
end
954954

955955
def ensure_writable_dir(dir) # :nodoc:
956-
begin
957-
Dir.mkdir dir, *[options[:dir_mode] && 0o755].compact
958-
rescue SystemCallError
959-
raise unless File.directory? dir
960-
end
956+
FileUtils.mkdir_p dir, mode: options[:dir_mode] && 0o755
961957

962958
raise Gem::FilePermissionError.new(dir) unless File.writable? dir
963959
end

test/rubygems/installer_test_case.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,23 @@ def util_installer(spec, gem_home, force=true)
221221
force: force)
222222
end
223223

224+
def test_ensure_writable_dir_creates_missing_parent_directories
225+
installer = setup_base_installer(false)
226+
227+
non_existent_parent = File.join(@tempdir, "non_existent_parent")
228+
target_dir = File.join(non_existent_parent, "target_dir")
229+
230+
refute_directory_exists non_existent_parent, "Parent directory should not exist yet"
231+
refute_directory_exists target_dir, "Target directory should not exist yet"
232+
233+
assert_nothing_raised do
234+
installer.send(:ensure_writable_dir, target_dir)
235+
end
236+
237+
assert_directory_exists non_existent_parent, "Parent directory should exist now"
238+
assert_directory_exists target_dir, "Target directory should exist now"
239+
end
240+
224241
@@symlink_supported = nil
225242

226243
# This is needed for Windows environment without symlink support enabled (the default

test/rubygems/test_gem_commands_install_command.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,4 +1583,31 @@ def test_suggest_update_if_enabled
15831583
assert_includes @ui.output, "A new release of RubyGems is available: 1.2.3 → 2.0.0!"
15841584
end
15851585
end
1586+
1587+
def test_execute_bindir_with_nonexistent_parent_dirs
1588+
spec_fetcher do |fetcher|
1589+
fetcher.gem "a", 2 do |s|
1590+
s.executables = %w[a_bin]
1591+
s.files = %w[bin/a_bin]
1592+
end
1593+
end
1594+
1595+
@cmd.options[:args] = %w[a]
1596+
1597+
nested_bin_dir = File.join(@tempdir, "not", "exists")
1598+
refute_directory_exists nested_bin_dir, "Nested bin directory should not exist yet"
1599+
1600+
@cmd.options[:bin_dir] = nested_bin_dir
1601+
1602+
use_ui @ui do
1603+
assert_raise Gem::MockGemUi::SystemExitException, @ui.error do
1604+
@cmd.execute
1605+
end
1606+
end
1607+
1608+
assert_directory_exists nested_bin_dir, "Nested bin directory should exist now"
1609+
assert_path_exist File.join(nested_bin_dir, "a_bin")
1610+
1611+
assert_equal %w[a-2], @cmd.installed_specs.map(&:full_name)
1612+
end
15861613
end

0 commit comments

Comments
 (0)