| 1 |
class FileStatTest |
| 2 |
class << self |
| 3 |
def chmod(mode, path) |
| 4 |
system("chmod #{mode} #{path}") |
| 5 |
yield |
| 6 |
ensure |
| 7 |
system("chmod 644 #{path}") |
| 8 |
end |
| 9 |
end |
| 10 |
end |
| 11 |
|
| 12 |
assert 'File::Stat.new' do |
| 13 |
assert_kind_of File::Stat, File::Stat.new('README.md') |
| 14 |
assert_raise(RuntimeError){ File::Stat.new('unknown.file') } |
| 15 |
end |
| 16 |
|
| 17 |
assert 'File.stat' do |
| 18 |
assert_kind_of File::Stat, File.stat('README.md') |
| 19 |
end |
| 20 |
|
| 21 |
assert 'File.lstat' do |
| 22 |
assert_kind_of File::Stat, File.lstat('README.md') |
| 23 |
end |
| 24 |
|
| 25 |
assert 'File::Stat#initialize_copy' do |
| 26 |
orig = File::Stat.new('README.md') |
| 27 |
copy = orig.dup |
| 28 |
assert_equal orig.inspect, copy.inspect |
| 29 |
end |
| 30 |
|
| 31 |
assert 'File::Stat#<=>' do |
| 32 |
stat1 = File::Stat.new('README.md') |
| 33 |
stat2 = File::Stat.new('README.md') |
| 34 |
assert_equal 0, stat1.<=>(stat2) |
| 35 |
assert_equal nil, stat1.<=>(1) |
| 36 |
assert_raise(ArgumentError) { stat1 < 1 } |
| 37 |
end |
| 38 |
|
| 39 |
assert 'File::Stat#dev' do |
| 40 |
stat = File::Stat.new('README.md') |
| 41 |
assert_kind_of Fixnum, stat.dev |
| 42 |
end |
| 43 |
|
| 44 |
assert 'File::Stat#dev_major' do |
| 45 |
stat = File::Stat.new('README.md') |
| 46 |
if stat.dev_major |
| 47 |
assert_equal Fixnum, stat.dev_major.class |
| 48 |
else |
| 49 |
assert_nil stat.dev_major ## not supported |
| 50 |
end |
| 51 |
end |
| 52 |
|
| 53 |
assert 'File::Stat#dev_minor' do |
| 54 |
stat = File::Stat.new('README.md') |
| 55 |
if stat.dev_minor |
| 56 |
assert_equal Fixnum, stat.dev_minor.class |
| 57 |
else |
| 58 |
assert_nil stat.dev_minor ## not supported |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
assert 'File::Stat#ino' do |
| 63 |
stat = File::Stat.new('README.md') |
| 64 |
assert_kind_of Numeric, stat.ino |
| 65 |
end |
| 66 |
|
| 67 |
assert 'File::Stat#mode' do |
| 68 |
stat = File::Stat.new('README.md') |
| 69 |
assert_kind_of Fixnum, stat.mode |
| 70 |
end |
| 71 |
|
| 72 |
assert 'File::Stat#nlink' do |
| 73 |
stat = File::Stat.new('README.md') |
| 74 |
assert_kind_of Fixnum, stat.nlink |
| 75 |
end |
| 76 |
|
| 77 |
assert 'File::Stat#uid' do |
| 78 |
stat = File::Stat.new('README.md') |
| 79 |
assert_kind_of Numeric, stat.uid |
| 80 |
end |
| 81 |
|
| 82 |
assert 'File::Stat#gid' do |
| 83 |
stat = File::Stat.new('README.md') |
| 84 |
assert_kind_of Numeric, stat.gid |
| 85 |
end |
| 86 |
|
| 87 |
assert 'File::Stat#rdev' do |
| 88 |
stat = File::Stat.new('README.md') |
| 89 |
assert_kind_of Fixnum, stat.rdev |
| 90 |
end |
| 91 |
|
| 92 |
assert 'File::Stat#rdev_major' do |
| 93 |
stat = File::Stat.new('README.md') |
| 94 |
if stat.rdev_major |
| 95 |
assert_equal Fixnum, stat.rdev_major.class |
| 96 |
else |
| 97 |
assert_nil stat.rdev_major ## not supported |
| 98 |
end |
| 99 |
end |
| 100 |
|
| 101 |
assert 'File::Stat#rdev_minor' do |
| 102 |
stat = File::Stat.new('README.md') |
| 103 |
if stat.rdev_minor |
| 104 |
assert_equal Fixnum, stat.rdev_minor.class |
| 105 |
else |
| 106 |
assert_nil stat.rdev_minor ## not supported |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
assert 'File::Stat#blocks' do |
| 111 |
stat = File::Stat.new('README.md') |
| 112 |
blocks = stat.blocks |
| 113 |
skip "This system not support `struct stat.st_blocks`" if blocks.nil? |
| 114 |
|
| 115 |
assert_kind_of Integer, blocks |
| 116 |
end |
| 117 |
|
| 118 |
assert 'File::Stat#atime' do |
| 119 |
stat = File::Stat.new('README.md') |
| 120 |
assert_kind_of Time, stat.atime |
| 121 |
end |
| 122 |
|
| 123 |
assert 'File::Stat#mtime' do |
| 124 |
stat = File::Stat.new('README.md') |
| 125 |
assert_kind_of Time, stat.mtime |
| 126 |
end |
| 127 |
|
| 128 |
assert 'File::Stat#ctime' do |
| 129 |
stat = File::Stat.new('README.md') |
| 130 |
assert_kind_of Time, stat.ctime |
| 131 |
end |
| 132 |
|
| 133 |
assert 'File::Stat#birthtime' do |
| 134 |
stat = File::Stat.new('README.md') |
| 135 |
begin |
| 136 |
assert_kind_of Time, stat.birthtime |
| 137 |
rescue NotImplementedError |
| 138 |
skip 'This system not support `struct stat.birthtimespec`' |
| 139 |
end |
| 140 |
end |
| 141 |
|
| 142 |
assert 'File::Stat#size' do |
| 143 |
stat = File::Stat.new('README.md') |
| 144 |
assert_true 0 < stat.size |
| 145 |
end |
| 146 |
|
| 147 |
assert 'File::Stat#blksize' do |
| 148 |
stat = File::Stat.new('README.md') |
| 149 |
blksize = stat.blksize |
| 150 |
skip "This system not support `struct stat.st_blksize`" if blksize.nil? |
| 151 |
|
| 152 |
assert_kind_of Integer, blksize |
| 153 |
end |
| 154 |
|
| 155 |
assert 'File::Stat#inspect' do |
| 156 |
stat = File::Stat.new('README.md') |
| 157 |
%w(dev ino mode nlink uid gid size blksize blocks atime mtime ctime).all? do |name| |
| 158 |
assert_include stat.inspect, name |
| 159 |
end |
| 160 |
end |
| 161 |
|
| 162 |
assert 'File::Stat#ftype' do |
| 163 |
stat = File::Stat.new('README.md') |
| 164 |
assert_equal "file", stat.ftype |
| 165 |
|
| 166 |
stat = File::Stat.new('bin') |
| 167 |
assert_equal "directory", stat.ftype |
| 168 |
end |
| 169 |
|
| 170 |
assert 'File::Stat#directory?' do |
| 171 |
stat = File::Stat.new('README.md') |
| 172 |
assert_false stat.directory? |
| 173 |
|
| 174 |
stat = File::Stat.new('bin') |
| 175 |
assert_true stat.directory? |
| 176 |
end |
| 177 |
|
| 178 |
assert 'File::Stat#readable?' do |
| 179 |
skip "when windows" if FileStatTest.win? |
| 180 |
|
| 181 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 182 |
FileStatTest.chmod("+r-w-x", "#{dir}/test/readable") do |
| 183 |
assert_true File::Stat.new("#{dir}/test/readable").readable? |
| 184 |
end |
| 185 |
FileStatTest.chmod("-r+w-x", "#{dir}/test/writable") do |
| 186 |
assert_false File::Stat.new("#{dir}/test/writable").readable? |
| 187 |
end |
| 188 |
FileStatTest.chmod("-r-w+x", "#{dir}/test/executable") do |
| 189 |
assert_false File::Stat.new("#{dir}/test/executable").readable? |
| 190 |
end |
| 191 |
end |
| 192 |
|
| 193 |
assert 'File::Stat#readable_real?' do |
| 194 |
skip "when windows" if FileStatTest.win? |
| 195 |
|
| 196 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 197 |
FileStatTest.chmod("+r-w-x", "#{dir}/test/readable") do |
| 198 |
assert_true File::Stat.new("#{dir}/test/readable").readable_real? |
| 199 |
end |
| 200 |
FileStatTest.chmod("-r+w-x", "#{dir}/test/writable") do |
| 201 |
assert_false File::Stat.new("#{dir}/test/writable").readable_real? |
| 202 |
end |
| 203 |
FileStatTest.chmod("-r-w+x", "#{dir}/test/executable") do |
| 204 |
assert_false File::Stat.new("#{dir}/test/executable").readable_real? |
| 205 |
end |
| 206 |
end |
| 207 |
|
| 208 |
assert 'File::Stat#world_readable?' do |
| 209 |
skip "when windows" if FileStatTest.win? |
| 210 |
|
| 211 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 212 |
FileStatTest.system("chmod 0400 #{dir}/test/readable") |
| 213 |
assert_equal nil, File::Stat.new("#{dir}/test/readable").world_readable? |
| 214 |
FileStatTest.system("chmod 0444 #{dir}/test/readable") |
| 215 |
assert_equal 0444, File::Stat.new("#{dir}/test/readable").world_readable? |
| 216 |
end |
| 217 |
|
| 218 |
assert 'File::Stat#writable?' do |
| 219 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 220 |
FileStatTest.chmod("+r-w-x", "#{dir}/test/readable") do |
| 221 |
assert_false File::Stat.new("#{dir}/test/readable").writable? |
| 222 |
end |
| 223 |
FileStatTest.chmod("-r+w-x", "#{dir}/test/writable") do |
| 224 |
assert_true File::Stat.new("#{dir}/test/writable").writable? |
| 225 |
end |
| 226 |
FileStatTest.chmod("-r-w+x", "#{dir}/test/executable") do |
| 227 |
assert_false File::Stat.new("#{dir}/test/executable").writable? |
| 228 |
end |
| 229 |
end |
| 230 |
|
| 231 |
assert 'File::Stat#writable_real?' do |
| 232 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 233 |
FileStatTest.chmod("+r-w-x", "#{dir}/test/readable") do |
| 234 |
assert_false File::Stat.new("#{dir}/test/readable").writable_real? |
| 235 |
end |
| 236 |
FileStatTest.chmod("-r+w-x", "#{dir}/test/writable") do |
| 237 |
assert_true File::Stat.new("#{dir}/test/writable").writable_real? |
| 238 |
end |
| 239 |
FileStatTest.chmod("-r-w+x", "#{dir}/test/executable") do |
| 240 |
assert_false File::Stat.new("#{dir}/test/executable").writable_real? |
| 241 |
end |
| 242 |
end |
| 243 |
|
| 244 |
assert 'File::Stat#world_writable?' do |
| 245 |
skip "when windows" if FileStatTest.win? |
| 246 |
|
| 247 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 248 |
FileStatTest.chmod("0600", "#{dir}/test/writable") do |
| 249 |
assert_equal nil, File::Stat.new("#{dir}/test/writable").world_writable? |
| 250 |
end |
| 251 |
FileStatTest.chmod("0666", "#{dir}/test/writable") do |
| 252 |
assert_equal 0666, File::Stat.new("#{dir}/test/writable").world_writable? |
| 253 |
end |
| 254 |
end |
| 255 |
|
| 256 |
assert 'File::Stat#executable?' do |
| 257 |
skip "when windows" if FileStatTest.win? |
| 258 |
|
| 259 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 260 |
FileStatTest.chmod("+r-w-x", "#{dir}/test/readable") do |
| 261 |
assert_false File::Stat.new("#{dir}/test/readable").executable? |
| 262 |
end |
| 263 |
FileStatTest.chmod("-r+w-x", "#{dir}/test/writable") do |
| 264 |
assert_false File::Stat.new("#{dir}/test/writable").executable? |
| 265 |
end |
| 266 |
FileStatTest.chmod("-r-w+x", "#{dir}/test/executable") do |
| 267 |
assert_true File::Stat.new("#{dir}/test/executable").executable? |
| 268 |
end |
| 269 |
end |
| 270 |
|
| 271 |
assert 'File::Stat#executable_real?' do |
| 272 |
skip "when windows" if FileStatTest.win? |
| 273 |
|
| 274 |
dir = __FILE__[0..-18] # 18 = /test/file-stat.rb |
| 275 |
FileStatTest.chmod("+r-w-x", "#{dir}/test/readable") do |
| 276 |
assert_false File::Stat.new("#{dir}/test/readable").executable_real? |
| 277 |
end |
| 278 |
FileStatTest.chmod("-r+w-x", "#{dir}/test/writable") do |
| 279 |
assert_false File::Stat.new("#{dir}/test/writable").executable_real? |
| 280 |
end |
| 281 |
FileStatTest.chmod("-r-w+x", "#{dir}/test/executable") do |
| 282 |
assert_true File::Stat.new("#{dir}/test/executable").executable_real? |
| 283 |
end |
| 284 |
end |
| 285 |
|
| 286 |
assert 'File::Stat#file?' do |
| 287 |
stat = File::Stat.new('README.md') |
| 288 |
assert_true stat.file? |
| 289 |
|
| 290 |
stat = File::Stat.new('bin') |
| 291 |
assert_false stat.file? |
| 292 |
end |
| 293 |
|
| 294 |
assert 'File::Stat#zero?' do |
| 295 |
stat = File::Stat.new('README.md') |
| 296 |
assert_false stat.zero? |
| 297 |
end |
| 298 |
|
| 299 |
assert 'File::Stat#size?' do |
| 300 |
stat = File::Stat.new('README.md') |
| 301 |
assert_true 0 < stat.size? |
| 302 |
end |
| 303 |
|
| 304 |
assert 'File::Stat#size with large file (2GB)' do |
| 305 |
large_file = "test_large_file.tmp" |
| 306 |
target_size = 2**31 # 2GB |
| 307 |
begin |
| 308 |
File.open(large_file, 'wb') do |f| |
| 309 |
f.seek(target_size - 1) |
| 310 |
f.write("\0") |
| 311 |
end |
| 312 |
stat = File::Stat.new(large_file) |
| 313 |
assert_equal target_size, stat.size |
| 314 |
ensure |
| 315 |
File.delete(large_file) if File.exist?(large_file) |
| 316 |
end |
| 317 |
end |
| 318 |
|
| 319 |
assert 'File::Stat#owned?' do |
| 320 |
stat = File::Stat.new('README.md') |
| 321 |
assert_true stat.owned? |
| 322 |
end |
| 323 |
|
| 324 |
assert 'File::Stat#owned_real?' do |
| 325 |
stat = File::Stat.new('README.md') |
| 326 |
assert_true stat.owned_real? |
| 327 |
end |
| 328 |
|
| 329 |
assert 'File::Stat#grpowned?' do |
| 330 |
is_unix = File::Stat.new('/dev/tty') rescue false |
| 331 |
if is_unix |
| 332 |
stat = File::Stat.new('README.md') |
| 333 |
assert_true stat.grpowned? |
| 334 |
else |
| 335 |
skip "is not supported" |
| 336 |
end |
| 337 |
end |
| 338 |
|
| 339 |
assert 'File::Stat#pipe?' do |
| 340 |
stat = File::Stat.new('README.md') |
| 341 |
assert_false stat.pipe? |
| 342 |
end |
| 343 |
|
| 344 |
assert 'File::Stat#symlink?' do |
| 345 |
stat = File::Stat.new('README.md') |
| 346 |
assert_false stat.symlink? |
| 347 |
end |
| 348 |
|
| 349 |
assert 'File::Stat#socket?' do |
| 350 |
stat = File::Stat.new('README.md') |
| 351 |
assert_false stat.socket? |
| 352 |
end |
| 353 |
|
| 354 |
assert 'File::Stat#blockdev?' do |
| 355 |
stat = File::Stat.new('README.md') |
| 356 |
assert_false stat.blockdev? |
| 357 |
end |
| 358 |
|
| 359 |
assert 'File::Stat#chardev?' do |
| 360 |
stat = File::Stat.new('README.md') |
| 361 |
assert_false stat.chardev? |
| 362 |
|
| 363 |
begin |
| 364 |
stat = File::Stat.new('/dev/tty') |
| 365 |
assert_true stat.chardev? |
| 366 |
rescue RuntimeError |
| 367 |
skip '/dev/tty is not found' |
| 368 |
end |
| 369 |
end |
| 370 |
|
| 371 |
assert 'File::Stat#setuid?' do |
| 372 |
stat = File::Stat.new('README.md') |
| 373 |
assert_false stat.setuid? |
| 374 |
end |
| 375 |
|
| 376 |
assert 'File::Stat#setgid?' do |
| 377 |
stat = File::Stat.new('README.md') |
| 378 |
assert_false stat.setgid? |
| 379 |
end |
| 380 |
|
| 381 |
assert 'File::Stat#sticky?' do |
| 382 |
stat = File::Stat.new('README.md') |
| 383 |
assert_false stat.sticky? |
| 384 |
end |