diff --git a/lib/pathname.rb b/lib/pathname.rb index a0db812..74fa2f2 100644 --- a/lib/pathname.rb +++ b/lib/pathname.rb @@ -1196,16 +1196,36 @@ def find(ignore_error: true) # :yield: pathname class Pathname # * FileUtils * - # Recursively deletes a directory, including all directories beneath it. - # - # See FileUtils.rm_rf - def rmtree(noop: nil, verbose: nil, secure: nil) - # The name "rmtree" is borrowed from File::Path of Perl. - # File::Path provides "mkpath" and "rmtree". - require 'fileutils' - FileUtils.rm_rf(@path, noop: noop, verbose: verbose, secure: secure) - self - end + # See FileUtils.mkdir_p. + def mkdir_p(...) require 'fileutils' ; FileUtils.mkdir_p(self, ...) end + + # See FileUtils.ln. + def ln(...) require 'fileutils' ; FileUtils.ln(self, ...) end + + # See FileUtils.ln_s. + def ln_s(...) require 'fileutils' ; FileUtils.ln_s(self, ...) end + + # See FileUtils.ln_sf. + def ln_sf(...) require 'fileutils' ; FileUtils.ln_sf(self, ...) end + + # See FileUtils.cp. + def cp(...) require 'fileutils' ; FileUtils.cp(self, ...) end + + # See FileUtils.cp_r. + def cp_r(...) require 'fileutils' ; FileUtils.cp_r(self, ...) end + + # See FileUtils.mv. + def mv(...) require 'fileutils' ; FileUtils.mv(self, ...) end + + # See FileUtils.rm. + def rm(...) require 'fileutils' ; FileUtils.rm(self, ...) end + + # See FileUtils.rm_r. + def rm_r(...) require 'fileutils' ; FileUtils.rm_r(self, ...) end + + # See FileUtils.rm_rf. + def rm_rf(...) require 'fileutils' ; FileUtils.rm_rf(self, ...) end + alias rmtree rm_rf end class Pathname # * tmpdir * diff --git a/test/pathname/test_pathname.rb b/test/pathname/test_pathname.rb index e80473e..77d1b41 100644 --- a/test/pathname/test_pathname.rb +++ b/test/pathname/test_pathname.rb @@ -1497,12 +1497,102 @@ def test_mkpath } end + # + # tests for FileUtils facades + # + + def test_mkdir_p + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a/b/c/d").mkdir_p + assert_file.directory?("a/b/c/d") + } + end + + def test_ln + return if !has_hardlink? + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("abc") + Pathname("a").ln("b") + assert_file.identical?("a", "b") + } + end + + def test_ln_s + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("abc") + Pathname("a").ln_s("b") + assert_equal "a", File.readlink("b") + } + end + + def test_ln_sf + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("abc") + 2.times do + assert_nothing_raised { Pathname("a").ln_sf("b") } + end + } + end + + def test_cp + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("abc") + Pathname("a").cp("b") + assert_equal "abc", Pathname("b").read + } + end + + def test_cp_r + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("1/a/b").mkdir_p + Pathname("1/a/b/c").write("abc") + Pathname("1").cp_r("2") + assert_equal "abc", Pathname("2/a/b/c").read + } + end + + def test_mv + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("abc") + Pathname("a").mv("b") + assert_file.not_exist?("a") + assert_equal "abc", Pathname("b").read + } + end + + def test_rm + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a").write("abc") + assert_file.exist?("a") + Pathname("a").rm + assert_file.not_exist?("a") + } + end + + def test_rm_r + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a/b/c/d").mkdir_p + assert_file.exist?("a/b/c/d") + Pathname("a").rm_r + assert_file.not_exist?("a") + } + end + + def test_rm_rf + with_tmpchdir('rubytest-pathname') {|dir| + Pathname("a/b/c/d").mkdir_p + assert_file.exist?("a/b/c/d") + 2.times { Pathname("a").rm_rf } + assert_file.not_exist?("a") + } + end + def test_rmtree + # rmtree is an alias for rm_rf with_tmpchdir('rubytest-pathname') {|dir| - Pathname("a/b/c/d").mkpath + Pathname("a/b/c/d").mkdir_p assert_file.exist?("a/b/c/d") - path = Pathname("a") - assert_equal(path, path.rmtree) + 2.times { Pathname("a").rm_rf } assert_file.not_exist?("a") } end