Ensure all RubyTracker RenderParsers are tested

Previously, only the PrismRenderParser or RipperRenderParser would be
tested depending on if the Prism gem is available. This meant that
PrismRenderParser was being tested on Ruby 3.3 and RipperRenderParser
was tested on Ruby < 3.3. Additionally, if someone were to add prism to
the rails/rails Gemfile because they wrote a tool that uses it then the
RipperRenderParser would end up completely untested.

This commit is a small refactor to enable testing both RenderParsers in
all Ruby versions so that the prism gem can be added to the Gemfile.
This commit is contained in:
Hartley McGuire 2024-01-24 18:39:59 -05:00
parent c7551d08fc
commit 0041af4c94
No known key found for this signature in database
GPG Key ID: E823FC1403858A82
5 changed files with 34 additions and 12 deletions

View File

@ -36,6 +36,8 @@ gem "json", ">= 2.0.0", "!=2.7.0"
# Workaround until Ruby ships with cgi version 0.3.6 or higher. # Workaround until Ruby ships with cgi version 0.3.6 or higher.
gem "cgi", ">= 0.3.6", require: false gem "cgi", ">= 0.3.6", require: false
gem "prism"
group :lint do group :lint do
gem "syntax_tree", "6.1.1", require: false gem "syntax_tree", "6.1.1", require: false
end end

View File

@ -370,6 +370,7 @@ GEM
path_expander (1.1.1) path_expander (1.1.1)
pg (1.5.4) pg (1.5.4)
prettier_print (1.2.1) prettier_print (1.2.1)
prism (0.19.0)
propshaft (0.8.0) propshaft (0.8.0)
actionpack (>= 7.0.0) actionpack (>= 7.0.0)
activesupport (>= 7.0.0) activesupport (>= 7.0.0)
@ -618,6 +619,7 @@ DEPENDENCIES
mysql2 (~> 0.5) mysql2 (~> 0.5)
nokogiri (>= 1.8.1, != 1.11.0) nokogiri (>= 1.8.1, != 1.11.0)
pg (~> 1.3) pg (~> 1.3)
prism
propshaft (>= 0.1.7) propshaft (>= 0.1.7)
puma (>= 5.0.3) puma (>= 5.0.3)
queue_classic (>= 4.0.0) queue_classic (>= 4.0.0)

View File

@ -17,8 +17,9 @@ module ActionView
true true
end end
def initialize(name, template, view_paths = nil) def initialize(name, template, view_paths = nil, parser_class: RenderParser::Default)
@name, @template, @view_paths = name, template, view_paths @name, @template, @view_paths = name, template, view_paths
@parser_class = parser_class
end end
private private
@ -29,7 +30,7 @@ module ActionView
compiled_source = template.handler.call(template, template.source) compiled_source = template.handler.call(template, template.source)
RenderParser.new(@name, compiled_source).render_calls.filter_map do |render_call| @parser_class.new(@name, compiled_source).render_calls.filter_map do |render_call|
next if render_call.end_with?("/_") next if render_call.end_with?("/_")
render_call.gsub(%r|/_|, "/") render_call.gsub(%r|/_|, "/")
end end

View File

@ -31,14 +31,10 @@ module ActionView
rescue LoadError rescue LoadError
require "ripper" require "ripper"
require_relative "render_parser/ripper_render_parser" require_relative "render_parser/ripper_render_parser"
Parser = RipperRenderParser Default = RipperRenderParser
else else
require_relative "render_parser/prism_render_parser" require_relative "render_parser/prism_render_parser"
Parser = PrismRenderParser Default = PrismRenderParser
end
def self.new(name, code)
Parser.new(name, code)
end end
end end
end end

View File

@ -3,6 +3,11 @@
require "abstract_unit" require "abstract_unit"
require "action_view/dependency_tracker" require "action_view/dependency_tracker"
require "action_view/render_parser/prism_render_parser"
require "ripper"
require "action_view/render_parser/ripper_render_parser"
class NeckbeardTracker class NeckbeardTracker
def self.call(name, template) def self.call(name, template)
["foo/#{name}"] ["foo/#{name}"]
@ -228,11 +233,9 @@ class ERBTrackerTest < Minitest::Test
end end
end end
class RubyTrackerTest < Minitest::Test module RubyTrackerTests
include SharedTrackerTests
def make_tracker(name, template) def make_tracker(name, template)
ActionView::DependencyTracker::RubyTracker.new(name, template) ActionView::DependencyTracker::RubyTracker.new(name, template, parser_class: parser_class)
end end
def test_dependencies_skip_unknown_options def test_dependencies_skip_unknown_options
@ -262,3 +265,21 @@ class RubyTrackerTest < Minitest::Test
assert_equal [], tracker.dependencies assert_equal [], tracker.dependencies
end end
end end
class RipperRubyTrackerTest < Minitest::Test
include SharedTrackerTests
include RubyTrackerTests
def parser_class
ActionView::RenderParser::RipperRenderParser
end
end
class PrismRubyTrackerTest < Minitest::Test
include SharedTrackerTests
include RubyTrackerTests
def parser_class
ActionView::RenderParser::PrismRenderParser
end
end