PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/ruby/1.8/gems/rake-0.8.7/test/test_application.rb

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