diff --git a/ChangeLog b/ChangeLog index d65de11a69..61998df60b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Mon Dec 23 18:37:16 2013 Nobuyoshi Nakada + + * test/fileutils/fileasserts.rb (assert_ownership_group): new + assertion for group ownership. + + * test/fileutils/test_fileutils.rb (test_chown{,_verbose,_noop}): + based on the patch by vajrasky (Vajrasky Kok) at + [ruby-core:59281]. [Feature #9286] + Mon Dec 23 15:53:45 2013 Nobuyoshi Nakada * hash.c (HAS_EXTRA_STATES): warn extra states only when something diff --git a/test/fileutils/fileasserts.rb b/test/fileutils/fileasserts.rb index c60606ee7f..2cc7e2316b 100644 --- a/test/fileutils/fileasserts.rb +++ b/test/fileutils/fileasserts.rb @@ -86,6 +86,15 @@ EOT File modes expected to be equal: <#{'%0*o' % [width, mode1]}>: "#{file1}" <#{'%0*o' % [width, mode2]}>: "#{file2}" +EOT + end + + def assert_ownership_group(expected, file) + actual = File.stat(file).gid + assert expected == actual, < + Actual: <#{actual}> EOT end end diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index c5f8734fea..a30d1ab94d 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -1,6 +1,7 @@ # $Id$ require 'fileutils' +require 'etc' require_relative 'fileasserts' require 'pathname' require 'tmpdir' @@ -111,6 +112,7 @@ class TestFileUtils def setup @prevdir = Dir.pwd + @groups = Process.groups tmproot = TMPROOT mymkdir tmproot unless File.directory?(tmproot) Dir.chdir tmproot @@ -1052,11 +1054,53 @@ class TestFileUtils } end if have_file_perm? - # FIXME: How can I test this method? def test_chown check_singleton :chown + + return unless @groups[1] + + input_group_1 = @groups[0] + assert_output_lines([]) { + touch 'tmp/a' + # integer input for group, nil for user + chown nil, input_group_1, 'tmp/a' + assert_ownership_group @groups[0], 'tmp/a' + } + + input_group_2 = Etc.getgrgid(@groups[1]).name + assert_output_lines([]) { + touch 'tmp/b' + # string input for group, -1 for user + chown -1, input_group_2, 'tmp/b' + assert_ownership_group @groups[1], 'tmp/b' + } end if have_file_perm? + def test_chown_verbose + assert_output_lines(["chown :#{@groups[0]} tmp/a1 tmp/a2"]) { + touch 'tmp/a1' + touch 'tmp/a2' + chown nil, @groups[0], ['tmp/a1', 'tmp/a2'], verbose: true + assert_ownership_group @groups[0], 'tmp/a1' + assert_ownership_group @groups[0], 'tmp/a2' + } + end if have_file_perm? + + def test_chown_noop + return unless @groups[1] + assert_output_lines([]) { + touch 'tmp/a' + chown nil, @groups[0], 'tmp/a', :noop => false + assert_ownership_group @groups[0], 'tmp/a' + chown nil, @groups[1], 'tmp/a', :noop => true + assert_ownership_group @groups[0], 'tmp/a' + chown nil, @groups[1], 'tmp/a' + assert_ownership_group @groups[1], 'tmp/a' + } + end if have_file_perm? + + # FIXME: Need to add test for chown with root account + # FIXME: How can I test this method? def test_chown_R check_singleton :chown_R