Skip to content

Commit

Permalink
Merge pull request #5129 from hlindberg/PUP-6083_deprecate-upper-case…
Browse files Browse the repository at this point in the history
…-resource-in-class

(PUP-6083) Deprecate use of UC/Resourceref as <ref> in Class[<ref>]
  • Loading branch information
thallgren committed Jul 20, 2016
2 parents 14c320e + 5924795 commit 143dd94
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
17 changes: 17 additions & 0 deletions lib/puppet/pops/evaluator/access_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,13 @@ def access_PHostClassType(o, scope, keys)
#
result = keys.each_with_index.map do |c, i|
name = if c.is_a?(Types::PResourceType) && !c.type_name.nil? && c.title.nil?
strict_check(c, i)
# type_name is already downcase. Don't waste time trying to downcase again
c.type_name
elsif c.is_a?(String)
c.downcase
elsif c.is_a?(Types::PTypeReferenceType)
strict_check(c, i)
c.type_string.downcase
else
fail(Issues::ILLEGAL_HOSTCLASS_NAME, @semantic.keys[i], {:name => c})
Expand Down Expand Up @@ -643,6 +645,21 @@ def access_PHostClassType(o, scope, keys)
# returns single type as type, else an array of types
return result_type_array ? result : result.pop
end

# PUP-6083 - Using Class[Foo] is deprecated since an arbitrary foo will trigger a "resource not found"
# @api private
def strict_check(name, index)
if Puppet[:strict] != :off
msg = 'Upper cased class-name in a Class[<class-name>] is deprecated, class-name should be a lowercase string'
case Puppet[:strict]
when :error
fail(Issues::ILLEGAL_HOSTCLASS_NAME, @semantic.keys[index], {:name => name})
when :warning
Puppet.warn_once(:deprecation, 'ClassReferenceInUpperCase', msg)
end
end
end

end
end
end
57 changes: 52 additions & 5 deletions spec/integration/parser/compiler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1169,15 +1169,62 @@ class c inherits b { notify { hi: } }
end.to raise_error(/Resource Override can only.*got: Class\[a\].*/)
end

describe "when resolving class references" do
describe 'when resolving class references' do
include Matchers::Resource
## BEFORE
describe 'and classname is a Resource Reference and strict == :error' do
before(:each) do
Puppet[:strict] = :error
end

it 'is reported as an error' do
expect {
compile_to_catalog(<<-PP)
notice Class[ToothFairy]
PP
}.to raise_error(/Illegal Class name in class reference. A TypeReference\['ToothFairy'\]-Type cannot be used where a String is expected/)
end
end

describe 'and classname is a Resource Reference and strict == :warning' do
before(:each) do
Puppet[:strict] = :warning
end

it 'is reported as a deprecation warning' do
expect {
compile_to_catalog(<<-PP)
notice Class[ToothFairy]
PP
expect(@logs).to have_matching_log(/Upper cased class-name in a Class\[<class-name>\] is deprecated, class-name should be a lowercase string/)

}.to_not raise_error()
end
end

describe 'and classname is a Resource Reference and strict == :off' do
before(:each) do
Puppet[:strict] = :off
end

it 'is not reported' do
expect {
compile_to_catalog(<<-PP)
notice Class[ToothFairy]
PP
expect(@logs).to_not have_matching_log(/Warning: Upper cased class-name in a Class\[<class-name>\] is deprecated, class-name should be a lowercase string/)

}.to_not raise_error()
end
end

it "should not favor local scope (with class included in topscope)" do
catalog = compile_to_catalog(<<-PP)
class experiment {
class baz {
}
notify {"x" : require => Class[Baz] }
notify {"y" : require => Class[Experiment::Baz] }
notify {"x" : require => Class['baz'] }
notify {"y" : require => Class['experiment::baz'] }
}
class baz {
}
Expand All @@ -1195,8 +1242,8 @@ class baz {
class experiment {
class baz {
}
notify {"x" : require => Class[Baz] }
notify {"y" : require => Class[Experiment::Baz] }
notify {"x" : require => Class['baz'] }
notify {"y" : require => Class['experiment::baz'] }
}
class baz {
}
Expand Down

0 comments on commit 143dd94

Please sign in to comment.