PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/TDDBC_Yokohama2_PCUnit/Ruby/doc/ruby/ruby-1.9.2/test/test_find.rb

https://bitbucket.org/aetos/tddbc_yokohama2
Ruby | 226 lines | 209 code | 17 blank | 0 comment | 8 complexity | bf31f3d67827908f74ba69ab4159a4ee MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0, AGPL-3.0, 0BSD, Unlicense
  1. require 'test/unit'
  2. require 'find'
  3. require 'tmpdir'
  4. class TestFind < Test::Unit::TestCase
  5. def test_empty
  6. Dir.mktmpdir {|d|
  7. a = []
  8. Find.find(d) {|f| a << f }
  9. assert_equal([d], a)
  10. }
  11. end
  12. def test_rec
  13. Dir.mktmpdir {|d|
  14. File.open("#{d}/a", "w"){}
  15. Dir.mkdir("#{d}/b")
  16. File.open("#{d}/b/a", "w"){}
  17. File.open("#{d}/b/b", "w"){}
  18. Dir.mkdir("#{d}/c")
  19. a = []
  20. Find.find(d) {|f| a << f }
  21. assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
  22. }
  23. end
  24. def test_relative
  25. Dir.mktmpdir {|d|
  26. File.open("#{d}/a", "w"){}
  27. Dir.mkdir("#{d}/b")
  28. File.open("#{d}/b/a", "w"){}
  29. File.open("#{d}/b/b", "w"){}
  30. Dir.mkdir("#{d}/c")
  31. a = []
  32. Dir.chdir(d) {
  33. Find.find(".") {|f| a << f }
  34. }
  35. assert_equal([".", "./a", "./b", "./b/a", "./b/b", "./c"], a)
  36. }
  37. end
  38. def test_dont_follow_symlink
  39. Dir.mktmpdir {|d|
  40. File.open("#{d}/a", "w"){}
  41. Dir.mkdir("#{d}/b")
  42. File.open("#{d}/b/a", "w"){}
  43. File.open("#{d}/b/b", "w"){}
  44. begin
  45. File.symlink("#{d}/b", "#{d}/c")
  46. rescue NotImplementedError
  47. skip "symlink is not supported."
  48. end
  49. a = []
  50. Find.find(d) {|f| a << f }
  51. assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
  52. }
  53. end
  54. def test_prune
  55. Dir.mktmpdir {|d|
  56. File.open("#{d}/a", "w"){}
  57. Dir.mkdir("#{d}/b")
  58. File.open("#{d}/b/a", "w"){}
  59. File.open("#{d}/b/b", "w"){}
  60. Dir.mkdir("#{d}/c")
  61. a = []
  62. Find.find(d) {|f|
  63. a << f
  64. Find.prune if f == "#{d}/b"
  65. }
  66. assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/c"], a)
  67. }
  68. end
  69. def test_countup3
  70. Dir.mktmpdir {|d|
  71. 1.upto(3) {|n| File.open("#{d}/#{n}", "w"){} }
  72. a = []
  73. Find.find(d) {|f| a << f }
  74. assert_equal([d, "#{d}/1", "#{d}/2", "#{d}/3"], a)
  75. }
  76. end
  77. def test_countdown3
  78. Dir.mktmpdir {|d|
  79. 3.downto(1) {|n| File.open("#{d}/#{n}", "w"){} }
  80. a = []
  81. Find.find(d) {|f| a << f }
  82. assert_equal([d, "#{d}/1", "#{d}/2", "#{d}/3"], a)
  83. }
  84. end
  85. def test_unreadable_dir
  86. skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
  87. Dir.mktmpdir {|d|
  88. Dir.mkdir(dir = "#{d}/dir")
  89. File.open(file = "#{dir}/foo", "w"){}
  90. begin
  91. File.chmod(0300, dir)
  92. a = []
  93. Find.find(d) {|f| a << f }
  94. assert_equal([d, dir], a)
  95. ensure
  96. File.chmod(0700, dir)
  97. end
  98. }
  99. end
  100. def test_unsearchable_dir
  101. Dir.mktmpdir {|d|
  102. Dir.mkdir(dir = "#{d}/dir")
  103. File.open(file = "#{dir}/foo", "w"){}
  104. begin
  105. File.chmod(0600, dir)
  106. a = []
  107. Find.find(d) {|f| a << f }
  108. assert_equal([d, dir, file], a)
  109. skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
  110. assert_raise(Errno::EACCES) { File.lstat(file) }
  111. ensure
  112. File.chmod(0700, dir)
  113. end
  114. }
  115. end
  116. def test_dangling_symlink
  117. Dir.mktmpdir {|d|
  118. begin
  119. File.symlink("foo", "#{d}/bar")
  120. rescue NotImplementedError
  121. skip "symlink is not supported."
  122. end
  123. a = []
  124. Find.find(d) {|f| a << f }
  125. assert_equal([d, "#{d}/bar"], a)
  126. assert_raise(Errno::ENOENT) { File.stat("#{d}/bar") }
  127. }
  128. end
  129. def test_dangling_symlink_stat_error
  130. Dir.mktmpdir {|d|
  131. begin
  132. File.symlink("foo", "#{d}/bar")
  133. rescue NotImplementedError
  134. skip "symlink is not supported."
  135. end
  136. assert_raise(Errno::ENOENT) {
  137. Find.find(d) {|f| File.stat(f) }
  138. }
  139. }
  140. end
  141. def test_change_dir_to_file
  142. Dir.mktmpdir {|d|
  143. Dir.mkdir(dir_1 = "#{d}/d1")
  144. File.open(file_a = "#{dir_1}/a", "w"){}
  145. File.open(file_b = "#{dir_1}/b", "w"){}
  146. File.open(file_c = "#{dir_1}/c", "w"){}
  147. Dir.mkdir(dir_d = "#{dir_1}/d")
  148. File.open(file_de = "#{dir_d}/e", "w"){}
  149. dir_2 = "#{d}/d2"
  150. a = []
  151. Find.find(d) {|f|
  152. a << f
  153. if f == file_b
  154. File.rename(dir_1, dir_2)
  155. File.open(dir_1, "w") {}
  156. end
  157. }
  158. assert_equal([d, dir_1, file_a, file_b, file_c, dir_d], a)
  159. }
  160. end
  161. def test_change_dir_to_symlink_loop
  162. Dir.mktmpdir {|d|
  163. Dir.mkdir(dir_1 = "#{d}/d1")
  164. File.open(file_a = "#{dir_1}/a", "w"){}
  165. File.open(file_b = "#{dir_1}/b", "w"){}
  166. File.open(file_c = "#{dir_1}/c", "w"){}
  167. Dir.mkdir(dir_d = "#{dir_1}/d")
  168. File.open(file_de = "#{dir_d}/e", "w"){}
  169. dir_2 = "#{d}/d2"
  170. a = []
  171. Find.find(d) {|f|
  172. a << f
  173. if f == file_b
  174. File.rename(dir_1, dir_2)
  175. begin
  176. File.symlink("d1", dir_1)
  177. rescue NotImplementedError
  178. skip "symlink is not supported."
  179. end
  180. end
  181. }
  182. assert_equal([d, dir_1, file_a, file_b, file_c, dir_d], a)
  183. }
  184. end
  185. def test_enumerator
  186. Dir.mktmpdir {|d|
  187. File.open("#{d}/a", "w"){}
  188. Dir.mkdir("#{d}/b")
  189. File.open("#{d}/b/a", "w"){}
  190. File.open("#{d}/b/b", "w"){}
  191. Dir.mkdir("#{d}/c")
  192. e = Find.find(d)
  193. a = []
  194. e.each {|f| a << f }
  195. assert_equal([d, "#{d}/a", "#{d}/b", "#{d}/b/a", "#{d}/b/b", "#{d}/c"], a)
  196. }
  197. end
  198. class TestInclude < Test::Unit::TestCase
  199. include Find
  200. def test_functional_call
  201. Dir.mktmpdir {|d|
  202. File.open("#{d}/a", "w"){}
  203. a = []
  204. find(d) {|f| a << f }
  205. assert_equal([d, "#{d}/a"], a)
  206. }
  207. end
  208. end
  209. end