2009-12-13 11:00:33 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'find'
|
|
|
|
require 'tmpdir'
|
|
|
|
|
|
|
|
class TestFind < Test::Unit::TestCase
|
|
|
|
def test_empty
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rec
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/a", "w"){}
|
2009-12-13 11:00:33 -05:00
|
|
|
Dir.mkdir("#{d}/b")
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/b/a", "w"){}
|
|
|
|
File.open("#{d}/b/b", "w"){}
|
2009-12-13 11:00:33 -05:00
|
|
|
Dir.mkdir("#{d}/c")
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-14 06:56:03 -05:00
|
|
|
def test_relative
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/a", "w"){}
|
2009-12-14 06:56:03 -05:00
|
|
|
Dir.mkdir("#{d}/b")
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/b/a", "w"){}
|
|
|
|
File.open("#{d}/b/b", "w"){}
|
2009-12-14 06:56:03 -05:00
|
|
|
Dir.mkdir("#{d}/c")
|
|
|
|
a = []
|
|
|
|
Dir.chdir(d) {
|
|
|
|
Find.find(".") {|f| a << f }
|
|
|
|
}
|
|
|
|
assert_equal([".", "./a", "./b", "./b/a", "./b/b", "./c"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_dont_follow_symlink
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/a", "w"){}
|
2009-12-14 06:56:03 -05:00
|
|
|
Dir.mkdir("#{d}/b")
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/b/a", "w"){}
|
|
|
|
File.open("#{d}/b/b", "w"){}
|
|
|
|
begin
|
|
|
|
File.symlink("#{d}/b", "#{d}/c")
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip "symlink is not supported."
|
|
|
|
end
|
2009-12-14 06:56:03 -05:00
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-13 11:00:33 -05:00
|
|
|
def test_prune
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/a", "w"){}
|
2009-12-13 11:00:33 -05:00
|
|
|
Dir.mkdir("#{d}/b")
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/b/a", "w"){}
|
|
|
|
File.open("#{d}/b/b", "w"){}
|
2009-12-13 11:00:33 -05:00
|
|
|
Dir.mkdir("#{d}/c")
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f|
|
|
|
|
a << f
|
|
|
|
Find.prune if f == "#{d}/b"
|
|
|
|
}
|
|
|
|
assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/c"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_countup3
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
1.upto(3) {|n| File.open("#{d}/#{n}", "w"){} }
|
2009-12-13 11:00:33 -05:00
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/1", "#{d}/2", "#{d}/3"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_countdown3
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
3.downto(1) {|n| File.open("#{d}/#{n}", "w"){} }
|
2009-12-13 11:00:33 -05:00
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/1", "#{d}/2", "#{d}/3"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unreadable_dir
|
2010-05-25 03:03:31 -04:00
|
|
|
skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-12-13 11:00:33 -05:00
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
Dir.mkdir(dir = "#{d}/dir")
|
2014-02-16 21:39:58 -05:00
|
|
|
File.open("#{dir}/foo", "w"){}
|
2009-12-13 11:00:33 -05:00
|
|
|
begin
|
|
|
|
File.chmod(0300, dir)
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, dir], a)
|
|
|
|
ensure
|
|
|
|
File.chmod(0700, dir)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unsearchable_dir
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
Dir.mkdir(dir = "#{d}/dir")
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open(file = "#{dir}/foo", "w"){}
|
2009-12-13 11:00:33 -05:00
|
|
|
begin
|
|
|
|
File.chmod(0600, dir)
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, dir, file], a)
|
2010-05-25 03:03:31 -04:00
|
|
|
skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
|
2009-12-13 11:00:33 -05:00
|
|
|
assert_raise(Errno::EACCES) { File.lstat(file) }
|
|
|
|
ensure
|
|
|
|
File.chmod(0700, dir)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-14 06:56:03 -05:00
|
|
|
def test_dangling_symlink
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
begin
|
|
|
|
File.symlink("foo", "#{d}/bar")
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip "symlink is not supported."
|
|
|
|
end
|
2009-12-14 06:56:03 -05:00
|
|
|
a = []
|
|
|
|
Find.find(d) {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/bar"], a)
|
|
|
|
assert_raise(Errno::ENOENT) { File.stat("#{d}/bar") }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-14 07:50:13 -05:00
|
|
|
def test_dangling_symlink_stat_error
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
begin
|
|
|
|
File.symlink("foo", "#{d}/bar")
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip "symlink is not supported."
|
|
|
|
end
|
2009-12-14 07:50:13 -05:00
|
|
|
assert_raise(Errno::ENOENT) {
|
|
|
|
Find.find(d) {|f| File.stat(f) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-15 12:21:09 -05:00
|
|
|
def test_change_dir_to_file
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
Dir.mkdir(dir_1 = "#{d}/d1")
|
|
|
|
File.open(file_a = "#{dir_1}/a", "w"){}
|
|
|
|
File.open(file_b = "#{dir_1}/b", "w"){}
|
|
|
|
File.open(file_c = "#{dir_1}/c", "w"){}
|
|
|
|
Dir.mkdir(dir_d = "#{dir_1}/d")
|
2014-02-16 21:39:58 -05:00
|
|
|
File.open("#{dir_d}/e", "w"){}
|
2009-12-15 12:21:09 -05:00
|
|
|
dir_2 = "#{d}/d2"
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f|
|
|
|
|
a << f
|
|
|
|
if f == file_b
|
|
|
|
File.rename(dir_1, dir_2)
|
|
|
|
File.open(dir_1, "w") {}
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_equal([d, dir_1, file_a, file_b, file_c, dir_d], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_change_dir_to_symlink_loop
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
Dir.mkdir(dir_1 = "#{d}/d1")
|
|
|
|
File.open(file_a = "#{dir_1}/a", "w"){}
|
|
|
|
File.open(file_b = "#{dir_1}/b", "w"){}
|
|
|
|
File.open(file_c = "#{dir_1}/c", "w"){}
|
|
|
|
Dir.mkdir(dir_d = "#{dir_1}/d")
|
2014-02-16 21:39:58 -05:00
|
|
|
File.open("#{dir_d}/e", "w"){}
|
2009-12-15 12:21:09 -05:00
|
|
|
dir_2 = "#{d}/d2"
|
|
|
|
a = []
|
|
|
|
Find.find(d) {|f|
|
|
|
|
a << f
|
|
|
|
if f == file_b
|
|
|
|
File.rename(dir_1, dir_2)
|
|
|
|
begin
|
|
|
|
File.symlink("d1", dir_1)
|
|
|
|
rescue NotImplementedError
|
|
|
|
skip "symlink is not supported."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assert_equal([d, dir_1, file_a, file_b, file_c, dir_d], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-14 06:56:03 -05:00
|
|
|
def test_enumerator
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/a", "w"){}
|
2009-12-14 06:56:03 -05:00
|
|
|
Dir.mkdir("#{d}/b")
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/b/a", "w"){}
|
|
|
|
File.open("#{d}/b/b", "w"){}
|
2009-12-14 06:56:03 -05:00
|
|
|
Dir.mkdir("#{d}/c")
|
|
|
|
e = Find.find(d)
|
|
|
|
a = []
|
|
|
|
e.each {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-09-07 00:34:27 -04:00
|
|
|
def test_encoding_ascii
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
File.open("#{d}/a", "w"){}
|
|
|
|
Dir.mkdir("#{d}/b")
|
|
|
|
a = []
|
|
|
|
Find.find(d.encode(Encoding::US_ASCII)) {|f| a << f }
|
|
|
|
a.each do |i|
|
|
|
|
assert(Encoding.compatible?(d.encode(Encoding.find('filesystem')), i))
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_encoding_non_ascii
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
File.open("#{d}/a", "w"){}
|
|
|
|
Dir.mkdir("#{d}/b")
|
|
|
|
euc_jp = Encoding::EUC_JP
|
|
|
|
win_31j = Encoding::Windows_31J
|
|
|
|
utf_8 = Encoding::UTF_8
|
|
|
|
a = []
|
|
|
|
Find.find(d.encode(euc_jp), d.encode(win_31j), d.encode(utf_8)) {|f| a << [f, f.encoding] }
|
|
|
|
assert_equal([[d, euc_jp], ["#{d}/a", euc_jp], ["#{d}/b", euc_jp],
|
|
|
|
[d, win_31j], ["#{d}/a", win_31j], ["#{d}/b", win_31j],
|
|
|
|
[d, utf_8], ["#{d}/a", utf_8], ["#{d}/b", utf_8]],
|
|
|
|
a)
|
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
a = []
|
|
|
|
Dir.mkdir("#{d}/\u{2660}")
|
|
|
|
Find.find("#{d}".encode(utf_8)) {|f| a << [f, f.encoding] }
|
|
|
|
assert_equal([[d, utf_8], ["#{d}/a", utf_8], ["#{d}/b", utf_8], ["#{d}/\u{2660}", utf_8]], a)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-12-13 17:48:13 -05:00
|
|
|
class TestInclude < Test::Unit::TestCase
|
|
|
|
include Find
|
|
|
|
|
|
|
|
def test_functional_call
|
|
|
|
Dir.mktmpdir {|d|
|
2009-12-14 19:16:48 -05:00
|
|
|
File.open("#{d}/a", "w"){}
|
2009-12-13 17:48:13 -05:00
|
|
|
a = []
|
|
|
|
find(d) {|f| a << f }
|
|
|
|
assert_equal([d, "#{d}/a"], a)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-13 11:00:33 -05:00
|
|
|
end
|