Skip to content

Commit c060770

Browse files
committed
(PUP-11047) Pass binding's source location
If eval is called with a binding, then ruby 3 will report the source location as ["(eval)", 1], whereas ruby 2 reports the source location from the binding, which in the test is the `functions4_spec.rb` file. The `Binding#source_location` method only exists in ruby 2.6 and up, so conditionally pass the source location if it's available. See https://bugs.ruby-lang.org/issues/4352
1 parent 5d7da2e commit c060770

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

spec/unit/functions4_spec.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ def load_constant(typed_name)
3939

4040
let(:loader) { FunctionAPISpecModule::TestFunctionLoader.new }
4141

42+
def parse_eval(code, here)
43+
if here.respond_to?(:source_location)
44+
eval(code, here, here.source_location[0], here.source_location[1])
45+
else
46+
# this can be removed when ruby < 2.6 is dropped
47+
eval(code, here)
48+
end
49+
end
50+
4251
it 'allows a simple function to be created without dispatch declaration' do
4352
f = Puppet::Functions.create_function('min') do
4453
def min(x,y)
@@ -484,7 +493,7 @@ def test(x)
484493
the_loader = loader()
485494
here = get_binding(the_loader)
486495
expect do
487-
eval(<<-CODE, here)
496+
parse_eval(<<-CODE, here)
488497
Puppet::Functions.create_function('testing::test') do
489498
local_types do
490499
type 'MyType += Array[Integer]'
@@ -505,7 +514,7 @@ def test(x)
505514
the_loader = loader()
506515
here = get_binding(the_loader)
507516
expect do
508-
eval(<<-CODE, here)
517+
parse_eval(<<-CODE, here)
509518
Puppet::Functions.create_function('testing::test') do
510519
dispatch :test do
511520
param 'Array[1+=1]', :x
@@ -525,7 +534,7 @@ def test(x)
525534
it 'uses return_type to validate returned value' do
526535
the_loader = loader()
527536
here = get_binding(the_loader)
528-
fc = eval(<<-CODE, here)
537+
fc = parse_eval(<<-CODE, here)
529538
Puppet::Functions.create_function('testing::test') do
530539
dispatch :test do
531540
param 'Integer', :x
@@ -547,7 +556,7 @@ def test(x)
547556
the_loader = loader()
548557
the_loader.add_type('myalias', type_alias_t('MyAlias', 'Integer'))
549558
here = get_binding(the_loader)
550-
fc = eval(<<-CODE, here)
559+
fc = parse_eval(<<-CODE, here)
551560
Puppet::Functions.create_function('testing::test') do
552561
dispatch :test do
553562
param 'MyAlias', :x
@@ -567,7 +576,7 @@ def test(x)
567576
it 'reports a reference to an unresolved type' do
568577
the_loader = loader()
569578
here = get_binding(the_loader)
570-
fc = eval(<<-CODE, here)
579+
fc = parse_eval(<<-CODE, here)
571580
Puppet::Functions.create_function('testing::test') do
572581
dispatch :test do
573582
param 'MyAlias', :x
@@ -586,7 +595,7 @@ def test(x)
586595
it 'create local Type aliases' do
587596
the_loader = loader()
588597
here = get_binding(the_loader)
589-
fc = eval(<<-CODE, here)
598+
fc = parse_eval(<<-CODE, here)
590599
Puppet::Functions.create_function('testing::test') do
591600
local_types do
592601
type 'MyType = Array[Integer]'
@@ -608,7 +617,7 @@ def test(x)
608617
it 'create nested local Type aliases' do
609618
the_loader = loader()
610619
here = get_binding(the_loader)
611-
fc = eval(<<-CODE, here)
620+
fc = parse_eval(<<-CODE, here)
612621
Puppet::Functions.create_function('testing::test') do
613622
local_types do
614623
type 'InnerType = Array[Integer]'
@@ -631,7 +640,7 @@ def test(x)
631640
it 'create self referencing local Type aliases' do
632641
the_loader = loader()
633642
here = get_binding(the_loader)
634-
fc = eval(<<-CODE, here)
643+
fc = parse_eval(<<-CODE, here)
635644
Puppet::Functions.create_function('testing::test') do
636645
local_types do
637646
type 'Tree = Hash[String,Variant[String,Tree]]'

0 commit comments

Comments
 (0)