Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions lib/pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 <tt>FileUtils.mkdir_p</tt>.
def mkdir_p(...) require 'fileutils' ; FileUtils.mkdir_p(self, ...) end

# See <tt>FileUtils.ln</tt>.
def ln(...) require 'fileutils' ; FileUtils.ln(self, ...) end

# See <tt>FileUtils.ln_s</tt>.
def ln_s(...) require 'fileutils' ; FileUtils.ln_s(self, ...) end

# See <tt>FileUtils.ln_sf</tt>.
def ln_sf(...) require 'fileutils' ; FileUtils.ln_sf(self, ...) end

# See <tt>FileUtils.cp</tt>.
def cp(...) require 'fileutils' ; FileUtils.cp(self, ...) end

# See <tt>FileUtils.cp_r</tt>.
def cp_r(...) require 'fileutils' ; FileUtils.cp_r(self, ...) end

# See <tt>FileUtils.mv</tt>.
def mv(...) require 'fileutils' ; FileUtils.mv(self, ...) end

# See <tt>FileUtils.rm</tt>.
def rm(...) require 'fileutils' ; FileUtils.rm(self, ...) end

# See <tt>FileUtils.rm_r</tt>.
def rm_r(...) require 'fileutils' ; FileUtils.rm_r(self, ...) end

# See <tt>FileUtils.rm_rf</tt>.
def rm_rf(...) require 'fileutils' ; FileUtils.rm_rf(self, ...) end
alias rmtree rm_rf
end

class Pathname # * tmpdir *
Expand Down
96 changes: 93 additions & 3 deletions test/pathname/test_pathname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down