Skip to content

Commit 8af16ba

Browse files
authored
Add mkdir_p to FileUtils.install (#104)
* Add mkdir_p to FileUtils.install * Adjust raise message. * adjust raise language * handle trailing slash in dest * simplify * Add tests
1 parent 0e3395f commit 8af16ba

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/fileutils.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1642,7 +1642,13 @@ def install(src, dest, mode: nil, owner: nil, group: nil, preserve: nil,
16421642
st = File.stat(s)
16431643
unless File.exist?(d) and compare_file(s, d)
16441644
remove_file d, true
1645-
copy_file s, d
1645+
if d.end_with?('/')
1646+
mkdir_p d
1647+
copy_file s, d + File.basename(s)
1648+
else
1649+
mkdir_p File.expand_path('..', d)
1650+
copy_file s, d
1651+
end
16461652
File.utime st.atime, st.mtime, d if preserve
16471653
File.chmod fu_mode(mode, st), d if mode
16481654
File.chown uid, gid, d if uid or gid

test/fileutils/test_fileutils.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,14 @@ def test_install_pathname
12371237
install Pathname.new('tmp/a'), 'tmp/b'
12381238
rm_f 'tmp/a'; touch 'tmp/a'
12391239
install Pathname.new('tmp/a'), Pathname.new('tmp/b')
1240+
my_rm_rf 'tmp/new_dir_end_with_slash'
1241+
install Pathname.new('tmp/a'), 'tmp/new_dir_end_with_slash/'
1242+
my_rm_rf 'tmp/new_dir_end_with_slash'
1243+
my_rm_rf 'tmp/new_dir'
1244+
install Pathname.new('tmp/a'), 'tmp/new_dir/a'
1245+
my_rm_rf 'tmp/new_dir'
1246+
install Pathname.new('tmp/a'), 'tmp/new_dir/new_dir_end_with_slash/'
1247+
my_rm_rf 'tmp/new_dir'
12401248
rm_f 'tmp/a'
12411249
touch 'tmp/a'
12421250
touch 'tmp/b'

0 commit comments

Comments
 (0)