Skip to content

Commit c0f6920

Browse files
authored
Merge pull request #8888 from lollipopman/add-source-ref-to-legacy-func-error
(PUP-11472) Add source_ref to legacy function error
2 parents de90f54 + 36c916f commit c0f6920

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

lib/puppet/pops/loader/ruby_legacy_function_instantiator.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Puppet::Pops::Loader::RubyLegacyFunctionInstantiator
1818
def self.create(loader, typed_name, source_ref, ruby_code_string)
1919
# Assert content of 3x function by parsing
2020
assertion_result = []
21-
if assert_code(ruby_code_string, assertion_result)
21+
if assert_code(ruby_code_string, source_ref, assertion_result)
2222
unless ruby_code_string.is_a?(String) && assertion_result.include?(:found_newfunction)
2323
raise ArgumentError, _("The code loaded from %{source_ref} does not seem to be a Puppet 3x API function - no 'newfunction' call.") % { source_ref: source_ref }
2424
end
@@ -69,15 +69,15 @@ def self.get_binding(loader_injected_arg)
6969
end
7070
private_class_method :get_binding
7171

72-
def self.assert_code(code_string, result)
72+
def self.assert_code(code_string, source_ref, result)
7373
ripped = Ripper.sexp(code_string)
7474
return false if ripped.nil? # Let the next real parse crash and tell where and what is wrong
75-
ripped.each {|x| walk(x, result) }
75+
ripped.each {|x| walk(x, source_ref, result) }
7676
true
7777
end
7878
private_class_method :assert_code
7979

80-
def self.walk(x, result)
80+
def self.walk(x, source_ref, result)
8181
return unless x.is_a?(Array)
8282
first = x[0]
8383
case first
@@ -89,13 +89,14 @@ def self.walk(x, result)
8989
when :def, :defs
9090
# There should not be any calls to def in a 3x function
9191
mname, mline = extract_name_line(find_identity(x))
92-
raise SecurityError, _("Illegal method definition of method '%{method_name}' on line %{line} in legacy function. See %{url} for more information") % {
92+
raise SecurityError, _("Illegal method definition of method '%{method_name}' in source %{source_ref} on line %{line} in legacy function. See %{url} for more information") % {
9393
method_name: mname,
94+
source_ref: source_ref,
9495
line: mline,
9596
url: "https://puppet.com/docs/puppet/latest/functions_refactor_legacy.html"
9697
}
9798
end
98-
x.each {|v| walk(v, result) }
99+
x.each {|v| walk(v, source_ref, result) }
99100
end
100101
private_class_method :walk
101102

spec/unit/pops/loaders/loaders_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ def compile_and_get_notifications(code)
606606
it "an illegal function is loaded" do
607607
expect {
608608
loader.load_typed(typed_name(:function, 'bad_func_load3')).value
609-
}.to raise_error(SecurityError, /Illegal method definition of method 'bad_func_load3_illegal_method' on line 8 in legacy function/)
609+
}.to raise_error(SecurityError, /Illegal method definition of method 'bad_func_load3_illegal_method' in source .*bad_func_load3.rb on line 8 in legacy function/)
610610
end
611611
end
612612

0 commit comments

Comments
 (0)