1 # frozen_string_literal: true
6 class TestFind < Test::Unit::TestCase
10 Find.find(d) {|f| a << f }
16 bug12087 = '[ruby-dev:49497] [Bug #12087]'
19 re = /#{Regexp.quote(path)}\z/
20 assert_raise_with_message(Errno::ENOENT, re, bug12087) {
28 File.open("#{d}/a", "w"){}
30 File.open("#{d}/b/a", "w"){}
31 File.open("#{d}/b/b", "w"){}
34 Find.find(d) {|f| a << f }
35 assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
41 File.open("#{d}/a", "w"){}
43 File.open("#{d}/b/a", "w"){}
44 File.open("#{d}/b/b", "w"){}
48 Find.find(".") {|f| a << f }
50 assert_equal([".", "./a", "./b", "./b/a", "./b/b", "./c"], a)
54 def test_dont_follow_symlink
56 File.open("#{d}/a", "w"){}
58 File.open("#{d}/b/a", "w"){}
59 File.open("#{d}/b/b", "w"){}
61 File.symlink("#{d}/b", "#{d}/c")
62 rescue NotImplementedError, Errno::EACCES
63 omit "symlink is not supported."
66 Find.find(d) {|f| a << f }
67 assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
73 File.open("#{d}/a", "w"){}
75 File.open("#{d}/b/a", "w"){}
76 File.open("#{d}/b/b", "w"){}
81 Find.prune if f == "#{d}/b"
83 assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/c"], a)
89 1.upto(3) {|n| File.open("#{d}/#{n}", "w"){} }
91 Find.find(d) {|f| a << f }
92 assert_equal([d, "#{d}/1", "#{d}/2", "#{d}/3"], a)
98 3.downto(1) {|n| File.open("#{d}/#{n}", "w"){} }
100 Find.find(d) {|f| a << f }
101 assert_equal([d, "#{d}/1", "#{d}/2", "#{d}/3"], a)
105 def test_unreadable_dir
106 omit "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
107 omit "because root can read anything" if Process.uid == 0
110 Dir.mkdir(dir = "#{d}/dir")
111 File.open("#{dir}/foo", "w"){}
113 File.chmod(0300, dir)
115 Find.find(d) {|f| a << f }
116 assert_equal([d, dir], a)
119 Find.find(d, ignore_error: true) {|f| a << f }
120 assert_equal([d, dir], a)
123 Find.find(d, ignore_error: true).each {|f| a << f }
124 assert_equal([d, dir], a)
127 assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(dir)}/) do
128 Find.find(d, ignore_error: false) {|f| a << f }
130 assert_equal([d, dir], a)
133 assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(dir)}/) do
134 Find.find(d, ignore_error: false).each {|f| a << f }
136 assert_equal([d, dir], a)
138 File.chmod(0700, dir)
143 def test_unsearchable_dir
145 Dir.mkdir(dir = "#{d}/dir")
146 File.open(file = "#{dir}/foo", "w"){}
148 File.chmod(0600, dir)
150 Find.find(d) {|f| a << f }
151 assert_equal([d, dir, file], a)
154 Find.find(d, ignore_error: true) {|f| a << f }
155 assert_equal([d, dir, file], a)
158 Find.find(d, ignore_error: true).each {|f| a << f }
159 assert_equal([d, dir, file], a)
161 omit "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
162 omit "skipped because root can read anything" if Process.uid == 0
165 assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(file)}/) do
166 Find.find(d, ignore_error: false) {|f| a << f }
168 assert_equal([d, dir, file], a)
171 assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(file)}/) do
172 Find.find(d, ignore_error: false).each {|f| a << f }
174 assert_equal([d, dir, file], a)
176 assert_raise(Errno::EACCES) { File.lstat(file) }
178 File.chmod(0700, dir)
183 def test_dangling_symlink
186 File.symlink("foo", "#{d}/bar")
187 rescue NotImplementedError, Errno::EACCES
188 omit "symlink is not supported."
191 Find.find(d) {|f| a << f }
192 assert_equal([d, "#{d}/bar"], a)
193 assert_raise(Errno::ENOENT) { File.stat("#{d}/bar") }
197 def test_dangling_symlink_stat_error
200 File.symlink("foo", "#{d}/bar")
201 rescue NotImplementedError, Errno::EACCES
202 omit "symlink is not supported."
204 assert_raise(Errno::ENOENT) {
205 Find.find(d) {|f| File.stat(f) }
210 def test_change_dir_to_file
212 Dir.mkdir(dir_1 = "#{d}/d1")
213 File.open(file_a = "#{dir_1}/a", "w"){}
214 File.open(file_b = "#{dir_1}/b", "w"){}
215 File.open(file_c = "#{dir_1}/c", "w"){}
216 Dir.mkdir(dir_d = "#{dir_1}/d")
217 File.open("#{dir_d}/e", "w"){}
223 File.rename(dir_1, dir_2)
224 File.open(dir_1, "w") {}
227 assert_equal([d, dir_1, file_a, file_b, file_c, dir_d], a)
231 def test_change_dir_to_symlink_loop
233 Dir.mkdir(dir_1 = "#{d}/d1")
234 File.open(file_a = "#{dir_1}/a", "w"){}
235 File.open(file_b = "#{dir_1}/b", "w"){}
236 File.open(file_c = "#{dir_1}/c", "w"){}
237 Dir.mkdir(dir_d = "#{dir_1}/d")
238 File.open("#{dir_d}/e", "w"){}
244 File.rename(dir_1, dir_2)
246 File.symlink("d1", dir_1)
247 rescue NotImplementedError, Errno::EACCES
248 omit "symlink is not supported."
252 assert_equal([d, dir_1, file_a, file_b, file_c, dir_d], a)
258 File.open("#{d}/a", "w"){}
260 File.open("#{d}/b/a", "w"){}
261 File.open("#{d}/b/b", "w"){}
266 assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
270 def test_encoding_ascii
272 File.open("#{d}/a", "w"){}
275 Find.find(d.encode(Encoding::US_ASCII)) {|f| a << f }
277 assert(Encoding.compatible?(d.encode(Encoding.find('filesystem')), i))
282 def test_encoding_non_ascii
284 File.open("#{d}/a", "w"){}
286 euc_jp = Encoding::EUC_JP
287 win_31j = Encoding::Windows_31J
288 utf_8 = Encoding::UTF_8
290 Find.find(d.encode(euc_jp), d.encode(win_31j), d.encode(utf_8)) {|f| a << [f, f.encoding] }
291 assert_equal([[d, euc_jp], ["#{d}/a", euc_jp], ["#{d}/b", euc_jp],
292 [d, win_31j], ["#{d}/a", win_31j], ["#{d}/b", win_31j],
293 [d, utf_8], ["#{d}/a", utf_8], ["#{d}/b", utf_8]],
295 if /mswin|mingw/ =~ RUBY_PLATFORM
297 Dir.mkdir("#{d}/\u{2660}")
298 Find.find("#{d}".encode(utf_8)) {|f| a << [f, f.encoding] }
299 assert_equal([[d, utf_8], ["#{d}/a", utf_8], ["#{d}/b", utf_8], ["#{d}/\u{2660}", utf_8]], a)
316 Find.find(c.new(d)) {|f| a << f }
321 class TestInclude < Test::Unit::TestCase
324 def test_functional_call
326 File.open("#{d}/a", "w"){}
328 find(d) {|f| a << f }
329 assert_equal([d, "#{d}/a"], a)