PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Languages/Ruby/Tests/Libraries/Rake-0.8.7/test/test_filelist.rb

http://github.com/IronLanguages/main
Ruby | 623 lines | 527 code | 89 blank | 7 comment | 10 complexity | 57f7184c29473a2bb9b903622ccac031 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #!/usr/bin/env ruby
  2. require 'test/unit'
  3. require 'rake'
  4. require 'test/capture_stdout'
  5. require 'test/rake_test_setup'
  6. class TestFileList < Test::Unit::TestCase
  7. FileList = Rake::FileList
  8. include CaptureStdout
  9. include TestMethods
  10. def setup
  11. create_test_data
  12. end
  13. def teardown
  14. # FileList.select_default_ignore_patterns
  15. FileUtils.rm_rf("testdata")
  16. end
  17. def test_delgating_methods_do_not_include_to_a_or_to_ary
  18. assert ! FileList::DELEGATING_METHODS.include?("to_a"), "should not include to_a"
  19. assert ! FileList::DELEGATING_METHODS.include?(:to_a), "should not include to_a"
  20. assert ! FileList::DELEGATING_METHODS.include?("to_ary"), "should not include to_ary"
  21. assert ! FileList::DELEGATING_METHODS.include?(:to_ary), "should not include to_ary"
  22. end
  23. def test_create
  24. fl = FileList.new
  25. assert_equal 0, fl.size
  26. end
  27. def test_create_with_args
  28. fl = FileList.new("testdata/*.c", "x")
  29. assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
  30. fl.sort
  31. end
  32. def test_create_with_block
  33. fl = FileList.new { |f| f.include("x") }
  34. assert_equal ["x"], fl.resolve
  35. end
  36. def test_create_with_brackets
  37. fl = FileList["testdata/*.c", "x"]
  38. assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
  39. fl.sort
  40. end
  41. def test_create_with_brackets_and_filelist
  42. fl = FileList[FileList["testdata/*.c", "x"]]
  43. assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
  44. fl.sort
  45. end
  46. def test_include_with_another_array
  47. fl = FileList.new.include(["x", "y", "z"])
  48. assert_equal ["x", "y", "z"].sort, fl.sort
  49. end
  50. def test_include_with_another_filelist
  51. fl = FileList.new.include(FileList["testdata/*.c", "x"])
  52. assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
  53. fl.sort
  54. end
  55. def test_append
  56. fl = FileList.new
  57. fl << "a.rb" << "b.rb"
  58. assert_equal ['a.rb', 'b.rb'], fl
  59. end
  60. def test_add_many
  61. fl = FileList.new
  62. fl.include %w(a d c)
  63. fl.include('x', 'y')
  64. assert_equal ['a', 'd', 'c', 'x', 'y'], fl
  65. assert_equal ['a', 'd', 'c', 'x', 'y'], fl.resolve
  66. end
  67. def test_add_return
  68. f = FileList.new
  69. g = f << "x"
  70. assert_equal f.object_id, g.object_id
  71. h = f.include("y")
  72. assert_equal f.object_id, h.object_id
  73. end
  74. def test_match
  75. fl = FileList.new
  76. fl.include('test/test*.rb')
  77. assert fl.include?("test/test_filelist.rb")
  78. assert fl.size > 3
  79. fl.each { |fn| assert_match(/\.rb$/, fn) }
  80. end
  81. def test_add_matching
  82. fl = FileList.new
  83. fl << "a.java"
  84. fl.include("test/*.rb")
  85. assert_equal "a.java", fl[0]
  86. assert fl.size > 2
  87. assert fl.include?("test/test_filelist.rb")
  88. end
  89. def test_multiple_patterns
  90. create_test_data
  91. fl = FileList.new
  92. fl.include('*.c', '*xist*')
  93. assert_equal [], fl
  94. fl.include('testdata/*.c', 'testdata/*xist*')
  95. assert_equal [
  96. 'testdata/x.c', 'testdata/xyz.c', 'testdata/abc.c', 'testdata/existing'
  97. ].sort, fl.sort
  98. end
  99. def test_square_bracket_pattern
  100. fl = FileList.new
  101. fl.include("testdata/abc.[ch]")
  102. assert fl.size == 2
  103. assert fl.include?("testdata/abc.c")
  104. assert fl.include?("testdata/abc.h")
  105. end
  106. def test_curly_bracket_pattern
  107. fl = FileList.new
  108. fl.include("testdata/abc.{c,h}")
  109. assert fl.size == 2
  110. assert fl.include?("testdata/abc.c")
  111. assert fl.include?("testdata/abc.h")
  112. end
  113. def test_reject
  114. fl = FileList.new
  115. fl.include %w(testdata/x.c testdata/abc.c testdata/xyz.c testdata/existing)
  116. fl.reject! { |fn| fn =~ %r{/x} }
  117. assert_equal [
  118. 'testdata/abc.c', 'testdata/existing'
  119. ], fl
  120. end
  121. def test_exclude
  122. fl = FileList['testdata/x.c', 'testdata/abc.c', 'testdata/xyz.c', 'testdata/existing']
  123. fl.each { |fn| touch fn, :verbose => false }
  124. x = fl.exclude(%r{/x.+\.})
  125. assert_equal FileList, x.class
  126. assert_equal %w(testdata/x.c testdata/abc.c testdata/existing), fl
  127. assert_equal fl.object_id, x.object_id
  128. fl.exclude('testdata/*.c')
  129. assert_equal ['testdata/existing'], fl
  130. fl.exclude('testdata/existing')
  131. assert_equal [], fl
  132. end
  133. def test_excluding_via_block
  134. fl = FileList['testdata/a.c', 'testdata/b.c', 'testdata/xyz.c']
  135. fl.exclude { |fn| fn.pathmap('%n') == 'xyz' }
  136. assert fl.exclude?("xyz.c"), "Should exclude xyz.c"
  137. assert_equal ['testdata/a.c', 'testdata/b.c'], fl
  138. end
  139. def test_exclude_return_on_create
  140. fl = FileList['testdata/*'].exclude(/.*\.[hcx]$/)
  141. assert_equal ['testdata/existing', 'testdata/cfiles'].sort, fl.sort
  142. assert_equal FileList, fl.class
  143. end
  144. def test_exclude_with_string_return_on_create
  145. fl = FileList['testdata/*'].exclude('testdata/abc.c')
  146. assert_equal %w(testdata/existing testdata/cfiles testdata/x.c testdata/abc.h testdata/abc.x testdata/xyz.c).sort, fl.sort
  147. assert_equal FileList, fl.class
  148. end
  149. def test_default_exclude
  150. fl = FileList.new
  151. fl.clear_exclude
  152. fl.include("**/*~", "**/*.bak", "**/core")
  153. assert fl.member?("testdata/core"), "Should include core"
  154. assert fl.member?("testdata/x.bak"), "Should include .bak files"
  155. end
  156. def test_unique
  157. fl = FileList.new
  158. fl << "x.c" << "a.c" << "b.rb" << "a.c"
  159. assert_equal ['x.c', 'a.c', 'b.rb', 'a.c'], fl
  160. fl.uniq!
  161. assert_equal ['x.c', 'a.c', 'b.rb'], fl
  162. end
  163. def test_to_string
  164. fl = FileList.new
  165. fl << "a.java" << "b.java"
  166. assert_equal "a.java b.java", fl.to_s
  167. assert_equal "a.java b.java", "#{fl}"
  168. end
  169. def test_to_array
  170. fl = FileList['a.java', 'b.java']
  171. assert_equal ['a.java', 'b.java'], fl.to_a
  172. assert_equal Array, fl.to_a.class
  173. assert_equal ['a.java', 'b.java'], fl.to_ary
  174. assert_equal Array, fl.to_ary.class
  175. end
  176. def test_to_s_pending
  177. fl = FileList['testdata/abc.*']
  178. result = fl.to_s
  179. assert_match(%r{testdata/abc\.c}, result)
  180. assert_match(%r{testdata/abc\.h}, result)
  181. assert_match(%r{testdata/abc\.x}, result)
  182. assert_match(%r{(testdata/abc\..\b ?){2}}, result)
  183. end
  184. def test_inspect_pending
  185. fl = FileList['testdata/abc.*']
  186. result = fl.inspect
  187. assert_match(%r{"testdata/abc\.c"}, result)
  188. assert_match(%r{"testdata/abc\.h"}, result)
  189. assert_match(%r{"testdata/abc\.x"}, result)
  190. assert_match(%r|^\[("testdata/abc\..", ){2}"testdata/abc\.."\]$|, result)
  191. end
  192. def test_sub
  193. fl = FileList["testdata/*.c"]
  194. f2 = fl.sub(/\.c$/, ".o")
  195. assert_equal FileList, f2.class
  196. assert_equal ["testdata/abc.o", "testdata/x.o", "testdata/xyz.o"].sort,
  197. f2.sort
  198. f3 = fl.gsub(/\.c$/, ".o")
  199. assert_equal FileList, f3.class
  200. assert_equal ["testdata/abc.o", "testdata/x.o", "testdata/xyz.o"].sort,
  201. f3.sort
  202. end
  203. def test_claim_to_be_a_kind_of_array
  204. fl = FileList['testdata/*.c']
  205. assert fl.is_a?(Array)
  206. assert fl.kind_of?(Array)
  207. end
  208. def test_claim_to_be_a_kind_of_filelist
  209. fl = FileList['testdata/*.c']
  210. assert fl.is_a?(FileList)
  211. assert fl.kind_of?(FileList)
  212. end
  213. def test_claim_to_be_a_filelist_instance
  214. fl = FileList['testdata/*.c']
  215. assert fl.instance_of?(FileList)
  216. end
  217. def test_dont_claim_to_be_an_array_instance
  218. fl = FileList['testdata/*.c']
  219. assert ! fl.instance_of?(Array)
  220. end
  221. def test_sub!
  222. f = "x/a.c"
  223. fl = FileList[f, "x/b.c"]
  224. res = fl.sub!(/\.c$/, ".o")
  225. assert_equal ["x/a.o", "x/b.o"].sort, fl.sort
  226. assert_equal "x/a.c", f
  227. assert_equal fl.object_id, res.object_id
  228. end
  229. def test_sub_with_block
  230. fl = FileList["src/org/onestepback/a.java", "src/org/onestepback/b.java"]
  231. # The block version doesn't work the way I want it to ...
  232. # f2 = fl.sub(%r{^src/(.*)\.java$}) { |x| "classes/" + $1 + ".class" }
  233. f2 = fl.sub(%r{^src/(.*)\.java$}, "classes/\\1.class")
  234. assert_equal [
  235. "classes/org/onestepback/a.class",
  236. "classes/org/onestepback/b.class"
  237. ].sort,
  238. f2.sort
  239. end
  240. def test_string_ext
  241. assert_equal "one.net", "one.two".ext("net")
  242. assert_equal "one.net", "one.two".ext(".net")
  243. assert_equal "one.net", "one".ext("net")
  244. assert_equal "one.net", "one".ext(".net")
  245. assert_equal "one.two.net", "one.two.c".ext(".net")
  246. assert_equal "one/two.net", "one/two.c".ext(".net")
  247. assert_equal "one.x/two.net", "one.x/two.c".ext(".net")
  248. assert_equal "one.x/two.net", "one.x/two".ext(".net")
  249. assert_equal ".onerc.net", ".onerc.dot".ext("net")
  250. assert_equal ".onerc.net", ".onerc".ext("net")
  251. assert_equal ".a/.onerc.net", ".a/.onerc".ext("net")
  252. assert_equal "one", "one.two".ext('')
  253. assert_equal "one", "one.two".ext
  254. assert_equal ".one", ".one.two".ext
  255. assert_equal ".one", ".one".ext
  256. assert_equal ".", ".".ext("c")
  257. assert_equal "..", "..".ext("c")
  258. # These only need to work in windows
  259. if Rake::Win32.windows?
  260. assert_equal "one.x\\two.net", "one.x\\two.c".ext(".net")
  261. assert_equal "one.x\\two.net", "one.x\\two".ext(".net")
  262. end
  263. end
  264. def test_filelist_ext
  265. assert_equal FileList['one.c', '.one.c'],
  266. FileList['one.net', '.one'].ext('c')
  267. end
  268. def test_gsub
  269. create_test_data
  270. fl = FileList["testdata/*.c"]
  271. f2 = fl.gsub(/a/, "A")
  272. assert_equal ["testdAtA/Abc.c", "testdAtA/x.c", "testdAtA/xyz.c"].sort,
  273. f2.sort
  274. end
  275. def test_gsub!
  276. create_test_data
  277. f = FileList["testdata/*.c"]
  278. f.gsub!(/a/, "A")
  279. assert_equal ["testdAtA/Abc.c", "testdAtA/x.c", "testdAtA/xyz.c"].sort,
  280. f.sort
  281. end
  282. def test_egrep_with_output
  283. files = FileList['test/test*.rb']
  284. the_line_number = __LINE__ + 1
  285. out = capture_stdout do files.egrep(/PUGH/) end
  286. assert_match(/:#{the_line_number}:/, out)
  287. end
  288. def test_egrep_with_block
  289. files = FileList['test/test*.rb']
  290. found = false
  291. the_line_number = __LINE__ + 1
  292. files.egrep(/XYZZY/) do |fn, ln, line |
  293. assert_equal 'test/test_filelist.rb', fn
  294. assert_equal the_line_number, ln
  295. assert_match(/files\.egrep/, line)
  296. found = true
  297. end
  298. assert found, "should have found a matching line"
  299. end
  300. def test_existing
  301. fl = FileList['testdata/abc.c', 'testdata/notthere.c']
  302. assert_equal ["testdata/abc.c"], fl.existing
  303. assert fl.existing.is_a?(FileList)
  304. end
  305. def test_existing!
  306. fl = FileList['testdata/abc.c', 'testdata/notthere.c']
  307. result = fl.existing!
  308. assert_equal ["testdata/abc.c"], fl
  309. assert_equal fl.object_id, result.object_id
  310. end
  311. def test_ignore_special
  312. f = FileList['testdata/*']
  313. assert ! f.include?("testdata/CVS"), "Should not contain CVS"
  314. assert ! f.include?("testdata/.svn"), "Should not contain .svn"
  315. assert ! f.include?("testdata/.dummy"), "Should not contain dot files"
  316. assert ! f.include?("testdata/x.bak"), "Should not contain .bak files"
  317. assert ! f.include?("testdata/x~"), "Should not contain ~ files"
  318. assert ! f.include?("testdata/core"), "Should not contain core files"
  319. end
  320. def test_clear_ignore_patterns
  321. f = FileList['testdata/*', 'testdata/.svn']
  322. f.clear_exclude
  323. assert f.include?("testdata/abc.c")
  324. assert f.include?("testdata/xyz.c")
  325. assert f.include?("testdata/CVS")
  326. assert f.include?("testdata/.svn")
  327. assert f.include?("testdata/x.bak")
  328. assert f.include?("testdata/x~")
  329. end
  330. def test_exclude_with_alternate_file_seps
  331. fl = FileList.new
  332. assert fl.exclude?("x/CVS/y")
  333. assert fl.exclude?("x\\CVS\\y")
  334. assert fl.exclude?("x/.svn/y")
  335. assert fl.exclude?("x\\.svn\\y")
  336. assert fl.exclude?("x/core")
  337. assert fl.exclude?("x\\core")
  338. end
  339. def test_add_default_exclude_list
  340. fl = FileList.new
  341. fl.exclude(/~\d+$/)
  342. assert fl.exclude?("x/CVS/y")
  343. assert fl.exclude?("x\\CVS\\y")
  344. assert fl.exclude?("x/.svn/y")
  345. assert fl.exclude?("x\\.svn\\y")
  346. assert fl.exclude?("x/core")
  347. assert fl.exclude?("x\\core")
  348. assert fl.exclude?("x/abc~1")
  349. end
  350. def test_basic_array_functions
  351. f = FileList['b', 'c', 'a']
  352. assert_equal 'b', f.first
  353. assert_equal 'b', f[0]
  354. assert_equal 'a', f.last
  355. assert_equal 'a', f[2]
  356. assert_equal 'a', f[-1]
  357. assert_equal ['a', 'b', 'c'], f.sort
  358. f.sort!
  359. assert_equal ['a', 'b', 'c'], f
  360. end
  361. def test_flatten
  362. assert_equal ['a', 'testdata/x.c', 'testdata/xyz.c', 'testdata/abc.c'].sort,
  363. ['a', FileList['testdata/*.c']].flatten.sort
  364. end
  365. def test_clone_and_dup
  366. a = FileList['a', 'b', 'c']
  367. c = a.clone
  368. d = a.dup
  369. a << 'd'
  370. assert_equal ['a', 'b', 'c', 'd'], a
  371. assert_equal ['a', 'b', 'c'], c
  372. assert_equal ['a', 'b', 'c'], d
  373. end
  374. def test_dup_and_clone_replicate_taint
  375. a = FileList['a', 'b', 'c']
  376. a.taint
  377. c = a.clone
  378. d = a.dup
  379. assert c.tainted?, "Clone should be tainted"
  380. assert d.tainted?, "Dup should be tainted"
  381. end
  382. def test_duped_items_will_thaw
  383. a = FileList['a', 'b', 'c']
  384. a.freeze
  385. d = a.dup
  386. d << 'more'
  387. assert_equal ['a', 'b', 'c', 'more'], d
  388. end
  389. def test_cloned_items_stay_frozen
  390. a = FileList['a', 'b', 'c']
  391. a.freeze
  392. c = a.clone
  393. assert_exception(TypeError, RuntimeError) do
  394. c << 'more'
  395. end
  396. end
  397. def test_array_comparisons
  398. fl = FileList['b', 'b']
  399. a = ['b', 'a']
  400. b = ['b', 'b']
  401. c = ['b', 'c']
  402. assert_equal( 1, fl <=> a )
  403. assert_equal( 0, fl <=> b )
  404. assert_equal( -1, fl <=> c )
  405. assert_equal( -1, a <=> fl )
  406. assert_equal( 0, b <=> fl )
  407. assert_equal( 1, c <=> fl )
  408. end
  409. def test_array_equality
  410. a = FileList['a', 'b']
  411. b = ['a', 'b']
  412. assert a == b
  413. assert b == a
  414. # assert a.eql?(b)
  415. # assert b.eql?(a)
  416. assert ! a.equal?(b)
  417. assert ! b.equal?(a)
  418. end
  419. def test_enumeration_methods
  420. a = FileList['a', 'b']
  421. b = a.collect { |it| it.upcase }
  422. assert_equal ['A', 'B'], b
  423. assert_equal FileList, b.class
  424. b = a.map { |it| it.upcase }
  425. assert_equal ['A', 'B'], b
  426. assert_equal FileList, b.class
  427. b = a.sort
  428. assert_equal ['a', 'b'], b
  429. assert_equal FileList, b.class
  430. b = a.sort_by { |it| it }
  431. assert_equal ['a', 'b'], b
  432. assert_equal FileList, b.class
  433. b = a.find_all { |it| it == 'b'}
  434. assert_equal ['b'], b
  435. assert_equal FileList, b.class
  436. b = a.select { |it| it.size == 1 }
  437. assert_equal ['a', 'b'], b
  438. assert_equal FileList, b.class
  439. b = a.reject { |it| it == 'b' }
  440. assert_equal ['a'], b
  441. assert_equal FileList, b.class
  442. b = a.grep(/./)
  443. assert_equal ['a', 'b'], b
  444. assert_equal FileList, b.class
  445. b = a.partition { |it| it == 'b' }
  446. assert_equal [['b'], ['a']], b
  447. assert_equal Array, b.class
  448. assert_equal FileList, b[0].class
  449. assert_equal FileList, b[1].class
  450. b = a.zip(['x', 'y']).to_a
  451. assert_equal [['a', 'x'], ['b', 'y']], b
  452. assert_equal Array, b.class
  453. assert_equal Array, b[0].class
  454. assert_equal Array, b[1].class
  455. end
  456. def test_array_operators
  457. a = ['a', 'b']
  458. b = ['c', 'd']
  459. f = FileList['x', 'y']
  460. g = FileList['w', 'z']
  461. r = f + g
  462. assert_equal ['x', 'y', 'w', 'z'], r
  463. assert_equal FileList, r.class
  464. r = a + g
  465. assert_equal ['a', 'b', 'w', 'z'], r
  466. assert_equal Array, r.class
  467. r = f + b
  468. assert_equal ['x', 'y', 'c', 'd'], r
  469. assert_equal FileList, r.class
  470. r = FileList['w', 'x', 'y', 'z'] - f
  471. assert_equal ['w', 'z'], r
  472. assert_equal FileList, r.class
  473. r = FileList['w', 'x', 'y', 'z'] & f
  474. assert_equal ['x', 'y'], r
  475. assert_equal FileList, r.class
  476. r = f * 2
  477. assert_equal ['x', 'y', 'x', 'y'], r
  478. assert_equal FileList, r.class
  479. r = f * ','
  480. assert_equal 'x,y', r
  481. assert_equal String, r.class
  482. r = f | ['a', 'x']
  483. assert_equal ['a', 'x', 'y'].sort, r.sort
  484. assert_equal FileList, r.class
  485. end
  486. def test_other_array_returning_methods
  487. f = FileList['a', nil, 'b']
  488. r = f.compact
  489. assert_equal ['a', 'b'], r
  490. assert_equal FileList, r.class
  491. f = FileList['a', 'b']
  492. r = f.concat(['x', 'y'])
  493. assert_equal ['a', 'b', 'x', 'y'], r
  494. assert_equal FileList, r.class
  495. f = FileList['a', ['b', 'c'], FileList['d', 'e']]
  496. r = f.flatten
  497. assert_equal ['a', 'b', 'c', 'd', 'e'], r
  498. assert_equal FileList, r.class
  499. f = FileList['a', 'b', 'a']
  500. r = f.uniq
  501. assert_equal ['a', 'b'], r
  502. assert_equal FileList, r.class
  503. f = FileList['a', 'b', 'c', 'd']
  504. r = f.values_at(1,3)
  505. assert_equal ['b', 'd'], r
  506. assert_equal FileList, r.class
  507. end
  508. def test_file_utils_can_use_filelists
  509. cfiles = FileList['testdata/*.c']
  510. cp cfiles, @cdir, :verbose => false
  511. assert File.exist?(File.join(@cdir, 'abc.c'))
  512. assert File.exist?(File.join(@cdir, 'xyz.c'))
  513. assert File.exist?(File.join(@cdir, 'x.c'))
  514. end
  515. def create_test_data
  516. verbose(false) do
  517. mkdir "testdata" unless File.exist? "testdata"
  518. mkdir "testdata/CVS" rescue nil
  519. mkdir "testdata/.svn" rescue nil
  520. @cdir = "testdata/cfiles"
  521. mkdir @cdir rescue nil
  522. touch "testdata/.dummy"
  523. touch "testdata/x.bak"
  524. touch "testdata/x~"
  525. touch "testdata/core"
  526. touch "testdata/x.c"
  527. touch "testdata/xyz.c"
  528. touch "testdata/abc.c"
  529. touch "testdata/abc.h"
  530. touch "testdata/abc.x"
  531. touch "testdata/existing"
  532. end
  533. end
  534. end