Skip to content

Commit ce6d50d

Browse files
nicklewiskris-bosland
authored andcommitted
(PUP-4242) Correctly validate named definitions in plans
This bit of code validates that the name of a definition in a Puppet manifest conforms to the naming convention of <module>::<dir>::<filename>. However, it determines the expected module name by searching up the path until it finds a known directory name that might contain a .pp file. Previously, it was only looking for 'manifests', 'functions', and 'types'. This meant that plans couldn't be loaded, since it couldn't find the expected module name.
1 parent 6e7835b commit ce6d50d

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/puppet/pops/validation/checker4_0.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,11 @@ def initial_manifest?(path)
595595
full_path == manifest_setting || full_path.start_with?(manifest_setting)
596596
end
597597

598-
# Returns module root directory index in path for files under 'manifests', 'functions' and 'types'
598+
# Returns module root directory index in path for files under 'manifests',
599+
# 'functions', 'types' and 'plans'
599600
def find_module_definition_dir(path)
600601
reverse_index = path.reverse_each.find_index do |dir|
601-
dir == 'manifests' || dir == 'functions' || dir == 'types'
602+
dir == 'manifests' || dir == 'functions' || dir == 'types' || dir == 'plans'
602603
end
603604
return reverse_index.nil? ? nil : (path.length - 1) - reverse_index
604605
end

spec/unit/pops/validator/validator_spec.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def validate(factory)
3434
expect(validate(parse('function ::aaa() {}'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_NAME)
3535
end
3636

37-
it 'should raise error for illegal class locations' do
37+
it 'should raise error for illegal definition locations' do
3838
expect(validate(parse('function aaa::ccc() {}', 'aaa/manifests/bbb.pp'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
3939
expect(validate(parse('class bbb() {}', 'aaa/manifests/init.pp'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
4040
expect(validate(parse('define aaa::bbb::ccc::eee() {}', 'aaa/manifests/bbb/ddd.pp'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
@@ -225,6 +225,18 @@ def validate(factory)
225225
context 'with --tasks set' do
226226
before(:each) { Puppet[:tasks] = true }
227227

228+
it 'raises an error for illegal plan names' do
229+
expect(validate(parse('plan aaa::ccc::eee() {}', 'aaa/plans/bbb/ccc/eee.pp'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
230+
expect(validate(parse('plan aaa() {}', 'aaa/plans/aaa.pp'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
231+
expect(validate(parse('plan aaa::bbb() {}', 'aaa/plans/bbb/bbb.pp'))).to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
232+
end
233+
234+
it 'accepts legal plan names' do
235+
expect(validate(parse('plan aaa::ccc::eee() {}', 'aaa/plans/ccc/eee.pp'))).not_to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
236+
expect(validate(parse('plan aaa() {}', 'aaa/plans/init.pp'))).not_to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
237+
expect(validate(parse('plan aaa::bbb() {}', 'aaa/plans/bbb.pp'))).not_to have_issue(Puppet::Pops::Issues::ILLEGAL_DEFINITION_LOCATION)
238+
end
239+
228240
it 'produces an error for application' do
229241
acceptor = validate(parse('application test {}'))
230242
expect(acceptor.error_count).to eql(1)

0 commit comments

Comments
 (0)