4 MRubyIOTestUtil.io_test_setup
6 assert('File.class', '15.2.21') do
7 assert_equal Class, File.class
10 assert('File.superclass', '15.2.21.2') do
11 assert_equal IO, File.superclass
14 assert('File#initialize', '15.2.21.4.1') do
15 io = File.open($mrbtest_io_rfname, "r")
17 assert_raise IOError do
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
27 assert_equal $mrbtest_io_rfname, io.path
28 assert_true io.closed?
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") }
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')
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')
56 assert('File#flock') do
57 f = File.open $mrbtest_io_rfname
59 assert_equal(f.flock(File::LOCK_SH), 0)
60 assert_equal(f.flock(File::LOCK_UN), 0)
61 assert_equal(f.flock(File::LOCK_EX | File::LOCK_NB), 0)
62 assert_equal(f.flock(File::LOCK_UN), 0)
63 rescue NotImplementedError => e
70 assert('File#mtime') do
72 File.open("#{$mrbtest_io_wfname}.mtime", 'w') do |f|
73 assert_equal Time, f.mtime.class
74 File.open("#{$mrbtest_io_wfname}.mtime", 'r') do |f2|
75 assert_equal true, f.mtime == f2.mtime
79 File.delete("#{$mrbtest_io_wfname}.mtime")
83 assert('File#size and File#truncate') do
84 fname = "#{$mrbtest_io_wfname}.resize"
86 File.open(fname, 'w') do |f|
87 assert_equal 0, f.size
88 assert_equal 0, f.truncate(100)
89 assert_equal 100, f.size
91 assert_equal 0, f.truncate(5)
92 assert_equal 5, f.size
99 assert('File.join') do
100 assert_equal "", File.join()
101 assert_equal "a", File.join("a")
102 assert_equal "/a", File.join("/a")
103 assert_equal "a/", File.join("a/")
104 assert_equal "a/b/c", File.join("a", "b", "c")
105 assert_equal "/a/b/c", File.join("/a", "b", "c")
106 assert_equal "a/b/c/", File.join("a", "b", "c/")
107 assert_equal "a/b/c", File.join("a/", "/b/", "/c")
108 assert_equal "a/b/c", File.join(["a", "b", "c"])
109 assert_equal "a/b/c", File.join("a", ["b", ["c"]])
112 assert('File.realpath') do
113 dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
115 sep = File::ALT_SEPARATOR || File::SEPARATOR
116 relative_path = "#{File.basename(dir)}#{sep}realpath_test"
117 path = "#{File._getwd}#{sep}#{relative_path}"
118 File.open(path, "w"){}
119 assert_equal path, File.realpath(relative_path)
121 unless MRubyIOTestUtil.win?
122 path1 = File.realpath($mrbtest_io_rfname)
123 path2 = File.realpath($mrbtest_io_symlinkname)
124 assert_equal path1, path2
127 File.delete path rescue nil
128 MRubyIOTestUtil.rmdir dir
131 assert_raise(ArgumentError) { File.realpath("TO\0DO") }
134 assert("File.readlink") do
136 exp = File.basename($mrbtest_io_rfname)
137 act = File.readlink($mrbtest_io_symlinkname)
138 assert_equal exp, act
139 rescue NotImplementedError => e
144 assert("File.readlink fails with non-symlink") do
145 skip "readlink is not supported on this platform" if MRubyIOTestUtil.win?
148 assert_raise(RuntimeError) {
150 File.readlink($mrbtest_io_rfname)
152 if Object.const_defined?(:SystemCallError) and e.kind_of?(SystemCallError)
153 raise RuntimeError, "SystemCallError converted to RuntimeError"
156 rescue NotImplementedError => e
161 rescue NotImplementedError => e
166 assert('File.expand_path') do
167 assert_equal "/", File.expand_path("..", "/tmp"), "parent path with base_dir (1)"
168 assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)"
170 assert_equal "/home", File.expand_path("/home"), "absolute"
171 assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir"
173 assert_equal "/hoge", File.expand_path("/tmp/..//hoge")
174 assert_equal "/hoge", File.expand_path("////tmp/..///////hoge")
176 assert_equal "/", File.expand_path("../../../..", "/")
177 if File._getwd[1] == ":"
178 drive_letter = File._getwd[0]
179 assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/"))
181 assert_equal "/", File.expand_path(([".."] * 100).join("/"))
185 assert('File.expand_path (with ENV)') do
186 skip unless Object.const_defined?(:ENV) && ENV['HOME']
188 assert_equal ENV['HOME'], File.expand_path("~/"), "home"
189 assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir"
191 assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
194 assert('File.path') do
195 assert_equal "", File.path("")
196 assert_equal "a/b/c", File.path("a/b/c")
197 assert_equal "a/../b/./c", File.path("a/../b/./c")
198 assert_raise(TypeError) { File.path(nil) }
199 assert_raise(TypeError) { File.path(123) }
202 assert('File.symlink') do
203 target_name = "/usr/bin"
204 if !File.exist?(target_name)
205 skip("target directory of File.symlink is not found")
209 tmpdir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
214 symlink_name = "#{tmpdir}/test-bin-dummy"
216 assert_equal 0, File.symlink(target_name, symlink_name)
217 assert_equal true, File.symlink?(symlink_name)
218 rescue NotImplementedError => e
221 File.delete symlink_name rescue nil
222 MRubyIOTestUtil.rmdir tmpdir rescue nil
226 assert('File.chmod') do
227 File.open("#{$mrbtest_io_wfname}.chmod-test", 'w') {}
229 assert_equal 1, File.chmod(0400, "#{$mrbtest_io_wfname}.chmod-test")
231 File.delete("#{$mrbtest_io_wfname}.chmod-test")
235 MRubyIOTestUtil.io_test_cleanup