PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/test/externals/ruby1.9/rake/test_application.rb

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