Add 'mrbgems/mruby-io/' from commit '3c8e1f94c44252c836f79a48bb17726da28e2756'
[mruby.git] / mrbgems / mruby-io / test / file.rb
blob941e91ac468e7d382d8766f17f787b9c14a226b3
1 ##
2 # IO Test
4 assert('File', '15.2.21') do
5   File.class == Class
6 end
8 assert('File', '15.2.21.2') do
9   File.superclass == IO
10 end
12 assert('File TEST SETUP') do
13   MRubyIOTestUtil.io_test_setup
14 end
16 assert('File#initialize', '15.2.21.4.1') do
17   io = File.open($mrbtest_io_rfname, "r")
18   assert_nil io.close
19   assert_raise IOError do
20     io.close
21   end
22 end
24 assert('File#path', '15.2.21.4.2') do
25   io = File.open($mrbtest_io_rfname, "r")
26   assert_equal $mrbtest_io_msg, io.read
27   assert_equal $mrbtest_io_rfname, io.path
28   io.close
29   assert_equal $mrbtest_io_rfname, io.path
30   io.closed?
31 end
33 assert('File.basename') do
34   assert_equal '/', File.basename('//')
35   assert_equal 'a', File.basename('/a/')
36   assert_equal 'b', File.basename('/a/b')
37   assert_equal 'b', File.basename('../a/b')
38 end
40 assert('File.dirname') do
41   assert_equal '.',    File.dirname('')
42   assert_equal '.',    File.dirname('a')
43   assert_equal '/',    File.dirname('/a')
44   assert_equal 'a',    File.dirname('a/b')
45   assert_equal '/a',   File.dirname('/a/b')
46 end
48 assert('File.extname') do
49   assert_equal '.txt', File.extname('foo/foo.txt')
50   assert_equal '.gz',  File.extname('foo/foo.tar.gz')
51   assert_equal '', File.extname('foo/bar')
52   assert_equal '', File.extname('foo/.bar')
53   assert_equal '', File.extname('foo.txt/bar')
54   assert_equal '', File.extname('.foo')
55 end
57 assert('IO#flock') do
58   f = File.open $mrbtest_io_rfname
59   begin
60     assert_equal(f.flock(File::LOCK_SH), 0)
61     assert_equal(f.flock(File::LOCK_UN), 0)
62     assert_equal(f.flock(File::LOCK_EX | File::LOCK_NB), 0)
63     assert_equal(f.flock(File::LOCK_UN), 0)
64   rescue NotImplementedError => e
65     skip e.message
66   ensure
67     f.close
68   end
69 end
71 assert('File.join') do
72   assert_equal "", File.join()
73   assert_equal "a", File.join("a")
74   assert_equal "/a", File.join("/a")
75   assert_equal "a/", File.join("a/")
76   assert_equal "a/b/c", File.join("a", "b", "c")
77   assert_equal "/a/b/c", File.join("/a", "b", "c")
78   assert_equal "a/b/c/", File.join("a", "b", "c/")
79   assert_equal "a/b/c", File.join("a/", "/b/", "/c")
80   assert_equal "a/b/c", File.join(["a", "b", "c"])
81   assert_equal "a/b/c", File.join("a", ["b", ["c"]])
82 end
84 assert('File.realpath') do
85   if File::ALT_SEPARATOR
86     readme_path = File._getwd + File::ALT_SEPARATOR + "README.md"
87     assert_equal readme_path, File.realpath("README.md")
88   else
89     dir = MRubyIOTestUtil.mkdtemp("mruby-io-test.XXXXXX")
90     begin
91       dir1 = File.realpath($mrbtest_io_rfname)
92       dir2 = File.realpath("./#{dir}//./../#{$mrbtest_io_symlinkname}")
93       assert_equal dir1, dir2
94     ensure
95       MRubyIOTestUtil.rmdir dir
96     end
97   end
98 end
100 assert("File.readlink") do
101   begin
102     assert_equal $mrbtest_io_rfname, File.readlink($mrbtest_io_symlinkname)
103   rescue NotImplementedError => e
104     skip e.message
105   end
108 assert("File.readlink fails with non-symlink") do
109   begin
110     assert_raise(RuntimeError) {
111       begin
112         File.readlink($mrbtest_io_rfname)
113       rescue => e
114         if Object.const_defined?(:SystemCallError) and e.kind_of?(SystemCallError)
115           raise RuntimeError, "SystemCallError converted to RuntimeError"
116         end
117         raise e
118       end
119     }
120   rescue NotImplementedError => e
121     skip e.message
122   end
125 assert('File TEST CLEANUP') do
126   assert_nil MRubyIOTestUtil.io_test_cleanup
129 assert('File.expand_path') do
130   assert_equal "/",    File.expand_path("..", "/tmp"),       "parent path with base_dir (1)"
131   assert_equal "/tmp", File.expand_path("..", "/tmp/mruby"), "parent path with base_dir (2)"
133   assert_equal "/home", File.expand_path("/home"),      "absolute"
134   assert_equal "/home", File.expand_path("/home", "."), "absolute with base_dir"
136   assert_equal "/hoge", File.expand_path("/tmp/..//hoge")
137   assert_equal "/hoge", File.expand_path("////tmp/..///////hoge")
139   assert_equal "/", File.expand_path("../../../..", "/")
140   if File._getwd[1] == ":"
141     drive_letter = File._getwd[0]
142     assert_equal drive_letter + ":\\", File.expand_path(([".."] * 100).join("/"))
143   else
144     assert_equal "/", File.expand_path(([".."] * 100).join("/"))
145   end
148 assert('File.expand_path (with ENV)') do
149   skip unless Object.const_defined?(:ENV) && ENV['HOME']
151   assert_equal ENV['HOME'], File.expand_path("~/"),      "home"
152   assert_equal ENV['HOME'], File.expand_path("~/", "/"), "home with base_dir"
154   assert_equal "#{ENV['HOME']}/user", File.expand_path("user", ENV['HOME']), "relative with base_dir"
157 assert('File.path') do
158   assert_equal "", File.path("")
159   assert_equal "a/b/c", File.path("a/b/c")
160   assert_equal "a/../b/./c", File.path("a/../b/./c")
161   assert_raise(TypeError) { File.path(nil) }
162   assert_raise(TypeError) { File.path(123) }
166 assert('File.symlink') do
167   target_name = "/usr/bin"
168   symlink_name = "test-bin-dummy"
169   if !File.exist?(target_name)
170     skip("target directory of File.symlink is not found")
171   else
172     assert_equal 0, File.symlink(target_name, symlink_name)
173     begin
174       assert_equal true, File.symlink?(symlink_name)
175     ensure
176       File.delete symlink_name
177     end
178   end
181 assert('File.chmod') do
182   File.open('chmod-test', 'w') {}
183   begin
184     assert_equal 1, File.chmod(0400, 'chmod-test')
185   ensure
186     File.delete('chmod-test')
187   end