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 | end
|
---|
55 |
|
---|
56 | assert('File#flock') do
|
---|
57 | f = File.open $mrbtest_io_rfname
|
---|
58 | begin
|
---|
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
|
---|
64 | skip e.message
|
---|
65 | ensure
|
---|
66 | f.close
|
---|
67 | end
|
---|
68 | end
|
---|
69 |
|
---|
70 | assert('File#mtime') do
|
---|
71 | begin
|
---|
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
|
---|
76 | end
|
---|
77 | end
|
---|
78 | ensure
|
---|
79 | File.delete("#{$mrbtest_io_wfname}.mtime")
|
---|
80 | end
|
---|
81 | end
|
---|
82 |
|
---|
83 | assert('File#size and File#truncate') do
|
---|
84 | fname = "#{$mrbtest_io_wfname}.resize"
|
---|
85 | begin
|
---|
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
|
---|
90 | assert_equal 0, f.pos
|
---|
91 | assert_equal 0, f.truncate(5)
|
---|
92 | assert_equal 5, f.size
|
---|
93 | end
|
---|
94 | ensure
|
---|
95 | File.delete(fname)
|
---|
96 | end
|
---|
97 | end
|
---|
98 |
|
---|
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"]])
|
---|
110 | end
|
---|
111 |
|
---|
112 | assert('File.realpath') do
|
---|
113 | if File::ALT_SEPARATOR
|
---|
114 | readme_path = File._getwd + File::ALT_SEPARATOR + "README.md"
|
---|
115 | assert_equal readme_path, File.realpath("README.md")
|
---|
116 | else
|
---|
117 | dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
|
---|
118 | begin
|
---|
119 | dir1 = File.realpath($mrbtest_io_rfname)
|
---|
120 | dir2 = File.realpath("./#{dir}//./../#{$mrbtest_io_symlinkname}")
|
---|
121 | assert_equal dir1, dir2
|
---|
122 | ensure
|
---|
123 | MRubyIOTestUtil.rmdir dir
|
---|
124 | end
|
---|
125 | end
|
---|
126 |
|
---|
127 | assert_raise(ArgumentError) { File.realpath("TO\0DO") }
|
---|
128 | end
|
---|
129 |
|
---|
130 | assert("File.readlink") do
|
---|
131 | begin
|
---|
132 | assert_equal $mrbtest_io_rfname, File.readlink($mrbtest_io_symlinkname)
|
---|
133 | rescue NotImplementedError => e
|
---|
134 | skip e.message
|
---|
135 | end
|
---|
136 | end
|
---|
137 |
|
---|
138 | assert("File.readlink fails with non-symlink") do
|
---|
139 | skip "readlink is not supported on this platform" if MRubyIOTestUtil.win?
|
---|
140 | begin
|
---|
141 | e2 = nil
|
---|
142 | assert_raise(RuntimeError) {
|
---|
143 | begin
|
---|
144 | File.readlink($mrbtest_io_rfname)
|
---|
145 | rescue => e
|
---|
146 | if Object.const_defined?(:SystemCallError) and e.kind_of?(SystemCallError)
|
---|
147 | raise RuntimeError, "SystemCallError converted to RuntimeError"
|
---|
148 | end
|
---|
149 | raise e
|
---|
150 | rescue NotImplementedError => e
|
---|
151 | e2 = e
|
---|
152 | end
|
---|
153 | }
|
---|
154 | raise e2 if e2
|
---|
155 | rescue NotImplementedError => e
|
---|
156 | skip e.message
|
---|
157 | end
|
---|
158 | end
|
---|
159 |
|
---|
160 | assert('File.expand_path') do
|
---|
161 | assert_equal "/", File.expand_path("..", "/tmp"), "parent path with base_dir (1)"
|
---|
162 | assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)"
|
---|
163 |
|
---|
164 | assert_equal "/home", File.expand_path("/home"), "absolute"
|
---|
165 | assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir"
|
---|
166 |
|
---|
167 | assert_equal "/hoge", File.expand_path("/tmp/..//hoge")
|
---|
168 | assert_equal "/hoge", File.expand_path("////tmp/..///////hoge")
|
---|
169 |
|
---|
170 | assert_equal "/", File.expand_path("../../../..", "/")
|
---|
171 | if File._getwd[1] == ":"
|
---|
172 | drive_letter = File._getwd[0]
|
---|
173 | assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/"))
|
---|
174 | else
|
---|
175 | assert_equal "/", File.expand_path(([".."] * 100).join("/"))
|
---|
176 | end
|
---|
177 | end
|
---|
178 |
|
---|
179 | assert('File.expand_path (with ENV)') do
|
---|
180 | skip unless Object.const_defined?(:ENV) && ENV['HOME']
|
---|
181 |
|
---|
182 | assert_equal ENV['HOME'], File.expand_path("~/"), "home"
|
---|
183 | assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir"
|
---|
184 |
|
---|
185 | assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
|
---|
186 | end
|
---|
187 |
|
---|
188 | assert('File.path') do
|
---|
189 | assert_equal "", File.path("")
|
---|
190 | assert_equal "a/b/c", File.path("a/b/c")
|
---|
191 | assert_equal "a/../b/./c", File.path("a/../b/./c")
|
---|
192 | assert_raise(TypeError) { File.path(nil) }
|
---|
193 | assert_raise(TypeError) { File.path(123) }
|
---|
194 | end
|
---|
195 |
|
---|
196 | assert('File.symlink') do
|
---|
197 | target_name = "/usr/bin"
|
---|
198 | if !File.exist?(target_name)
|
---|
199 | skip("target directory of File.symlink is not found")
|
---|
200 | end
|
---|
201 |
|
---|
202 | begin
|
---|
203 | tmpdir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
|
---|
204 | rescue => e
|
---|
205 | skip e.message
|
---|
206 | end
|
---|
207 |
|
---|
208 | symlink_name = "#{tmpdir}/test-bin-dummy"
|
---|
209 | begin
|
---|
210 | assert_equal 0, File.symlink(target_name, symlink_name)
|
---|
211 | assert_equal true, File.symlink?(symlink_name)
|
---|
212 | rescue NotImplementedError => e
|
---|
213 | skip e.message
|
---|
214 | ensure
|
---|
215 | File.delete symlink_name rescue nil
|
---|
216 | MRubyIOTestUtil.rmdir tmpdir rescue nil
|
---|
217 | end
|
---|
218 | end
|
---|
219 |
|
---|
220 | assert('File.chmod') do
|
---|
221 | File.open("#{$mrbtest_io_wfname}.chmod-test", 'w') {}
|
---|
222 | begin
|
---|
223 | assert_equal 1, File.chmod(0400, "#{$mrbtest_io_wfname}.chmod-test")
|
---|
224 | ensure
|
---|
225 | File.delete("#{$mrbtest_io_wfname}.chmod-test")
|
---|
226 | end
|
---|
227 | end
|
---|
228 |
|
---|
229 | MRubyIOTestUtil.io_test_cleanup
|
---|