PageRenderTime 48ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/rake/test_filelist.rb

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