2008-04-16 11:48:54 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
require 'tmpdir'
|
|
|
|
require 'tempfile'
|
2011-05-21 21:06:37 -04:00
|
|
|
require 'pathname'
|
|
|
|
|
2008-04-16 11:48:54 -04:00
|
|
|
require_relative 'envutil'
|
|
|
|
|
|
|
|
class TestRubyOptions < Test::Unit::TestCase
|
2011-05-21 21:06:37 -04:00
|
|
|
def write_file(filename, content)
|
|
|
|
File.open(filename, "w") {|f|
|
|
|
|
f << content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_tmpchdir
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
d = Pathname.new(d).realpath.to_s
|
|
|
|
Dir.chdir(d) {
|
|
|
|
yield d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-04-16 11:48:54 -04:00
|
|
|
def test_source_file
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "", [], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_usage
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-h)) do |r, e|
|
|
|
|
assert_operator(r.size, :<=, 24)
|
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--help)) do |r, e|
|
|
|
|
assert_operator(r.size, :<=, 24)
|
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_option_variables
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(["-e", 'p [$-p, $-l, $-a]']) do |r, e|
|
|
|
|
assert_equal(["[false, false, false]"], r)
|
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-p -l -a -e) + ['p [$-p, $-l, $-a]'],
|
|
|
|
"foo\nbar\nbaz\n") do |r, e|
|
2008-04-16 11:48:54 -04:00
|
|
|
assert_equal(
|
|
|
|
[ '[true, true, true]', 'foo',
|
|
|
|
'[true, true, true]', 'bar',
|
|
|
|
'[true, true, true]', 'baz' ], r)
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_warning
|
2010-01-25 17:08:29 -05:00
|
|
|
save_rubyopt = ENV['RUBYOPT']
|
|
|
|
ENV['RUBYOPT'] = nil
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-W0 -e) + ['p $-W'], "", %w(0), [])
|
|
|
|
assert_in_out_err(%w(-W1 -e) + ['p $-W'], "", %w(1), [])
|
|
|
|
assert_in_out_err(%w(-Wx -e) + ['p $-W'], "", %w(1), [])
|
|
|
|
assert_in_out_err(%w(-W -e) + ['p $-W'], "", %w(2), [])
|
2010-01-25 17:08:29 -05:00
|
|
|
ensure
|
|
|
|
ENV['RUBYOPT'] = save_rubyopt
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_level
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-T -e) + [""], "", [],
|
|
|
|
/no -e allowed in tainted mode \(SecurityError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-T4 -S foo.rb), "", [],
|
|
|
|
/no -S allowed in tainted mode \(SecurityError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_debug
|
2011-01-13 21:02:12 -05:00
|
|
|
assert_in_out_err(["--disable-gems", "-de", "p $DEBUG"], "", %w(true), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2011-01-13 21:02:12 -05:00
|
|
|
assert_in_out_err(["--disable-gems", "--debug", "-e", "p $DEBUG"],
|
|
|
|
"", %w(true), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verbose
|
2011-01-13 21:02:12 -05:00
|
|
|
assert_in_out_err(["-vve", ""]) do |r, e|
|
2009-02-02 02:07:59 -05:00
|
|
|
assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_equal RUBY_DESCRIPTION, r.join.chomp
|
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--verbose -e) + ["p $VERBOSE"], "", %w(true), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--verbose), "", [], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_copyright
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--copyright), "",
|
|
|
|
/^ruby - Copyright \(C\) 1993-\d+ Yukihiro Matsumoto$/, [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--verbose -e) + ["p $VERBOSE"], "", %w(true), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_enable
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--enable all -e) + [""], "", [], [])
|
|
|
|
assert_in_out_err(%w(--enable-all -e) + [""], "", [], [])
|
|
|
|
assert_in_out_err(%w(--enable=all -e) + [""], "", [], [])
|
|
|
|
assert_in_out_err(%w(--enable foobarbazqux -e) + [""], "", [],
|
|
|
|
/unknown argument for --enable: `foobarbazqux'/)
|
|
|
|
assert_in_out_err(%w(--enable), "", [], /missing argument for --enable/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_disable
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--disable all -e) + [""], "", [], [])
|
|
|
|
assert_in_out_err(%w(--disable-all -e) + [""], "", [], [])
|
|
|
|
assert_in_out_err(%w(--disable=all -e) + [""], "", [], [])
|
|
|
|
assert_in_out_err(%w(--disable foobarbazqux -e) + [""], "", [],
|
|
|
|
/unknown argument for --disable: `foobarbazqux'/)
|
|
|
|
assert_in_out_err(%w(--disable), "", [], /missing argument for --disable/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_kanji
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-KU), "p '\u3042'") do |r, e|
|
|
|
|
assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
2012-07-02 22:10:59 -04:00
|
|
|
assert_in_out_err(%w(-KE -e) + [""], "", [], /-Ke/)
|
|
|
|
assert_in_out_err(%w(-KS -e) + [""], "", [], /-Ks/)
|
|
|
|
assert_in_out_err(%w(-KN -e) + [""], "", [], /-Kn/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_version
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--version)) do |r, e|
|
2009-02-02 02:07:59 -05:00
|
|
|
assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_equal RUBY_DESCRIPTION, r.join.chomp
|
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-e), "", [], /no code specified for -e \(RuntimeError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_require
|
2008-07-15 11:26:04 -04:00
|
|
|
require "pp"
|
|
|
|
assert_in_out_err(%w(-r pp -e) + ["pp 1"], "", %w(1), [])
|
|
|
|
assert_in_out_err(%w(-rpp -e) + ["pp 1"], "", %w(1), [])
|
2010-12-17 03:04:35 -05:00
|
|
|
assert_in_out_err(%w(-ep\ 1 -r), "", %w(1), [])
|
|
|
|
assert_in_out_err(%w(-r), "", [], [])
|
2008-07-15 11:26:04 -04:00
|
|
|
rescue LoadError
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_include
|
|
|
|
d = Dir.tmpdir
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(["-I" + d, "-e", ""], "", [], [])
|
|
|
|
assert_in_out_err(["-I", d, "-e", ""], "", [], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_separator
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-000 -e) + ["print gets"], "foo\nbar\0baz", %W(foo bar\0baz), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-0141 -e) + ["print gets"], "foo\nbar\0baz", %w(foo ba), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-0e) + ["print gets"], "foo\nbar\0baz", %W(foo bar\0), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_autosplit
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-an -F: -e) + ["p $F"], "foo:bar:baz\nqux:quux:quuux\n",
|
|
|
|
['["foo", "bar", "baz\n"]', '["qux", "quux", "quuux\n"]'], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_chdir
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-C), "", [], /Can't chdir/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-C test_ruby_test_rubyoptions_foobarbazqux), "", [], /Can't chdir/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
|
|
|
d = Dir.tmpdir
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(["-C", d, "-e", "puts Dir.pwd"]) do |r, e|
|
|
|
|
assert(File.identical?(r.join, d))
|
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_yydebug
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(["-ye", ""]) do |r, e|
|
|
|
|
assert_equal([], r)
|
|
|
|
assert_not_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--yydebug -e) + [""]) do |r, e|
|
|
|
|
assert_equal([], r)
|
|
|
|
assert_not_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_encoding
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-Eutf-8), "p '\u3042'", [], /invalid multibyte char/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--encoding), "", [], /missing argument for --encoding/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--encoding test_ruby_test_rubyoptions_foobarbazqux), "", [],
|
|
|
|
/unknown encoding name - test_ruby_test_rubyoptions_foobarbazqux \(RuntimeError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--encoding utf-8), "p '\u3042'", [], /invalid multibyte char/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_syntax_check
|
2010-06-23 01:32:46 -04:00
|
|
|
assert_in_out_err(%w(-c -e a=1+1 -e !a), "", ["Syntax OK"], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invalid_option
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(--foobarbazqux), "", [], /invalid option --foobarbazqux/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%W(-\r -e) + [""], "", [], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%W(-\rx), "", [], /invalid option -\\x0D \(-h will show valid options\) \(RuntimeError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%W(-\x01), "", [], /invalid option -\\x01 \(-h will show valid options\) \(RuntimeError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-Z), "", [], /invalid option -Z \(-h will show valid options\) \(RuntimeError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_rubyopt
|
|
|
|
rubyopt_orig = ENV['RUBYOPT']
|
|
|
|
|
|
|
|
ENV['RUBYOPT'] = ' - -'
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "", [], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
|
|
|
ENV['RUBYOPT'] = '-e "p 1"'
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "", [], /invalid switch in RUBYOPT: -e \(RuntimeError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
|
|
|
ENV['RUBYOPT'] = '-T1'
|
2011-01-13 21:02:12 -05:00
|
|
|
assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
|
|
|
ENV['RUBYOPT'] = '-T4'
|
2011-01-13 21:02:12 -05:00
|
|
|
assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-10-16 18:21:09 -04:00
|
|
|
ENV['RUBYOPT'] = '-Eus-ascii -KN'
|
|
|
|
assert_in_out_err(%w(-Eutf-8 -KU), "p '\u3042'") do |r, e|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
|
2012-07-02 22:10:59 -04:00
|
|
|
assert_match(/-Ku/, e.join)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
ensure
|
|
|
|
if rubyopt_orig
|
|
|
|
ENV['RUBYOPT'] = rubyopt_orig
|
|
|
|
else
|
|
|
|
ENV.delete('RUBYOPT')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_search
|
|
|
|
rubypath_orig = ENV['RUBYPATH']
|
|
|
|
path_orig = ENV['PATH']
|
|
|
|
|
|
|
|
t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
|
|
|
|
t.puts "p 1"
|
|
|
|
t.close
|
|
|
|
|
|
|
|
@verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
|
|
|
|
|
|
|
ENV['PATH'] = File.dirname(t.path)
|
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
|
|
|
ENV['RUBYPATH'] = File.dirname(t.path)
|
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
|
|
|
ensure
|
|
|
|
if rubypath_orig
|
|
|
|
ENV['RUBYPATH'] = rubypath_orig
|
|
|
|
else
|
|
|
|
ENV.delete('RUBYPATH')
|
|
|
|
end
|
|
|
|
if path_orig
|
|
|
|
ENV['PATH'] = path_orig
|
|
|
|
else
|
|
|
|
ENV.delete('PATH')
|
|
|
|
end
|
|
|
|
t.close(true) if t
|
|
|
|
$VERBOSE = @verbose
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shebang
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "#! /test_r_u_b_y_test_r_u_b_y_options_foobarbazqux\r\np 1\r\n",
|
2009-08-11 11:06:51 -04:00
|
|
|
[], /: no Ruby script found in input/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "#! /test_r_u_b_y_test_r_u_b_y_options_foobarbazqux -foo -bar\r\np 1\r\n",
|
2009-08-11 11:06:51 -04:00
|
|
|
[], /: no Ruby script found in input/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err([], "#!ruby -KU -Eutf-8\r\np \"\u3042\"\r\n") do |r, e|
|
|
|
|
assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
|
2012-07-02 22:10:59 -04:00
|
|
|
assert_match(/-Ku/, e.join)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
2010-12-04 21:27:13 -05:00
|
|
|
|
|
|
|
bug4118 = '[ruby-dev:42680]'
|
|
|
|
assert_in_out_err(%w[], "#!/bin/sh\n""#!shebang\n""#!ruby\n""puts __LINE__\n",
|
|
|
|
%w[4], [], bug4118)
|
|
|
|
assert_in_out_err(%w[-x], "#!/bin/sh\n""#!shebang\n""#!ruby\n""puts __LINE__\n",
|
|
|
|
%w[4], [], bug4118)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sflag
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(- -abc -def=foo -ghi-jkl -- -xyz),
|
2010-01-25 17:08:29 -05:00
|
|
|
"#!ruby -s\np [$abc, $def, $ghi_jkl, defined?($xyz)]\n",
|
2008-07-15 11:26:04 -04:00
|
|
|
['[true, "foo", true, nil]'], [])
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(- -#), "#!ruby -s\n", [],
|
|
|
|
/invalid name for global variable - -# \(NameError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2008-07-15 11:26:04 -04:00
|
|
|
assert_in_out_err(%w(- -#=foo), "#!ruby -s\n", [],
|
|
|
|
/invalid name for global variable - -# \(NameError\)/)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
2009-05-07 08:44:25 -04:00
|
|
|
|
2009-09-23 04:08:32 -04:00
|
|
|
def test_assignment_in_conditional
|
|
|
|
t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
|
|
|
|
t.puts "if a = 1"
|
|
|
|
t.puts "end"
|
|
|
|
t.puts "0.times do"
|
|
|
|
t.puts " if b = 2"
|
2010-06-16 04:21:28 -04:00
|
|
|
t.puts " a += b"
|
2009-09-23 04:08:32 -04:00
|
|
|
t.puts " end"
|
|
|
|
t.puts "end"
|
|
|
|
t.close
|
|
|
|
err = ["#{t.path}:1: warning: found = in conditional, should be ==",
|
|
|
|
"#{t.path}:4: warning: found = in conditional, should be =="]
|
|
|
|
err = /\A(#{Regexp.quote(t.path)}):1(: warning: found = in conditional, should be ==)\n\1:4\2\Z/
|
|
|
|
bug2136 = '[ruby-dev:39363]'
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], err, bug2136)
|
|
|
|
assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, bug2136)
|
|
|
|
ensure
|
|
|
|
t.close(true) if t
|
|
|
|
end
|
|
|
|
|
2009-05-07 08:44:25 -04:00
|
|
|
def test_indentation_check
|
|
|
|
t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
|
|
|
|
t.puts "begin"
|
|
|
|
t.puts " end"
|
|
|
|
t.close
|
2009-09-23 04:08:32 -04:00
|
|
|
err = ["#{t.path}:2: warning: mismatched indentations at 'end' with 'begin' at 1"]
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], err)
|
|
|
|
assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err)
|
2010-11-14 02:49:06 -05:00
|
|
|
|
|
|
|
t.open
|
|
|
|
t.puts "# -*- warn-indent: false -*-"
|
|
|
|
t.puts "begin"
|
|
|
|
t.puts " end"
|
|
|
|
t.close
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
|
|
|
|
|
|
|
|
err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 3"]
|
|
|
|
t.open
|
|
|
|
t.puts "# -*- warn-indent: false -*-"
|
|
|
|
t.puts "# -*- warn-indent: true -*-"
|
|
|
|
t.puts "begin"
|
|
|
|
t.puts " end"
|
|
|
|
t.close
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]')
|
|
|
|
|
|
|
|
err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 2"]
|
|
|
|
t.open
|
|
|
|
t.puts "# -*- warn-indent: true -*-"
|
|
|
|
t.puts "begin"
|
|
|
|
t.puts "# -*- warn-indent: false -*-"
|
|
|
|
t.puts " end"
|
|
|
|
t.close
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
|
2009-05-07 08:44:25 -04:00
|
|
|
ensure
|
|
|
|
t.close(true) if t
|
|
|
|
end
|
2009-10-14 00:24:16 -04:00
|
|
|
|
|
|
|
def test_notfound
|
|
|
|
notexist = "./notexist.rb"
|
|
|
|
rubybin = Regexp.quote(EnvUtil.rubybin)
|
2010-06-04 20:57:02 -04:00
|
|
|
pat = Regexp.quote(notexist)
|
|
|
|
bug1573 = '[ruby-core:23717]'
|
2009-10-14 00:24:16 -04:00
|
|
|
assert_equal(false, File.exist?(notexist))
|
2010-07-20 14:30:46 -04:00
|
|
|
assert_in_out_err(["-r", notexist, "-ep"], "", [], /.* -- #{pat} \(LoadError\)/, bug1573)
|
|
|
|
assert_in_out_err([notexist], "", [], /#{rubybin}:.* -- #{pat} \(LoadError\)/, bug1573)
|
2009-10-14 00:24:16 -04:00
|
|
|
end
|
2010-01-25 07:18:50 -05:00
|
|
|
|
2010-03-07 01:54:29 -05:00
|
|
|
def test_program_name
|
|
|
|
ruby = EnvUtil.rubybin
|
|
|
|
IO.popen([ruby, '-e', 'print $0']) {|f|
|
|
|
|
assert_equal('-e', f.read)
|
|
|
|
}
|
|
|
|
IO.popen([ruby, '-'], 'r+') {|f|
|
|
|
|
f << 'print $0'
|
|
|
|
f.close_write
|
|
|
|
assert_equal('-', f.read)
|
|
|
|
}
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
n1 = File.join(d, 't1')
|
|
|
|
open(n1, 'w') {|f| f << 'print $0' }
|
|
|
|
IO.popen([ruby, n1]) {|f|
|
|
|
|
assert_equal(n1, f.read)
|
|
|
|
}
|
|
|
|
if File.respond_to? :symlink
|
|
|
|
n2 = File.join(d, 't2')
|
|
|
|
File.symlink(n1, n2)
|
|
|
|
IO.popen([ruby, n2]) {|f|
|
|
|
|
assert_equal(n2, f.read)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
Dir.chdir(d) {
|
|
|
|
n3 = '-e'
|
|
|
|
open(n3, 'w') {|f| f << 'print $0' }
|
|
|
|
IO.popen([ruby, '--', n3]) {|f|
|
|
|
|
assert_equal(n3, f.read)
|
|
|
|
}
|
|
|
|
n4 = '-'
|
|
|
|
IO.popen([ruby, '--', n4], 'r+') {|f|
|
|
|
|
f << 'print $0'
|
|
|
|
f.close_write
|
|
|
|
assert_equal(n4, f.read)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-05-14 06:53:29 -04:00
|
|
|
def test_set_program_name
|
2012-01-20 03:27:24 -05:00
|
|
|
skip "platform dependent feature" if /linux|freebsd|netbsd|openbsd|darwin/ !~ RUBY_PLATFORM
|
2011-05-14 06:53:29 -04:00
|
|
|
|
2011-05-21 21:06:37 -04:00
|
|
|
with_tmpchdir do
|
|
|
|
write_file("test-script", "$0 = 'hello world'; sleep 60")
|
|
|
|
|
|
|
|
pid = spawn(EnvUtil.rubybin, "test-script")
|
|
|
|
sleep 0.1
|
|
|
|
ps = `ps -p #{pid} -o command`
|
|
|
|
assert_match(/hello world/, ps)
|
|
|
|
Process.kill :KILL, pid
|
|
|
|
end
|
2011-05-14 06:53:29 -04:00
|
|
|
end
|
|
|
|
|
2010-01-25 07:18:50 -05:00
|
|
|
def test_segv_test
|
2010-05-16 08:20:02 -04:00
|
|
|
opts = {}
|
2010-05-14 00:59:15 -04:00
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
additional = '[\s\w\.\']*'
|
|
|
|
else
|
|
|
|
opts[:rlimit_core] = 0
|
|
|
|
additional = ""
|
|
|
|
end
|
2010-01-25 07:18:50 -05:00
|
|
|
assert_in_out_err(["-e", "Process.kill :SEGV, $$"], "", [],
|
|
|
|
%r(\A
|
2010-02-15 04:00:34 -05:00
|
|
|
-e:(?:1:)?\s\[BUG\]\sSegmentation\sfault\n
|
2010-01-25 07:18:50 -05:00
|
|
|
#{ Regexp.quote(RUBY_DESCRIPTION) }\n\n
|
2010-10-23 11:25:20 -04:00
|
|
|
--\sControl\sframe\sinformation\s-+\n
|
2010-01-25 07:18:50 -05:00
|
|
|
(?:c:.*\n)*
|
2010-02-15 04:03:33 -05:00
|
|
|
(?:
|
2010-01-27 09:25:27 -05:00
|
|
|
--\sRuby\slevel\sbacktrace\sinformation\s----------------------------------------\n
|
2010-06-16 04:21:28 -04:00
|
|
|
-e:1:in\s\`<main>\'\n
|
|
|
|
-e:1:in\s\`kill\'\n
|
2010-02-15 04:00:34 -05:00
|
|
|
)?
|
|
|
|
\n
|
2010-01-25 07:18:50 -05:00
|
|
|
(?:
|
|
|
|
--\sC\slevel\sbacktrace\sinformation\s-------------------------------------------\n
|
2010-02-12 11:18:52 -05:00
|
|
|
(?:(?:.*\s)?\[0x\h+\]\n)*\n
|
2010-01-25 07:18:50 -05:00
|
|
|
)?
|
2010-10-23 11:25:20 -04:00
|
|
|
(?m:.*)
|
2010-01-25 07:18:50 -05:00
|
|
|
\[NOTE\]\n
|
|
|
|
You\smay\shave\sencountered\sa\sbug\sin\sthe\sRuby\sinterpreter\sor\sextension\slibraries.\n
|
|
|
|
Bug\sreports\sare\swelcome.\n
|
2010-05-14 00:59:15 -04:00
|
|
|
For\sdetails:\shttp:\/\/www.ruby-lang.org/bugreport.html\n
|
|
|
|
\n
|
|
|
|
(?:#{additional})
|
|
|
|
\z
|
2010-01-25 19:00:38 -05:00
|
|
|
)x,
|
|
|
|
nil,
|
2010-05-14 00:59:15 -04:00
|
|
|
opts)
|
2010-01-25 07:18:50 -05:00
|
|
|
end
|
2010-06-13 02:47:44 -04:00
|
|
|
|
|
|
|
def test_DATA
|
|
|
|
t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"])
|
|
|
|
t.puts "puts DATA.read.inspect"
|
|
|
|
t.puts "__END__"
|
|
|
|
t.puts "foo"
|
|
|
|
t.puts "bar"
|
|
|
|
t.puts "baz"
|
|
|
|
t.close
|
|
|
|
assert_in_out_err([t.path], "", %w("foo\\nbar\\nbaz\\n"), [])
|
|
|
|
ensure
|
|
|
|
t.close(true) if t
|
|
|
|
end
|
2010-06-20 01:41:07 -04:00
|
|
|
|
|
|
|
def test_unused_variable
|
|
|
|
feature3446 = '[ruby-dev:41620]'
|
2010-07-21 23:09:31 -04:00
|
|
|
assert_in_out_err(["-we", "a=1"], "", [], [], feature3446)
|
|
|
|
assert_in_out_err(["-we", "def foo\n a=1\nend"], "", [], ["-e:2: warning: assigned but unused variable - a"], feature3446)
|
|
|
|
assert_in_out_err(["-we", "def foo\n eval('a=1')\nend"], "", [], [], feature3446)
|
|
|
|
assert_in_out_err(["-we", "1.times do\n a=1\nend"], "", [], [], feature3446)
|
|
|
|
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)
|
2010-12-08 07:36:20 -05:00
|
|
|
assert_in_out_err(["-we", "def foo\n"" 1.times do |a| end\n""end"], "", [], [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_shadowing_variable
|
|
|
|
bug4130 = '[ruby-dev:42718]'
|
|
|
|
assert_in_out_err(["-we", "def foo\n"" a=1\n"" 1.times do |a| end\n"" a\n""end"],
|
|
|
|
"", [], ["-e:3: warning: shadowing outer local variable - a"], bug4130)
|
|
|
|
assert_in_out_err(["-we", "def foo\n"" a=1\n"" 1.times do |a| end\n""end"],
|
|
|
|
"", [],
|
|
|
|
["-e:3: warning: shadowing outer local variable - a",
|
|
|
|
"-e:2: warning: assigned but unused variable - a",
|
|
|
|
], bug4130)
|
2010-06-20 01:41:07 -04:00
|
|
|
end
|
2010-06-27 10:31:19 -04:00
|
|
|
|
|
|
|
def test_script_from_stdin
|
|
|
|
begin
|
|
|
|
require 'pty'
|
|
|
|
require 'io/console'
|
|
|
|
rescue LoadError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
require 'timeout'
|
|
|
|
result = nil
|
2010-08-11 11:21:09 -04:00
|
|
|
IO.pipe {|r, w|
|
2010-09-29 08:06:42 -04:00
|
|
|
begin
|
|
|
|
PTY.open {|m, s|
|
2011-08-24 12:23:23 -04:00
|
|
|
s.echo = false
|
2010-09-29 08:06:42 -04:00
|
|
|
m.print("\C-d")
|
|
|
|
pid = spawn(EnvUtil.rubybin, :in => s, :out => w)
|
|
|
|
w.close
|
|
|
|
assert_nothing_raised('[ruby-dev:37798]') do
|
|
|
|
result = Timeout.timeout(3) {r.read}
|
|
|
|
end
|
|
|
|
Process.wait pid
|
|
|
|
}
|
|
|
|
rescue RuntimeError
|
|
|
|
skip $!
|
|
|
|
end
|
2010-08-11 11:21:09 -04:00
|
|
|
}
|
2010-06-29 18:29:12 -04:00
|
|
|
assert_equal("", result, '[ruby-dev:37798]')
|
2010-08-11 11:21:09 -04:00
|
|
|
IO.pipe {|r, w|
|
|
|
|
PTY.open {|m, s|
|
2011-08-24 12:23:23 -04:00
|
|
|
s.echo = false
|
2010-08-11 11:21:09 -04:00
|
|
|
pid = spawn(EnvUtil.rubybin, :in => s, :out => w)
|
|
|
|
w.close
|
|
|
|
m.print("$stdin.read; p $stdin.gets\n\C-d")
|
|
|
|
m.print("abc\n\C-d")
|
|
|
|
m.print("zzz\n")
|
|
|
|
result = r.read
|
2010-08-11 13:16:20 -04:00
|
|
|
Process.wait pid
|
2010-08-11 11:21:09 -04:00
|
|
|
}
|
|
|
|
}
|
2010-06-29 18:29:12 -04:00
|
|
|
assert_equal("\"zzz\\n\"\n", result, '[ruby-core:30910]')
|
2010-06-27 10:31:19 -04:00
|
|
|
end
|
2010-09-20 10:23:16 -04:00
|
|
|
|
|
|
|
def test_unmatching_glob
|
|
|
|
bug3851 = '[ruby-core:32478]'
|
|
|
|
a = "a[foo"
|
|
|
|
Dir.mktmpdir do |dir|
|
|
|
|
open(File.join(dir, a), "w") {|f| f.puts("p 42")}
|
|
|
|
assert_in_out_err(["-C", dir, a], "", ["42"], [], bug3851)
|
|
|
|
File.unlink(File.join(dir, a))
|
|
|
|
assert_in_out_err(["-C", dir, a], "", [], /LoadError/, bug3851)
|
|
|
|
end
|
|
|
|
end
|
2012-03-25 22:46:04 -04:00
|
|
|
|
|
|
|
def test_script_is_directory
|
|
|
|
feature2408 = '[ruby-core:26925]'
|
|
|
|
assert_in_out_err(%w[.], "", [], /Is a directory -- \./, feature2408)
|
|
|
|
end
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|