Skip to content

Commit 9e1ba56

Browse files
(PUP-11078) Puppet generate should return non-zero when failing
Previously, puppet generate would return an exit code with the value of 0 even when failing to generate a new custom type. ``` > puppet generate types Notice: Generating Puppet resource types. Error: <envpath>/modules/my_module/lib/puppet/type/my_module.rb: title patterns that use procs are not supported. > echo $? 0 ``` This commit fixes this issue by creating a new Puppet::Generator method that checks whether a type has raised an error or not. Since we want to continue checking inputs even if one has failed, this comes in handy since we can exit at the end of the run, keeping the functionality the same.
1 parent d1bad44 commit 9e1ba56

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

lib/puppet/face/generate.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
Puppet::FileSystem::mkpath(outputdir)
5959

6060
generator.generate(inputs, outputdir, options[:force])
61+
62+
exit(1) if generator.bad_input?
6163
nil
6264
end
6365
end

lib/puppet/generate/type.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ def self.find_inputs(format = :pcore, environment = Puppet.lookup(:current_envir
134134
inputs.sort_by! { |input| input.path }
135135
end
136136

137+
def self.bad_input?
138+
@bad_input
139+
end
137140
# Generates files for the given inputs.
138141
# If a file is up to date (newer than input) it is kept.
139142
# If a file is out of date it is regenerated.
@@ -170,6 +173,8 @@ def self.generate(inputs, outputdir = nil, force = false)
170173
}
171174

172175
up_to_date = true
176+
@bad_input = false
177+
173178
Puppet.notice _('Generating Puppet resource types.')
174179
inputs.each do |input|
175180
if !force && input.up_to_date?(outputdir)
@@ -187,6 +192,7 @@ def self.generate(inputs, outputdir = nil, force = false)
187192
raise
188193
rescue Exception => e
189194
# Log the exception and move on to the next input
195+
@bad_input = true
190196
Puppet.log_exception(e, _("Failed to load custom type '%{type_name}' from '%{input}': %{message}") % { type_name: type_name, input: input, message: e.message })
191197
next
192198
end
@@ -205,6 +211,7 @@ def self.generate(inputs, outputdir = nil, force = false)
205211
begin
206212
model = Models::Type::Type.new(type)
207213
rescue Exception => e
214+
@bad_input = true
208215
# Move on to the next input
209216
Puppet.log_exception(e, "#{input}: #{e.message}")
210217
next
@@ -214,6 +221,7 @@ def self.generate(inputs, outputdir = nil, force = false)
214221
begin
215222
result = model.render(templates[input.template_path])
216223
rescue Exception => e
224+
@bad_input = true
217225
Puppet.log_exception(e)
218226
raise
219227
end
@@ -227,6 +235,7 @@ def self.generate(inputs, outputdir = nil, force = false)
227235
file.write(result)
228236
end
229237
rescue Exception => e
238+
@bad_input = true
230239
Puppet.log_exception(e, _("Failed to generate '%{effective_output_path}': %{message}") % { effective_output_path: effective_output_path, message: e.message })
231240
# Move on to the next input
232241
next

spec/unit/face/generate_spec.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,70 @@ module Puppet
221221
end
222222

223223
end
224+
225+
context "in an environment with a faulty type" do
226+
let(:dir) do
227+
dir_containing('environments', { 'testing_generate2' => {
228+
'environment.conf' => "modulepath = modules",
229+
'manifests' => { 'site.pp' => "" },
230+
'modules' => {
231+
'm3' => {
232+
'lib' => { 'puppet' => { 'type' => {
233+
'test3.rb' => <<-EOF
234+
module Puppet
235+
Type.newtype(:test3) do
236+
@doc = "Docs for resource"
237+
def self.title_patterns
238+
identity = lambda {|x| x}
239+
[
240+
[
241+
/^(.*)_(.*)$/,
242+
[
243+
[:name, identity ]
244+
]
245+
]
246+
]
247+
end
248+
newproperty(:message) do
249+
desc "Docs for 'message' property"
250+
end
251+
newparam(:name) do
252+
desc "Docs for 'name' parameter"
253+
isnamevar
254+
end
255+
end; end
256+
EOF
257+
} }
258+
}
259+
}
260+
}}})
261+
end
262+
263+
let(:modulepath) do
264+
File.join(dir, 'testing_generate2', 'modules')
265+
end
266+
267+
let(:m3) do
268+
File.join(modulepath, 'm3')
269+
end
270+
271+
around(:each) do |example|
272+
Puppet.settings.initialize_global_settings
273+
Puppet[:manifest] = ''
274+
loader = Puppet::Environments::Directories.new(dir, [])
275+
Puppet.override(:environments => loader) do
276+
Puppet.override(:current_environment => loader.get('testing_generate2')) do
277+
example.run
278+
end
279+
end
280+
end
281+
282+
it 'fails when using procs for title patterns' do
283+
expect {
284+
genface.types(:format => 'pcore')
285+
}.to exit_with(1)
286+
end
287+
end
224288
end
225289

226290
def from_an_interactive_terminal

0 commit comments

Comments
 (0)