Skip to content

Commit 65bcabd

Browse files
committed
(PUP-11725) Turn on strict mode by default
To start moving towards enabling strict mode by default, in PUP-11689 strict_variables was turned on. This commit continues that by setting strict to default to error instead of warning. Additionally, rspec tests that failed after the change were updated. I came across some unexpected behavior after setting strict to error so I set those tests to pending and created a ticket, PUP-11751, so those could be looked into later.
1 parent fa0250e commit 65bcabd

File tree

21 files changed

+151
-57
lines changed

21 files changed

+151
-57
lines changed

lib/puppet/defaults.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def self.initialize_default_settings!(settings)
188188
",
189189
},
190190
:strict => {
191-
:default => :warning,
191+
:default => :error,
192192
:type => :symbolic_enum,
193193
:values => [:off, :warning, :error],
194194
:desc => "The strictness level of puppet. Allowed values are:

spec/integration/application/apply_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ class mod {
203203
end
204204

205205
it 'can apply the catalog' do
206+
Puppet[:strict] = :warning
206207
catalog = compile_to_catalog('include mod', node)
207208

208209
Puppet[:environment] = env_name
@@ -212,6 +213,20 @@ class mod {
212213
apply.run
213214
}.to output(%r{Notify\[The Street 23\]/message: defined 'message' as 'The Street 23'}).to_stdout
214215
end
216+
217+
it 'can apply the catalog with no warning' do
218+
pending("See PUP-11751")
219+
Puppet[:strict] = :warning
220+
logs = []
221+
Puppet::Util::Log.with_destination(Puppet::Test::LogCollector.new(logs)) do
222+
catalog = compile_to_catalog('include mod', node)
223+
Puppet[:environment] = env_name
224+
apply.command_line.args = ['--catalog', file_containing('manifest', catalog.to_json)]
225+
apply.run
226+
end
227+
# expected to have no warnings
228+
expect(logs.select { |log| log.level == :warning }.map { |log| log.message }).to be_empty
229+
end
215230
end
216231

217232
it "raises if the environment directory does not exist" do
@@ -438,6 +453,11 @@ def bogus()
438453
end
439454

440455
context 'and the file is not serialized with rich_data' do
456+
# do not want to stub out behavior in tests
457+
before :each do
458+
Puppet[:strict] = :warning
459+
end
460+
441461
it 'will notify a string that is the result of Regexp#inspect (from Runtime3xConverter)' do
442462
catalog = compile_to_catalog(execute, node)
443463
apply.command_line.args = ['--catalog', file_containing('manifest', catalog.to_json)]

spec/integration/parser/compiler_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class c inherits b { notify { hi: } }
158158
it 'makes $settings::strict available as string' do
159159
node = Puppet::Node.new("testing")
160160
catalog = compile_to_catalog(<<-MANIFEST, node)
161-
notify { 'test': message => $settings::strict == 'warning' }
161+
notify { 'test': message => $settings::strict == 'error' }
162162
MANIFEST
163163
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, true)
164164
end
@@ -174,7 +174,7 @@ class c inherits b { notify { hi: } }
174174
it 'makes all server settings available as $settings::all_local hash' do
175175
node = Puppet::Node.new("testing")
176176
catalog = compile_to_catalog(<<-MANIFEST, node)
177-
notify { 'test': message => $settings::all_local['strict'] == 'warning' }
177+
notify { 'test': message => $settings::all_local['strict'] == 'error' }
178178
MANIFEST
179179
expect(catalog).to have_resource("Notify[test]").with_parameter(:message, true)
180180
end
@@ -694,7 +694,7 @@ class a { $_a = 10}
694694
end
695695

696696
it 'a missing variable as default causes an evaluation error' do
697-
# when strict variables not on
697+
# strict mode on by default for 8.x
698698
expect {
699699
compile_to_catalog(<<-MANIFEST)
700700
class a ($b=$x) { notify {test: message=>"yes ${undef == $b}" } }
@@ -703,9 +703,9 @@ class a ($b=$x) { notify {test: message=>"yes ${undef == $b}" } }
703703
}.to raise_error(/Evaluation Error: Unknown variable: 'x'/)
704704
end
705705

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
706+
it 'a missing variable as default value becomes undef when strict mode is off' do
708707
Puppet[:strict_variables] = false
708+
Puppet[:strict] = :warning
709709
catalog = compile_to_catalog(<<-MANIFEST)
710710
class a ($b=$x) { notify {test: message=>"yes ${undef == $b}" } }
711711
include a

spec/integration/parser/conditionals_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@
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
71+
# strict mode is off so behavior this test is trying to check isn't stubbed out
7272
Puppet[:strict_variables] = false
73+
Puppet[:strict] = :warning
7374
catalog = compile_to_catalog(<<-CODE)
7475
if $undef_var {
7576
} else {

spec/integration/parser/scope_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,9 @@ 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
279+
# strict mode is off so behavior this test is trying to check isn't stubbed out
280280
Puppet[:strict_variables] = false
281+
Puppet[:strict] = :warning
281282
expect_the_message_not_to_be("from topscope") do <<-"MANIFEST"
282283
class c {
283284
notify { 'something': message => "$#{ref}" }
@@ -299,8 +300,9 @@ class a inherits parent { }
299300

300301
['a:.b', '::a::b'].each do |ref|
301302
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+
# strict mode is off so behavior this test is trying to check isn't stubbed out
303304
Puppet[:strict_variables] = false
305+
Puppet[:strict] = :warning
304306
expect_the_message_not_to_be("from node") do <<-MANIFEST
305307
class c {
306308
notify { 'something': message => "$a::b" }

spec/unit/application/lookup_spec.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ def run_lookup(lookup)
503503
include PuppetSpec::Files
504504

505505
it "is unaffected by global variables unless '--compile' is used" do
506+
# strict mode is off so behavior this test is trying to check isn't stubbed out
507+
Puppet[:strict_variables] = false
508+
Puppet[:strict] = :warning
506509
lookup.options[:node] = node
507510
lookup.options[:render_as] = :s
508511
allow(lookup.command_line).to receive(:args).and_return(['c'])
@@ -673,8 +676,9 @@ def run_lookup(lookup)
673676
let(:node) { Puppet::Node.new("testnode", :facts => facts, :environment => 'puppet_func_provider') }
674677

675678
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
679+
# strict mode is off so behavior this test is trying to check isn't stubbed out
677680
Puppet[:strict_variables] = false
681+
Puppet[:strict] = :warning
678682
lookup.options[:node] = node
679683
allow(lookup.command_line).to receive(:args).and_return(['c'])
680684
lookup.options[:render_as] = :s

spec/unit/application/resource_spec.rb

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,24 @@
129129
end
130130

131131
describe "when printing output" do
132-
it "should not emit puppet class tags when printing yaml" do
133-
Puppet::Type.newtype(:stringify) do
134-
ensurable
135-
newparam(:name, isnamevar: true)
136-
newproperty(:string)
137-
end
132+
Puppet::Type.newtype(:stringify) do
133+
ensurable
134+
newparam(:name, isnamevar: true)
135+
newproperty(:string)
136+
end
138137

139-
Puppet::Type.type(:stringify).provide(:stringify) do
140-
def exists?
141-
true
142-
end
138+
Puppet::Type.type(:stringify).provide(:stringify) do
139+
def exists?
140+
true
141+
end
143142

144-
def string
145-
Puppet::Util::Execution::ProcessOutput.new('test', 0)
146-
end
143+
def string
144+
Puppet::Util::Execution::ProcessOutput.new('test', 0)
147145
end
146+
end
147+
148+
it "should not emit puppet class tags when printing yaml when strict mode is off" do
149+
Puppet[:strict] = :warning
148150

149151
@resource_app.options[:to_yaml] = true
150152
allow(@resource_app.command_line).to receive(:args).and_return(['stringify', 'hello', 'ensure=present', 'string=asd'])
@@ -158,6 +160,13 @@ def string
158160
expect { @resource_app.main }.not_to raise_error
159161
end
160162

163+
it "should raise an error when printing yaml by default" do
164+
@resource_app.options[:to_yaml] = true
165+
allow(@resource_app.command_line).to receive(:args).and_return(['stringify', 'hello', 'ensure=present', 'string=asd'])
166+
expect { @resource_app.main }.to raise_error( Puppet::PreformattedError,
167+
/Stringify\[hello\]\['string'\] contains a Puppet::Util::Execution::ProcessOutput value. It will be converted to the String 'test'/)
168+
end
169+
161170
it "should ensure all values to be printed are in the external encoding" do
162171
resources = [
163172
Puppet::Type.type(:user).new(:name => "\u2603".force_encoding(Encoding::UTF_8)).to_resource,

spec/unit/data_providers/hiera_data_provider_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def compile(environment, code = nil)
3535
node = Puppet::Node.new("testnode", :facts => facts, :environment => environment)
3636
compiler = Puppet::Parser::Compiler.new(node)
3737
compiler.topscope['domain'] = 'example.com'
38+
compiler.topscope['my_fact'] = 'server3'
3839
block_given? ? compiler.compile { |catalog| yield(compiler); catalog } : compiler.compile
3940
end
4041

@@ -164,6 +165,7 @@ def extract_notifications(catalog)
164165
expect(extract_notifications(compiler.compile)).to include('server2')
165166

166167
compiler = Puppet::Parser::Compiler.new(node)
168+
compiler.topscope['my_fact'] = 'server3'
167169
expect(extract_notifications(compiler.compile)).to include('In name.yaml')
168170
end
169171

spec/unit/functions/epp_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
expect { eval_template("<%= $kryptonite == undef %>")}.to raise_error(/Evaluation Error: Unknown variable: 'kryptonite'./)
2323
end
2424

25-
it "get nil accessing a variable that does not exist when strict_variables is off" do
25+
it "get nil accessing a variable that does not exist when strict mode is off" do
2626
Puppet[:strict_variables] = false
27+
Puppet[:strict] = :warning
2728
expect(eval_template("<%= $kryptonite == undef %>")).to eq("true")
2829
end
2930

spec/unit/functions/inline_epp_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
expect { eval_template("<%= $kryptonite == undef %>")}.to raise_error(/Evaluation Error: Unknown variable: 'kryptonite'./)
2121
end
2222

23-
it "get nil accessing a variable that does not exist when strict_variables is off" do
23+
it "get nil accessing a variable that does not exist when strict mode is off" do
2424
Puppet[:strict_variables] = false
25+
Puppet[:strict] = :warning
2526
expect(eval_template("<%= $kryptonite == undef %>")).to eq("true")
2627
end
2728

0 commit comments

Comments
 (0)