[前][次][番号順一覧][スレッド一覧]

ruby-changes:32308

From: nobu <ko1@a...>
Date: Tue, 24 Dec 2013 23:20:54 +0900 (JST)
Subject: [ruby-changes:32308] nobu:r44387 (trunk): test_fileutils.rb: tests for chown

nobu	2013-12-24 23:20:47 +0900 (Tue, 24 Dec 2013)

  New Revision: 44387

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=44387

  Log:
    test_fileutils.rb: tests for chown
    
    * test/fileutils/fileasserts.rb (assert_ownership_user): new
      assertion for user ownership.
    * test/fileutils/test_fileutils.rb (test_chown_error),
      (test_chown_without_permission, test_chown_with_root):
      based on the patch by vajrasky (Vajrasky Kok) at
      [ruby-core:59298].  [Feature #9292]

  Modified files:
    trunk/ChangeLog
    trunk/test/fileutils/fileasserts.rb
    trunk/test/fileutils/test_fileutils.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 44386)
+++ ChangeLog	(revision 44387)
@@ -1,3 +1,13 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Dec 24 23:20:38 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* test/fileutils/fileasserts.rb (assert_ownership_user): new
+	  assertion for user ownership.
+
+	* test/fileutils/test_fileutils.rb (test_chown_error),
+	  (test_chown_without_permission, test_chown_with_root):
+	  based on the patch by vajrasky (Vajrasky Kok) at
+	  [ruby-core:59298].  [Feature #9292]
+
 Tue Dec 24 16:28:05 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* proc.c (rb_mod_define_method): consider visibility only if self
Index: test/fileutils/test_fileutils.rb
===================================================================
--- test/fileutils/test_fileutils.rb	(revision 44386)
+++ test/fileutils/test_fileutils.rb	(revision 44387)
@@ -69,6 +69,29 @@ class TestFileUtils < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L69
       return true
     end
 
+    def root_in_posix?
+      if Process.respond_to?('uid')
+        return Process.uid == 0
+      else
+        return false
+      end
+    end
+
+    def distinct_uids(n = 2)
+      return unless user = Etc.getpwent
+      uids = [user.uid]
+      while user = Etc.getpwent
+        uid = user.uid
+        unless uids.include?(uid)
+          uids << uid
+          break if uids.size >= n
+        end
+      end
+      uids
+    ensure
+      Etc.endpwent
+    end
+
     begin
       tmproot = TMPROOT
       Dir.mkdir tmproot unless File.directory?(tmproot)
@@ -1098,7 +1121,58 @@ class TestFileUtils < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/fileutils/test_fileutils.rb#L1121
     }
   end if have_file_perm?
 
-  # FIXME: Need to add test for chown with root account
+  if have_file_perm?
+    def test_chown_error
+      uid = distinct_uids(1)
+      return unless uid
+
+      touch 'tmp/a'
+
+      assert_raise_with_message(ArgumentError, "can't find user for ") {
+        chown '', @groups[0], 'tmp/a'
+      }
+
+      assert_raise_with_message(ArgumentError, "can't find group for ") {
+        chown uid, '', 'tmp/a'
+      }
+
+      assert_raise_with_message(Errno::ENOENT, /No such file or directory/) {
+        chown nil, @groups[0], ''
+      }
+    end
+
+    if root_in_posix?
+      def test_chown_with_root
+        uid_1, uid_2 = distinct_uids(2)
+        return unless uid_1 and uid_2
+
+        gid = @groups[0] # Most of the time, root only has one group
+
+        files = ['tmp/a1', 'tmp/a2']
+        files.each {|file| touch file}
+        [uid_1, uid_2].each {|uid|
+          assert_output_lines(["chown #{uid}:#{gid} tmp/a1 tmp/a2"]) {
+            chown uid, gid, files, verbose: true
+            files.each {|file|
+              assert_ownership_group gid, file
+              assert_ownership_user uid, file
+            }
+          }
+        }
+      end
+    else
+      def test_chown_without_permission
+        uid_1, uid_2 = distinct_uids(2)
+        return unless uid_1 and uid_2
+
+        touch 'tmp/a'
+        exception = assert_raise(Errno::EPERM) {
+          chown uid_1, nil, 'tmp/a'
+          chown uid_2, nil, 'tmp/a'
+        }
+      end
+    end
+  end
 
   # FIXME: How can I test this method?
   def test_chown_R
Index: test/fileutils/fileasserts.rb
===================================================================
--- test/fileutils/fileasserts.rb	(revision 44386)
+++ test/fileutils/fileasserts.rb	(revision 44387)
@@ -97,6 +97,15 @@ File group ownership of "#{file}" unexpe https://github.com/ruby/ruby/blob/trunk/test/fileutils/fileasserts.rb#L97
    Actual: <#{actual}>
 EOT
       end
+
+      def assert_ownership_user(expected, file)
+        actual = File.stat(file).uid
+        assert expected == actual, <<EOT
+File user ownership of "#{file}" unexpected:
+ Expected: <#{expected}>
+   Actual: <#{actual}>
+EOT
+      end
     end
   end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]