diff options
Diffstat (limited to 'test/ruby/test_rubyoptions.rb')
| -rw-r--r-- | test/ruby/test_rubyoptions.rb | 277 |
1 files changed, 93 insertions, 184 deletions
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index 214b89c9b8..54213c4698 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -1,19 +1,10 @@ # -*- coding: us-ascii -*- require 'test/unit' -require 'timeout' require 'tmpdir' require 'tempfile' -require_relative '../lib/jit_support' class TestRubyOptions < Test::Unit::TestCase - NO_JIT_DESCRIPTION = - if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # checking -DMJIT_FORCE_ENABLE - RUBY_DESCRIPTION.sub(/\+JIT /, '') - else - RUBY_DESCRIPTION - end - def write_file(filename, content) File.open(filename, "w") {|f| f << content @@ -35,7 +26,7 @@ class TestRubyOptions < Test::Unit::TestCase def test_usage assert_in_out_err(%w(-h)) do |r, e| - assert_operator(r.size, :<=, 25) + assert_operator(r.size, :<=, 24) longer = r[1..-1].select {|x| x.size > 80} assert_equal([], longer) assert_equal([], e) @@ -72,23 +63,21 @@ class TestRubyOptions < Test::Unit::TestCase ENV['RUBYOPT'] = nil 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(2), []) + 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(-We) + ['p $-W'], "", %w(2), []) assert_in_out_err(%w(-w -W0 -e) + ['p $-W'], "", %w(0), []) - assert_in_out_err(%w(-W:deprecated -e) + ['p Warning[:deprecated]'], "", %w(true), []) - assert_in_out_err(%w(-W:no-deprecated -e) + ['p Warning[:deprecated]'], "", %w(false), []) - assert_in_out_err(%w(-W:experimental -e) + ['p Warning[:experimental]'], "", %w(true), []) - assert_in_out_err(%w(-W:no-experimental -e) + ['p Warning[:experimental]'], "", %w(false), []) - assert_in_out_err(%w(-W:qux), "", [], /unknown warning category: `qux'/) - assert_in_out_err(%w(-w -e) + ['p Warning[:deprecated]'], "", %w(true), []) - assert_in_out_err(%w(-W -e) + ['p Warning[:deprecated]'], "", %w(true), []) - assert_in_out_err(%w(-We) + ['p Warning[:deprecated]'], "", %w(true), []) - assert_in_out_err(%w(-e) + ['p Warning[:deprecated]'], "", %w(false), []) ensure ENV['RUBYOPT'] = save_rubyopt end + def test_safe_level + assert_in_out_err(%w(-T -e) + [""], "", [], + /no -e allowed in tainted mode \(SecurityError\)/) + + assert_in_out_err(%w(-T4 -S foo.rb), "", [], + /no -S allowed in tainted mode \(SecurityError\)/) + end + def test_debug assert_in_out_err(["--disable-gems", "-de", "p $DEBUG"], "", %w(true), []) @@ -107,23 +96,10 @@ class TestRubyOptions < Test::Unit::TestCase end private_constant :VERSION_PATTERN - VERSION_PATTERN_WITH_JIT = - case RUBY_ENGINE - when 'ruby' - /^ruby #{q[RUBY_VERSION]}(?:[p ]|dev|rc).*? \+JIT \[#{q[RUBY_PLATFORM]}\]$/ - else - VERSION_PATTERN - end - private_constant :VERSION_PATTERN_WITH_JIT - def test_verbose assert_in_out_err(["-vve", ""]) do |r, e| assert_match(VERSION_PATTERN, r[0]) - if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? && !mjit_force_enabled? # checking -DMJIT_FORCE_ENABLE - assert_equal(NO_JIT_DESCRIPTION, r[0]) - else - assert_equal(RUBY_DESCRIPTION, r[0]) - end + assert_equal(RUBY_DESCRIPTION, r[0]) assert_equal([], e) end @@ -140,11 +116,9 @@ class TestRubyOptions < Test::Unit::TestCase end def test_enable - if JITSupport.supported? - assert_in_out_err(%w(--enable all -e) + [""], "", [], []) - assert_in_out_err(%w(--enable-all -e) + [""], "", [], []) - assert_in_out_err(%w(--enable=all -e) + [""], "", [], []) - end + 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/) @@ -159,7 +133,6 @@ class TestRubyOptions < Test::Unit::TestCase 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"], []) - assert_in_out_err(%w(--disable-gems -e) + ['p defined? DidYouMean'], "", ["nil"], []) end def test_kanji @@ -182,45 +155,9 @@ class TestRubyOptions < Test::Unit::TestCase def test_version assert_in_out_err(%w(--version)) do |r, e| assert_match(VERSION_PATTERN, r[0]) - if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # checking -DMJIT_FORCE_ENABLE - assert_equal(EnvUtil.invoke_ruby(['-e', 'print RUBY_DESCRIPTION'], '', true).first, r[0]) - else - assert_equal(RUBY_DESCRIPTION, r[0]) - end + assert_equal(RUBY_DESCRIPTION, r[0]) assert_equal([], e) end - - return if RbConfig::CONFIG["MJIT_SUPPORT"] == 'no' - - [ - %w(--version --jit --disable=jit), - %w(--version --enable=jit --disable=jit), - %w(--version --enable-jit --disable-jit), - ].each do |args| - assert_in_out_err(args) do |r, e| - assert_match(VERSION_PATTERN, r[0]) - assert_match(NO_JIT_DESCRIPTION, r[0]) - assert_equal([], e) - end - end - - if JITSupport.supported? - [ - %w(--version --jit), - %w(--version --enable=jit), - %w(--version --enable-jit), - ].each do |args| - assert_in_out_err(args) do |r, e| - assert_match(VERSION_PATTERN_WITH_JIT, r[0]) - if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? # checking -DMJIT_FORCE_ENABLE - assert_equal(RUBY_DESCRIPTION, r[0]) - else - assert_equal(EnvUtil.invoke_ruby(['--jit', '-e', 'print RUBY_DESCRIPTION'], '', true).first, r[0]) - end - assert_equal([], e) - end - end - end end def test_eval @@ -255,7 +192,7 @@ class TestRubyOptions < Test::Unit::TestCase end def test_autosplit - assert_in_out_err(%w(-W0 -an -F: -e) + ["p $F"], "foo:bar:baz\nqux:quux:quuux\n", + 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"]'], []) end @@ -289,8 +226,8 @@ 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|aix|android/ =~ RUBY_PLATFORM && - (str = "\u3042".force_encoding(Encoding.find("external"))).valid_encoding? + 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, @@ -312,7 +249,7 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(%W(-\r -e) + [""], "", [], []) - assert_in_out_err(%W(-\rx), "", [], /invalid option -\\r \(-h will show valid options\) \(RuntimeError\)/) + assert_in_out_err(%W(-\rx), "", [], /invalid option -\\x0D \(-h will show valid options\) \(RuntimeError\)/) assert_in_out_err(%W(-\x01), "", [], /invalid option -\\x01 \(-h will show valid options\) \(RuntimeError\)/) @@ -328,6 +265,12 @@ class TestRubyOptions < Test::Unit::TestCase ENV['RUBYOPT'] = '-e "p 1"' assert_in_out_err([], "", [], /invalid switch in RUBYOPT: -e \(RuntimeError\)/) + ENV['RUBYOPT'] = '-T1' + assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/) + + ENV['RUBYOPT'] = '-T4' + assert_in_out_err(["--disable-gems"], "", [], /no program input from stdin allowed in tainted mode \(SecurityError\)/) + ENV['RUBYOPT'] = '-Eus-ascii -KN' assert_in_out_err(%w(-Eutf-8 -KU), "p '\u3042'") do |r, e| assert_equal("\"\u3042\"", r.join.force_encoding(Encoding::UTF_8)) @@ -338,20 +281,6 @@ class TestRubyOptions < Test::Unit::TestCase 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"]) - assert_in_out_err(%w(), "p Warning[:deprecated]", ["true"]) - assert_in_out_err(%w(-W0), "p Warning[:deprecated]", ["false"]) - assert_in_out_err(%w(-W1), "p Warning[:deprecated]", ["false"]) - assert_in_out_err(%w(-W2), "p Warning[:deprecated]", ["true"]) - ENV['RUBYOPT'] = '-W:deprecated' - assert_in_out_err(%w(), "p Warning[:deprecated]", ["true"]) - ENV['RUBYOPT'] = '-W:no-deprecated' - assert_in_out_err(%w(), "p Warning[:deprecated]", ["false"]) - ENV['RUBYOPT'] = '-W:experimental' - assert_in_out_err(%w(), "p Warning[:experimental]", ["true"]) - ENV['RUBYOPT'] = '-W:no-experimental' - assert_in_out_err(%w(), "p Warning[:experimental]", ["false"]) - ENV['RUBYOPT'] = '-W:qux' - assert_in_out_err(%w(), "", [], /unknown warning category: `qux'/) ensure if rubyopt_orig ENV['RUBYOPT'] = rubyopt_orig @@ -457,7 +386,7 @@ class TestRubyOptions < Test::Unit::TestCase t.puts " end" t.puts "end" t.flush - warning = ' warning: found `= literal\' in conditional, should be ==' + warning = ' warning: found = in conditional, should be ==' err = ["#{t.path}:1:#{warning}", "#{t.path}:4:#{warning}", ] @@ -494,27 +423,13 @@ class TestRubyOptions < Test::Unit::TestCase "begin", "if false", "for _ in []", "while false", "def foo", "class X", "module M", ["-> do", "end"], ["-> {", "}"], - ["if false;", "else ; end"], - ["if false;", "elsif false ; end"], - ["begin", "rescue ; end"], - ["begin rescue", "else ; end"], - ["begin", "ensure ; end"], - [" case nil", "when true; end"], - ["case nil; when true", "end"], - ["if false;", "end", "if true\nelse ", "end"], - ["else", " end", "_ = if true\n"], - ["begin\n def f() = nil", "end"], - ["begin\n def self.f() = nil", "end"], ].each do - |b, e = 'end', pre = nil, post = nil| - src = ["#{pre}#{b}\n", " #{e}\n#{post}"] - k = b[/\A\s*(\S+)/, 1] - e = e[/\A\s*(\S+)/, 1] - n = 1 + src[0].count("\n") - n1 = 1 + (pre ? pre.count("\n") : 0) - - a.for("no directives with #{src}") do - err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n1}"] + |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 @@ -523,7 +438,7 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err) end - a.for("false directive with #{src}") do + a.for("false directive with #{b}") do t.rewind t.truncate(0) t.puts "# -*- warn-indent: false -*-" @@ -532,8 +447,8 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]') end - a.for("false and true directives with #{src}") do - err = ["#{t.path}:#{n+2}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n1+2}"] + 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 -*-" @@ -543,7 +458,7 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]') end - a.for("false directives after #{src}") do + a.for("false directives after #{b}") do t.rewind t.truncate(0) t.puts "# -*- warn-indent: true -*-" @@ -554,8 +469,8 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]') end - a.for("BOM with #{src}") do - err = ["#{t.path}:#{n}: warning: mismatched indentations at '#{e}' with '#{k}' at #{n1}"] + 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}" @@ -627,7 +542,7 @@ class TestRubyOptions < Test::Unit::TestCase if /linux|freebsd|netbsd|openbsd|darwin/ =~ RUBY_PLATFORM PSCMD = EnvUtil.find_executable("ps", "-o", "command", "-p", $$.to_s) {|out| /ruby/=~out} - PSCMD&.pop + PSCMD.pop if PSCMD end def test_set_program_name @@ -638,18 +553,14 @@ class TestRubyOptions < Test::Unit::TestCase pid = spawn(EnvUtil.rubybin, "test-script") ps = nil - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) - stop = now + 30 - begin + 10.times do sleep 0.1 ps = `#{PSCMD.join(' ')} #{pid}` break if /hello world/ =~ ps - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) - end until Process.wait(pid, Process::WNOHANG) || now > stop + end assert_match(/hello world/, ps) - assert_operator now, :<, stop Process.kill :KILL, pid - EnvUtil.timeout(5) { Process.wait(pid) } + Process.wait(pid) end end @@ -668,25 +579,24 @@ class TestRubyOptions < Test::Unit::TestCase pid = spawn(EnvUtil.rubybin, "test-script") ps = nil - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) - stop = now + 30 - begin + 10.times do sleep 0.1 ps = `#{PSCMD.join(' ')} #{pid}` break if /hello world/ =~ ps - now = Process.clock_gettime(Process::CLOCK_MONOTONIC) - end until Process.wait(pid, Process::WNOHANG) || now > stop + end assert_match(/hello world/, ps) - assert_operator now, :<, stop Process.kill :KILL, pid - Timeout.timeout(5) { Process.wait(pid) } + Process.wait(pid) end end module SEGVTest opts = {} - unless /mswin|mingw/ =~ RUBY_PLATFORM + if /mswin|mingw/ =~ RUBY_PLATFORM + additional = /[\s\w\.\']*/ + else opts[:rlimit_core] = 0 + additional = nil end ExecOptions = opts.freeze @@ -695,42 +605,45 @@ class TestRubyOptions < Test::Unit::TestCase -e:(?:1:)?\s\[BUG\]\sSegmentation\sfault.*\n )x, %r( - #{ Regexp.quote(NO_JIT_DESCRIPTION) }\n\n + #{ Regexp.quote(RUBY_DESCRIPTION) }\n\n )x, %r( (?:--\s(?:.+\n)*\n)? --\sControl\sframe\sinformation\s-+\n - (?:(?:c:.*\n)|(?:^\s+.+\n))* - \n + (?:c:.*\n)* )x, %r( (?: --\sRuby\slevel\sbacktrace\sinformation\s----------------------------------------\n - (?:-e:1:in\s\`(?:block\sin\s)?<main>\'\n)* + -e:1:in\s\`<main>\'\n -e:1:in\s\`kill\'\n - \n )? )x, %r( - (?:--\sMachine(?:.+\n)*\n)? - )x, - %r( (?: --\sC\slevel\sbacktrace\sinformation\s-------------------------------------------\n - (?:(?:.*\s)?\[0x\h+\].*\n|.*:\d+\n)*\n + (?:(?:.*\s)?\[0x\h+\]\n)*\n )? )x, + :*, %r( - (?:--\sOther\sruntime\sinformation\s-+\n - (?:.*\n)* + \[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) - skip if ENV['RUBY_ON_BUG'] - test_stdin = "" opt = SEGVTest::ExecOptions.dup list = SEGVTest::ExpectedStderrList @@ -792,6 +705,20 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(["-w", "-"], "eval('a=1')", [], [], feature7730) 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) + feature6693 = '[ruby-core:46160]' + assert_in_out_err(["-we", "def foo\n"" _a=1\n"" 1.times do |_a| end\n""end"], + "", [], [], feature6693) + end + def test_script_from_stdin begin require 'pty' @@ -809,7 +736,7 @@ class TestRubyOptions < Test::Unit::TestCase pid = spawn(EnvUtil.rubybin, :in => s, :out => w) w.close assert_nothing_raised('[ruby-dev:37798]') do - result = EnvUtil.timeout(3) {r.read} + result = Timeout.timeout(3) {r.read} end Process.wait pid } @@ -849,11 +776,11 @@ class TestRubyOptions < Test::Unit::TestCase def test_command_line_glob_nonascii bug10555 = '[ruby-dev:48752] [Bug #10555]' name = "\u{3042}.txt" - expected = name.encode("external") rescue "?.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: "external") + bug10555, encoding: "locale") end end @@ -888,7 +815,7 @@ class TestRubyOptions < Test::Unit::TestCase 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("external", replace: "?")} + ougai = Ougai.map {|f| f.encode("locale", replace: "?")} assert_in_out_err(["-e", "puts ARGV", "*.txt"], "", ougai) end end @@ -986,10 +913,8 @@ class TestRubyOptions < Test::Unit::TestCase [["disable", "false"], ["enable", "true"]].each do |opt, exp| %W[frozen_string_literal frozen-string-literal].each do |arg| key = "#{opt}=#{arg}" - negopt = exp == "true" ? "disable" : "enable" - env = {"RUBYOPT"=>"--#{negopt}=#{arg}"} a.for(key) do - assert_in_out_err([env, "--disable=gems", "--#{key}"], 'p("foo".frozen?)', [exp]) + assert_in_out_err(["--disable=gems", "--#{key}"], 'p("foo".frozen?)', [exp]) end end end @@ -1006,7 +931,7 @@ class TestRubyOptions < Test::Unit::TestCase def test_frozen_string_literal_debug with_debug_pat = /created at/ - wo_debug_pat = /can\'t modify frozen String: "\w+" \(FrozenError\)\n\z/ + wo_debug_pat = /can\'t modify frozen String \(FrozenError\)\n\z/ frozen = [ ["--enable-frozen-string-literal", true], ["--disable-frozen-string-literal", false], @@ -1022,13 +947,9 @@ class TestRubyOptions < Test::Unit::TestCase frozen.product(debugs) do |(opt1, freeze), (opt2, debug)| opt = opts + [opt1, opt2].compact err = !freeze ? [] : debug ? with_debug_pat : wo_debug_pat - [ - ['"foo" << "bar"', err], - ['"foo#{123}bar" << "bar"', []], - ['+"foo#{123}bar" << "bar"', []], - ['-"foo#{123}bar" << "bar"', wo_debug_pat], - ].each do |code, expected| - assert_in_out_err(opt, code, [], expected, "#{opt} #{code}") + assert_in_out_err(opt, '"foo" << "bar"', [], err) + if freeze + assert_in_out_err(opt, '"foo#{123}bar" << "bar"', [], err) end end end @@ -1066,22 +987,10 @@ class TestRubyOptions < Test::Unit::TestCase end end - def test_null_script - skip "#{IO::NULL} is not a character device" unless File.chardev?(IO::NULL) - assert_in_out_err([IO::NULL], success: true) - end - - def test_jit_debug - # mswin uses prebuilt precompiled header. Thus it does not show a pch compilation log to check "-O0 -O1". - if JITSupport.supported? && !RUBY_PLATFORM.match?(/mswin/) - env = { 'MJIT_SEARCH_BUILD_DIR' => 'true' } - assert_in_out_err([env, "--jit-debug=-O0 -O1", "--jit-verbose=2", "" ], "", [], /-O0 -O1/) - end - end - - private - - def mjit_force_enabled? - "#{RbConfig::CONFIG['CFLAGS']} #{RbConfig::CONFIG['CPPFLAGS']}".match?(/(\A|\s)-D ?MJIT_FORCE_ENABLE\b/) + 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 |
