| 1 |
## |
| 2 |
# File Test |
| 3 |
|
| 4 |
MRubyIOTestUtil.io_test_setup |
| 5 |
|
| 6 |
assert('File.class', '15.2.21') do |
| 7 |
assert_equal Class, File.class |
| 8 |
end |
| 9 |
|
| 10 |
assert('File.superclass', '15.2.21.2') do |
| 11 |
assert_equal IO, File.superclass |
| 12 |
end |
| 13 |
|
| 14 |
assert('File#initialize', '15.2.21.4.1') do |
| 15 |
io = File.open($mrbtest_io_rfname, "r") |
| 16 |
assert_nil io.close |
| 17 |
assert_raise IOError do |
| 18 |
io.close |
| 19 |
end |
| 20 |
end |
| 21 |
|
| 22 |
assert('File#path', '15.2.21.4.2') do |
| 23 |
io = File.open($mrbtest_io_rfname, "r") |
| 24 |
assert_equal $mrbtest_io_msg, io.read |
| 25 |
assert_equal $mrbtest_io_rfname, io.path |
| 26 |
io.close |
| 27 |
assert_equal $mrbtest_io_rfname, io.path |
| 28 |
assert_true io.closed? |
| 29 |
end |
| 30 |
|
| 31 |
assert('File.basename') do |
| 32 |
assert_equal '/', File.basename('//') |
| 33 |
assert_equal 'a', File.basename('/a/') |
| 34 |
assert_equal 'b', File.basename('/a/b') |
| 35 |
assert_equal 'b', File.basename('../a/b') |
| 36 |
assert_raise(ArgumentError) { File.basename("/a/b\0") } |
| 37 |
end |
| 38 |
|
| 39 |
assert('File.dirname') do |
| 40 |
assert_equal '.', File.dirname('') |
| 41 |
assert_equal '.', File.dirname('a') |
| 42 |
assert_equal '/', File.dirname('/a') |
| 43 |
assert_equal 'a', File.dirname('a/b') |
| 44 |
assert_equal '/a', File.dirname('/a/b') |
| 45 |
end |
| 46 |
|
| 47 |
assert('File.extname') do |
| 48 |
assert_equal '.txt', File.extname('foo/foo.txt') |
| 49 |
assert_equal '.gz', File.extname('foo/foo.tar.gz') |
| 50 |
assert_equal '', File.extname('foo/bar') |
| 51 |
assert_equal '', File.extname('foo/.bar') |
| 52 |
assert_equal '', File.extname('foo.txt/bar') |
| 53 |
assert_equal '', File.extname('.foo') |
| 54 |
assert_equal '.rb', File.extname('.a.rb') |
| 55 |
assert_equal '.', File.extname('foo.') |
| 56 |
end |
| 57 |
|
| 58 |
assert('File#flock') do |
| 59 |
f = File.open $mrbtest_io_rfname |
| 60 |
begin |
| 61 |
assert_equal(f.flock(File::LOCK_SH), 0) |
| 62 |
assert_equal(f.flock(File::LOCK_UN), 0) |
| 63 |
assert_equal(f.flock(File::LOCK_EX | File::LOCK_NB), 0) |
| 64 |
assert_equal(f.flock(File::LOCK_UN), 0) |
| 65 |
rescue NotImplementedError => e |
| 66 |
skip e.message |
| 67 |
ensure |
| 68 |
f.close |
| 69 |
end |
| 70 |
end |
| 71 |
|
| 72 |
assert('File#atime') do |
| 73 |
begin |
| 74 |
File.open("#{$mrbtest_io_wfname}.atime", 'w') do |f| |
| 75 |
assert_equal Time, f.mtime.class |
| 76 |
File.open("#{$mrbtest_io_wfname}.atime", 'r') do |f2| |
| 77 |
assert_equal true, f.atime == f2.atime |
| 78 |
end |
| 79 |
end |
| 80 |
ensure |
| 81 |
File.delete("#{$mrbtest_io_wfname}.atime") |
| 82 |
end |
| 83 |
end |
| 84 |
|
| 85 |
assert('File#ctime') do |
| 86 |
begin |
| 87 |
File.open("#{$mrbtest_io_wfname}.ctime", 'w') do |f| |
| 88 |
assert_equal Time, f.ctime.class |
| 89 |
File.open("#{$mrbtest_io_wfname}.ctime", 'r') do |f2| |
| 90 |
assert_equal true, f.ctime == f2.ctime |
| 91 |
end |
| 92 |
end |
| 93 |
ensure |
| 94 |
File.delete("#{$mrbtest_io_wfname}.ctime") |
| 95 |
end |
| 96 |
end |
| 97 |
|
| 98 |
assert('File#mtime') do |
| 99 |
begin |
| 100 |
File.open("#{$mrbtest_io_wfname}.mtime", 'w') do |f| |
| 101 |
assert_equal Time, f.mtime.class |
| 102 |
File.open("#{$mrbtest_io_wfname}.mtime", 'r') do |f2| |
| 103 |
assert_equal true, f.mtime == f2.mtime |
| 104 |
end |
| 105 |
end |
| 106 |
ensure |
| 107 |
File.delete("#{$mrbtest_io_wfname}.mtime") |
| 108 |
end |
| 109 |
end |
| 110 |
|
| 111 |
assert('File#size and File#truncate') do |
| 112 |
fname = "#{$mrbtest_io_wfname}.resize" |
| 113 |
begin |
| 114 |
File.open(fname, 'w') do |f| |
| 115 |
assert_equal 0, f.size |
| 116 |
assert_equal 0, f.truncate(100) |
| 117 |
assert_equal 100, f.size |
| 118 |
assert_equal 0, f.pos |
| 119 |
assert_equal 0, f.truncate(5) |
| 120 |
assert_equal 5, f.size |
| 121 |
end |
| 122 |
ensure |
| 123 |
File.delete(fname) |
| 124 |
end |
| 125 |
end |
| 126 |
|
| 127 |
assert('File.join') do |
| 128 |
assert_equal "", File.join() |
| 129 |
assert_equal "a", File.join("a") |
| 130 |
assert_equal "/a", File.join("/a") |
| 131 |
assert_equal "a/", File.join("a/") |
| 132 |
assert_equal "a/b/c", File.join("a", "b", "c") |
| 133 |
assert_equal "/a/b/c", File.join("/a", "b", "c") |
| 134 |
assert_equal "a/b/c/", File.join("a", "b", "c/") |
| 135 |
assert_equal "a/b/c", File.join("a/", "/b/", "/c") |
| 136 |
assert_equal "a/b/c", File.join(["a", "b", "c"]) |
| 137 |
assert_equal "a/b/c", File.join("a", ["b", ["c"]]) |
| 138 |
end |
| 139 |
|
| 140 |
assert('File.realpath') do |
| 141 |
dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX") |
| 142 |
begin |
| 143 |
sep = File::ALT_SEPARATOR || File::SEPARATOR |
| 144 |
relative_path = "#{File.basename(dir)}#{sep}realpath_test" |
| 145 |
path = "#{File._getwd}#{sep}#{relative_path}" |
| 146 |
File.open(path, "w"){} |
| 147 |
assert_equal path, File.realpath(relative_path) |
| 148 |
|
| 149 |
unless MRubyIOTestUtil.win? |
| 150 |
path1 = File.realpath($mrbtest_io_rfname) |
| 151 |
path2 = File.realpath($mrbtest_io_symlinkname) |
| 152 |
assert_equal path1, path2 |
| 153 |
end |
| 154 |
ensure |
| 155 |
File.delete path rescue nil |
| 156 |
MRubyIOTestUtil.rmdir dir |
| 157 |
end |
| 158 |
|
| 159 |
assert_raise(ArgumentError) { File.realpath("TO\0DO") } |
| 160 |
end |
| 161 |
|
| 162 |
assert("File.readlink") do |
| 163 |
begin |
| 164 |
exp = File.basename($mrbtest_io_rfname) |
| 165 |
act = File.readlink($mrbtest_io_symlinkname) |
| 166 |
assert_equal exp, act |
| 167 |
rescue NotImplementedError => e |
| 168 |
skip e.message |
| 169 |
end |
| 170 |
end |
| 171 |
|
| 172 |
assert("File.readlink fails with non-symlink") do |
| 173 |
skip "readlink is not supported on this platform" if MRubyIOTestUtil.win? |
| 174 |
begin |
| 175 |
e2 = nil |
| 176 |
assert_raise(RuntimeError) { |
| 177 |
begin |
| 178 |
File.readlink($mrbtest_io_rfname) |
| 179 |
rescue => e |
| 180 |
if Object.const_defined?(:SystemCallError) and e.kind_of?(SystemCallError) |
| 181 |
raise RuntimeError, "SystemCallError converted to RuntimeError" |
| 182 |
end |
| 183 |
raise e |
| 184 |
rescue NotImplementedError => e |
| 185 |
e2 = e |
| 186 |
end |
| 187 |
} |
| 188 |
raise e2 if e2 |
| 189 |
rescue NotImplementedError => e |
| 190 |
skip e.message |
| 191 |
end |
| 192 |
end |
| 193 |
|
| 194 |
assert('File.expand_path') do |
| 195 |
assert_equal "/", File.expand_path("..", "/tmp"), "parent path with base_dir (1)" |
| 196 |
assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)" |
| 197 |
|
| 198 |
assert_equal "/home", File.expand_path("/home"), "absolute" |
| 199 |
assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir" |
| 200 |
|
| 201 |
assert_equal "/hoge", File.expand_path("/tmp/..//hoge") |
| 202 |
assert_equal "/hoge", File.expand_path("////tmp/..///////hoge") |
| 203 |
|
| 204 |
assert_equal "/", File.expand_path("../../../..", "/") |
| 205 |
if File._getwd[1] == ":" |
| 206 |
drive_letter = File._getwd[0] |
| 207 |
assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/")) |
| 208 |
else |
| 209 |
assert_equal "/", File.expand_path(([".."] * 100).join("/")) |
| 210 |
end |
| 211 |
end |
| 212 |
|
| 213 |
assert('File.expand_path (with ENV)') do |
| 214 |
skip unless Object.const_defined?(:ENV) && ENV['HOME'] |
| 215 |
|
| 216 |
assert_equal ENV['HOME'], File.expand_path("~/"), "home" |
| 217 |
assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir" |
| 218 |
|
| 219 |
assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir" |
| 220 |
end |
| 221 |
|
| 222 |
assert('File.path') do |
| 223 |
assert_equal "", File.path("") |
| 224 |
assert_equal "a/b/c", File.path("a/b/c") |
| 225 |
assert_equal "a/../b/./c", File.path("a/../b/./c") |
| 226 |
assert_raise(TypeError) { File.path(nil) } |
| 227 |
assert_raise(TypeError) { File.path(123) } |
| 228 |
end |
| 229 |
|
| 230 |
assert('File.symlink') do |
| 231 |
target_name = "/usr/bin" |
| 232 |
if !File.exist?(target_name) |
| 233 |
skip("target directory of File.symlink is not found") |
| 234 |
end |
| 235 |
|
| 236 |
begin |
| 237 |
tmpdir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX") |
| 238 |
rescue => e |
| 239 |
skip e.message |
| 240 |
end |
| 241 |
|
| 242 |
symlink_name = "#{tmpdir}/test-bin-dummy" |
| 243 |
begin |
| 244 |
assert_equal 0, File.symlink(target_name, symlink_name) |
| 245 |
assert_equal true, File.symlink?(symlink_name) |
| 246 |
rescue NotImplementedError => e |
| 247 |
skip e.message |
| 248 |
ensure |
| 249 |
File.delete symlink_name rescue nil |
| 250 |
MRubyIOTestUtil.rmdir tmpdir rescue nil |
| 251 |
end |
| 252 |
end |
| 253 |
|
| 254 |
assert('File.chmod') do |
| 255 |
File.open("#{$mrbtest_io_wfname}.chmod-test", 'w') {} |
| 256 |
begin |
| 257 |
assert_equal 1, File.chmod(0400, "#{$mrbtest_io_wfname}.chmod-test") |
| 258 |
ensure |
| 259 |
File.delete("#{$mrbtest_io_wfname}.chmod-test") |
| 260 |
end |
| 261 |
end |
| 262 |
|
| 263 |
MRubyIOTestUtil.io_test_cleanup |