PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_rubyoptions.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 556 lines | 474 code | 81 blank | 1 comment | 18 complexity | d29c6a01bbc2560e321493e93c8037a8 MD5 | raw file
  1. require 'test/unit'
  2. require 'tmpdir'
  3. require 'tempfile'
  4. require 'pathname'
  5. require_relative 'envutil'
  6. class TestRubyOptions < Test::Unit::TestCase
  7. def write_file(filename, content)
  8. File.open(filename, "w") {|f|
  9. f << content
  10. }
  11. end
  12. def with_tmpchdir
  13. Dir.mktmpdir {|d|
  14. d = Pathname.new(d).realpath.to_s
  15. Dir.chdir(d) {
  16. yield d
  17. }
  18. }
  19. end
  20. def test_source_file
  21. assert_in_out_err([], "", [], [])
  22. end
  23. def test_usage
  24. assert_in_out_err(%w(-h)) do |r, e|
  25. assert_operator(r.size, :<=, 24)
  26. assert_equal([], e)
  27. end
  28. assert_in_out_err(%w(--help)) do |r, e|
  29. assert_operator(r.size, :<=, 24)
  30. assert_equal([], e)
  31. end
  32. end
  33. def test_option_variables
  34. assert_in_out_err(["-e", 'p [$-p, $-l, $-a]']) do |r, e|
  35. assert_equal(["[false, false, false]"], r)
  36. assert_equal([], e)
  37. end
  38. assert_in_out_err(%w(-p -l -a -e) + ['p [$-p, $-l, $-a]'],
  39. "foo\nbar\nbaz\n") do |r, e|
  40. assert_equal(
  41. [ '[true, true, true]', 'foo',
  42. '[true, true, true]', 'bar',
  43. '[true, true, true]', 'baz' ], r)
  44. assert_equal([], e)
  45. end
  46. end
  47. def test_warning
  48. save_rubyopt = ENV['RUBYOPT']
  49. ENV['RUBYOPT'] = nil
  50. assert_in_out_err(%w(-W0 -e) + ['p $-W'], "", %w(0), [])
  51. assert_in_out_err(%w(-W1 -e) + ['p $-W'], "", %w(1), [])
  52. assert_in_out_err(%w(-Wx -e) + ['p $-W'], "", %w(1), [])
  53. assert_in_out_err(%w(-W -e) + ['p $-W'], "", %w(2), [])
  54. ensure
  55. ENV['RUBYOPT'] = save_rubyopt
  56. end
  57. def test_safe_level
  58. assert_in_out_err(%w(-T -e) + [""], "", [],
  59. /no -e allowed in tainted mode \(SecurityError\)/)
  60. assert_in_out_err(%w(-T4 -S foo.rb), "", [],
  61. /no -S allowed in tainted mode \(SecurityError\)/)
  62. end
  63. def test_debug
  64. assert_in_out_err(["--disable-gems", "-de", "p $DEBUG"], "", %w(true), [])
  65. assert_in_out_err(["--disable-gems", "--debug", "-e", "p $DEBUG"],
  66. "", %w(true), [])
  67. end
  68. def test_verbose
  69. assert_in_out_err(["-vve", ""]) do |r, e|
  70. assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
  71. assert_equal RUBY_DESCRIPTION, r.join.chomp
  72. assert_equal([], e)
  73. end
  74. assert_in_out_err(%w(--verbose -e) + ["p $VERBOSE"], "", %w(true), [])
  75. assert_in_out_err(%w(--verbose), "", [], [])
  76. end
  77. def test_copyright
  78. assert_in_out_err(%w(--copyright), "",
  79. /^ruby - Copyright \(C\) 1993-\d+ Yukihiro Matsumoto$/, [])
  80. assert_in_out_err(%w(--verbose -e) + ["p $VERBOSE"], "", %w(true), [])
  81. end
  82. def test_enable
  83. assert_in_out_err(%w(--enable all -e) + [""], "", [], [])
  84. assert_in_out_err(%w(--enable-all -e) + [""], "", [], [])
  85. assert_in_out_err(%w(--enable=all -e) + [""], "", [], [])
  86. assert_in_out_err(%w(--enable foobarbazqux -e) + [""], "", [],
  87. /unknown argument for --enable: `foobarbazqux'/)
  88. assert_in_out_err(%w(--enable), "", [], /missing argument for --enable/)
  89. end
  90. def test_disable
  91. assert_in_out_err(%w(--disable all -e) + [""], "", [], [])
  92. assert_in_out_err(%w(--disable-all -e) + [""], "", [], [])
  93. assert_in_out_err(%w(--disable=all -e) + [""], "", [], [])
  94. assert_in_out_err(%w(--disable foobarbazqux -e) + [""], "", [],
  95. /unknown argument for --disable: `foobarbazqux'/)
  96. assert_in_out_err(%w(--disable), "", [], /missing argument for --disable/)
  97. end
  98. def test_kanji
  99. assert_in_out_err(%w(-KU), "p '\u3042'") do |r, e|
  100. assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
  101. end
  102. assert_in_out_err(%w(-KE -e) + [""], "", [], [])
  103. assert_in_out_err(%w(-KS -e) + [""], "", [], [])
  104. assert_in_out_err(%w(-KN -e) + [""], "", [], [])
  105. end
  106. def test_version
  107. assert_in_out_err(%w(--version)) do |r, e|
  108. assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
  109. assert_equal RUBY_DESCRIPTION, r.join.chomp
  110. assert_equal([], e)
  111. end
  112. end
  113. def test_eval
  114. assert_in_out_err(%w(-e), "", [], /no code specified for -e \(RuntimeError\)/)
  115. end
  116. def test_require
  117. require "pp"
  118. assert_in_out_err(%w(-r pp -e) + ["pp 1"], "", %w(1), [])
  119. assert_in_out_err(%w(-rpp -e) + ["pp 1"], "", %w(1), [])
  120. assert_in_out_err(%w(-ep\ 1 -r), "", %w(1), [])
  121. assert_in_out_err(%w(-r), "", [], [])
  122. rescue LoadError
  123. end
  124. def test_include
  125. d = Dir.tmpdir
  126. assert_in_out_err(["-I" + d, "-e", ""], "", [], [])
  127. assert_in_out_err(["-I", d, "-e", ""], "", [], [])
  128. end
  129. def test_separator
  130. assert_in_out_err(%w(-000 -e) + ["print gets"], "foo\nbar\0baz", %W(foo bar\0baz), [])
  131. assert_in_out_err(%w(-0141 -e) + ["print gets"], "foo\nbar\0baz", %w(foo ba), [])
  132. assert_in_out_err(%w(-0e) + ["print gets"], "foo\nbar\0baz", %W(foo bar\0), [])
  133. end
  134. def test_autosplit
  135. assert_in_out_err(%w(-an -F: -e) + ["p $F"], "foo:bar:baz\nqux:quux:quuux\n",
  136. ['["foo", "bar", "baz\n"]', '["qux", "quux", "quuux\n"]'], [])
  137. end
  138. def test_chdir
  139. assert_in_out_err(%w(-C), "", [], /Can't chdir/)
  140. assert_in_out_err(%w(-C test_ruby_test_rubyoptions_foobarbazqux), "", [], /Can't chdir/)
  141. d = Dir.tmpdir
  142. assert_in_out_err(["-C", d, "-e", "puts Dir.pwd"]) do |r, e|
  143. assert(File.identical?(r.join, d))
  144. assert_equal([], e)
  145. end
  146. end
  147. def test_yydebug
  148. assert_in_out_err(["-ye", ""]) do |r, e|
  149. assert_equal([], r)
  150. assert_not_equal([], e)
  151. end
  152. assert_in_out_err(%w(--yydebug -e) + [""]) do |r, e|
  153. assert_equal([], r)
  154. assert_not_equal([], e)
  155. end
  156. end
  157. def test_encoding
  158. assert_in_out_err(%w(-Eutf-8), "p '\u3042'", [], /invalid multibyte char/)
  159. assert_in_out_err(%w(--encoding), "", [], /missing argument for --encoding/)
  160. assert_in_out_err(%w(--encoding test_ruby_test_rubyoptions_foobarbazqux), "", [],
  161. /unknown encoding name - test_ruby_test_rubyoptions_foobarbazqux \(RuntimeError\)/)
  162. assert_in_out_err(%w(--encoding utf-8), "p '\u3042'", [], /invalid multibyte char/)
  163. end
  164. def test_syntax_check
  165. assert_in_out_err(%w(-c -e a=1+1 -e !a), "", ["Syntax OK"], [])
  166. end
  167. def test_invalid_option
  168. assert_in_out_err(%w(--foobarbazqux), "", [], /invalid option --foobarbazqux/)
  169. assert_in_out_err(%W(-\r -e) + [""], "", [], [])
  170. assert_in_out_err(%W(-\rx), "", [], /invalid option -\\x0D \(-h will show valid options\) \(RuntimeError\)/)
  171. assert_in_out_err(%W(-\x01), "", [], /invalid option -\\x01 \(-h will show valid options\) \(RuntimeError\)/)
  172. assert_in_out_err(%w(-Z), "", [], /invalid option -Z \(-h will show valid options\) \(RuntimeError\)/)
  173. end
  174. def test_rubyopt
  175. rubyopt_orig = ENV['RUBYOPT']
  176. ENV['RUBYOPT'] = ' - -'
  177. assert_in_out_err([], "", [], [])
  178. ENV['RUBYOPT'] = '-e "p 1"'
  179. assert_in_out_err([], "", [], /invalid switch in RUBYOPT: -e \(RuntimeError\)/)
  180. ENV['RUBYOPT'] = '-T1'
  181. assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/)
  182. ENV['RUBYOPT'] = '-T4'
  183. assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/)
  184. ENV['RUBYOPT'] = '-Eus-ascii -KN'
  185. assert_in_out_err(%w(-Eutf-8 -KU), "p '\u3042'") do |r, e|
  186. assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
  187. assert_equal([], e)
  188. end
  189. ensure
  190. if rubyopt_orig
  191. ENV['RUBYOPT'] = rubyopt_orig
  192. else
  193. ENV.delete('RUBYOPT')
  194. end
  195. end
  196. def test_search
  197. rubypath_orig = ENV['RUBYPATH']
  198. path_orig = ENV['PATH']
  199. t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
  200. t.puts "p 1"
  201. t.close
  202. @verbose = $VERBOSE
  203. $VERBOSE = nil
  204. ENV['PATH'] = File.dirname(t.path)
  205. assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), [])
  206. ENV['RUBYPATH'] = File.dirname(t.path)
  207. assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), [])
  208. ensure
  209. if rubypath_orig
  210. ENV['RUBYPATH'] = rubypath_orig
  211. else
  212. ENV.delete('RUBYPATH')
  213. end
  214. if path_orig
  215. ENV['PATH'] = path_orig
  216. else
  217. ENV.delete('PATH')
  218. end
  219. t.close(true) if t
  220. $VERBOSE = @verbose
  221. end
  222. def test_shebang
  223. assert_in_out_err([], "#! /test_r_u_b_y_test_r_u_b_y_options_foobarbazqux\r\np 1\r\n",
  224. [], /: no Ruby script found in input/)
  225. assert_in_out_err([], "#! /test_r_u_b_y_test_r_u_b_y_options_foobarbazqux -foo -bar\r\np 1\r\n",
  226. [], /: no Ruby script found in input/)
  227. assert_in_out_err([], "#!ruby -KU -Eutf-8\r\np \"\u3042\"\r\n") do |r, e|
  228. assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
  229. assert_equal([], e)
  230. end
  231. bug4118 = '[ruby-dev:42680]'
  232. assert_in_out_err(%w[], "#!/bin/sh\n""#!shebang\n""#!ruby\n""puts __LINE__\n",
  233. %w[4], [], bug4118)
  234. assert_in_out_err(%w[-x], "#!/bin/sh\n""#!shebang\n""#!ruby\n""puts __LINE__\n",
  235. %w[4], [], bug4118)
  236. end
  237. def test_sflag
  238. assert_in_out_err(%w(- -abc -def=foo -ghi-jkl -- -xyz),
  239. "#!ruby -s\np [$abc, $def, $ghi_jkl, defined?($xyz)]\n",
  240. ['[true, "foo", true, nil]'], [])
  241. assert_in_out_err(%w(- -#), "#!ruby -s\n", [],
  242. /invalid name for global variable - -# \(NameError\)/)
  243. assert_in_out_err(%w(- -#=foo), "#!ruby -s\n", [],
  244. /invalid name for global variable - -# \(NameError\)/)
  245. end
  246. def test_assignment_in_conditional
  247. t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
  248. t.puts "if a = 1"
  249. t.puts "end"
  250. t.puts "0.times do"
  251. t.puts " if b = 2"
  252. t.puts " a += b"
  253. t.puts " end"
  254. t.puts "end"
  255. t.close
  256. err = ["#{t.path}:1: warning: found = in conditional, should be ==",
  257. "#{t.path}:4: warning: found = in conditional, should be =="]
  258. err = /\A(#{Regexp.quote(t.path)}):1(: warning: found = in conditional, should be ==)\n\1:4\2\Z/
  259. bug2136 = '[ruby-dev:39363]'
  260. assert_in_out_err(["-w", t.path], "", [], err, bug2136)
  261. assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, bug2136)
  262. ensure
  263. t.close(true) if t
  264. end
  265. def test_indentation_check
  266. t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
  267. t.puts "begin"
  268. t.puts " end"
  269. t.close
  270. err = ["#{t.path}:2: warning: mismatched indentations at 'end' with 'begin' at 1"]
  271. assert_in_out_err(["-w", t.path], "", [], err)
  272. assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err)
  273. t.open
  274. t.puts "# -*- warn-indent: false -*-"
  275. t.puts "begin"
  276. t.puts " end"
  277. t.close
  278. assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
  279. err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 3"]
  280. t.open
  281. t.puts "# -*- warn-indent: false -*-"
  282. t.puts "# -*- warn-indent: true -*-"
  283. t.puts "begin"
  284. t.puts " end"
  285. t.close
  286. assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]')
  287. err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 2"]
  288. t.open
  289. t.puts "# -*- warn-indent: true -*-"
  290. t.puts "begin"
  291. t.puts "# -*- warn-indent: false -*-"
  292. t.puts " end"
  293. t.close
  294. assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
  295. ensure
  296. t.close(true) if t
  297. end
  298. def test_notfound
  299. notexist = "./notexist.rb"
  300. rubybin = Regexp.quote(EnvUtil.rubybin)
  301. pat = Regexp.quote(notexist)
  302. bug1573 = '[ruby-core:23717]'
  303. assert_equal(false, File.exist?(notexist))
  304. assert_in_out_err(["-r", notexist, "-ep"], "", [], /.* -- #{pat} \(LoadError\)/, bug1573)
  305. assert_in_out_err([notexist], "", [], /#{rubybin}:.* -- #{pat} \(LoadError\)/, bug1573)
  306. end
  307. def test_program_name
  308. ruby = EnvUtil.rubybin
  309. IO.popen([ruby, '-e', 'print $0']) {|f|
  310. assert_equal('-e', f.read)
  311. }
  312. IO.popen([ruby, '-'], 'r+') {|f|
  313. f << 'print $0'
  314. f.close_write
  315. assert_equal('-', f.read)
  316. }
  317. Dir.mktmpdir {|d|
  318. n1 = File.join(d, 't1')
  319. open(n1, 'w') {|f| f << 'print $0' }
  320. IO.popen([ruby, n1]) {|f|
  321. assert_equal(n1, f.read)
  322. }
  323. if File.respond_to? :symlink
  324. n2 = File.join(d, 't2')
  325. File.symlink(n1, n2)
  326. IO.popen([ruby, n2]) {|f|
  327. assert_equal(n2, f.read)
  328. }
  329. end
  330. Dir.chdir(d) {
  331. n3 = '-e'
  332. open(n3, 'w') {|f| f << 'print $0' }
  333. IO.popen([ruby, '--', n3]) {|f|
  334. assert_equal(n3, f.read)
  335. }
  336. n4 = '-'
  337. IO.popen([ruby, '--', n4], 'r+') {|f|
  338. f << 'print $0'
  339. f.close_write
  340. assert_equal(n4, f.read)
  341. }
  342. }
  343. }
  344. end
  345. def test_set_program_name
  346. skip "platform dependent feature" if /linux|freebsd|netbsd|openbsd|darwin/ !~ RUBY_PLATFORM
  347. with_tmpchdir do
  348. write_file("test-script", "$0 = 'hello world'; sleep 60")
  349. pid = spawn(EnvUtil.rubybin, "test-script")
  350. sleep 0.1
  351. ps = `ps -p #{pid} -o command`
  352. assert_match(/hello world/, ps)
  353. Process.kill :KILL, pid
  354. end
  355. end
  356. def test_segv_test
  357. opts = {}
  358. if /mswin|mingw/ =~ RUBY_PLATFORM
  359. additional = '[\s\w\.\']*'
  360. else
  361. opts[:rlimit_core] = 0
  362. additional = ""
  363. end
  364. assert_in_out_err(["-e", "Process.kill :SEGV, $$"], "", [],
  365. %r(\A
  366. -e:(?:1:)?\s\[BUG\]\sSegmentation\sfault\n
  367. #{ Regexp.quote(RUBY_DESCRIPTION) }\n\n
  368. --\sControl\sframe\sinformation\s-+\n
  369. (?:c:.*\n)*
  370. (?:
  371. --\sRuby\slevel\sbacktrace\sinformation\s----------------------------------------\n
  372. -e:1:in\s\`<main>\'\n
  373. -e:1:in\s\`kill\'\n
  374. )?
  375. \n
  376. (?:
  377. --\sC\slevel\sbacktrace\sinformation\s-------------------------------------------\n
  378. (?:(?:.*\s)?\[0x\h+\]\n)*\n
  379. )?
  380. (?m:.*)
  381. \[NOTE\]\n
  382. You\smay\shave\sencountered\sa\sbug\sin\sthe\sRuby\sinterpreter\sor\sextension\slibraries.\n
  383. Bug\sreports\sare\swelcome.\n
  384. For\sdetails:\shttp:\/\/www.ruby-lang.org/bugreport.html\n
  385. \n
  386. (?:#{additional})
  387. \z
  388. )x,
  389. nil,
  390. opts)
  391. end
  392. def test_DATA
  393. t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
  394. t.puts "puts DATA.read.inspect"
  395. t.puts "__END__"
  396. t.puts "foo"
  397. t.puts "bar"
  398. t.puts "baz"
  399. t.close
  400. assert_in_out_err([t.path], "", %w("foo\\nbar\\nbaz\\n"), [])
  401. ensure
  402. t.close(true) if t
  403. end
  404. def test_unused_variable
  405. feature3446 = '[ruby-dev:41620]'
  406. assert_in_out_err(["-we", "a=1"], "", [], [], feature3446)
  407. assert_in_out_err(["-we", "def foo\n a=1\nend"], "", [], ["-e:2: warning: assigned but unused variable - a"], feature3446)
  408. assert_in_out_err(["-we", "def foo\n eval('a=1')\nend"], "", [], [], feature3446)
  409. assert_in_out_err(["-we", "1.times do\n a=1\nend"], "", [], [], feature3446)
  410. assert_in_out_err(["-we", "def foo\n 1.times do\n a=1\n end\nend"], "", [], ["-e:3: warning: assigned but unused variable - a"], feature3446)
  411. assert_in_out_err(["-we", "def foo\n"" 1.times do |a| end\n""end"], "", [], [])
  412. end
  413. def test_shadowing_variable
  414. bug4130 = '[ruby-dev:42718]'
  415. assert_in_out_err(["-we", "def foo\n"" a=1\n"" 1.times do |a| end\n"" a\n""end"],
  416. "", [], ["-e:3: warning: shadowing outer local variable - a"], bug4130)
  417. assert_in_out_err(["-we", "def foo\n"" a=1\n"" 1.times do |a| end\n""end"],
  418. "", [],
  419. ["-e:3: warning: shadowing outer local variable - a",
  420. "-e:2: warning: assigned but unused variable - a",
  421. ], bug4130)
  422. end
  423. def test_script_from_stdin
  424. begin
  425. require 'pty'
  426. require 'io/console'
  427. rescue LoadError
  428. return
  429. end
  430. require 'timeout'
  431. result = nil
  432. IO.pipe {|r, w|
  433. begin
  434. PTY.open {|m, s|
  435. s.echo = false
  436. m.print("\C-d")
  437. pid = spawn(EnvUtil.rubybin, :in => s, :out => w)
  438. w.close
  439. assert_nothing_raised('[ruby-dev:37798]') do
  440. result = Timeout.timeout(3) {r.read}
  441. end
  442. Process.wait pid
  443. }
  444. rescue RuntimeError
  445. skip $!
  446. end
  447. }
  448. assert_equal("", result, '[ruby-dev:37798]')
  449. IO.pipe {|r, w|
  450. PTY.open {|m, s|
  451. s.echo = false
  452. pid = spawn(EnvUtil.rubybin, :in => s, :out => w)
  453. w.close
  454. m.print("$stdin.read; p $stdin.gets\n\C-d")
  455. m.print("abc\n\C-d")
  456. m.print("zzz\n")
  457. result = r.read
  458. Process.wait pid
  459. }
  460. }
  461. assert_equal("\"zzz\\n\"\n", result, '[ruby-core:30910]')
  462. end
  463. def test_unmatching_glob
  464. bug3851 = '[ruby-core:32478]'
  465. a = "a[foo"
  466. Dir.mktmpdir do |dir|
  467. open(File.join(dir, a), "w") {|f| f.puts("p 42")}
  468. assert_in_out_err(["-C", dir, a], "", ["42"], [], bug3851)
  469. File.unlink(File.join(dir, a))
  470. assert_in_out_err(["-C", dir, a], "", [], /LoadError/, bug3851)
  471. end
  472. end
  473. end