Skip to content

Commit 785e3e6

Browse files
committed
(PUP-11689) Turn on strict_variables to enable strict mode by default
This commit sets strict_variables to true and modifies any tests that fail due to this change. To completely enable strict mode by default, strict will also need to be set to error instead of warning. This work will be done in PUP-11725.
1 parent 65b793d commit 785e3e6

File tree

10 files changed

+48
-17
lines changed

10 files changed

+48
-17
lines changed

lib/puppet/defaults.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2210,7 +2210,7 @@ def self.initialize_default_settings!(settings)
22102210
EOT
22112211
},
22122212
:strict_variables => {
2213-
:default => false,
2213+
:default => true,
22142214
:type => :boolean,
22152215
:desc => <<-'EOT'
22162216
Causes an evaluation error when referencing unknown variables. (This does not affect

spec/integration/application/agent_spec.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -993,13 +993,11 @@ def mounts
993993
agent.command_line.args << '--test'
994994
expect {
995995
agent.run
996-
}.to exit_with(2)
996+
}.to exit_with(1)
997997
.and output(
998-
match(/defined 'message' as 'legacy '/)
999-
.and match(/defined 'message' as 'custom a custom value'/)
1000-
.and match(/defined 'message' as 'external an external value'/)
1001-
).to_stdout
1002-
.and output(/Warning: Unknown variable: 'osfamily'/).to_stderr
998+
match(/Error: Evaluation Error: Unknown variable: 'osfamily'/)
999+
.and match(/Error: Could not retrieve catalog from remote server: Error 500 on SERVER:/)
1000+
).to_stderr
10031001
end
10041002
end
10051003
end

spec/integration/parser/compiler_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,19 @@ class a { $_a = 10}
693693
end.to raise_error(/Illegal variable name/)
694694
end
695695

696-
it 'a missing variable as default value becomes undef' do
697-
# strict variables not on
696+
it 'a missing variable as default causes an evaluation error' do
697+
# when strict variables not on
698+
expect {
699+
compile_to_catalog(<<-MANIFEST)
700+
class a ($b=$x) { notify {test: message=>"yes ${undef == $b}" } }
701+
include a
702+
MANIFEST
703+
}.to raise_error(/Evaluation Error: Unknown variable: 'x'/)
704+
end
705+
706+
it 'a missing variable as default value becomes undef when strict_variables is off' do
707+
# strict variables on by default for 8.x
708+
Puppet[:strict_variables] = false
698709
catalog = compile_to_catalog(<<-MANIFEST)
699710
class a ($b=$x) { notify {test: message=>"yes ${undef == $b}" } }
700711
include a

spec/integration/parser/conditionals_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
end
6969

7070
it "evaluates undefined variables as false" do
71+
# strict_variables is off so behavior this test is trying to check isn't stubbed out
72+
Puppet[:strict_variables] = false
7173
catalog = compile_to_catalog(<<-CODE)
7274
if $undef_var {
7375
} else {

spec/integration/parser/scope_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ class a inherits parent { }
276276

277277
['a:.b', '::a::b'].each do |ref|
278278
it "does not resolve a qualified name on the form #{ref} against top scope" do
279+
# strict_variables is off so behavior this test is trying to check isn't stubbed out
280+
Puppet[:strict_variables] = false
279281
expect_the_message_not_to_be("from topscope") do <<-"MANIFEST"
280282
class c {
281283
notify { 'something': message => "$#{ref}" }
@@ -297,6 +299,8 @@ class a inherits parent { }
297299

298300
['a:.b', '::a::b'].each do |ref|
299301
it "does not resolve a qualified name on the form #{ref} against node scope" do
302+
# strict_variables is off so behavior this test is trying to check isn't stubbed out
303+
Puppet[:strict_variables] = false
300304
expect_the_message_not_to_be("from node") do <<-MANIFEST
301305
class c {
302306
notify { 'something': message => "$a::b" }
@@ -626,8 +630,9 @@ class bar {
626630
end
627631
end
628632

629-
it "finds nil when the only set variable is in the dynamic scope" do
630-
expect_the_message_to_be(nil) do <<-MANIFEST
633+
it "raises an evaluation error when the only set variable is in the dynamic scope" do
634+
expect {
635+
compile_to_catalog(<<-MANIFEST)
631636
node default {
632637
include baz
633638
}
@@ -641,7 +646,7 @@ class baz {
641646
include bar
642647
}
643648
MANIFEST
644-
end
649+
}.to raise_error(/Evaluation Error: Unknown variable: 'var'./)
645650
end
646651

647652
it "ignores the value in the dynamic scope for a defined type" do

spec/unit/application/lookup_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,8 @@ def run_lookup(lookup)
673673
let(:node) { Puppet::Node.new("testnode", :facts => facts, :environment => 'puppet_func_provider') }
674674

675675
it "works OK in the absense of '--compile'" do
676+
# strict_variables is off so behavior this test is trying to check isn't stubbed out
677+
Puppet[:strict_variables] = false
676678
lookup.options[:node] = node
677679
allow(lookup.command_line).to receive(:args).and_return(['c'])
678680
lookup.options[:render_as] = :s

spec/unit/functions/epp_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
expect(eval_template("all your base <%= $what::is %> to us")).to eq("all your base are belong to us")
1919
end
2020

21-
it "get nil accessing a variable that does not exist" do
21+
it "gets error accessing a variable that does not exist" do
22+
expect { eval_template("<%= $kryptonite == undef %>")}.to raise_error(/Evaluation Error: Unknown variable: 'kryptonite'./)
23+
end
24+
25+
it "get nil accessing a variable that does not exist when strict_variables is off" do
26+
Puppet[:strict_variables] = false
2227
expect(eval_template("<%= $kryptonite == undef %>")).to eq("true")
2328
end
2429

spec/unit/functions/inline_epp_spec.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
expect(eval_template("all your base <%= $what %> to us")).to eq("all your base are belong to us")
1717
end
1818

19-
it "get nil accessing a variable that does not exist" do
19+
it "gets error accessing a variable that does not exist" do
20+
expect { eval_template("<%= $kryptonite == undef %>")}.to raise_error(/Evaluation Error: Unknown variable: 'kryptonite'./)
21+
end
22+
23+
it "get nil accessing a variable that does not exist when strict_variables is off" do
24+
Puppet[:strict_variables] = false
2025
expect(eval_template("<%= $kryptonite == undef %>")).to eq("true")
2126
end
2227

spec/unit/functions/return_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
end
2020

2121
it 'with undef value as function result when not given an argument' do
22+
# strict_variables is off so behavior this test is trying to check isn't stubbed out
23+
Puppet[:strict_variables] = false
2224
expect(compile_to_catalog(<<-CODE)).to have_resource('Notify[xy]')
2325
function please_return() {
2426
[1,2,3].map |$x| { if $x == 1 { return() } 200 }

spec/unit/indirector/catalog/compiler_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ def a_request_that_contains(facts)
514514

515515
it "should not add 'pe_serverversion' when FOSS" do
516516
allow(Puppet::Node.indirection).to receive(:find).with(node_name, anything).and_return(node)
517-
catalog = compiler.find(request)
518-
519-
expect(catalog.resource('notify', 'PE Version: 2019.2.0')).to be_nil
517+
expect {
518+
catalog = compiler.find(request)
519+
catalog.resource('notify', 'PE Version: 2019.2.0')
520+
}.to raise_error(/Evaluation Error: Unknown variable: 'pe_serverversion'./)
520521
end
521522

522523
it "should add 'pe_serverversion' when PE" do

0 commit comments

Comments
 (0)