summaryrefslogtreecommitdiff
path: root/test/fileutils
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-09 09:03:32 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-07-09 09:03:32 +0000
commit3488db9455a65d080d0852ef4716f0c58a3bb52f (patch)
treef11fe53d7e51ffbc05123d7384cd8a7c48e6ae06 /test/fileutils
parent3ac1eeb34dd618592cfc86065e5a875f716645dc (diff)
* test/fileutils/fileasserts.rb
(Test::Unit::FileAssertions#{assert_filemode,assert_equal_timestamp}): New utility assertion methods for testing file modes. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/fileutils')
-rw-r--r--test/fileutils/fileasserts.rb21
-rw-r--r--test/fileutils/test_fileutils.rb94
2 files changed, 66 insertions, 49 deletions
diff --git a/test/fileutils/fileasserts.rb b/test/fileutils/fileasserts.rb
index e78deee06c..c60606ee7f 100644
--- a/test/fileutils/fileasserts.rb
+++ b/test/fileutils/fileasserts.rb
@@ -67,6 +67,27 @@ EOT
assert_equal(expected.tv_sec, actual.tv_sec, full_message)
end
+ def assert_filemode(expected, file, message=nil, mask: 07777)
+ width = ('%o' % mask).size
+ actual = File.stat(file).mode & mask
+ assert expected == actual, <<EOT
+File mode of "#{file}" unexpected:
+ Expected: <#{'%0*o' % [width, expected]}>
+ Actual: <#{'%0*o' % [width, actual]}>
+EOT
+ end
+
+ def assert_equal_filemode(file1, file2, message=nil, mask: 07777)
+ mode1, mode2 = [file1, file2].map { |file|
+ File.stat(file).mode & mask
+ }
+ width = ('%o' % mask).size
+ assert mode1 == mode2, <<EOT
+File modes expected to be equal:
+ <#{'%0*o' % [width, mode1]}>: "#{file1}"
+ <#{'%0*o' % [width, mode2]}>: "#{file2}"
+EOT
+ end
end
end
end
diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb
index 6e5ba243d0..65825bd393 100644
--- a/test/fileutils/test_fileutils.rb
+++ b/test/fileutils/test_fileutils.rb
@@ -249,9 +249,7 @@ class TestFileUtils
touch 'tmp/cptmp'
chmod 0755, 'tmp/cptmp'
cp 'tmp/cptmp', 'tmp/cptmp2'
- assert_equal(File.stat('tmp/cptmp').mode,
- File.stat('tmp/cptmp2').mode,
- bug4507)
+ assert_equal_filemode('tmp/cptmp', 'tmp/cptmp2', bug4507)
end
def test_cp_preserve_permissions_dir
@@ -262,12 +260,8 @@ class TestFileUtils
mkdir 'tmp/cptmp/d2'
chmod 0700, 'tmp/cptmp/d2'
cp_r 'tmp/cptmp', 'tmp/cptmp2', :preserve => true
- assert_equal(File.stat('tmp/cptmp/d1').mode,
- File.stat('tmp/cptmp2/d1').mode,
- bug7246)
- assert_equal(File.stat('tmp/cptmp/d2').mode,
- File.stat('tmp/cptmp2/d2').mode,
- bug7246)
+ assert_equal_filemode('tmp/cptmp/d1', 'tmp/cptmp2/d1', bug7246)
+ assert_equal_filemode('tmp/cptmp/d2', 'tmp/cptmp2/d2', bug7246)
end
def test_cp_symlink
@@ -771,7 +765,7 @@ class TestFileUtils
mkdir 'tmp/tmp', :mode => 0700
assert_directory 'tmp/tmp'
- assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
+ assert_filemode 0700, 'tmp/tmp', mask: 0777 if have_file_perm?
Dir.rmdir 'tmp/tmp'
# EISDIR on OS X, FreeBSD; EEXIST on Linux; Errno::EACCES on Windows
@@ -783,7 +777,7 @@ class TestFileUtils
def test_mkdir_file_perm
mkdir 'tmp/tmp', :mode => 07777
assert_directory 'tmp/tmp'
- assert_equal 07777, (File.stat('tmp/tmp').mode & 07777)
+ assert_filemode 07777, 'tmp/tmp'
Dir.rmdir 'tmp/tmp'
end if have_file_perm?
@@ -840,13 +834,13 @@ class TestFileUtils
mkdir_p 'tmp/tmp/tmp', :mode => 0700
assert_directory 'tmp/tmp'
assert_directory 'tmp/tmp/tmp'
- assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
- assert_equal 0700, (File.stat('tmp/tmp/tmp').mode & 0777) if have_file_perm?
+ assert_filemode 0700, 'tmp/tmp', mask: 0777 if have_file_perm?
+ assert_filemode 0700, 'tmp/tmp/tmp', mask: 0777 if have_file_perm?
rm_rf 'tmp/tmp'
mkdir_p 'tmp/tmp', :mode => 0
assert_directory 'tmp/tmp'
- assert_equal 0, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
+ assert_filemode 0, 'tmp/tmp', mask: 0777 if have_file_perm?
# DO NOT USE rm_rf here.
# (rm(1) try to chdir to parent directory, it fails to remove directory.)
Dir.rmdir 'tmp/tmp'
@@ -858,7 +852,7 @@ class TestFileUtils
def test_mkdir_p_file_perm
mkdir_p 'tmp/tmp/tmp', :mode => 07777
assert_directory 'tmp/tmp/tmp'
- assert_equal 07777, (File.stat('tmp/tmp/tmp').mode & 07777)
+ assert_filemode 07777, 'tmp/tmp/tmp'
Dir.rmdir 'tmp/tmp/tmp'
Dir.rmdir 'tmp/tmp'
end if have_file_perm?
@@ -877,12 +871,12 @@ class TestFileUtils
File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
install 'tmp/aaa', 'tmp/bbb', :mode => 0600
assert_equal "aaa\n", File.read('tmp/bbb')
- assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
+ assert_filemode 0600, 'tmp/bbb', mask: 0777 if have_file_perm?
t = File.mtime('tmp/bbb')
install 'tmp/aaa', 'tmp/bbb'
assert_equal "aaa\n", File.read('tmp/bbb')
- assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
+ assert_filemode 0600, 'tmp/bbb', mask: 0777 if have_file_perm?
assert_equal_time t, File.mtime('tmp/bbb')
File.unlink 'tmp/aaa'
@@ -938,9 +932,9 @@ class TestFileUtils
touch 'tmp/a'
chmod 0700, 'tmp/a'
- assert_equal 0700, File.stat('tmp/a').mode & 0777
+ assert_filemode 0700, 'tmp/a'
chmod 0500, 'tmp/a'
- assert_equal 0500, File.stat('tmp/a').mode & 0777
+ assert_filemode 0500, 'tmp/a'
end if have_file_perm?
def test_chmod_symbol_mode
@@ -948,23 +942,25 @@ class TestFileUtils
touch 'tmp/a'
chmod "u=wrx,g=rx,o=x", 'tmp/a'
- assert_equal 0751, File.stat('tmp/a').mode & 07777
+ assert_filemode 0751, 'tmp/a'
chmod "g+w-x", 'tmp/a'
- assert_equal 0761, File.stat('tmp/a').mode & 07777
+ assert_filemode 0761, 'tmp/a'
chmod "o+r,g=o+w,o-r,u-o", 'tmp/a' # 761 => 763 => 773 => 771 => 671
- assert_equal 0671, File.stat('tmp/a').mode & 07777
+ assert_filemode 0671, 'tmp/a'
+ chmod "go=u", 'tmp/a'
+ assert_filemode 0666, 'tmp/a'
chmod "u=wrx,g=,o=", 'tmp/a'
- assert_equal 0700, File.stat('tmp/a').mode & 0777
+ assert_filemode 0700, 'tmp/a'
chmod "u=rx,go=", 'tmp/a'
- assert_equal 0500, File.stat('tmp/a').mode & 0777
+ assert_filemode 0500, 'tmp/a'
chmod "+wrx", 'tmp/a'
- assert_equal 0777, File.stat('tmp/a').mode & 0777
+ assert_filemode 0777, 'tmp/a'
chmod "u+s,o=s", 'tmp/a'
- assert_equal 04770, File.stat('tmp/a').mode & 07777
+ assert_filemode 04770, 'tmp/a'
chmod "u-w,go-wrx", 'tmp/a'
- assert_equal 04500, File.stat('tmp/a').mode & 07777
+ assert_filemode 04500, 'tmp/a'
chmod "+s", 'tmp/a'
- assert_equal 06500, File.stat('tmp/a').mode & 07777
+ assert_filemode 06500, 'tmp/a'
# FreeBSD ufs and tmpfs don't allow to change sticky bit against
# regular file. It's slightly strange. Anyway it's no effect bit.
@@ -972,9 +968,9 @@ class TestFileUtils
# NetBSD, OpenBSD and Solaris also denies it.
if /freebsd|netbsd|openbsd|solaris/ !~ RUBY_PLATFORM
chmod "u+t,o+t", 'tmp/a'
- assert_equal 07500, File.stat('tmp/a').mode & 07777
+ assert_filemode 07500, 'tmp/a'
chmod "a-t,a-s", 'tmp/a'
- assert_equal 0500, File.stat('tmp/a').mode & 07777
+ assert_filemode 0500, 'tmp/a'
end
assert_raise_with_message(ArgumentError, /invalid\b.*\bfile mode/) {
@@ -1006,15 +1002,15 @@ class TestFileUtils
mkdir_p 'tmp/dir/dir'
touch %w( tmp/dir/file tmp/dir/dir/file )
chmod_R 0700, 'tmp/dir'
- assert_equal 0700, File.stat('tmp/dir').mode & 0777
- assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
- assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
- assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
+ assert_filemode 0700, 'tmp/dir', mask: 0777
+ assert_filemode 0700, 'tmp/dir/file', mask: 0777
+ assert_filemode 0700, 'tmp/dir/dir', mask: 0777
+ assert_filemode 0700, 'tmp/dir/dir/file', mask: 0777
chmod_R 0500, 'tmp/dir'
- assert_equal 0500, File.stat('tmp/dir').mode & 0777
- assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
- assert_equal 0500, File.stat('tmp/dir/dir').mode & 0777
- assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
+ assert_filemode 0500, 'tmp/dir', mask: 0777
+ assert_filemode 0500, 'tmp/dir/file', mask: 0777
+ assert_filemode 0500, 'tmp/dir/dir', mask: 0777
+ assert_filemode 0500, 'tmp/dir/dir/file', mask: 0777
chmod_R 0700, 'tmp/dir' # to remove
end if have_file_perm?
@@ -1024,15 +1020,15 @@ class TestFileUtils
mkdir_p 'tmp/dir/dir'
touch %w( tmp/dir/file tmp/dir/dir/file )
chmod_R "u=wrx,g=,o=", 'tmp/dir'
- assert_equal 0700, File.stat('tmp/dir').mode & 0777
- assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
- assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
- assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
+ assert_filemode 0700, 'tmp/dir', mask: 0777
+ assert_filemode 0700, 'tmp/dir/file', mask: 0777
+ assert_filemode 0700, 'tmp/dir/dir', mask: 0777
+ assert_filemode 0700, 'tmp/dir/dir/file', mask: 0777
chmod_R "u=xr,g+X,o=", 'tmp/dir'
- assert_equal 0510, File.stat('tmp/dir').mode & 0777
- assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
- assert_equal 0510, File.stat('tmp/dir/dir').mode & 0777
- assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
+ assert_filemode 0510, 'tmp/dir', mask: 0777
+ assert_filemode 0500, 'tmp/dir/file', mask: 0777
+ assert_filemode 0510, 'tmp/dir/dir', mask: 0777
+ assert_filemode 0500, 'tmp/dir/dir/file', mask: 0777
chmod_R 0700, 'tmp/dir' # to remove
end if have_file_perm?
@@ -1042,9 +1038,9 @@ class TestFileUtils
assert_output_lines(["chmod 700 tmp/a", "chmod 500 tmp/a"]) {
touch 'tmp/a'
chmod 0700, 'tmp/a', verbose: true
- assert_equal 0700, File.stat('tmp/a').mode & 0777
+ assert_filemode 0700, 'tmp/a', mask: 0777
chmod 0500, 'tmp/a', verbose: true
- assert_equal 0500, File.stat('tmp/a').mode & 0777
+ assert_filemode 0500, 'tmp/a', mask: 0777
}
end if have_file_perm?
@@ -1052,7 +1048,7 @@ class TestFileUtils
assert_output_lines(["chmod 700 tmp/a"], FileUtils) {
touch 'tmp/a'
FileUtils.chmod 0700, 'tmp/a', verbose: true
- assert_equal 0700, File.stat('tmp/a').mode & 0777
+ assert_filemode 0700, 'tmp/a', mask: 0777
}
end if have_file_perm?