PageRenderTime 61ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/jruby-1.1.6RC1/lib/ruby/gems/1.8/gems/rake-0.8.3/test/test_application.rb

https://bitbucket.org/nicksieger/advent-jruby
Ruby | 694 lines | 606 code | 82 blank | 6 comment | 5 complexity | b490e9bd10855fe34a64730a44a2607d MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON
  1. #!/usr/bin/env ruby
  2. begin
  3. require 'rubygems'
  4. rescue LoadError
  5. # got no gems
  6. end
  7. require 'test/unit'
  8. require 'rake'
  9. require 'test/rake_test_setup'
  10. require 'test/capture_stdout'
  11. require 'test/in_environment'
  12. TESTING_REQUIRE = [ ]
  13. ######################################################################
  14. class TestApplication < Test::Unit::TestCase
  15. include CaptureStdout
  16. include InEnvironment
  17. def setup
  18. @app = Rake::Application.new
  19. @app.options.rakelib = []
  20. end
  21. def test_constant_warning
  22. err = capture_stderr do @app.instance_eval { const_warning("Task") } end
  23. assert_match(/warning/i, err)
  24. assert_match(/deprecated/i, err)
  25. assert_match(/Task/i, err)
  26. end
  27. def test_display_tasks
  28. @app.options.show_task_pattern = //
  29. @app.last_description = "COMMENT"
  30. @app.define_task(Rake::Task, "t")
  31. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  32. assert_match(/^rake t/, out)
  33. assert_match(/# COMMENT/, out)
  34. end
  35. def test_display_tasks_with_long_comments
  36. in_environment('RAKE_COLUMNS' => '80') do
  37. @app.options.show_task_pattern = //
  38. @app.last_description = "1234567890" * 8
  39. @app.define_task(Rake::Task, "t")
  40. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  41. assert_match(/^rake t/, out)
  42. assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
  43. end
  44. end
  45. def test_display_tasks_with_task_name_wider_than_tty_display
  46. in_environment('RAKE_COLUMNS' => '80') do
  47. @app.options.show_task_pattern = //
  48. description = "something short"
  49. task_name = "task name" * 80
  50. @app.last_description = "something short"
  51. @app.define_task(Rake::Task, task_name )
  52. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  53. # Ensure the entire task name is output and we end up showing no description
  54. assert_match(/rake #{task_name} # .../, out)
  55. end
  56. end
  57. def test_display_tasks_with_very_long_task_name_to_a_non_tty_shows_name_and_comment
  58. @app.options.show_task_pattern = //
  59. @app.tty_output = false
  60. description = "something short"
  61. task_name = "task name" * 80
  62. @app.last_description = "something short"
  63. @app.define_task(Rake::Task, task_name )
  64. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  65. # Ensure the entire task name is output and we end up showing no description
  66. assert_match(/rake #{task_name} # #{description}/, out)
  67. end
  68. def test_display_tasks_with_long_comments_to_a_non_tty_shows_entire_comment
  69. @app.options.show_task_pattern = //
  70. @app.tty_output = false
  71. @app.last_description = "1234567890" * 8
  72. @app.define_task(Rake::Task, "t")
  73. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  74. assert_match(/^rake t/, out)
  75. assert_match(/# #{@app.last_description}/, out)
  76. end
  77. def test_display_tasks_with_long_comments_to_a_non_tty_with_columns_set_truncates_comments
  78. in_environment("RAKE_COLUMNS" => '80') do
  79. @app.options.show_task_pattern = //
  80. @app.tty_output = false
  81. @app.last_description = "1234567890" * 8
  82. @app.define_task(Rake::Task, "t")
  83. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  84. assert_match(/^rake t/, out)
  85. assert_match(/# 12345678901234567890123456789012345678901234567890123456789012345\.\.\./, out)
  86. end
  87. end
  88. def test_display_tasks_with_full_descriptions
  89. @app.options.show_task_pattern = //
  90. @app.options.full_description = true
  91. @app.last_description = "COMMENT"
  92. @app.define_task(Rake::Task, "t")
  93. out = capture_stdout do @app.instance_eval { display_tasks_and_comments } end
  94. assert_match(/^rake t$/, out)
  95. assert_match(/^ {4}COMMENT$/, out)
  96. end
  97. def test_finding_rakefile
  98. assert_match(/[Rr]akefile/, @app.instance_eval { have_rakefile })
  99. end
  100. def test_not_finding_rakefile
  101. @app.instance_eval { @rakefiles = ['NEVER_FOUND'] }
  102. assert( ! @app.instance_eval do have_rakefile end )
  103. assert_nil @app.rakefile
  104. end
  105. def test_load_rakefile
  106. in_environment("PWD" => "test/data/unittest") do
  107. @app.instance_eval do
  108. handle_options
  109. options.silent = true
  110. load_rakefile
  111. end
  112. assert_equal "rakefile", @app.rakefile.downcase
  113. assert_match(%r(unittest$), Dir.pwd)
  114. end
  115. end
  116. def test_load_rakefile_from_subdir
  117. in_environment("PWD" => "test/data/unittest/subdir") do
  118. @app.instance_eval do
  119. handle_options
  120. options.silent = true
  121. load_rakefile
  122. end
  123. assert_equal "rakefile", @app.rakefile.downcase
  124. assert_match(%r(unittest$), Dir.pwd)
  125. end
  126. end
  127. def test_load_rakefile_not_found
  128. in_environment("PWD" => "/", "RAKE_SYSTEM" => 'not_exist') do
  129. @app.instance_eval do
  130. handle_options
  131. options.silent = true
  132. end
  133. ex = assert_raise(RuntimeError) do
  134. @app.instance_eval do raw_load_rakefile end
  135. end
  136. assert_match(/no rakefile found/i, ex.message)
  137. end
  138. end
  139. def test_load_from_system_rakefile
  140. in_environment('RAKE_SYSTEM' => 'test/data/sys') do
  141. @app.options.rakelib = []
  142. @app.instance_eval do
  143. handle_options
  144. options.silent = true
  145. options.load_system = true
  146. load_rakefile
  147. end
  148. assert_equal "test/data/sys", @app.system_dir
  149. assert_nil @app.rakefile
  150. end
  151. end
  152. def test_load_from_system_rakefile_on_unix
  153. flexmock(@app, :windows? => false,
  154. :win32_system_dir => nil,
  155. :load => nil)
  156. flexmock(File).should_receive(:expand_path).with("~").and_return("/HOME")
  157. flexmock(File).should_receive(:expand_path).and_return { |fn| fn }
  158. in_environment('RAKE_SYSTEM' => nil) do
  159. @app.options.rakelib = []
  160. @app.instance_eval do
  161. handle_options
  162. options.silent = true
  163. options.load_system = true
  164. load_rakefile
  165. end
  166. assert_equal "/HOME/.rake", @app.system_dir
  167. end
  168. end
  169. def test_windows
  170. assert ! (@app.windows? && @app.unix?)
  171. end
  172. def test_load_from_system_rakefile_on_windows
  173. flexmock(Rake::Win32, :windows? => true)
  174. flexmock(@app, :standard_system_dir => "XX")
  175. flexmock(@app).should_receive(:directory?).with("/AD/Rake").and_return(true)
  176. flexmock(@app).should_receive(:load).and_return(nil)
  177. in_environment('RAKE_SYSTEM' => nil, 'APPDATA' => '/AD') do
  178. @app.options.rakelib = []
  179. @app.instance_eval do
  180. handle_options
  181. options.silent = true
  182. options.load_system = true
  183. load_rakefile
  184. end
  185. assert_equal "/AD/Rake", @app.system_dir
  186. end
  187. end
  188. def test_loading_imports
  189. mock = flexmock("loader")
  190. mock.should_receive(:load).with("x.dummy").once
  191. @app.instance_eval do
  192. add_loader("dummy", mock)
  193. add_import("x.dummy")
  194. load_imports
  195. end
  196. end
  197. def test_building_imported_files_on_demand
  198. mock = flexmock("loader")
  199. mock.should_receive(:load).with("x.dummy").once
  200. mock.should_receive(:make_dummy).with_no_args.once
  201. @app.instance_eval do
  202. intern(Rake::Task, "x.dummy").enhance do mock.make_dummy end
  203. add_loader("dummy", mock)
  204. add_import("x.dummy")
  205. load_imports
  206. end
  207. end
  208. def test_good_run
  209. ran = false
  210. ARGV.clear
  211. ARGV << '--rakelib=""'
  212. @app.options.silent = true
  213. @app.instance_eval do
  214. intern(Rake::Task, "default").enhance { ran = true }
  215. end
  216. in_environment("PWD" => "test/data/default") do
  217. @app.run
  218. end
  219. assert ran
  220. end
  221. def test_display_task_run
  222. ran = false
  223. ARGV.clear
  224. ARGV << '-f' << '-s' << '--tasks' << '--rakelib=""'
  225. @app.last_description = "COMMENT"
  226. @app.define_task(Rake::Task, "default")
  227. out = capture_stdout { @app.run }
  228. assert @app.options.show_tasks
  229. assert ! ran
  230. assert_match(/rake default/, out)
  231. assert_match(/# COMMENT/, out)
  232. end
  233. def test_display_prereqs
  234. ran = false
  235. ARGV.clear
  236. ARGV << '-f' << '-s' << '--prereqs' << '--rakelib=""'
  237. @app.last_description = "COMMENT"
  238. t = @app.define_task(Rake::Task, "default")
  239. t.enhance([:a, :b])
  240. @app.define_task(Rake::Task, "a")
  241. @app.define_task(Rake::Task, "b")
  242. out = capture_stdout { @app.run }
  243. assert @app.options.show_prereqs
  244. assert ! ran
  245. assert_match(/rake a$/, out)
  246. assert_match(/rake b$/, out)
  247. assert_match(/rake default\n( *(a|b)\n){2}/m, out)
  248. end
  249. def test_bad_run
  250. @app.intern(Rake::Task, "default").enhance { fail }
  251. ARGV.clear
  252. ARGV << '-f' << '-s' << '--rakelib=""'
  253. assert_raise(SystemExit) {
  254. err = capture_stderr { @app.run }
  255. assert_match(/see full trace/, err)
  256. }
  257. ensure
  258. ARGV.clear
  259. end
  260. def test_bad_run_with_trace
  261. @app.intern(Rake::Task, "default").enhance { fail }
  262. ARGV.clear
  263. ARGV << '-f' << '-s' << '-t'
  264. assert_raise(SystemExit) {
  265. err = capture_stderr { capture_stdout { @app.run } }
  266. assert_no_match(/see full trace/, err)
  267. }
  268. ensure
  269. ARGV.clear
  270. end
  271. def test_run_with_bad_options
  272. @app.intern(Rake::Task, "default").enhance { fail }
  273. ARGV.clear
  274. ARGV << '-f' << '-s' << '--xyzzy'
  275. assert_raise(SystemExit) {
  276. err = capture_stderr { capture_stdout { @app.run } }
  277. }
  278. ensure
  279. ARGV.clear
  280. end
  281. end
  282. ######################################################################
  283. class TestApplicationOptions < Test::Unit::TestCase
  284. include CaptureStdout
  285. def setup
  286. clear_argv
  287. RakeFileUtils.verbose_flag = false
  288. RakeFileUtils.nowrite_flag = false
  289. TESTING_REQUIRE.clear
  290. end
  291. def teardown
  292. clear_argv
  293. RakeFileUtils.verbose_flag = false
  294. RakeFileUtils.nowrite_flag = false
  295. end
  296. def clear_argv
  297. while ! ARGV.empty?
  298. ARGV.pop
  299. end
  300. end
  301. def test_default_options
  302. opts = command_line
  303. assert_nil opts.classic_namespace
  304. assert_nil opts.dryrun
  305. assert_nil opts.full_description
  306. assert_nil opts.ignore_system
  307. assert_nil opts.load_system
  308. assert_nil opts.nosearch
  309. assert_equal ['rakelib'], opts.rakelib
  310. assert_nil opts.show_prereqs
  311. assert_nil opts.show_task_pattern
  312. assert_nil opts.show_tasks
  313. assert_nil opts.silent
  314. assert_nil opts.trace
  315. assert_equal ['rakelib'], opts.rakelib
  316. assert ! RakeFileUtils.verbose_flag
  317. assert ! RakeFileUtils.nowrite_flag
  318. end
  319. def test_dry_run
  320. flags('--dry-run', '-n') do |opts|
  321. assert opts.dryrun
  322. assert opts.trace
  323. assert RakeFileUtils.verbose_flag
  324. assert RakeFileUtils.nowrite_flag
  325. end
  326. end
  327. def test_describe
  328. flags('--describe') do |opts|
  329. assert opts.full_description
  330. assert opts.show_tasks
  331. assert_equal(//.to_s, opts.show_task_pattern.to_s)
  332. end
  333. end
  334. def test_describe_with_pattern
  335. flags('--describe=X') do |opts|
  336. assert opts.full_description
  337. assert opts.show_tasks
  338. assert_equal(/X/.to_s, opts.show_task_pattern.to_s)
  339. end
  340. end
  341. def test_execute
  342. $xyzzy = 0
  343. flags('--execute=$xyzzy=1', '-e $xyzzy=1') do |opts|
  344. assert_equal 1, $xyzzy
  345. assert_equal :exit, @exit
  346. $xyzzy = 0
  347. end
  348. end
  349. def test_execute_and_continue
  350. $xyzzy = 0
  351. flags('--execute-continue=$xyzzy=1', '-E $xyzzy=1') do |opts|
  352. assert_equal 1, $xyzzy
  353. assert_not_equal :exit, @exit
  354. $xyzzy = 0
  355. end
  356. end
  357. def test_execute_and_print
  358. $xyzzy = 0
  359. flags('--execute-print=$xyzzy="pugh"', '-p $xyzzy="pugh"') do |opts|
  360. assert_equal 'pugh', $xyzzy
  361. assert_equal :exit, @exit
  362. assert_match(/^pugh$/, @out)
  363. $xyzzy = 0
  364. end
  365. end
  366. def test_help
  367. flags('--help', '-H', '-h') do |opts|
  368. assert_match(/\Arake/, @out)
  369. assert_match(/\boptions\b/, @out)
  370. assert_match(/\btargets\b/, @out)
  371. assert_equal :exit, @exit
  372. assert_equal :exit, @exit
  373. end
  374. end
  375. def test_libdir
  376. flags(['--libdir', 'xx'], ['-I', 'xx'], ['-Ixx']) do |opts|
  377. $:.include?('xx')
  378. end
  379. ensure
  380. $:.delete('xx')
  381. end
  382. def test_rakefile
  383. flags(['--rakefile', 'RF'], ['--rakefile=RF'], ['-f', 'RF'], ['-fRF']) do |opts|
  384. assert_equal ['RF'], @app.instance_eval { @rakefiles }
  385. end
  386. end
  387. def test_rakelib
  388. flags(['--rakelibdir', 'A:B:C'], ['--rakelibdir=A:B:C'], ['-R', 'A:B:C'], ['-RA:B:C']) do |opts|
  389. assert_equal ['A', 'B', 'C'], opts.rakelib
  390. end
  391. end
  392. def test_require
  393. flags(['--require', 'test/reqfile'], '-rtest/reqfile2', '-rtest/reqfile3') do |opts|
  394. end
  395. assert TESTING_REQUIRE.include?(1)
  396. assert TESTING_REQUIRE.include?(2)
  397. assert TESTING_REQUIRE.include?(3)
  398. assert_equal 3, TESTING_REQUIRE.size
  399. end
  400. def test_missing_require
  401. ex = assert_raises(LoadError) do
  402. flags(['--require', 'test/missing']) do |opts|
  403. end
  404. end
  405. assert_match(/no such file/, ex.message)
  406. assert_match(/test\/missing/, ex.message)
  407. end
  408. def test_prereqs
  409. flags('--prereqs', '-P') do |opts|
  410. assert opts.show_prereqs
  411. end
  412. end
  413. def test_quiet
  414. flags('--quiet', '-q') do |opts|
  415. assert ! RakeFileUtils.verbose_flag
  416. assert ! opts.silent
  417. end
  418. end
  419. def test_no_search
  420. flags('--nosearch', '--no-search', '-N') do |opts|
  421. assert opts.nosearch
  422. end
  423. end
  424. def test_silent
  425. flags('--silent', '-s') do |opts|
  426. assert ! RakeFileUtils.verbose_flag
  427. assert opts.silent
  428. end
  429. end
  430. def test_system
  431. flags('--system', '-g') do |opts|
  432. assert opts.load_system
  433. end
  434. end
  435. def test_no_system
  436. flags('--no-system', '-G') do |opts|
  437. assert opts.ignore_system
  438. end
  439. end
  440. def test_trace
  441. flags('--trace', '-t') do |opts|
  442. assert opts.trace
  443. assert RakeFileUtils.verbose_flag
  444. assert ! RakeFileUtils.nowrite_flag
  445. end
  446. end
  447. def test_trace_rules
  448. flags('--rules') do |opts|
  449. assert opts.trace_rules
  450. end
  451. end
  452. def test_tasks
  453. flags('--tasks', '-T') do |opts|
  454. assert opts.show_tasks
  455. assert_equal(//.to_s, opts.show_task_pattern.to_s)
  456. end
  457. flags(['--tasks', 'xyz'], ['-Txyz']) do |opts|
  458. assert opts.show_tasks
  459. assert_equal(/xyz/, opts.show_task_pattern)
  460. end
  461. end
  462. def test_verbose
  463. flags('--verbose', '-V') do |opts|
  464. assert RakeFileUtils.verbose_flag
  465. assert ! opts.silent
  466. end
  467. end
  468. def test_version
  469. flags('--version', '-V') do |opts|
  470. assert_match(/\bversion\b/, @out)
  471. assert_match(/\b#{RAKEVERSION}\b/, @out)
  472. assert_equal :exit, @exit
  473. end
  474. end
  475. def test_classic_namespace
  476. flags(['--classic-namespace'], ['-C', '-T', '-P', '-n', '-s', '-t']) do |opts|
  477. assert opts.classic_namespace
  478. assert_equal opts.show_tasks, $show_tasks
  479. assert_equal opts.show_prereqs, $show_prereqs
  480. assert_equal opts.trace, $trace
  481. assert_equal opts.dryrun, $dryrun
  482. assert_equal opts.silent, $silent
  483. end
  484. end
  485. def test_bad_option
  486. capture_stderr do
  487. ex = assert_raise(OptionParser::InvalidOption) do
  488. flags('--bad-option')
  489. end
  490. if ex.message =~ /^While/ # Ruby 1.9 error message
  491. assert_match(/while parsing/i, ex.message)
  492. else # Ruby 1.8 error message
  493. assert_match(/(invalid|unrecognized) option/i, ex.message)
  494. assert_match(/--bad-option/, ex.message)
  495. end
  496. end
  497. end
  498. def test_task_collection
  499. command_line("a", "b")
  500. assert_equal ["a", "b"], @tasks.sort
  501. end
  502. def test_default_task_collection
  503. command_line()
  504. assert_equal ["default"], @tasks
  505. end
  506. def test_environment_definition
  507. ENV.delete('TESTKEY')
  508. command_line("a", "TESTKEY=12")
  509. assert_equal ["a"], @tasks.sort
  510. assert '12', ENV['TESTKEY']
  511. end
  512. private
  513. def flags(*sets)
  514. sets.each do |set|
  515. ARGV.clear
  516. @out = capture_stdout {
  517. @exit = catch(:system_exit) { opts = command_line(*set) }
  518. }
  519. yield(@app.options) if block_given?
  520. end
  521. end
  522. def command_line(*options)
  523. options.each do |opt| ARGV << opt end
  524. @app = Rake::Application.new
  525. def @app.exit(*args)
  526. throw :system_exit, :exit
  527. end
  528. @app.instance_eval do
  529. collect_tasks handle_options
  530. end
  531. @tasks = @app.top_level_tasks
  532. @app.options
  533. end
  534. end
  535. class TestTaskArgumentParsing < Test::Unit::TestCase
  536. def setup
  537. @app = Rake::Application.new
  538. end
  539. def test_name_only
  540. name, args = @app.parse_task_string("name")
  541. assert_equal "name", name
  542. assert_equal [], args
  543. end
  544. def test_empty_args
  545. name, args = @app.parse_task_string("name[]")
  546. assert_equal "name", name
  547. assert_equal [], args
  548. end
  549. def test_one_argument
  550. name, args = @app.parse_task_string("name[one]")
  551. assert_equal "name", name
  552. assert_equal ["one"], args
  553. end
  554. def test_two_arguments
  555. name, args = @app.parse_task_string("name[one,two]")
  556. assert_equal "name", name
  557. assert_equal ["one", "two"], args
  558. end
  559. def test_can_handle_spaces_between_args
  560. name, args = @app.parse_task_string("name[one, two,\tthree , \tfour]")
  561. assert_equal "name", name
  562. assert_equal ["one", "two", "three", "four"], args
  563. end
  564. def test_keeps_embedded_spaces
  565. name, args = @app.parse_task_string("name[a one ana, two]")
  566. assert_equal "name", name
  567. assert_equal ["a one ana", "two"], args
  568. end
  569. end
  570. class TestTaskArgumentParsing < Test::Unit::TestCase
  571. include InEnvironment
  572. def test_terminal_width_using_env
  573. app = Rake::Application.new
  574. in_environment('RAKE_COLUMNS' => '1234') do
  575. assert_equal 1234, app.terminal_width
  576. end
  577. end
  578. def test_terminal_width_using_stty
  579. app = Rake::Application.new
  580. flexmock(app,
  581. :unix? => true,
  582. :dynamic_width_stty => 1235,
  583. :dynamic_width_tput => 0)
  584. in_environment('RAKE_COLUMNS' => nil) do
  585. assert_equal 1235, app.terminal_width
  586. end
  587. end
  588. def test_terminal_width_using_tput
  589. app = Rake::Application.new
  590. flexmock(app,
  591. :unix? => true,
  592. :dynamic_width_stty => 0,
  593. :dynamic_width_tput => 1236)
  594. in_environment('RAKE_COLUMNS' => nil) do
  595. assert_equal 1236, app.terminal_width
  596. end
  597. end
  598. def test_terminal_width_using_hardcoded_80
  599. app = Rake::Application.new
  600. flexmock(app, :unix? => false)
  601. in_environment('RAKE_COLUMNS' => nil) do
  602. assert_equal 80, app.terminal_width
  603. end
  604. end
  605. def test_terminal_width_with_failure
  606. app = Rake::Application.new
  607. flexmock(app).should_receive(:unix?).and_throw(RuntimeError)
  608. in_environment('RAKE_COLUMNS' => nil) do
  609. assert_equal 80, app.terminal_width
  610. end
  611. end
  612. end