1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

envutil.rb: validate syntax

* test/ruby/envutil.rb (assert_valid_syntax): move from
  test_syntax.rb.
* test/ruby/envutil.rb (assert_normal_exit): validate syntax before
  running because this assertion passes even if the code fails by
  SyntaxError.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38485 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2012-12-20 02:05:37 +00:00
parent 3ec171510e
commit 0de805f62b
3 changed files with 25 additions and 15 deletions

View file

@ -1,3 +1,12 @@
Thu Dec 20 11:05:26 2012 Nobuyoshi Nakada <nobu@ruby-lang.org>
* test/ruby/envutil.rb (assert_valid_syntax): move from
test_syntax.rb.
* test/ruby/envutil.rb (assert_normal_exit): validate syntax before
running because this assertion passes even if the code fails by
SyntaxError.
Thu Dec 20 10:29:58 2012 Martin Bosslet <Martin.Bosslet@googlemail.com> Thu Dec 20 10:29:58 2012 Martin Bosslet <Martin.Bosslet@googlemail.com>
* test/openssl/test_pkey_dh.rb: revert special treatment of * test/openssl/test_pkey_dh.rb: revert special treatment of

View file

@ -133,7 +133,23 @@ module Test
module Unit module Unit
module Assertions module Assertions
public public
def assert_valid_syntax(code, fname, mesg = fname)
code = code.dup.force_encoding("ascii-8bit")
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
"#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
}
code.force_encoding("us-ascii")
verbose, $VERBOSE = $VERBOSE, nil
yield if defined?(yield)
assert_nothing_raised(SyntaxError, mesg) do
assert_equal(:ok, catch {|tag| eval(code, binding, fname, 0)}, mesg)
end
ensure
$VERBOSE = verbose
end
def assert_normal_exit(testsrc, message = '', opt = {}) def assert_normal_exit(testsrc, message = '', opt = {})
assert_valid_syntax(testsrc, caller_locations(1, 1)[0].path)
if opt.include?(:child_env) if opt.include?(:child_env)
opt = opt.dup opt = opt.dup
child_env = [opt.delete(:child_env)] || [] child_env = [opt.delete(:child_env)] || []

View file

@ -2,21 +2,6 @@ require 'test/unit'
require_relative 'envutil' require_relative 'envutil'
class TestSyntax < Test::Unit::TestCase class TestSyntax < Test::Unit::TestCase
def assert_valid_syntax(code, fname, mesg = fname)
code = code.dup.force_encoding("ascii-8bit")
code.sub!(/\A(?:\xef\xbb\xbf)?(\s*\#.*$)*(\n)?/n) {
"#$&#{"\n" if $1 && !$2}BEGIN{throw tag, :ok}\n"
}
code.force_encoding("us-ascii")
verbose, $VERBOSE = $VERBOSE, nil
yield if defined?(yield)
assert_nothing_raised(SyntaxError, mesg) do
assert_equal(:ok, catch {|tag| eval(code, binding, fname, 0)}, mesg)
end
ensure
$VERBOSE = verbose
end
def test_syntax def test_syntax
assert_nothing_raised(Exception) do assert_nothing_raised(Exception) do
for script in Dir[File.expand_path("../../../{lib,sample,ext,test}/**/*.rb", __FILE__)].sort for script in Dir[File.expand_path("../../../{lib,sample,ext,test}/**/*.rb", __FILE__)].sort