Skip to content

Commit 12fd93f

Browse files
dejmeduskarreiro
authored andcommitted
Missing inline snippets should display same error as filebased
1 parent 5ceb0e9 commit 12fd93f

File tree

6 files changed

+106
-14
lines changed

6 files changed

+106
-14
lines changed

lib/liquid/locales/en.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
render: "Argument error in tag 'render' - Dynamically chosen templates are not allowed"
3535
disabled:
3636
tag: "usage is not allowed in this context"
37+
file_system:
38+
includes: "This liquid context does not allow includes"

lib/liquid/snippet_drop.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
module Liquid
44
class SnippetDrop < Drop
5-
attr_reader :body, :name
5+
attr_reader :body, :name, :filename
66

7-
def initialize(body, name)
7+
def initialize(body, name, filename)
88
super()
99
@body = body
1010
@name = name
11+
@filename = filename
1112
end
1213

1314
def to_partial

lib/liquid/tags/render.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ def render_tag(context, output)
5151

5252
if template.respond_to?(:to_partial)
5353
partial = template.to_partial
54-
template_name = template.name
54+
template_name = template.filename
55+
context_variable_name = @alias_name || template.name
5556
elsif @template_name_expr.is_a?(String)
5657
partial = PartialCache.load(template, context: context, parse_context: parse_context)
5758
template_name = partial.name
59+
context_variable_name = @alias_name || template_name.split('/').last
5860
else
59-
raise ::ArgumentError, parse_context.locale.t("errors.argument.render")
61+
raise Liquid::ArgumentError, parse_context.locale.t("errors.file_system.includes")
6062
end
6163

62-
context_variable_name = @alias_name || template_name.split('/').last
63-
6464
render_partial_func = ->(var, forloop) {
6565
inner_context = context.new_isolated_subcontext
6666
inner_context.template_name = template_name

lib/liquid/tags/snippet.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def initialize(tag_name, markup, options)
2626
end
2727

2828
def render_to_output_buffer(context, output)
29-
snippet_drop = SnippetDrop.new(@body, @to)
29+
snippet_drop = SnippetDrop.new(@body, @to, context.template_name)
3030
context.scopes.last[@to] = snippet_drop
3131
context.resource_limits.increment_assign_score(assign_score_of(snippet_drop))
3232
output

test/integration/tags/render_tag_test.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,6 @@ def test_sub_contexts_count_towards_the_same_recursion_limit
101101
end
102102
end
103103

104-
def test_dynamically_chosen_templates_are_not_allowed
105-
error = assert_raises(::ArgumentError) do
106-
Template.parse('{% assign name = "snippet" %}{% render name %}').render!
107-
end
108-
assert_equal("Argument error in tag 'render' - Dynamically chosen templates are not allowed", error.message)
109-
end
110-
111104
def test_rigid_parsing_errors
112105
with_error_modes(:lax, :strict) do
113106
assert_template_result(

test/integration/tags/snippet_test.rb

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,54 @@ def test_snippet_with_invalid_identifier
480480

481481
assert_match("Expected end_of_string but found id", exception.message)
482482
end
483+
484+
def test_render_with_non_existent_tag
485+
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
486+
{% snippet foo %}
487+
{% render non_existent %}
488+
{% endsnippet %}
489+
490+
{% render foo %}
491+
LIQUID
492+
493+
expected = <<~TEXT
494+
495+
496+
497+
Liquid error (index line 2): This liquid context does not allow includes
498+
TEXT
499+
template.name = "index"
500+
501+
assert_equal(expected, template.render('errors' => ErrorDrop.new))
502+
end
503+
504+
def test_render_handles_errors
505+
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true)
506+
{% snippet foo %}
507+
{% render non_existent %} will raise an error.
508+
509+
Bla bla test.
510+
511+
This is an argument error: {{ 'test' | slice: 'not a number' }}
512+
{% endsnippet %}
513+
514+
{% render foo %}
515+
LIQUID
516+
517+
expected = <<~TEXT
518+
519+
520+
521+
Liquid error (index line 2): This liquid context does not allow includes will raise an error.
522+
523+
Bla bla test.
524+
525+
This is an argument error: Liquid error (index line 6): invalid integer
526+
TEXT
527+
template.name = "index"
528+
529+
assert_equal(expected, template.render('errors' => ErrorDrop.new))
530+
end
483531
end
484532

485533
class RigidMode < SnippetTest
@@ -956,6 +1004,54 @@ def test_render_with_invalid_identifier
9561004
assert_match("Expected a string or identifier, found 123", exception.message)
9571005
end
9581006

1007+
def test_render_with_non_existent_tag
1008+
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true, error_mode: :rigid)
1009+
{% snippet foo %}
1010+
{% render non_existent %}
1011+
{% endsnippet %}
1012+
1013+
{% render foo %}
1014+
LIQUID
1015+
1016+
expected = <<~TEXT
1017+
1018+
1019+
1020+
Liquid error (index line 2): This liquid context does not allow includes
1021+
TEXT
1022+
template.name = "index"
1023+
1024+
assert_equal(expected, template.render('errors' => ErrorDrop.new))
1025+
end
1026+
1027+
def test_render_handles_errors
1028+
template = Liquid::Template.parse(<<~LIQUID.chomp, line_numbers: true, error_mode: :rigid)
1029+
{% snippet foo %}
1030+
{% render non_existent %} will raise an error.
1031+
1032+
Bla bla test.
1033+
1034+
This is an argument error: {{ 'test' | slice: 'not a number' }}
1035+
{% endsnippet %}
1036+
1037+
{% render foo %}
1038+
LIQUID
1039+
1040+
expected = <<~TEXT
1041+
1042+
1043+
1044+
Liquid error (index line 2): This liquid context does not allow includes will raise an error.
1045+
1046+
Bla bla test.
1047+
1048+
This is an argument error: Liquid error (index line 6): invalid integer
1049+
TEXT
1050+
template.name = "index"
1051+
1052+
assert_equal(expected, template.render('errors' => ErrorDrop.new))
1053+
end
1054+
9591055
def test_render_with_no_identifier
9601056
template = "{% render %}"
9611057

0 commit comments

Comments
 (0)