diff options
author | Satadru Pramanik, DO, MPH, MEng <satadru@gmail.com> | 2023-02-09 22:37:26 -0500 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-02-10 03:37:39 +0000 |
commit | 94aed6ece5517344760816e521ee5d62366dfafb (patch) | |
tree | 51edd613cac1fcca39ef43bc74645dfa16e2b004 | |
parent | 194520f80e1cdb71faa055d731450855a1ddb8d1 (diff) | |
download | ruby-94aed6ece5517344760816e521ee5d62366dfafb.tar.gz |
[ruby/fileutils] Add mkdir_p to FileUtils.install
(https://github.com/ruby/fileutils/pull/104)
* Add mkdir_p to FileUtils.install
* Adjust raise message.
* adjust raise language
* handle trailing slash in dest
* simplify
* Add tests
-rw-r--r-- | lib/fileutils.rb | 8 | ||||
-rw-r--r-- | test/fileutils/test_fileutils.rb | 8 |
2 files changed, 15 insertions, 1 deletions
diff --git a/lib/fileutils.rb b/lib/fileutils.rb index b495078f93..7cbf9d8fa6 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -1642,7 +1642,13 @@ module FileUtils st = File.stat(s) unless File.exist?(d) and compare_file(s, d) remove_file d, true - copy_file s, d + if d.end_with?('/') + mkdir_p d + copy_file s, d + File.basename(s) + else + mkdir_p File.expand_path('..', d) + copy_file s, d + end File.utime st.atime, st.mtime, d if preserve File.chmod fu_mode(mode, st), d if mode File.chown uid, gid, d if uid or gid diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index 3994da7433..481f913d0c 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -1237,6 +1237,14 @@ class TestFileUtils < Test::Unit::TestCase install Pathname.new('tmp/a'), 'tmp/b' rm_f 'tmp/a'; touch 'tmp/a' install Pathname.new('tmp/a'), Pathname.new('tmp/b') + my_rm_rf 'tmp/new_dir_end_with_slash' + install Pathname.new('tmp/a'), 'tmp/new_dir_end_with_slash/' + my_rm_rf 'tmp/new_dir_end_with_slash' + my_rm_rf 'tmp/new_dir' + install Pathname.new('tmp/a'), 'tmp/new_dir/a' + my_rm_rf 'tmp/new_dir' + install Pathname.new('tmp/a'), 'tmp/new_dir/new_dir_end_with_slash/' + my_rm_rf 'tmp/new_dir' rm_f 'tmp/a' touch 'tmp/a' touch 'tmp/b' |