[PRISM] Refactor parser support into its own module

This commit is contained in:
Kevin Newton 2024-07-18 15:04:55 -04:00
parent 69e65b9b5a
commit 1c81d1a69d
Notes: git 2024-07-19 01:16:11 +00:00
5 changed files with 41 additions and 28 deletions

View File

@ -2,6 +2,7 @@
require 'test/unit' require 'test/unit'
require 'tmpdir' require 'tmpdir'
require_relative '../../lib/jit_support' require_relative '../../lib/jit_support'
require_relative '../../lib/parser_support'
class TestBugReporter < Test::Unit::TestCase class TestBugReporter < Test::Unit::TestCase
def test_bug_reporter_add def test_bug_reporter_add
@ -9,7 +10,7 @@ class TestBugReporter < Test::Unit::TestCase
omit "flaky with RJIT" if JITSupport.rjit_enabled? omit "flaky with RJIT" if JITSupport.rjit_enabled?
description = RUBY_DESCRIPTION description = RUBY_DESCRIPTION
description = description.sub(/\+PRISM /, '') unless EnvUtil.invoke_ruby(["-v"], "", true, false)[0].include?("+PRISM") description = description.sub(/\+PRISM /, '') unless ParserSupport.prism_enabled_in_subprocess?
description = description.sub(/\+RJIT /, '') unless JITSupport.rjit_force_enabled? description = description.sub(/\+RJIT /, '') unless JITSupport.rjit_force_enabled?
expected_stderr = [ expected_stderr = [
:*, :*,

View File

@ -0,0 +1,20 @@
# frozen_string_literal: true
module ParserSupport
module_function
# Determines whether or not Prism is being used in the current process. This
# would have been determined by `--parser=prism` on either the command line or
# from within various environment variables.
def prism_enabled?
RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
end
# Determines whether or not Prism would be used by a subprocess. This is
# necessary because some tests run in subprocesses, and we need to know if we
# expect Prism to be used by those tests. This happens if Prism is configured
# as the default parser.
def prism_enabled_in_subprocess?
EnvUtil.invoke_ruby(["-v"], "", true, false)[0].include?("+PRISM")
end
end

View File

@ -2,6 +2,7 @@
require 'test/unit' require 'test/unit'
require 'tempfile' require 'tempfile'
require 'pp' require 'pp'
require_relative '../lib/parser_support'
class RubyVM class RubyVM
module AbstractSyntaxTree module AbstractSyntaxTree
@ -337,7 +338,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_node_id_for_location def test_node_id_for_location
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
exception = begin exception = begin
raise raise
@ -358,7 +359,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_of_proc_and_method def test_of_proc_and_method
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
proc = Proc.new { 1 + 2 } proc = Proc.new { 1 + 2 }
method = self.method(__method__) method = self.method(__method__)
@ -389,7 +390,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_of_backtrace_location def test_of_backtrace_location
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
backtrace_location, lineno = sample_backtrace_location backtrace_location, lineno = sample_backtrace_location
node = RubyVM::AbstractSyntaxTree.of(backtrace_location) node = RubyVM::AbstractSyntaxTree.of(backtrace_location)
@ -402,7 +403,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_of_proc_and_method_under_eval def test_of_proc_and_method_under_eval
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
keep_script_lines_back = RubyVM.keep_script_lines keep_script_lines_back = RubyVM.keep_script_lines
RubyVM.keep_script_lines = false RubyVM.keep_script_lines = false
@ -433,7 +434,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_of_proc_and_method_under_eval_with_keep_script_lines def test_of_proc_and_method_under_eval_with_keep_script_lines
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO
keep_script_lines_back = RubyVM.keep_script_lines keep_script_lines_back = RubyVM.keep_script_lines
@ -465,7 +466,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_of_backtrace_location_under_eval def test_of_backtrace_location_under_eval
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
keep_script_lines_back = RubyVM.keep_script_lines keep_script_lines_back = RubyVM.keep_script_lines
RubyVM.keep_script_lines = false RubyVM.keep_script_lines = false
@ -485,7 +486,7 @@ class TestAst < Test::Unit::TestCase
end end
def test_of_backtrace_location_under_eval_with_keep_script_lines def test_of_backtrace_location_under_eval_with_keep_script_lines
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO
keep_script_lines_back = RubyVM.keep_script_lines keep_script_lines_back = RubyVM.keep_script_lines
@ -779,7 +780,7 @@ dummy
end end
def test_keep_script_lines_for_of def test_keep_script_lines_for_of
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
proc = Proc.new { 1 + 2 } proc = Proc.new { 1 + 2 }
method = self.method(__method__) method = self.method(__method__)
@ -792,7 +793,7 @@ dummy
end end
def test_keep_script_lines_for_of_with_existing_SCRIPT_LINES__that_has__FILE__as_a_key def test_keep_script_lines_for_of_with_existing_SCRIPT_LINES__that_has__FILE__as_a_key
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
# This test confirms that the bug that previously occurred because of # This test confirms that the bug that previously occurred because of
# `AbstractSyntaxTree.of`s unnecessary dependence on SCRIPT_LINES__ does not reproduce. # `AbstractSyntaxTree.of`s unnecessary dependence on SCRIPT_LINES__ does not reproduce.
@ -861,7 +862,7 @@ dummy
end end
def test_e_option def test_e_option
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
assert_in_out_err(["-e", "def foo; end; pp RubyVM::AbstractSyntaxTree.of(method(:foo)).type"], assert_in_out_err(["-e", "def foo; end; pp RubyVM::AbstractSyntaxTree.of(method(:foo)).type"],
"", [":SCOPE"], []) "", [":SCOPE"], [])
@ -1298,13 +1299,6 @@ dummy
private private
# We can't revisit instruction sequences to find node ids if the prism
# compiler was used instead of the parse.y compiler. In that case, we'll omit
# some tests.
def compiling_with_prism?
RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
end
def assert_error_tolerant(src, expected, keep_tokens: false) def assert_error_tolerant(src, expected, keep_tokens: false)
assert_ast_eqaul(src, expected, error_tolerant: true, keep_tokens: keep_tokens) assert_ast_eqaul(src, expected, error_tolerant: true, keep_tokens: keep_tokens)
end end

View File

@ -5,6 +5,7 @@ require 'timeout'
require 'tmpdir' require 'tmpdir'
require 'tempfile' require 'tempfile'
require_relative '../lib/jit_support' require_relative '../lib/jit_support'
require_relative '../lib/parser_support'
class TestRubyOptions < Test::Unit::TestCase class TestRubyOptions < Test::Unit::TestCase
def self.rjit_enabled? = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled? def self.rjit_enabled? = defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
@ -14,7 +15,7 @@ class TestRubyOptions < Test::Unit::TestCase
# here so that the various tests that reference RUBY_DESCRIPTION don't have to # here so that the various tests that reference RUBY_DESCRIPTION don't have to
# worry about it. The flag itself is tested in its own test. # worry about it. The flag itself is tested in its own test.
RUBY_DESCRIPTION = RUBY_DESCRIPTION =
if EnvUtil.invoke_ruby(["-v"], "", true, false)[0].include?("+PRISM") if ParserSupport.prism_enabled_in_subprocess?
::RUBY_DESCRIPTION ::RUBY_DESCRIPTION
else else
::RUBY_DESCRIPTION.sub(/\+PRISM /, '') ::RUBY_DESCRIPTION.sub(/\+PRISM /, '')
@ -367,6 +368,8 @@ class TestRubyOptions < Test::Unit::TestCase
end end
def test_yydebug def test_yydebug
omit if ParserSupport.prism_enabled_in_subprocess?
assert_in_out_err(["-ye", ""]) do |r, e| assert_in_out_err(["-ye", ""]) do |r, e|
assert_not_equal([], r) assert_not_equal([], r)
assert_equal([], e) assert_equal([], e)
@ -1178,6 +1181,8 @@ class TestRubyOptions < Test::Unit::TestCase
end end
def test_dump_parsetree_error_tolerant def test_dump_parsetree_error_tolerant
omit if ParserSupport.prism_enabled_in_subprocess?
assert_in_out_err(['--dump=parse', '-e', 'begin'], assert_in_out_err(['--dump=parse', '-e', 'begin'],
"", [], /unexpected end-of-input/, success: false) "", [], /unexpected end-of-input/, success: false)
assert_in_out_err(['--dump=parse', '--dump=+error_tolerant', '-e', 'begin'], assert_in_out_err(['--dump=parse', '--dump=+error_tolerant', '-e', 'begin'],

View File

@ -1,5 +1,6 @@
# frozen_string_literal: false # frozen_string_literal: false
require 'test/unit' require 'test/unit'
require_relative '../lib/parser_support'
class TestRubyVM < Test::Unit::TestCase class TestRubyVM < Test::Unit::TestCase
def test_stat def test_stat
@ -32,7 +33,7 @@ class TestRubyVM < Test::Unit::TestCase
end end
def test_keep_script_lines def test_keep_script_lines
omit if compiling_with_prism? omit if ParserSupport.prism_enabled?
pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO
prev_conf = RubyVM.keep_script_lines prev_conf = RubyVM.keep_script_lines
@ -69,12 +70,4 @@ class TestRubyVM < Test::Unit::TestCase
ensure ensure
RubyVM.keep_script_lines = prev_conf RubyVM.keep_script_lines = prev_conf
end end
private
# RubyVM.keep_script_lines does not mean anything in the context of prism, so
# we should omit tests that are looking for that functionality.
def compiling_with_prism?
RubyVM::InstructionSequence.compile("").to_a[4][:parser] == :prism
end
end end