Project

General

Profile

Feature #9286 ยป add_unit_test_chown_normal_user.patch

vajrasky (Vajrasky Kok), 12/23/2013 06:32 PM

View differences:

test/fileutils/test_fileutils.rb (working copy)
1 1
# $Id$
2 2

  
3 3
require 'fileutils'
4
require 'etc'
4 5
require_relative 'fileasserts'
5 6
require 'pathname'
6 7
require 'tmpdir'
......
111 112

  
112 113
  def setup
113 114
    @prevdir = Dir.pwd
115
    @groups = Process.groups
114 116
    tmproot = TMPROOT
115 117
    mymkdir tmproot unless File.directory?(tmproot)
116 118
    Dir.chdir tmproot
......
1052 1054
    }
1053 1055
  end if have_file_perm?
1054 1056

  
1055
  # FIXME: How can I test this method?
1056 1057
  def test_chown
1057 1058
    check_singleton :chown
1059

  
1060
    assert_output_lines([]) {
1061
      touch 'tmp/a'
1062
      # integer input for group, nil for user
1063
      chown nil, @groups[0], 'tmp/a'
1064
      assert_ownership_group @groups[0], 'tmp/a'
1065
    }
1066

  
1067
    if @groups[1]
1068
      assert_output_lines([]) {
1069
        touch 'tmp/b'
1070
        # string input for group, -1 for user
1071
        chown -1, Etc.getgrgid(@groups[1]).name, 'tmp/b'
1072
        assert_ownership_group @groups[1], 'tmp/b'
1073
      }
1074
    end
1058 1075
  end if have_file_perm?
1059 1076

  
1077
  def test_chown_verbose
1078
    assert_output_lines(["chown :#{@groups[0]} tmp/a1 tmp/a2"]) {
1079
      touch 'tmp/a1'
1080
      touch 'tmp/a2'
1081
      chown nil, @groups[0], ['tmp/a1', 'tmp/a2'], :verbose => true
1082
      assert_ownership_group @groups[0], 'tmp/a1'
1083
      assert_ownership_group @groups[0], 'tmp/a2'
1084
    }
1085
  end if have_file_perm?
1086

  
1087
  def test_chown_noop
1088
    if @groups[1]
1089
      touch 'tmp/a'
1090
      chown nil, @groups[0], 'tmp/a', :noop => false
1091
      assert_ownership_group @groups[0], 'tmp/a'
1092
      chown nil, @groups[1], 'tmp/a', :noop => true
1093
      assert_ownership_group @groups[0], 'tmp/a'
1094
      chown nil, @groups[1], 'tmp/a'
1095
      assert_ownership_group @groups[1], 'tmp/a'
1096
    end
1097
  end if have_file_perm?
1098

  
1099
  def test_chown_error
1100
    touch 'tmp/a'
1101

  
1102
    exception = assert_raise(Errno::EPERM) {
1103
        chown 'root', @groups[0], 'tmp/a'
1104
    }
1105
    assert_equal exception.message,
1106
                 "Operation not permitted @ chown_internal - tmp/a"
1107

  
1108
    exception = assert_raise(ArgumentError) {
1109
        chown '', @groups[0], 'tmp/a'
1110
    }
1111
    assert_equal exception.message, "can't find user for "
1112

  
1113
    exception = assert_raise(Errno::ENOENT) {
1114
        chown nil, @groups[0], ''
1115
    }
1116
    assert_equal exception.message,
1117
                 "No such file or directory @ chown_internal - "
1118
  end if have_file_perm?
1119

  
1120
  # FIXME: Need to add test for chown with root account
1121

  
1060 1122
  # FIXME: How can I test this method?
1061 1123
  def test_chown_R
1062 1124
    check_singleton :chown_R
test/fileutils/fileasserts.rb (working copy)
88 88
 <#{'%0*o' % [width, mode2]}>: "#{file2}"
89 89
EOT
90 90
      end
91

  
92
      def assert_ownership_group(expected, file)
93
        actual = File.stat(file).gid
94
        assert expected == actual, <<EOT
95
File group ownership of "#{file}" unexpected:
96
 Expected: <#{'%s' % expected}>
97
   Actual: <#{'%s' % actual}>
98
EOT
99
      end
91 100
    end
92 101
  end
93 102
end