2012-11-23 03:31:52 -05:00
|
|
|
# -*- coding: us-ascii -*-
|
2008-04-16 11:48:54 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
require 'tmpdir'
|
|
|
|
require 'tempfile'
|
2018-02-22 10:11:12 -05:00
|
|
|
require_relative '../lib/jit_support'
|
2011-05-21 21:06:37 -04:00
|
|
|
|
2008-04-16 11:48:54 -04:00
|
|
|
class TestRubyOptions < Test::Unit::TestCase
|
2018-04-28 05:11:25 -04:00
|
|
|
NO_JIT_DESCRIPTION =
|
|
|
|
if RubyVM::MJIT.enabled?
|
|
|
|
RUBY_DESCRIPTION.sub(/\+JIT /, '')
|
|
|
|
else
|
|
|
|
RUBY_DESCRIPTION
|
|
|
|
end
|
|
|
|
|
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|
|
2013-07-03 01:11:58 -04:00
|
|
|
d = File.realpath(d)
|
2011-05-21 21:06:37 -04:00
|
|
|
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|
|
mjit.c: merge MJIT infrastructure
that allows to JIT-compile Ruby methods by generating C code and
using C compiler. See the first comment of mjit.c to know what this
file does.
mjit.c is authored by Vladimir Makarov <vmakarov@redhat.com>.
After he invented great method JIT infrastructure for MRI as MJIT,
Lars Kanis <lars@greiz-reinsdorf.de> sent the patch to support MinGW
in MJIT. In addition to merging it, I ported pthread to Windows native
threads. Now this MJIT infrastructure can be compiled on Visual Studio.
This commit simplifies mjit.c to decrease code at initial merge. For
example, this commit does not provide multiple JIT threads support.
We can resurrect them later if we really want them, but I wanted to minimize
diff to make it easier to review this patch.
`/tmp/_mjitXXX` file is renamed to `/tmp/_ruby_mjitXXX` because non-Ruby
developers may not know the name "mjit" and the file name should make
sure it's from Ruby and not from some harmful programs. TODO: it may be
better to store this to some temporary directory which Ruby is already using
by Tempfile, if it's not bad for performance.
mjit.h: New. It has `mjit_exec` interface similar to `vm_exec`, which is
for triggering MJIT. This drops interface for AOT compared to the original
MJIT.
Makefile.in: define macros to let MJIT know the path of MJIT header.
Probably we can refactor this to reduce the number of macros (TODO).
win32/Makefile.sub: ditto.
common.mk: compile mjit.o and mjit_compile.o. Unlike original MJIT, this
commit separates MJIT infrastructure and JIT compiler code as independent
object files. As initial patch is NOT going to have ultra-fast JIT compiler,
it's likely to replace JIT compiler, e.g. original MJIT's compiler or some
future JIT impelementations which are not public now.
inits.c: define MJIT module. This is added because `MJIT.enabled?` was
necessary for testing.
test/lib/zombie_hunter.rb: skip if `MJIT.enabled?`. Obviously this
wouldn't work with current code when JIT is enabled.
test/ruby/test_io.rb: skip this too. This would make no sense with MJIT.
ruby.c: define MJIT CLI options. As major difference from original MJIT,
"-j:l"/"--jit:llvm" are renamed to "--jit-cc" because I want to support
not only gcc/clang but also cl.exe (Visual Studio) in the future. But it
takes only "--jit-cc=gcc", "--jit-cc=clang" for now. And only long "--jit"
options are allowed since some Ruby committers preferred it at Ruby
developers Meeting on January, and some of options are renamed.
This file also triggers to initialize MJIT thread and variables.
eval.c: finalize MJIT worker thread and variables.
test/ruby/test_rubyoptions.rb: fix number of CLI options for --jit.
thread_pthread.c: change for pthread abstraction in MJIT. Prefix rb_ for
functions which are used by other files.
thread_win32.c: ditto, for Windows. Those pthread porting is one of major
works that YARV-MJIT created, which is my fork of MJIT, in Feature 14235.
thread.c: follow rb_ prefix changes
vm.c: trigger MJIT call on VM invocation. Also trigger `mjit_mark` to avoid
SEGV by race between JIT and GC of ISeq. The improvement was provided by
wanabe <s.wanabe@gmail.com>.
In JIT compiler I created and am going to add in my next commit, I found
that having `mjit_exec` after `vm_loop_start:` is harmful because the
JIT-ed function doesn't proceed other ISeqs on RESTORE_REGS of leave insn.
Executing non-FINISH frame is unexpected for my JIT compiler and
`exception_handler` triggers executions of such ISeqs. So `mjit_exec`
here should be executed only when it directly comes from `vm_exec` call.
`RubyVM::MJIT` module and `.enabled?` method is added so that we can skip
some tests which don't expect JIT threads or compiler file descriptors.
vm_insnhelper.h: trigger MJIT on method calls during VM execution.
vm_core.h: add fields required for mjit.c. `bp` must be `cfp[6]` because
rb_control_frame_struct is likely to be casted to another struct. The
last position is the safest place to add the new field.
vm_insnhelper.c: save initial value of cfp->ep as cfp->bp. This is an
optimization which are done in both MJIT and YARV-MJIT. So this change
is added in this commit. Calculating bp from ep is a little heavy work,
so bp is kind of cache for it.
iseq.c: notify ISeq GC to MJIT. We should know which iseq in MJIT queue
is GCed to avoid SEGV. TODO: unload some GCed units in some safe way.
gc.c: add hooks so that MJIT can wait GC, and vice versa. Simultaneous
JIT and GC executions may cause SEGV and so we should synchronize them.
cont.c: save continuation information in MJIT worker. As MJIT shouldn't
unload JIT-ed code which is being used, MJIT wants to know full list of
saved execution contexts for continuation and detect ISeqs in use.
mjit_compile.c: added empty JIT compiler so that you can reuse this commit
to build your own JIT compiler. This commit tries to compile ISeqs but
all of them are considered as not supported in this commit. So you can't
use JIT compiler in this commit yet while we added --jit option now.
Patch author: Vladimir Makarov <vmakarov@redhat.com>.
Contributors:
Takashi Kokubun <takashikkbn@gmail.com>.
wanabe <s.wanabe@gmail.com>.
Lars Kanis <lars@greiz-reinsdorf.de>.
Part of Feature 12589 and 14235.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62189 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 01:58:09 -05:00
|
|
|
assert_operator(r.size, :<=, 25)
|
2012-11-23 03:31:52 -05:00
|
|
|
longer = r[1..-1].select {|x| x.size > 80}
|
|
|
|
assert_equal([], longer)
|
2012-11-22 07:36:22 -05:00
|
|
|
assert_equal([], e)
|
2012-11-23 03:31:57 -05:00
|
|
|
end
|
2012-11-24 05:31:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_usage_long
|
2012-11-23 03:31:57 -05:00
|
|
|
assert_in_out_err(%w(--help)) do |r, e|
|
|
|
|
longer = r[1..-1].select {|x| x.size > 80}
|
|
|
|
assert_equal([], longer)
|
|
|
|
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]'],
|
2017-01-19 02:18:23 -05:00
|
|
|
"foo\nbar\nbaz") 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
|
|
|
|
|
2017-01-19 02:18:23 -05:00
|
|
|
|
2008-04-16 11:48:54 -04:00
|
|
|
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), [])
|
2015-12-29 07:23:04 -05:00
|
|
|
assert_in_out_err(%w(-w -W0 -e) + ['p $-W'], "", %w(0), [])
|
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
|
|
|
|
|
2016-03-14 20:28:17 -04:00
|
|
|
q = Regexp.method(:quote)
|
|
|
|
VERSION_PATTERN =
|
2016-03-14 12:24:04 -04:00
|
|
|
case RUBY_ENGINE
|
|
|
|
when 'jruby'
|
2016-03-14 20:28:17 -04:00
|
|
|
/^jruby #{q[RUBY_ENGINE_VERSION]} \(#{q[RUBY_VERSION]}\).*? \[#{
|
|
|
|
q[RbConfig::CONFIG["host_os"]]}-#{q[RbConfig::CONFIG["host_cpu"]]}\]$/
|
2016-03-14 12:24:04 -04:00
|
|
|
else
|
2016-03-14 20:28:17 -04:00
|
|
|
/^ruby #{q[RUBY_VERSION]}(?:[p ]|dev|rc).*? \[#{q[RUBY_PLATFORM]}\]$/
|
2016-03-14 12:24:04 -04:00
|
|
|
end
|
2016-03-14 20:28:17 -04:00
|
|
|
private_constant :VERSION_PATTERN
|
2016-03-14 12:24:04 -04:00
|
|
|
|
2018-02-22 09:53:17 -05:00
|
|
|
VERSION_PATTERN_WITH_JIT =
|
|
|
|
case RUBY_ENGINE
|
2018-02-22 10:34:17 -05:00
|
|
|
when 'ruby'
|
2018-02-22 09:53:17 -05:00
|
|
|
/^ruby #{q[RUBY_VERSION]}(?:[p ]|dev|rc).*? \+JIT \[#{q[RUBY_PLATFORM]}\]$/
|
2018-02-22 10:34:17 -05:00
|
|
|
else
|
|
|
|
VERSION_PATTERN
|
2018-02-22 09:53:17 -05:00
|
|
|
end
|
|
|
|
private_constant :VERSION_PATTERN_WITH_JIT
|
|
|
|
|
2008-04-16 11:48:54 -04:00
|
|
|
def test_verbose
|
2011-01-13 21:02:12 -05:00
|
|
|
assert_in_out_err(["-vve", ""]) do |r, e|
|
2016-03-14 20:28:17 -04:00
|
|
|
assert_match(VERSION_PATTERN, r[0])
|
2018-05-29 04:47:07 -04:00
|
|
|
assert_equal(RUBY_DESCRIPTION, r[0])
|
2008-07-15 11:26:04 -04:00
|
|
|
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/)
|
2016-03-14 15:39:24 -04:00
|
|
|
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"], [])
|
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-04 12:01:01 -04:00
|
|
|
line = '-eputs"\xc2\xa1".encoding'
|
|
|
|
env = {'RUBYOPT' => nil}
|
|
|
|
assert_in_out_err([env, '-Ke', line], "", ["EUC-JP"], [])
|
|
|
|
assert_in_out_err([env, '-KE', line], "", ["EUC-JP"], [])
|
|
|
|
assert_in_out_err([env, '-Ks', line], "", ["Windows-31J"], [])
|
|
|
|
assert_in_out_err([env, '-KS', line], "", ["Windows-31J"], [])
|
|
|
|
assert_in_out_err([env, '-Ku', line], "", ["UTF-8"], [])
|
|
|
|
assert_in_out_err([env, '-KU', line], "", ["UTF-8"], [])
|
|
|
|
assert_in_out_err([env, '-Kn', line], "", ["ASCII-8BIT"], [])
|
|
|
|
assert_in_out_err([env, '-KN', line], "", ["ASCII-8BIT"], [])
|
|
|
|
assert_in_out_err([env, '-wKe', line], "", ["EUC-JP"], /-K/)
|
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|
|
2016-03-14 20:28:17 -04:00
|
|
|
assert_match(VERSION_PATTERN, r[0])
|
2018-02-22 09:53:17 -05:00
|
|
|
if RubyVM::MJIT.enabled?
|
|
|
|
assert_equal(EnvUtil.invoke_ruby(['-e', 'print RUBY_DESCRIPTION'], '', true).first, r[0])
|
|
|
|
else
|
|
|
|
assert_equal(RUBY_DESCRIPTION, r[0])
|
|
|
|
end
|
|
|
|
assert_equal([], e)
|
|
|
|
end
|
2018-02-22 10:11:12 -05:00
|
|
|
if JITSupport.supported?
|
|
|
|
assert_in_out_err(%w(--version --jit)) do |r, e|
|
|
|
|
assert_match(VERSION_PATTERN_WITH_JIT, r[0])
|
|
|
|
if RubyVM::MJIT.enabled?
|
|
|
|
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)
|
2018-02-22 09:53:17 -05:00
|
|
|
end
|
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), [])
|
2017-07-10 03:37:18 -04:00
|
|
|
|
|
|
|
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"), [])
|
2017-09-19 20:53:47 -04:00
|
|
|
|
|
|
|
assert_in_out_err(%w(-00 -e) + ["p gets, gets"], "foo\nbar\n\n\n\nbaz\n", %w("foo\nbar\n\n" "baz\n"), [])
|
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|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_file.identical?(r.join, d)
|
2008-07-15 11:26:04 -04:00
|
|
|
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|
|
2015-12-13 09:46:09 -05:00
|
|
|
assert_not_equal([], 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(--yydebug -e) + [""]) do |r, e|
|
2015-12-13 09:46:09 -05:00
|
|
|
assert_not_equal([], r)
|
|
|
|
assert_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(--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
|
|
|
|
2014-10-07 15:33:48 -04:00
|
|
|
if /mswin|mingw|aix/ =~ RUBY_PLATFORM &&
|
2012-07-31 16:58:08 -04:00
|
|
|
(str = "\u3042".force_encoding(Encoding.find("locale"))).valid_encoding?
|
|
|
|
# This result depends on locale because LANG=C doesn't affect locale
|
|
|
|
# on Windows.
|
2014-10-07 15:33:48 -04:00
|
|
|
# On AIX, the source encoding of stdin with LANG=C is ISO-8859-1,
|
|
|
|
# which allows \u3042.
|
2012-07-31 16:58:08 -04:00
|
|
|
out, err = [str], []
|
|
|
|
else
|
|
|
|
out, err = [], /invalid multibyte char/
|
|
|
|
end
|
|
|
|
assert_in_out_err(%w(-Eutf-8), "puts '\u3042'", out, err)
|
|
|
|
assert_in_out_err(%w(--encoding utf-8), "puts '\u3042'", out, err)
|
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-04 12:01:01 -04:00
|
|
|
assert_equal([], e)
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
2015-12-29 05:12:48 -05:00
|
|
|
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"])
|
2008-04-16 11:48:54 -04:00
|
|
|
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']
|
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t|
|
|
|
|
t.puts "p 1"
|
|
|
|
t.close
|
2008-04-16 11:48:54 -04:00
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
@verbose = $VERBOSE
|
|
|
|
$VERBOSE = nil
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2017-09-15 08:03:41 -04:00
|
|
|
path, name = File.split(t.path)
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2017-09-13 13:07:19 -04:00
|
|
|
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
|
2008-04-16 11:48:54 -04:00
|
|
|
|
2017-09-13 13:07:19 -04:00
|
|
|
ENV['RUBYPATH'] = path
|
|
|
|
assert_in_out_err(%w(-S) + [name], "", %w(1), [])
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
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
|
|
|
|
$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
|
|
|
|
2017-06-01 00:48:05 -04:00
|
|
|
warning = /mswin|mingw/ =~ RUBY_PLATFORM ? [] : /shebang line ending with \\r/
|
2016-03-03 23:34:17 -05:00
|
|
|
assert_in_out_err([{'RUBYOPT' => nil}], "#!ruby -KU -Eutf-8\r\np \"\u3042\"\r\n",
|
2016-03-07 14:39:32 -05:00
|
|
|
["\"\u3042\""], warning,
|
|
|
|
encoding: Encoding::UTF_8)
|
2010-12-04 21:27:13 -05:00
|
|
|
|
|
|
|
bug4118 = '[ruby-dev:42680]'
|
2012-10-25 10:06:31 -04:00
|
|
|
assert_in_out_err(%w[], "#!/bin/sh\n""#!shebang\n""#!ruby\n""puts __LINE__\n",
|
2010-12-04 21:27:13 -05:00
|
|
|
%w[4], [], bug4118)
|
2012-10-25 10:06:31 -04:00
|
|
|
assert_in_out_err(%w[-x], "#!/bin/sh\n""#!shebang\n""#!ruby\n""puts __LINE__\n",
|
2010-12-04 21:27:13 -05:00
|
|
|
%w[4], [], bug4118)
|
2017-08-10 01:54:56 -04:00
|
|
|
|
|
|
|
assert_ruby_status(%w[], "#! ruby -- /", '[ruby-core:82267] [Bug #13786]')
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|
|
|
|
|
2017-10-24 07:24:19 -04:00
|
|
|
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
|
|
|
|
|
2008-04-16 11:48:54 -04:00
|
|
|
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
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t|
|
|
|
|
t.puts "if a = 1"
|
|
|
|
t.puts "end"
|
|
|
|
t.puts "0.times do"
|
|
|
|
t.puts " if b = 2"
|
|
|
|
t.puts " a += b"
|
|
|
|
t.puts " end"
|
|
|
|
t.puts "end"
|
|
|
|
t.flush
|
2018-02-28 23:10:25 -05:00
|
|
|
warning = ' warning: found `= literal\' in conditional, should be =='
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
err = ["#{t.path}:1:#{warning}",
|
|
|
|
"#{t.path}:4:#{warning}",
|
|
|
|
]
|
|
|
|
bug2136 = '[ruby-dev:39363]'
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], err, bug2136)
|
|
|
|
assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, bug2136)
|
|
|
|
|
|
|
|
t.rewind
|
|
|
|
t.truncate(0)
|
|
|
|
t.puts "if a = ''; end"
|
|
|
|
t.puts "if a = []; end"
|
|
|
|
t.puts "if a = [1]; end"
|
|
|
|
t.puts "if a = [a]; end"
|
|
|
|
t.puts "if a = {}; end"
|
|
|
|
t.puts "if a = {1=>2}; end"
|
|
|
|
t.puts "if a = {3=>a}; end"
|
|
|
|
t.flush
|
|
|
|
err = ["#{t.path}:1:#{warning}",
|
|
|
|
"#{t.path}:2:#{warning}",
|
|
|
|
"#{t.path}:3:#{warning}",
|
|
|
|
"#{t.path}:5:#{warning}",
|
|
|
|
"#{t.path}:6:#{warning}",
|
|
|
|
]
|
|
|
|
feature4299 = '[ruby-dev:43083]'
|
|
|
|
assert_in_out_err(["-w", t.path], "", [], err, feature4299)
|
|
|
|
assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, feature4299)
|
|
|
|
}
|
2009-09-23 04:08:32 -04:00
|
|
|
end
|
|
|
|
|
2009-05-07 08:44:25 -04:00
|
|
|
def test_indentation_check
|
2016-03-22 01:13:55 -04:00
|
|
|
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",
|
2016-03-22 01:19:01 -04:00
|
|
|
["-> do", "end"], ["-> {", "}"],
|
2018-03-19 04:21:26 -04:00
|
|
|
["if false;", "else ; end"],
|
|
|
|
["if false;", "elsif false ; end"],
|
|
|
|
["begin", "rescue ; end"],
|
|
|
|
["begin rescue", "else ; end"],
|
|
|
|
["begin", "ensure ; end"],
|
2018-04-12 18:13:09 -04:00
|
|
|
[" case nil", "when true; end"],
|
2018-04-12 08:01:51 -04:00
|
|
|
["case nil; when true", "end"],
|
2016-03-22 01:13:55 -04:00
|
|
|
].each do
|
|
|
|
|b, e = 'end'|
|
|
|
|
src = ["#{b}\n", " #{e}\n"]
|
2018-04-12 18:13:09 -04:00
|
|
|
k = b[/\A\s*(\S+)/, 1]
|
|
|
|
e = e[/\A\s*(\S+)/, 1]
|
2016-03-22 01:13:55 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
2017-10-11 01:34:24 -04:00
|
|
|
|
|
|
|
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
|
2016-03-22 01:13:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-07 08:44:25 -04:00
|
|
|
end
|
2009-10-14 00:24:16 -04:00
|
|
|
|
|
|
|
def test_notfound
|
|
|
|
notexist = "./notexist.rb"
|
2017-05-30 07:59:26 -04:00
|
|
|
dir, *rubybin = RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME', 'EXEEXT')
|
|
|
|
rubybin = "#{dir}/#{rubybin.join('')}"
|
2017-07-13 19:55:41 -04:00
|
|
|
rubybin.tr!('/', '\\') if /mswin|mingw/ =~ RUBY_PLATFORM
|
2013-07-05 03:58:14 -04:00
|
|
|
rubybin = Regexp.quote(rubybin)
|
2010-06-04 20:57:02 -04:00
|
|
|
pat = Regexp.quote(notexist)
|
|
|
|
bug1573 = '[ruby-core:23717]'
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.not_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')
|
2015-08-27 21:24:36 -04:00
|
|
|
begin
|
|
|
|
File.symlink(n1, n2)
|
|
|
|
rescue Errno::EACCES
|
|
|
|
else
|
|
|
|
IO.popen([ruby, n2]) {|f|
|
|
|
|
assert_equal(n2, f.read)
|
|
|
|
}
|
|
|
|
end
|
2010-03-07 01:54:29 -05:00
|
|
|
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
|
|
|
|
|
2014-07-07 04:42:57 -04:00
|
|
|
if /linux|freebsd|netbsd|openbsd|darwin/ =~ RUBY_PLATFORM
|
|
|
|
PSCMD = EnvUtil.find_executable("ps", "-o", "command", "-p", $$.to_s) {|out| /ruby/=~out}
|
2018-05-09 08:51:14 -04:00
|
|
|
PSCMD&.pop
|
2014-07-07 04:42:57 -04:00
|
|
|
end
|
|
|
|
|
2011-05-14 06:53:29 -04:00
|
|
|
def test_set_program_name
|
2014-07-07 04:42:57 -04:00
|
|
|
skip "platform dependent feature" unless defined?(PSCMD) and PSCMD
|
2011-05-14 06:53:29 -04:00
|
|
|
|
2011-05-21 21:06:37 -04:00
|
|
|
with_tmpchdir do
|
2013-08-07 10:12:08 -04:00
|
|
|
write_file("test-script", "$0 = 'hello world'; /test-script/ =~ Process.argv0 or $0 = 'Process.argv0 changed!'; sleep 60")
|
2011-05-21 21:06:37 -04:00
|
|
|
|
2013-08-07 10:12:04 -04:00
|
|
|
pid = spawn(EnvUtil.rubybin, "test-script")
|
|
|
|
ps = nil
|
2018-05-31 02:57:47 -04:00
|
|
|
begin
|
2013-08-07 10:12:04 -04:00
|
|
|
sleep 0.1
|
2014-07-07 04:42:57 -04:00
|
|
|
ps = `#{PSCMD.join(' ')} #{pid}`
|
2013-08-07 10:12:04 -04:00
|
|
|
break if /hello world/ =~ ps
|
2018-05-31 03:10:39 -04:00
|
|
|
end until Process.wait(pid, Process::WNOHANG)
|
2013-08-07 10:12:04 -04:00
|
|
|
assert_match(/hello world/, ps)
|
|
|
|
Process.kill :KILL, pid
|
|
|
|
Process.wait(pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_setproctitle
|
2014-07-07 04:42:57 -04:00
|
|
|
skip "platform dependent feature" unless defined?(PSCMD) and PSCMD
|
2013-08-07 10:12:04 -04:00
|
|
|
|
2017-10-19 07:24:03 -04:00
|
|
|
assert_separately([], "#{<<-"{#"}\n#{<<-'};'}")
|
|
|
|
{#
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
Process.setproctitle("hello\0")
|
|
|
|
end
|
|
|
|
};
|
|
|
|
|
2013-08-07 10:12:04 -04:00
|
|
|
with_tmpchdir do
|
|
|
|
write_file("test-script", "$_0 = $0.dup; Process.setproctitle('hello world'); $0 == $_0 or Process.setproctitle('$0 changed!'); sleep 60")
|
|
|
|
|
2011-05-21 21:06:37 -04:00
|
|
|
pid = spawn(EnvUtil.rubybin, "test-script")
|
2013-03-21 10:55:26 -04:00
|
|
|
ps = nil
|
2018-05-31 02:57:47 -04:00
|
|
|
begin
|
2013-03-21 10:55:26 -04:00
|
|
|
sleep 0.1
|
2014-07-07 04:42:57 -04:00
|
|
|
ps = `#{PSCMD.join(' ')} #{pid}`
|
2013-03-21 10:55:26 -04:00
|
|
|
break if /hello world/ =~ ps
|
2018-05-31 03:10:39 -04:00
|
|
|
end until Process.wait(pid, Process::WNOHANG)
|
2011-05-21 21:06:37 -04:00
|
|
|
assert_match(/hello world/, ps)
|
|
|
|
Process.kill :KILL, pid
|
2013-06-19 03:47:12 -04:00
|
|
|
Process.wait(pid)
|
2011-05-21 21:06:37 -04:00
|
|
|
end
|
2011-05-14 06:53:29 -04:00
|
|
|
end
|
|
|
|
|
2013-07-27 14:10:36 -04:00
|
|
|
module SEGVTest
|
2010-05-16 08:20:02 -04:00
|
|
|
opts = {}
|
2010-05-14 00:59:15 -04:00
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
2014-10-15 06:57:17 -04:00
|
|
|
additional = /[\s\w\.\']*/
|
2010-05-14 00:59:15 -04:00
|
|
|
else
|
|
|
|
opts[:rlimit_core] = 0
|
2014-10-15 06:57:17 -04:00
|
|
|
additional = nil
|
2010-05-14 00:59:15 -04:00
|
|
|
end
|
2013-07-27 14:10:36 -04:00
|
|
|
ExecOptions = opts.freeze
|
|
|
|
|
2014-10-14 20:04:50 -04:00
|
|
|
ExpectedStderrList = [
|
|
|
|
%r(
|
|
|
|
-e:(?:1:)?\s\[BUG\]\sSegmentation\sfault.*\n
|
|
|
|
)x,
|
|
|
|
%r(
|
2018-04-28 05:11:25 -04:00
|
|
|
#{ Regexp.quote(NO_JIT_DESCRIPTION) }\n\n
|
2014-10-14 20:04:50 -04:00
|
|
|
)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
|
|
|
|
)?
|
2014-10-15 01:59:11 -04:00
|
|
|
)x,
|
|
|
|
%r(
|
2014-10-14 20:04:50 -04:00
|
|
|
(?:
|
|
|
|
--\sC\slevel\sbacktrace\sinformation\s-------------------------------------------\n
|
|
|
|
(?:(?:.*\s)?\[0x\h+\]\n)*\n
|
|
|
|
)?
|
2014-10-14 23:27:37 -04:00
|
|
|
)x,
|
|
|
|
:*,
|
|
|
|
%r(
|
2014-10-14 20:04:50 -04:00
|
|
|
\[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
|
2017-01-12 21:57:45 -05:00
|
|
|
(?:
|
|
|
|
\[IMPORTANT\]\n
|
2017-01-12 22:24:22 -05:00
|
|
|
(?:.+\n)+
|
2017-01-12 21:57:45 -05:00
|
|
|
\n
|
|
|
|
)?
|
2014-10-15 06:57:17 -04:00
|
|
|
)x,
|
2014-10-14 20:04:50 -04:00
|
|
|
]
|
2014-10-15 06:57:17 -04:00
|
|
|
ExpectedStderrList << additional if additional
|
2013-07-27 14:10:36 -04:00
|
|
|
end
|
|
|
|
|
2014-10-14 20:04:50 -04:00
|
|
|
def assert_segv(args, message=nil)
|
|
|
|
test_stdin = ""
|
|
|
|
opt = SEGVTest::ExecOptions.dup
|
2015-03-03 03:19:43 -05:00
|
|
|
list = SEGVTest::ExpectedStderrList
|
2013-07-27 14:10:36 -04:00
|
|
|
|
2015-03-03 03:19:43 -05:00
|
|
|
assert_in_out_err(args, test_stdin, //, list, encoding: "ASCII-8BIT", **opt)
|
2014-10-14 20:04:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_segv_test
|
2014-10-16 14:11:49 -04:00
|
|
|
assert_segv(["--disable-gems", "-e", "Process.kill :SEGV, $$"])
|
2013-07-27 14:10:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_segv_loaded_features
|
2012-12-11 12:13:18 -05:00
|
|
|
bug7402 = '[ruby-core:49573]'
|
2013-12-25 02:28:05 -05:00
|
|
|
|
2015-03-03 00:20:22 -05:00
|
|
|
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',
|
|
|
|
])
|
2012-12-11 12:13:18 -05:00
|
|
|
assert_not_predicate(status, :success?, "segv but success #{bug7402}")
|
2013-07-27 14:10:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_segv_setproctitle
|
2012-12-25 03:00:50 -05:00
|
|
|
bug7597 = '[ruby-dev:46786]'
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(["test_ruby_test_bug7597", ".rb"]) {|t|
|
|
|
|
t.write "f" * 100
|
|
|
|
t.flush
|
2014-10-14 20:04:50 -04:00
|
|
|
assert_segv(["--disable-gems", "-e", "$0=ARGV[0]; Process.kill :SEGV, $$", t.path], bug7597)
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
}
|
2010-01-25 07:18:50 -05:00
|
|
|
end
|
2010-06-13 02:47:44 -04:00
|
|
|
|
|
|
|
def test_DATA
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t|
|
|
|
|
t.puts "puts DATA.read.inspect"
|
|
|
|
t.puts "__END__"
|
|
|
|
t.puts "foo"
|
|
|
|
t.puts "bar"
|
|
|
|
t.puts "baz"
|
|
|
|
t.flush
|
|
|
|
assert_in_out_err([t.path], "", %w("foo\\nbar\\nbaz\\n"), [])
|
|
|
|
}
|
2010-06-13 02:47:44 -04:00
|
|
|
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)
|
2012-10-25 10:06:31 -04:00
|
|
|
assert_in_out_err(["-we", "def foo\n"" 1.times do |a| end\n""end"], "", [], [])
|
2012-07-07 18:36:25 -04:00
|
|
|
feature6693 = '[ruby-core:46160]'
|
|
|
|
assert_in_out_err(["-we", "def foo\n _a=1\nend"], "", [], [], feature6693)
|
2012-11-30 01:24:40 -05:00
|
|
|
bug7408 = '[ruby-core:49659]'
|
|
|
|
assert_in_out_err(["-we", "def foo\n a=1\n :a\nend"], "", [], ["-e:2: warning: assigned but unused variable - a"], bug7408)
|
2013-01-29 23:22:12 -05:00
|
|
|
feature7730 = '[ruby-core:51580]'
|
|
|
|
assert_in_out_err(["-w", "-"], "a=1", [], ["-:1: warning: assigned but unused variable - a"], feature7730)
|
2013-01-31 02:33:30 -05:00
|
|
|
assert_in_out_err(["-w", "-"], "eval('a=1')", [], [], feature7730)
|
2010-12-08 07:36:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_shadowing_variable
|
|
|
|
bug4130 = '[ruby-dev:42718]'
|
2012-10-25 10:06:31 -04:00
|
|
|
assert_in_out_err(["-we", "def foo\n"" a=1\n"" 1.times do |a| end\n"" a\n""end"],
|
2010-12-08 07:36:20 -05:00
|
|
|
"", [], ["-e:3: warning: shadowing outer local variable - a"], bug4130)
|
2012-10-25 10:06:31 -04:00
|
|
|
assert_in_out_err(["-we", "def foo\n"" a=1\n"" 1.times do |a| end\n""end"],
|
2010-12-08 07:36:20 -05:00
|
|
|
"", [],
|
|
|
|
["-e:3: warning: shadowing outer local variable - a",
|
|
|
|
"-e:2: warning: assigned but unused variable - a",
|
|
|
|
], bug4130)
|
2012-07-07 18:36:25 -04:00
|
|
|
feature6693 = '[ruby-core:46160]'
|
2012-10-25 10:06:31 -04:00
|
|
|
assert_in_out_err(["-we", "def foo\n"" _a=1\n"" 1.times do |_a| end\n""end"],
|
2012-07-07 18:36:25 -04:00
|
|
|
"", [], [], feature6693)
|
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
|
|
|
|
2016-04-22 20:03:37 -04:00
|
|
|
case RUBY_PLATFORM
|
|
|
|
when /mswin|mingw/
|
2014-11-29 02:53:17 -05:00
|
|
|
def test_command_line_glob_nonascii
|
|
|
|
bug10555 = '[ruby-dev:48752] [Bug #10555]'
|
|
|
|
name = "\u{3042}.txt"
|
2014-11-29 10:02:02 -05:00
|
|
|
expected = name.encode("locale") rescue "?.txt"
|
2014-11-29 02:53:17 -05:00
|
|
|
with_tmpchdir do |dir|
|
|
|
|
open(name, "w") {}
|
2014-11-29 10:02:02 -05:00
|
|
|
assert_in_out_err(["-e", "puts ARGV", "?.txt"], "", [expected], [],
|
|
|
|
bug10555, encoding: "locale")
|
2014-11-29 02:53:17 -05:00
|
|
|
end
|
|
|
|
end
|
2014-11-29 10:02:58 -05:00
|
|
|
|
|
|
|
def test_command_line_progname_nonascii
|
|
|
|
bug10555 = '[ruby-dev:48752] [Bug #10555]'
|
2015-01-01 03:50:55 -05:00
|
|
|
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"
|
2014-11-29 10:02:58 -05:00
|
|
|
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
|
2015-03-05 18:03:35 -05:00
|
|
|
|
|
|
|
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
|
2014-11-29 02:53:17 -05:00
|
|
|
|
2014-12-05 05:35:43 -05:00
|
|
|
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")
|
2014-12-05 06:37:10 -05:00
|
|
|
ougai = Ougai.map {|f| f.encode("locale", replace: "?")}
|
|
|
|
assert_in_out_err(["-e", "puts ARGV", "*.txt"], "", ougai)
|
2014-12-05 05:35:43 -05:00
|
|
|
end
|
|
|
|
end
|
2016-04-26 10:35:24 -04:00
|
|
|
|
|
|
|
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
|
2016-04-22 20:03:37 -04:00
|
|
|
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
|
2014-12-05 05:35:43 -05:00
|
|
|
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
|
2012-10-13 23:30:50 -04:00
|
|
|
|
|
|
|
def test_pflag_gsub
|
|
|
|
bug7157 = '[ruby-core:47967]'
|
|
|
|
assert_in_out_err(['-p', '-e', 'gsub(/t.*/){"TEST"}'], %[test], %w[TEST], [], bug7157)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pflag_sub
|
|
|
|
bug7157 = '[ruby-core:47967]'
|
|
|
|
assert_in_out_err(['-p', '-e', 'sub(/t.*/){"TEST"}'], %[test], %w[TEST], [], bug7157)
|
|
|
|
end
|
2014-10-28 03:22:43 -04:00
|
|
|
|
2016-03-10 01:19:55 -05:00
|
|
|
def assert_norun_with_rflag(*opt)
|
2014-10-28 03:22:43 -04:00
|
|
|
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
|
2016-03-10 01:19:55 -05:00
|
|
|
opts = ['-C', dir, '-r', "./#{base}", *opt]
|
|
|
|
_, e = assert_in_out_err([*opts, '-ep'], "", //)
|
|
|
|
stderr.concat(e) if e
|
2014-10-28 03:22:43 -04:00
|
|
|
stderr << "---"
|
2016-03-10 01:19:55 -05:00
|
|
|
_, e = assert_in_out_err([*opts, base], "", //)
|
|
|
|
stderr.concat(e) if e
|
2014-10-28 03:22:43 -04:00
|
|
|
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')
|
2016-03-10 01:19:55 -05:00
|
|
|
assert_norun_with_rflag('--dump=parsetree', '-e', '#frozen-string-literal: true')
|
2014-10-28 03:22:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_dump_insns_with_rflag
|
|
|
|
assert_norun_with_rflag('--dump=insns')
|
|
|
|
end
|
2015-09-27 02:47:00 -04:00
|
|
|
|
|
|
|
def test_frozen_string_literal
|
2015-09-27 05:27:57 -04:00
|
|
|
all_assertions do |a|
|
2015-09-27 02:47:00 -04:00
|
|
|
[["disable", "false"], ["enable", "true"]].each do |opt, exp|
|
2015-09-27 05:27:57 -04:00
|
|
|
%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;
|
2015-09-27 02:47:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-10-23 15:02:55 -04:00
|
|
|
|
|
|
|
def test_frozen_string_literal_debug
|
2015-12-09 12:02:02 -05:00
|
|
|
with_debug_pat = /created at/
|
2017-12-11 19:46:34 -05:00
|
|
|
wo_debug_pat = /can\'t modify frozen String \(FrozenError\)\n\z/
|
2015-12-09 12:48:26 -05:00
|
|
|
frozen = [
|
|
|
|
["--enable-frozen-string-literal", true],
|
|
|
|
["--disable-frozen-string-literal", false],
|
|
|
|
[nil, false],
|
|
|
|
]
|
2016-02-19 02:48:02 -05:00
|
|
|
debugs = [
|
2015-12-09 12:48:26 -05:00
|
|
|
["--debug-frozen-string-literal", true],
|
|
|
|
["--debug=frozen-string-literal", true],
|
|
|
|
["--debug", true],
|
|
|
|
[nil, false],
|
|
|
|
]
|
|
|
|
opts = ["--disable=gems"]
|
2016-02-19 02:48:02 -05:00
|
|
|
frozen.product(debugs) do |(opt1, freeze), (opt2, debug)|
|
2015-12-09 12:48:26 -05:00
|
|
|
opt = opts + [opt1, opt2].compact
|
|
|
|
err = !freeze ? [] : debug ? with_debug_pat : wo_debug_pat
|
2018-02-15 19:49:46 -05:00
|
|
|
[
|
|
|
|
['"foo" << "bar"', err],
|
|
|
|
['"foo#{123}bar" << "bar"', err],
|
2018-02-15 20:15:35 -05:00
|
|
|
['+"foo#{123}bar" << "bar"', []],
|
|
|
|
['-"foo#{123}bar" << "bar"', freeze && debug ? with_debug_pat : wo_debug_pat],
|
2018-02-15 19:49:46 -05:00
|
|
|
].each do |code, expected|
|
|
|
|
assert_in_out_err(opt, code, [], expected, [opt, code])
|
2015-12-09 12:48:26 -05:00
|
|
|
end
|
|
|
|
end
|
2015-10-23 15:02:55 -04:00
|
|
|
end
|
2017-10-21 11:43:05 -04:00
|
|
|
|
|
|
|
def test___dir__encoding
|
2017-12-16 08:25:11 -05:00
|
|
|
lang = {"LC_ALL"=>ENV["LC_ALL"]||ENV["LANG"]}
|
2017-10-21 11:43:05 -04:00
|
|
|
with_tmpchdir do
|
|
|
|
testdir = "\u30c6\u30b9\u30c8"
|
|
|
|
Dir.mkdir(testdir)
|
|
|
|
Dir.chdir(testdir) do
|
|
|
|
open("test.rb", "w") do |f|
|
|
|
|
f.puts <<-END
|
2017-12-16 08:25:11 -05:00
|
|
|
if __FILE__.encoding == __dir__.encoding
|
|
|
|
p true
|
|
|
|
else
|
|
|
|
puts "__FILE__: \#{__FILE__.encoding}, __dir__: \#{__dir__.encoding}"
|
|
|
|
end
|
2017-10-21 11:43:05 -04:00
|
|
|
END
|
|
|
|
end
|
2017-12-16 08:25:11 -05:00
|
|
|
r, = EnvUtil.invoke_ruby([lang, "test.rb"], "", true)
|
2017-10-21 11:43:05 -04:00
|
|
|
assert_equal "true", r.chomp, "the encoding of __FILE__ and __dir__ should be same"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-11-11 23:45:51 -05:00
|
|
|
|
|
|
|
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
|
2018-04-13 02:26:30 -04:00
|
|
|
|
|
|
|
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
|
2008-04-16 11:48:54 -04:00
|
|
|
end
|