summaryrefslogtreecommitdiff
path: root/test/ruby/test_rubyoptions.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/ruby/test_rubyoptions.rb')
-rw-r--r--test/ruby/test_rubyoptions.rb532
1 files changed, 423 insertions, 109 deletions
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb
index b6be5011cd..54213c4698 100644
--- a/test/ruby/test_rubyoptions.rb
+++ b/test/ruby/test_rubyoptions.rb
@@ -4,8 +4,6 @@ require 'test/unit'
require 'tmpdir'
require 'tempfile'
-require_relative 'envutil'
-
class TestRubyOptions < Test::Unit::TestCase
def write_file(filename, content)
File.open(filename, "w") {|f|
@@ -50,7 +48,7 @@ class TestRubyOptions < Test::Unit::TestCase
end
assert_in_out_err(%w(-p -l -a -e) + ['p [$-p, $-l, $-a]'],
- "foo\nbar\nbaz\n") do |r, e|
+ "foo\nbar\nbaz") do |r, e|
assert_equal(
[ '[true, true, true]', 'foo',
'[true, true, true]', 'bar',
@@ -59,6 +57,7 @@ class TestRubyOptions < Test::Unit::TestCase
end
end
+
def test_warning
save_rubyopt = ENV['RUBYOPT']
ENV['RUBYOPT'] = nil
@@ -66,6 +65,7 @@ class TestRubyOptions < Test::Unit::TestCase
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), [])
+ assert_in_out_err(%w(-w -W0 -e) + ['p $-W'], "", %w(0), [])
ensure
ENV['RUBYOPT'] = save_rubyopt
end
@@ -85,10 +85,21 @@ class TestRubyOptions < Test::Unit::TestCase
"", %w(true), [])
end
+ q = Regexp.method(:quote)
+ VERSION_PATTERN =
+ case RUBY_ENGINE
+ when 'jruby'
+ /^jruby #{q[RUBY_ENGINE_VERSION]} \(#{q[RUBY_VERSION]}\).*? \[#{
+ q[RbConfig::CONFIG["host_os"]]}-#{q[RbConfig::CONFIG["host_cpu"]]}\]$/
+ else
+ /^ruby #{q[RUBY_VERSION]}(?:[p ]|dev|rc).*? \[#{q[RUBY_PLATFORM]}\]$/
+ end
+ private_constant :VERSION_PATTERN
+
def test_verbose
assert_in_out_err(["-vve", ""]) do |r, e|
- assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
- assert_equal RUBY_DESCRIPTION, r.join.chomp
+ assert_match(VERSION_PATTERN, r[0])
+ assert_equal(RUBY_DESCRIPTION, r[0])
assert_equal([], e)
end
@@ -120,6 +131,8 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err(%w(--disable foobarbazqux -e) + [""], "", [],
/unknown argument for --disable: `foobarbazqux'/)
assert_in_out_err(%w(--disable), "", [], /missing argument for --disable/)
+ assert_in_out_err(%w(--disable-gems -e) + ['p defined? Gem'], "", ["nil"], [])
+ assert_in_out_err(%w(--disable-did_you_mean -e) + ['p defined? DidYouMean'], "", ["nil"], [])
end
def test_kanji
@@ -141,8 +154,8 @@ class TestRubyOptions < Test::Unit::TestCase
def test_version
assert_in_out_err(%w(--version)) do |r, e|
- assert_match(/^ruby #{RUBY_VERSION}(?:[p ]|dev).*? \[#{RUBY_PLATFORM}\]$/, r.join)
- assert_equal RUBY_DESCRIPTION, r.join.chomp
+ assert_match(VERSION_PATTERN, r[0])
+ assert_equal(RUBY_DESCRIPTION, r[0])
assert_equal([], e)
end
end
@@ -172,6 +185,10 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err(%w(-0141 -e) + ["print gets"], "foo\nbar\0baz", %w(foo ba), [])
assert_in_out_err(%w(-0e) + ["print gets"], "foo\nbar\0baz", %W(foo bar\0), [])
+
+ assert_in_out_err(%w(-00 -e) + ["p gets, gets"], "foo\nbar\n\nbaz\nzot\n\n\n", %w("foo\nbar\n\n" "baz\nzot\n\n"), [])
+
+ assert_in_out_err(%w(-00 -e) + ["p gets, gets"], "foo\nbar\n\n\n\nbaz\n", %w("foo\nbar\n\n" "baz\n"), [])
end
def test_autosplit
@@ -193,13 +210,13 @@ class TestRubyOptions < Test::Unit::TestCase
def test_yydebug
assert_in_out_err(["-ye", ""]) do |r, e|
- assert_equal([], r)
- assert_not_equal([], e)
+ assert_not_equal([], r)
+ assert_equal([], e)
end
assert_in_out_err(%w(--yydebug -e) + [""]) do |r, e|
- assert_equal([], r)
- assert_not_equal([], e)
+ assert_not_equal([], r)
+ assert_equal([], e)
end
end
@@ -209,10 +226,12 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err(%w(--encoding test_ruby_test_rubyoptions_foobarbazqux), "", [],
/unknown encoding name - test_ruby_test_rubyoptions_foobarbazqux \(RuntimeError\)/)
- if /mswin|mingw/ =~ RUBY_PLATFORM &&
+ if /mswin|mingw|aix/ =~ RUBY_PLATFORM &&
(str = "\u3042".force_encoding(Encoding.find("locale"))).valid_encoding?
# This result depends on locale because LANG=C doesn't affect locale
# on Windows.
+ # On AIX, the source encoding of stdin with LANG=C is ISO-8859-1,
+ # which allows \u3042.
out, err = [str], []
else
out, err = [], /invalid multibyte char/
@@ -258,6 +277,10 @@ class TestRubyOptions < Test::Unit::TestCase
assert_equal([], e)
end
+ ENV['RUBYOPT'] = '-w'
+ assert_in_out_err(%w(), "p $VERBOSE", ["true"])
+ assert_in_out_err(%w(-W1), "p $VERBOSE", ["false"])
+ assert_in_out_err(%w(-W0), "p $VERBOSE", ["nil"])
ensure
if rubyopt_orig
ENV['RUBYOPT'] = rubyopt_orig
@@ -277,13 +300,15 @@ class TestRubyOptions < Test::Unit::TestCase
@verbose = $VERBOSE
$VERBOSE = nil
- ENV['PATH'] = File.dirname(t.path)
-
- assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), [])
+ path, name = File.split(t.path)
- ENV['RUBYPATH'] = File.dirname(t.path)
+ ENV['PATH'] = (path_orig && RbConfig::CONFIG['LIBPATHENV'] == 'PATH') ?
+ [path, path_orig].join(File::PATH_SEPARATOR) : path
+ assert_in_out_err(%w(-S) + [name], "", %w(1), [])
+ ENV['PATH'] = path_orig
- assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), [])
+ ENV['RUBYPATH'] = path
+ assert_in_out_err(%w(-S) + [name], "", %w(1), [])
}
ensure
@@ -307,16 +332,36 @@ class TestRubyOptions < Test::Unit::TestCase
assert_in_out_err([], "#! /test_r_u_b_y_test_r_u_b_y_options_foobarbazqux -foo -bar\r\np 1\r\n",
[], /: no Ruby script found in input/)
- assert_in_out_err([{'RUBYOPT' => nil}], "#!ruby -KU -Eutf-8\r\np \"\u3042\"\r\n") do |r, e|
- assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8))
- assert_equal([], e)
- end
+ warning = /mswin|mingw/ =~ RUBY_PLATFORM ? [] : /shebang line ending with \\r/
+ assert_in_out_err([{'RUBYOPT' => nil}], "#!ruby -KU -Eutf-8\r\np \"\u3042\"\r\n",
+ ["\"\u3042\""], warning,
+ encoding: Encoding::UTF_8)
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)
+
+ assert_ruby_status(%w[], "#! ruby -- /", '[ruby-core:82267] [Bug #13786]')
+
+ assert_ruby_status(%w[], "#!")
+ assert_in_out_err(%w[-c], "#!", ["Syntax OK"])
+ end
+
+ def test_flag_in_shebang
+ Tempfile.create(%w"pflag .rb") do |script|
+ code = "#!ruby -p"
+ script.puts(code)
+ script.close
+ assert_in_out_err([script.path, script.path], '', [code])
+ end
+ Tempfile.create(%w"sflag .rb") do |script|
+ script.puts("#!ruby -s")
+ script.puts("p $abc")
+ script.close
+ assert_in_out_err([script.path, "-abc=foo"], '', ['"foo"'])
+ end
end
def test_sflag
@@ -372,45 +417,78 @@ class TestRubyOptions < Test::Unit::TestCase
end
def test_indentation_check
- Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t|
- t.puts "begin"
- t.puts " end"
- t.flush
- 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)
+ all_assertions do |a|
+ Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) do |t|
+ [
+ "begin", "if false", "for _ in []", "while false",
+ "def foo", "class X", "module M",
+ ["-> do", "end"], ["-> {", "}"],
+ ].each do
+ |b, e = 'end'|
+ src = ["#{b}\n", " #{e}\n"]
+ k = b[/\A\S+/]
+
+ a.for("no directives with #{b}") do
+ err = ["#{t.path}:2: warning: mismatched indentations at '#{e}' with '#{k}' at 1"]
+ t.rewind
+ t.truncate(0)
+ t.puts src
+ t.flush
+ assert_in_out_err(["-w", t.path], "", [], err)
+ assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err)
+ end
- t.rewind
- t.puts "# -*- warn-indent: false -*-"
- t.puts "begin"
- t.puts " end"
- t.flush
- assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
+ a.for("false directive with #{b}") do
+ t.rewind
+ t.truncate(0)
+ t.puts "# -*- warn-indent: false -*-"
+ t.puts src
+ t.flush
+ assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
+ end
- err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 3"]
- t.rewind
- t.puts "# -*- warn-indent: false -*-"
- t.puts "# -*- warn-indent: true -*-"
- t.puts "begin"
- t.puts " end"
- t.flush
- assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]')
+ a.for("false and true directives with #{b}") do
+ err = ["#{t.path}:4: warning: mismatched indentations at '#{e}' with '#{k}' at 3"]
+ t.rewind
+ t.truncate(0)
+ t.puts "# -*- warn-indent: false -*-"
+ t.puts "# -*- warn-indent: true -*-"
+ t.puts src
+ t.flush
+ assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]')
+ end
- err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 2"]
- t.rewind
- t.puts "# -*- warn-indent: true -*-"
- t.puts "begin"
- t.puts "# -*- warn-indent: false -*-"
- t.puts " end"
- t.flush
- assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
- }
+ a.for("false directives after #{b}") do
+ t.rewind
+ t.truncate(0)
+ t.puts "# -*- warn-indent: true -*-"
+ t.puts src[0]
+ t.puts "# -*- warn-indent: false -*-"
+ t.puts src[1]
+ t.flush
+ assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]')
+ end
+
+ a.for("BOM with #{b}") do
+ err = ["#{t.path}:2: warning: mismatched indentations at '#{e}' with '#{k}' at 1"]
+ t.rewind
+ t.truncate(0)
+ t.print "\u{feff}"
+ t.puts src
+ t.flush
+ assert_in_out_err(["-w", t.path], "", [], err)
+ assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err)
+ end
+ end
+ end
+ end
end
def test_notfound
notexist = "./notexist.rb"
- rubybin = EnvUtil.rubybin.dup
- rubybin.gsub!(%r(/), '\\') if /mswin|mingw/ =~ RUBY_PLATFORM
+ dir, *rubybin = RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME', 'EXEEXT')
+ rubybin = "#{dir}/#{rubybin.join('')}"
+ rubybin.tr!('/', '\\') if /mswin|mingw/ =~ RUBY_PLATFORM
rubybin = Regexp.quote(rubybin)
pat = Regexp.quote(notexist)
bug1573 = '[ruby-core:23717]'
@@ -437,10 +515,14 @@ class TestRubyOptions < Test::Unit::TestCase
}
if File.respond_to? :symlink
n2 = File.join(d, 't2')
- File.symlink(n1, n2)
- IO.popen([ruby, n2]) {|f|
- assert_equal(n2, f.read)
- }
+ begin
+ File.symlink(n1, n2)
+ rescue Errno::EACCES
+ else
+ IO.popen([ruby, n2]) {|f|
+ assert_equal(n2, f.read)
+ }
+ end
end
Dir.chdir(d) {
n3 = '-e'
@@ -458,8 +540,13 @@ class TestRubyOptions < Test::Unit::TestCase
}
end
+ if /linux|freebsd|netbsd|openbsd|darwin/ =~ RUBY_PLATFORM
+ PSCMD = EnvUtil.find_executable("ps", "-o", "command", "-p", $$.to_s) {|out| /ruby/=~out}
+ PSCMD.pop if PSCMD
+ end
+
def test_set_program_name
- skip "platform dependent feature" if /linux|freebsd|netbsd|openbsd|darwin/ !~ RUBY_PLATFORM
+ skip "platform dependent feature" unless defined?(PSCMD) and PSCMD
with_tmpchdir do
write_file("test-script", "$0 = 'hello world'; /test-script/ =~ Process.argv0 or $0 = 'Process.argv0 changed!'; sleep 60")
@@ -468,7 +555,7 @@ class TestRubyOptions < Test::Unit::TestCase
ps = nil
10.times do
sleep 0.1
- ps = `ps -p #{pid} -o command`
+ ps = `#{PSCMD.join(' ')} #{pid}`
break if /hello world/ =~ ps
end
assert_match(/hello world/, ps)
@@ -478,7 +565,14 @@ class TestRubyOptions < Test::Unit::TestCase
end
def test_setproctitle
- skip "platform dependent feature" if /linux|freebsd|netbsd|openbsd|darwin/ !~ RUBY_PLATFORM
+ skip "platform dependent feature" unless defined?(PSCMD) and PSCMD
+
+ assert_separately([], "#{<<-"{#"}\n#{<<-'};'}")
+ {#
+ assert_raise(ArgumentError) do
+ Process.setproctitle("hello\0")
+ end
+ };
with_tmpchdir do
write_file("test-script", "$_0 = $0.dup; Process.setproctitle('hello world'); $0 == $_0 or Process.setproctitle('$0 changed!'); sleep 60")
@@ -487,7 +581,7 @@ class TestRubyOptions < Test::Unit::TestCase
ps = nil
10.times do
sleep 0.1
- ps = `ps -p #{pid} -o command`
+ ps = `#{PSCMD.join(' ')} #{pid}`
break if /hello world/ =~ ps
end
assert_match(/hello world/, ps)
@@ -499,74 +593,86 @@ class TestRubyOptions < Test::Unit::TestCase
module SEGVTest
opts = {}
if /mswin|mingw/ =~ RUBY_PLATFORM
- additional = '[\s\w\.\']*'
+ additional = /[\s\w\.\']*/
else
opts[:rlimit_core] = 0
- additional = ""
+ additional = nil
end
ExecOptions = opts.freeze
- ExpectedStderr =
- %r(\A
- -e:(?:1:)?\s\[BUG\]\sSegmentation\sfault.*\n
- #{ Regexp.quote(RUBY_DESCRIPTION) }\n\n
- (?:--\s(?:.+\n)*\n)?
- --\sControl\sframe\sinformation\s-+\n
- (?:c:.*\n)*
- (?:
- --\sRuby\slevel\sbacktrace\sinformation\s----------------------------------------\n
- -e:1:in\s\`<main>\'\n
- -e:1:in\s\`kill\'\n
- )?
- \n
- (?:
- --\sC\slevel\sbacktrace\sinformation\s-------------------------------------------\n
- (?:(?:.*\s)?\[0x\h+\]\n)*\n
- )?
- (?m:.*)
- \[NOTE\]\n
- You\smay\shave\sencountered\sa\sbug\sin\sthe\sRuby\sinterpreter\sor\sextension\slibraries.\n
- Bug\sreports\sare\swelcome.\n
- (?:.*\n)?
- For\sdetails:\shttp:\/\/.*\.ruby-lang\.org/.*\n
- \n
- (?:#{additional})
- \z
- )x
+ ExpectedStderrList = [
+ %r(
+ -e:(?:1:)?\s\[BUG\]\sSegmentation\sfault.*\n
+ )x,
+ %r(
+ #{ Regexp.quote(RUBY_DESCRIPTION) }\n\n
+ )x,
+ %r(
+ (?:--\s(?:.+\n)*\n)?
+ --\sControl\sframe\sinformation\s-+\n
+ (?:c:.*\n)*
+ )x,
+ %r(
+ (?:
+ --\sRuby\slevel\sbacktrace\sinformation\s----------------------------------------\n
+ -e:1:in\s\`<main>\'\n
+ -e:1:in\s\`kill\'\n
+ )?
+ )x,
+ %r(
+ (?:
+ --\sC\slevel\sbacktrace\sinformation\s-------------------------------------------\n
+ (?:(?:.*\s)?\[0x\h+\]\n)*\n
+ )?
+ )x,
+ :*,
+ %r(
+ \[NOTE\]\n
+ You\smay\shave\sencountered\sa\sbug\sin\sthe\sRuby\sinterpreter\sor\sextension\slibraries.\n
+ Bug\sreports\sare\swelcome.\n
+ (?:.*\n)?
+ For\sdetails:\shttp:\/\/.*\.ruby-lang\.org/.*\n
+ \n
+ (?:
+ \[IMPORTANT\]\n
+ (?:.+\n)+
+ \n
+ )?
+ )x,
+ ]
+ ExpectedStderrList << additional if additional
+ end
+
+ def assert_segv(args, message=nil)
+ test_stdin = ""
+ opt = SEGVTest::ExecOptions.dup
+ list = SEGVTest::ExpectedStderrList
+
+ assert_in_out_err(args, test_stdin, //, list, encoding: "ASCII-8BIT", **opt)
end
def test_segv_test
- opts = SEGVTest::ExecOptions.dup
- expected_stderr = SEGVTest::ExpectedStderr
-
- assert_in_out_err(["--disable-gems", "-e", "Process.kill :SEGV, $$"], "", [], expected_stderr, nil, opts)
+ assert_segv(["--disable-gems", "-e", "Process.kill :SEGV, $$"])
end
def test_segv_loaded_features
- opts = SEGVTest::ExecOptions.dup
-
bug7402 = '[ruby-core:49573]'
- status = assert_in_out_err(['-e', 'class Bogus; def to_str; exit true; end; end',
- '-e', '$".clear',
- '-e', '$".unshift Bogus.new',
- '-e', '(p $"; abort) unless $".size == 1',
- '-e', 'Process.kill :SEGV, $$'],
- "", [], /#<Bogus:/,
- nil,
- opts)
+
+ status = assert_segv(['-e', 'END {Process.kill :SEGV, $$}',
+ '-e', 'class Bogus; def to_str; exit true; end; end',
+ '-e', '$".clear',
+ '-e', '$".unshift Bogus.new',
+ '-e', '(p $"; abort) unless $".size == 1',
+ ])
assert_not_predicate(status, :success?, "segv but success #{bug7402}")
end
def test_segv_setproctitle
- opts = SEGVTest::ExecOptions.dup
- expected_stderr = SEGVTest::ExpectedStderr
-
bug7597 = '[ruby-dev:46786]'
Tempfile.create(["test_ruby_test_bug7597", ".rb"]) {|t|
t.write "f" * 100
t.flush
- assert_in_out_err(["--disable-gems", "-e", "$0=ARGV[0]; Process.kill :SEGV, $$", t.path],
- "", [], expected_stderr, bug7597, opts)
+ assert_segv(["--disable-gems", "-e", "$0=ARGV[0]; Process.kill :SEGV, $$", t.path], bug7597)
}
end
@@ -665,6 +771,92 @@ class TestRubyOptions < Test::Unit::TestCase
end
end
+ case RUBY_PLATFORM
+ when /mswin|mingw/
+ def test_command_line_glob_nonascii
+ bug10555 = '[ruby-dev:48752] [Bug #10555]'
+ name = "\u{3042}.txt"
+ expected = name.encode("locale") rescue "?.txt"
+ with_tmpchdir do |dir|
+ open(name, "w") {}
+ assert_in_out_err(["-e", "puts ARGV", "?.txt"], "", [expected], [],
+ bug10555, encoding: "locale")
+ end
+ end
+
+ def test_command_line_progname_nonascii
+ bug10555 = '[ruby-dev:48752] [Bug #10555]'
+ name = expected = nil
+ unless (0x80..0x10000).any? {|c|
+ name = c.chr(Encoding::UTF_8)
+ expected = name.encode("locale") rescue nil
+ }
+ skip "can't make locale name"
+ end
+ name << ".rb"
+ expected << ".rb"
+ with_tmpchdir do |dir|
+ open(name, "w") {|f| f.puts "puts File.basename($0)"}
+ assert_in_out_err([name], "", [expected], [],
+ bug10555, encoding: "locale")
+ end
+ end
+
+ def test_command_line_glob_with_dir
+ bug10941 = '[ruby-core:68430] [Bug #10941]'
+ with_tmpchdir do |dir|
+ Dir.mkdir('test')
+ assert_in_out_err(["-e", "", "test/*"], "", [], [], bug10941)
+ end
+ end
+
+ Ougai = %W[\u{68ee}O\u{5916}.txt \u{68ee 9d0e 5916}.txt \u{68ee 9dd7 5916}.txt]
+ def test_command_line_glob_noncodepage
+ with_tmpchdir do |dir|
+ Ougai.each {|f| open(f, "w") {}}
+ assert_in_out_err(["-Eutf-8", "-e", "puts ARGV", "*"], "", Ougai, encoding: "utf-8")
+ ougai = Ougai.map {|f| f.encode("locale", replace: "?")}
+ assert_in_out_err(["-e", "puts ARGV", "*.txt"], "", ougai)
+ end
+ end
+
+ def assert_e_script_encoding(str, args = [])
+ cmds = [
+ EnvUtil::LANG_ENVS.inject({}) {|h, k| h[k] = ENV[k]; h},
+ *args,
+ '-e', "s = '#{str}'",
+ '-e', 'puts s.encoding.name',
+ '-e', 'puts s.dump',
+ ]
+ assert_in_out_err(cmds, "", [str.encoding.name, str.dump], [],
+ "#{str.encoding}:#{str.dump} #{args.inspect}")
+ end
+
+ # tested codepages: 437 850 852 855 932 65001
+ # Since the codepage is shared all processes per conhost.exe, do
+ # not chcp, or parallel test may break.
+ def test_locale_codepage
+ locale = Encoding.find("locale")
+ list = %W"\u{c7} \u{452} \u{3066 3059 3068}"
+ list.each do |s|
+ assert_e_script_encoding(s, %w[-U])
+ end
+ list.each do |s|
+ s = s.encode(locale) rescue next
+ assert_e_script_encoding(s)
+ assert_e_script_encoding(s, %W[-E#{locale.name}])
+ end
+ end
+ when /cygwin/
+ def test_command_line_non_ascii
+ assert_separately([{"LC_ALL"=>"ja_JP.SJIS"}, "-", "\u{3042}".encode("SJIS")], <<-"end;")
+ bug12184 = '[ruby-dev:49519] [Bug #12184]'
+ a = ARGV[0]
+ assert_equal([Encoding::SJIS, 130, 160], [a.encoding, *a.bytes], bug12184)
+ end;
+ end
+ end
+
def test_script_is_directory
feature2408 = '[ruby-core:26925]'
assert_in_out_err(%w[.], "", [], /Is a directory -- \./, feature2408)
@@ -679,4 +871,126 @@ class TestRubyOptions < Test::Unit::TestCase
bug7157 = '[ruby-core:47967]'
assert_in_out_err(['-p', '-e', 'sub(/t.*/){"TEST"}'], %[test], %w[TEST], [], bug7157)
end
+
+ def assert_norun_with_rflag(*opt)
+ bug10435 = "[ruby-dev:48712] [Bug #10435]: should not run with #{opt} option"
+ stderr = []
+ Tempfile.create(%w"bug10435- .rb") do |script|
+ dir, base = File.split(script.path)
+ script.puts "abort ':run'"
+ script.close
+ opts = ['-C', dir, '-r', "./#{base}", *opt]
+ _, e = assert_in_out_err([*opts, '-ep'], "", //)
+ stderr.concat(e) if e
+ stderr << "---"
+ _, e = assert_in_out_err([*opts, base], "", //)
+ stderr.concat(e) if e
+ end
+ assert_not_include(stderr, ":run", bug10435)
+ end
+
+ def test_dump_syntax_with_rflag
+ assert_norun_with_rflag('-c')
+ assert_norun_with_rflag('--dump=syntax')
+ end
+
+ def test_dump_yydebug_with_rflag
+ assert_norun_with_rflag('-y')
+ assert_norun_with_rflag('--dump=yydebug')
+ end
+
+ def test_dump_parsetree_with_rflag
+ assert_norun_with_rflag('--dump=parsetree')
+ assert_norun_with_rflag('--dump=parsetree', '-e', '#frozen-string-literal: true')
+ end
+
+ def test_dump_insns_with_rflag
+ assert_norun_with_rflag('--dump=insns')
+ end
+
+ def test_frozen_string_literal
+ all_assertions do |a|
+ [["disable", "false"], ["enable", "true"]].each do |opt, exp|
+ %W[frozen_string_literal frozen-string-literal].each do |arg|
+ key = "#{opt}=#{arg}"
+ a.for(key) do
+ assert_in_out_err(["--disable=gems", "--#{key}"], 'p("foo".frozen?)', [exp])
+ end
+ end
+ end
+ %W"disable enable".product(%W[false true]) do |opt, exp|
+ a.for("#{opt}=>#{exp}") do
+ assert_in_out_err(["-w", "--disable=gems", "--#{opt}=frozen-string-literal"], <<-"end;", [exp])
+ #-*- frozen-string-literal: #{exp} -*-
+ p("foo".frozen?)
+ end;
+ end
+ end
+ end
+ end
+
+ def test_frozen_string_literal_debug
+ with_debug_pat = /created at/
+ wo_debug_pat = /can\'t modify frozen String \(FrozenError\)\n\z/
+ frozen = [
+ ["--enable-frozen-string-literal", true],
+ ["--disable-frozen-string-literal", false],
+ [nil, false],
+ ]
+ debugs = [
+ ["--debug-frozen-string-literal", true],
+ ["--debug=frozen-string-literal", true],
+ ["--debug", true],
+ [nil, false],
+ ]
+ opts = ["--disable=gems"]
+ frozen.product(debugs) do |(opt1, freeze), (opt2, debug)|
+ opt = opts + [opt1, opt2].compact
+ err = !freeze ? [] : debug ? with_debug_pat : wo_debug_pat
+ assert_in_out_err(opt, '"foo" << "bar"', [], err)
+ if freeze
+ assert_in_out_err(opt, '"foo#{123}bar" << "bar"', [], err)
+ end
+ end
+ end
+
+ def test___dir__encoding
+ lang = {"LC_ALL"=>ENV["LC_ALL"]||ENV["LANG"]}
+ with_tmpchdir do
+ testdir = "\u30c6\u30b9\u30c8"
+ Dir.mkdir(testdir)
+ Dir.chdir(testdir) do
+ open("test.rb", "w") do |f|
+ f.puts <<-END
+ if __FILE__.encoding == __dir__.encoding
+ p true
+ else
+ puts "__FILE__: \#{__FILE__.encoding}, __dir__: \#{__dir__.encoding}"
+ end
+ END
+ end
+ r, = EnvUtil.invoke_ruby([lang, "test.rb"], "", true)
+ assert_equal "true", r.chomp, "the encoding of __FILE__ and __dir__ should be same"
+ end
+ end
+ end
+
+ def test_cwd_encoding
+ with_tmpchdir do
+ testdir = "\u30c6\u30b9\u30c8"
+ Dir.mkdir(testdir)
+ Dir.chdir(testdir) do
+ File.write("a.rb", "require './b'")
+ File.write("b.rb", "puts 'ok'")
+ assert_ruby_status([{"RUBYLIB"=>"."}, *%w[-E cp932:utf-8 a.rb]])
+ end
+ end
+ end
+
+ def test_argv_tainted
+ assert_separately(%w[- arg], "#{<<~"begin;"}\n#{<<~'end;'}")
+ begin;
+ assert_predicate(ARGV[0], :tainted?, '[ruby-dev:50596] [Bug #14941]')
+ end;
+ end
end