Skip to content

Commit

Permalink
Merge pull request #87 from da-ar/pdk-996
Browse files Browse the repository at this point in the history
(PDK-996) Provide better messaging when type cannot be resolved
  • Loading branch information
DavidS authored May 31, 2018
2 parents b7f6b81 + 6c28bd3 commit da454a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
11 changes: 2 additions & 9 deletions contrib/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,7 @@
# Code modified from: https://gist.github.com/hanloong/9849098
require 'English'

ADDED = %r{A|AM}

changed_files = `git status --porcelain`.split(%r{\n})
changed_files = changed_files.select do |file_name_with_status|
file_name_with_status =~ ADDED
end
changed_files = changed_files.map do |file_name_with_status|
file_name_with_status.split(' ')[1]
end
changed_files = `git diff --name-only --cached --diff-filter=ACM`.split(%r{\n})
changed_files = changed_files.select { |file_name|
File.extname(file_name) == '.rb'
}.join(' ')
Expand All @@ -20,4 +12,5 @@ system("bundle exec rubocop -a #{changed_files}") unless changed_files.empty?
if $CHILD_STATUS.to_s[-1].to_i.zero? && !changed_files.empty?
system("git add #{changed_files}")
end

exit $CHILD_STATUS.to_s[-1].to_i
9 changes: 8 additions & 1 deletion lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,14 @@ def insync?(is)
end
end

type = Puppet::Pops::Types::TypeParser.singleton.parse(options[:type])
begin
type = Puppet::Pops::Types::TypeParser.singleton.parse(options[:type])
rescue Puppet::ParseErrorWithIssue => e
raise Puppet::DevError, "The type of the `#{name}` attribute `#{options[:type]}` could not be parsed: #{e.message}"
rescue Puppet::ParseError => e
raise Puppet::DevError, "The type of the `#{name}` attribute `#{options[:type]}` is not recognised: #{e.message}"
end

if param_or_property == :newproperty
define_method(:should) do
if type.is_a? Puppet::Pops::Types::PBooleanType
Expand Down
30 changes: 30 additions & 0 deletions spec/puppet/resource_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,36 @@ def set(_context, _changes); end
end
end

context 'when registering a type with badly formed attribute type' do
let(:definition) do
{
name: 'bad_syntax',
attributes: {
name: {
type: 'Optional[String',
},
},
}
end

it { expect { described_class.register_type(definition) }.to raise_error Puppet::DevError, %r{The type of the `name` attribute `Optional\[String` could not be parsed:} }
end

context 'when registering a type with unknown attribute type' do
let(:definition) do
{
name: 'wibble',
attributes: {
name: {
type: 'wibble',
},
},
}
end

it { expect { described_class.register_type(definition) }.to raise_error Puppet::DevError, %r{The type of the `name` attribute `wibble` is not recognised:} }
end

context 'when registering a namevar that is not called `name`' do
let(:definition) do
{
Expand Down

0 comments on commit da454a7

Please sign in to comment.