summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2024-07-18 15:04:55 -0400
committerKevin Newton <[email protected]>2024-07-18 21:15:54 -0400
commit1c81d1a69dfc909baf128d6d1ffe636f2e8a3194 (patch)
tree0834c78e22274666364d4b6ccfdff70598af997c
parent69e65b9b5ad03d40eb6cfa7323d871465c28d960 (diff)
[PRISM] Refactor parser support into its own module
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11201
-rw-r--r--test/-ext-/bug_reporter/test_bug_reporter.rb3
-rw-r--r--test/lib/parser_support.rb20
-rw-r--r--test/ruby/test_ast.rb28
-rw-r--r--test/ruby/test_rubyoptions.rb7
-rw-r--r--test/ruby/test_rubyvm.rb11
5 files changed, 41 insertions, 28 deletions
diff --git a/test/-ext-/bug_reporter/test_bug_reporter.rb b/test/-ext-/bug_reporter/test_bug_reporter.rb
index bafe489d8a..8c810c2741 100644
--- a/test/-ext-/bug_reporter/test_bug_reporter.rb
+++ b/test/-ext-/bug_reporter/test_bug_reporter.rb
@@ -2,6 +2,7 @@
require 'test/unit'
require 'tmpdir'
require_relative '../../lib/jit_support'
+require_relative '../../lib/parser_support'
class TestBugReporter < Test::Unit::TestCase
def test_bug_reporter_add
@@ -9,7 +10,7 @@ class TestBugReporter < Test::Unit::TestCase
omit "flaky with RJIT" if JITSupport.rjit_enabled?
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?
expected_stderr = [
:*,
diff --git a/test/lib/parser_support.rb b/test/lib/parser_support.rb
new file mode 100644
index 0000000000..0daf45e87b
--- /dev/null
+++ b/test/lib/parser_support.rb
@@ -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
diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb
index 705107dded..ebef14c14b 100644
--- a/test/ruby/test_ast.rb
+++ b/test/ruby/test_ast.rb
@@ -2,6 +2,7 @@
require 'test/unit'
require 'tempfile'
require 'pp'
+require_relative '../lib/parser_support'
class RubyVM
module AbstractSyntaxTree
@@ -337,7 +338,7 @@ class TestAst < Test::Unit::TestCase
end
def test_node_id_for_location
- omit if compiling_with_prism?
+ omit if ParserSupport.prism_enabled?
exception = begin
raise
@@ -358,7 +359,7 @@ class TestAst < Test::Unit::TestCase
end
def test_of_proc_and_method
- omit if compiling_with_prism?
+ omit if ParserSupport.prism_enabled?
proc = Proc.new { 1 + 2 }
method = self.method(__method__)
@@ -389,7 +390,7 @@ class TestAst < Test::Unit::TestCase
end
def test_of_backtrace_location
- omit if compiling_with_prism?
+ omit if ParserSupport.prism_enabled?
backtrace_location, lineno = sample_backtrace_location
node = RubyVM::AbstractSyntaxTree.of(backtrace_location)
@@ -402,7 +403,7 @@ class TestAst < Test::Unit::TestCase
end
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
RubyVM.keep_script_lines = false
@@ -433,7 +434,7 @@ class TestAst < Test::Unit::TestCase
end
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
keep_script_lines_back = RubyVM.keep_script_lines
@@ -465,7 +466,7 @@ class TestAst < Test::Unit::TestCase
end
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
RubyVM.keep_script_lines = false
@@ -485,7 +486,7 @@ class TestAst < Test::Unit::TestCase
end
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
keep_script_lines_back = RubyVM.keep_script_lines
@@ -779,7 +780,7 @@ dummy
end
def test_keep_script_lines_for_of
- omit if compiling_with_prism?
+ omit if ParserSupport.prism_enabled?
proc = Proc.new { 1 + 2 }
method = self.method(__method__)
@@ -792,7 +793,7 @@ dummy
end
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
# `AbstractSyntaxTree.of`s unnecessary dependence on SCRIPT_LINES__ does not reproduce.
@@ -861,7 +862,7 @@ dummy
end
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"],
"", [":SCOPE"], [])
@@ -1298,13 +1299,6 @@ dummy
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)
assert_ast_eqaul(src, expected, error_tolerant: true, keep_tokens: keep_tokens)
end
diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb
index b6f0e131f0..40cbfd906d 100644
--- a/test/ruby/test_rubyoptions.rb
+++ b/test/ruby/test_rubyoptions.rb
@@ -5,6 +5,7 @@ require 'timeout'
require 'tmpdir'
require 'tempfile'
require_relative '../lib/jit_support'
+require_relative '../lib/parser_support'
class TestRubyOptions < Test::Unit::TestCase
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
# worry about it. The flag itself is tested in its own test.
RUBY_DESCRIPTION =
- if EnvUtil.invoke_ruby(["-v"], "", true, false)[0].include?("+PRISM")
+ if ParserSupport.prism_enabled_in_subprocess?
::RUBY_DESCRIPTION
else
::RUBY_DESCRIPTION.sub(/\+PRISM /, '')
@@ -367,6 +368,8 @@ class TestRubyOptions < Test::Unit::TestCase
end
def test_yydebug
+ omit if ParserSupport.prism_enabled_in_subprocess?
+
assert_in_out_err(["-ye", ""]) do |r, e|
assert_not_equal([], r)
assert_equal([], e)
@@ -1178,6 +1181,8 @@ class TestRubyOptions < Test::Unit::TestCase
end
def test_dump_parsetree_error_tolerant
+ omit if ParserSupport.prism_enabled_in_subprocess?
+
assert_in_out_err(['--dump=parse', '-e', 'begin'],
"", [], /unexpected end-of-input/, success: false)
assert_in_out_err(['--dump=parse', '--dump=+error_tolerant', '-e', 'begin'],
diff --git a/test/ruby/test_rubyvm.rb b/test/ruby/test_rubyvm.rb
index 053a914a8a..225cb45f33 100644
--- a/test/ruby/test_rubyvm.rb
+++ b/test/ruby/test_rubyvm.rb
@@ -1,5 +1,6 @@
# frozen_string_literal: false
require 'test/unit'
+require_relative '../lib/parser_support'
class TestRubyVM < Test::Unit::TestCase
def test_stat
@@ -32,7 +33,7 @@ class TestRubyVM < Test::Unit::TestCase
end
def test_keep_script_lines
- omit if compiling_with_prism?
+ omit if ParserSupport.prism_enabled?
pend if ENV['RUBY_ISEQ_DUMP_DEBUG'] # TODO
prev_conf = RubyVM.keep_script_lines
@@ -69,12 +70,4 @@ class TestRubyVM < Test::Unit::TestCase
ensure
RubyVM.keep_script_lines = prev_conf
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