From ed6e20624293cccd64d6a3f84a7c9f6071970da7 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 27 Feb 2023 14:06:16 +0100 Subject: [PATCH 1/2] Disable SimpleCov on truffleruby as it adds around 5 seconds to tests for little value --- test/test_helper.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 2c8f6466..f7f8be61 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,10 +1,12 @@ # frozen_string_literal: true -require "simplecov" -SimpleCov.start do - add_filter("idempotency_test.rb") unless ENV["CI"] - add_group("lib", "lib") - add_group("test", "test") +unless RUBY_ENGINE == "truffleruby" + require "simplecov" + SimpleCov.start do + add_filter("idempotency_test.rb") unless ENV["CI"] + add_group("lib", "lib") + add_group("test", "test") + end end $LOAD_PATH.unshift(File.expand_path("../lib", __dir__)) From d3410ffad8ac4f9fbc53eecc111a3dc84b1c6e52 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Mon, 27 Feb 2023 14:06:27 +0100 Subject: [PATCH 2/2] Disable reflection type verification on truffleruby * It fails transiently and Ripper does not seem to provide any way to investigate the actual error/exception. --- lib/syntax_tree/reflection.rb | 3 +- test/test_helper.rb | 58 ++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/lib/syntax_tree/reflection.rb b/lib/syntax_tree/reflection.rb index bf4b95f3..b2ffec6d 100644 --- a/lib/syntax_tree/reflection.rb +++ b/lib/syntax_tree/reflection.rb @@ -176,7 +176,8 @@ def parse_comments(statements, index) program = SyntaxTree.parse(SyntaxTree.read(File.expand_path("node.rb", __dir__))) - main_statements = program.statements.body.last.bodystmt.statements.body + program_statements = program.statements + main_statements = program_statements.body.last.bodystmt.statements.body main_statements.each_with_index do |main_statement, main_statement_index| # Ensure we are only looking at class declarations. next unless main_statement.is_a?(SyntaxTree::ClassDeclaration) diff --git a/test/test_helper.rb b/test/test_helper.rb index f7f8be61..8015be14 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,36 +13,38 @@ require "syntax_tree" require "syntax_tree/cli" -# Here we are going to establish type verification whenever a new node is -# created. We do this through the reflection module, which in turn parses the -# source code of the node classes. -require "syntax_tree/reflection" -SyntaxTree::Reflection.nodes.each do |name, node| - next if name == :Statements - - clazz = SyntaxTree.const_get(name) - parameters = clazz.instance_method(:initialize).parameters - - # First, verify that all of the parameters listed in the list of attributes. - # If there are any parameters that aren't listed in the attributes, then - # something went wrong with the parsing in the reflection module. - raise unless (parameters.map(&:last) - node.attributes.keys).empty? - - # Now we're going to use an alias chain to redefine the initialize method to - # include type checking. - clazz.alias_method(:initialize_without_verify, :initialize) - clazz.define_method(:initialize) do |**kwargs| - kwargs.each do |kwarg, value| - attribute = node.attributes.fetch(kwarg) - - unless attribute.type === value - raise TypeError, - "invalid type for #{name}##{kwarg}, expected " \ - "#{attribute.type.inspect}, got #{value.inspect}" +unless RUBY_ENGINE == "truffleruby" + # Here we are going to establish type verification whenever a new node is + # created. We do this through the reflection module, which in turn parses the + # source code of the node classes. + require "syntax_tree/reflection" + SyntaxTree::Reflection.nodes.each do |name, node| + next if name == :Statements + + clazz = SyntaxTree.const_get(name) + parameters = clazz.instance_method(:initialize).parameters + + # First, verify that all of the parameters listed in the list of attributes. + # If there are any parameters that aren't listed in the attributes, then + # something went wrong with the parsing in the reflection module. + raise unless (parameters.map(&:last) - node.attributes.keys).empty? + + # Now we're going to use an alias chain to redefine the initialize method to + # include type checking. + clazz.alias_method(:initialize_without_verify, :initialize) + clazz.define_method(:initialize) do |**kwargs| + kwargs.each do |kwarg, value| + attribute = node.attributes.fetch(kwarg) + + unless attribute.type === value + raise TypeError, + "invalid type for #{name}##{kwarg}, expected " \ + "#{attribute.type.inspect}, got #{value.inspect}" + end end - end - initialize_without_verify(**kwargs) + initialize_without_verify(**kwargs) + end end end