diff options
Diffstat (limited to 'test/ruby/test_system.rb')
| -rw-r--r-- | test/ruby/test_system.rb | 118 |
1 files changed, 98 insertions, 20 deletions
diff --git a/test/ruby/test_system.rb b/test/ruby/test_system.rb index f1372781cf..3fcdaa6aad 100644 --- a/test/ruby/test_system.rb +++ b/test/ruby/test_system.rb @@ -1,6 +1,6 @@ +# frozen_string_literal: false require 'test/unit' require 'tmpdir' -require_relative 'envutil' class TestSystem < Test::Unit::TestCase def test_system @@ -84,30 +84,34 @@ class TestSystem < Test::Unit::TestCase ENV["PATH"] = path end File.unlink tmpfilename + + testname = '[ruby-core:44505]' + assert_match(/Windows/, `ver`, testname) + assert_equal 0, $?.to_i, testname end } end def test_system_at - if /mswin|mingw/ =~ RUBY_PLATFORM - bug4393 = '[ruby-core:35218]' + if /mswin|mingw/ =~ RUBY_PLATFORM + bug4393 = '[ruby-core:35218]' - # @ + builtin command - assert_equal("foo\n", `@echo foo`, bug4393); - assert_equal("foo\n", `@@echo foo`, bug4393); - assert_equal("@@foo\n", `@@echo @@foo`, bug4393); + # @ + builtin command + assert_equal("foo\n", `@echo foo`, bug4393); + assert_equal("foo\n", `@@echo foo`, bug4393); + assert_equal("@@foo\n", `@@echo @@foo`, bug4393); - # @ + non builtin command - Dir.mktmpdir("ruby_script_tmp") {|tmpdir| - tmpfilename = "#{tmpdir}/ruby_script_tmp.#{$$}" + # @ + non builtin command + Dir.mktmpdir("ruby_script_tmp") {|tmpdir| + tmpfilename = "#{tmpdir}/ruby_script_tmp.#{$$}" - tmp = open(tmpfilename, "w") - tmp.print "foo\nbar\nbaz\n@foo"; - tmp.close + tmp = open(tmpfilename, "w") + tmp.print "foo\nbar\nbaz\n@foo"; + tmp.close - assert_match(/\Abar\nbaz\n?\z/, `@@findstr "ba" #{tmpfilename.gsub("/", "\\")}`, bug4393); - } - end + assert_match(/\Abar\nbaz\n?\z/, `@@findstr "ba" #{tmpfilename.gsub("/", "\\")}`, bug4393); + } + end end def test_system_redirect_win @@ -115,11 +119,44 @@ class TestSystem < Test::Unit::TestCase return end - cmd = "%WINDIR%/system32/ping.exe \"BFI3CHL671\" > out.txt 2>NUL" - assert_equal(false, system(cmd), '[ruby-talk:258939]'); + Dir.mktmpdir("ruby_script_tmp") do |tmpdir| + cmd = nil + message = proc do + [ + '[ruby-talk:258939]', + "out.txt:", + *File.readlines("out.txt").map{|s|" "+s.inspect}, + "err.txt:", + *File.readlines("err.txt").map{|s|" "+s.inspect}, + "system(#{cmd.inspect})" + ].join("\n") + end + class << message + alias to_s call + end + Dir.chdir(tmpdir) do + open("input.txt", "w") {|f| f.puts "BFI3CHL671"} + cmd = "%WINDIR%/system32/find.exe \"BFI3CHL671\" input.txt > out.txt 2>err.txt" + assert_equal(true, system(cmd), message) + cmd = "\"%WINDIR%/system32/find.exe\" \"BFI3CHL671\" input.txt > out.txt 2>err.txt" + assert_equal(true, system(cmd), message) + cmd = "\"%WINDIR%/system32/find.exe BFI3CHL671\" input.txt > out.txt 2>err.txt" + assert_equal(false, system(cmd), message) + end + end + end - cmd = "\"%WINDIR%/system32/ping.exe BFI3CHL671\" > out.txt 2>NUL" - assert_equal(false, system(cmd), '[ruby-talk:258939]'); + def test_system_closed + assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}") + begin; + ios = [] + ObjectSpace.each_object(IO) {|io| ios << io} + `echo` + ObjectSpace.each_object(IO) do |io| + next if ios.include?(io) + assert_nothing_raised {io.close} + end + end; end def test_empty_evstr @@ -136,4 +173,45 @@ class TestSystem < Test::Unit::TestCase assert_equal(true, system(tmpfilename), '[ruby-core:32745]') } end if File.executable?("/bin/sh") + + def test_system_exception + ruby = EnvUtil.rubybin + assert_nothing_raised do + system('feature_14235', exception: false) + end + assert_nothing_raised do + system(ruby, "-e", "abort", exception: false) + end + assert_nothing_raised do + system("'#{ruby}' -e abort", exception: false) + end + assert_raise(Errno::ENOENT) do + system('feature_14235', exception: true) + end + assert_raise_with_message(RuntimeError, /\ACommand failed with exit /) do + system(ruby, "-e", "abort", exception: true) + end + assert_raise_with_message(RuntimeError, /\ACommand failed with exit /) do + system("'#{ruby}' -e abort", exception: true) + end + end + + def test_system_exception_nonascii + Dir.mktmpdir("ruby_script_tmp") do |tmpdir| + name = "\u{30c6 30b9 30c8}" + tmpfilename = "#{tmpdir}/#{name}.cmd" + message = /#{name}\.cmd/ + assert_raise_with_message(Errno::ENOENT, message) do + system(tmpfilename, exception: true) + end + open(tmpfilename, "w") {|f| + f.print "@" if /mingw|mswin/ =~ RUBY_PLATFORM + f.puts "exit 127" + f.chmod(0755) + } + assert_raise_with_message(RuntimeError, message) do + system(tmpfilename, exception: true) + end + end + end end |
