Skip to content
Browse files

added FileUtils#chmod and FileUtils#chmod_R

  • Loading branch information...
1 parent 0a75fe8 commit 96faf01675c45e395d784a012e51b79c12ab8e6d @mmriis committed
Showing with 59 additions and 0 deletions.
  1. +23 −0 lib/fakefs/fileutils.rb
  2. +36 −0 test/fakefs_test.rb
View
23 lib/fakefs/fileutils.rb
@@ -128,6 +128,29 @@ def chown(user, group, list, options={})
def chown_R(user, group, list, options={})
chown(user, group, list, options={})
end
+
+ def chmod(mode, list, options={})
+ list = Array(list)
+ list.each do |f|
+ if File.exists?(f)
+ File.chmod(mode, f)
+ else
+ raise Errno::ENOENT, f
+ end
+ end
+ list
+ end
+
+ def chmod_R(mode, list, options={})
+ list = Array(list)
+ list.each do |file|
+ chmod(mode, file)
+ FileSystem.find("#{file}/**/**").each do |f|
+ chmod(mode, f.to_s)
+ end
+ end
+ list
+ end
def touch(list, options={})
Array(list).each do |f|
View
36 test/fakefs_test.rb
@@ -715,6 +715,42 @@ def test_can_chown_R_files
resp = FileUtils.chown_R('no', 'no', '/path')
assert_equal ['/path'], resp
end
+
+ def test_can_chmod_files
+ good = "file.txt"
+ bad = "nofile.txt"
+ FileUtils.touch(good)
+
+ assert_equal [good], FileUtils.chmod(0600, good, :verbose => true)
+ assert_equal File.stat(good).mode, 0100600
+ assert_raises(Errno::ENOENT) do
+ FileUtils.chmod(0600, bad)
+ end
+
+ assert_equal [good], FileUtils.chmod(0666, good)
+ assert_equal File.stat(good).mode, 0100666
+ assert_raises(Errno::ENOENT) do
+ FileUtils.chmod(0666, bad)
+ end
+
+ assert_equal [good], FileUtils.chmod(0644, [good])
+ assert_equal File.stat(good).mode, 0100644
+ assert_raises(Errno::ENOENT) do
+ FileUtils.chmod(0644, bad)
+ end
+ end
+
+ def test_can_chmod_R_files
+ FileUtils.mkdir_p "/path/sub"
+ FileUtils.touch "/path/file1"
+ FileUtils.touch "/path/sub/file2"
+
+ assert_equal ["/path"], FileUtils.chmod_R(0600, "/path")
+ assert_equal File.stat("/path").mode, 0100600
+ assert_equal File.stat("/path/file1").mode, 0100600
+ assert_equal File.stat("/path/sub").mode, 0100600
+ assert_equal File.stat("/path/sub/file2").mode, 0100600
+ end
def test_dir_globs_paths
FileUtils.mkdir_p '/path'

0 comments on commit 96faf01

Please sign in to comment.
Something went wrong with that request. Please try again.