lib/puppet-check/puppet_parser.rb in puppet-check-2.0.0 vs lib/puppet-check/puppet_parser.rb in puppet-check-2.0.1

- old
+ new

@@ -15,33 +15,37 @@ errors = [] Puppet::Util::Log.newdestination(Puppet::Test::LogCollector.new(errors)) # check puppet syntax begin + # initialize message + message = '' # in puppet >= 6.5 the return of this method is a hash with the error new_error = Puppet::Face[:parser, :current].validate(file) # puppet 6.5 output format is now a hash from the face api - if Puppet::PUPPETVERSION.to_f >= 6.5 && new_error != {} - next PuppetCheck.settings[:error_files].push("#{file}:\n#{new_error.values.map(&:to_s).join("\n").gsub(/ \(file: #{File.absolute_path(file)}(, |\))/, '').gsub(/Could not parse.*: /, '')}") + if Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('6.5.0') && new_error != {} + message = new_error.values.map(&:to_s).join("\n").gsub(/ \(file: #{File.absolute_path(file)}(, |\))/, '').gsub(/Could not parse.*: /, '') end # this is the actual error that we need to rescue Puppet::Face from rescue SystemExit # puppet 5.4-6.4 has a new validator output format and eof errors have fake dir env info - if Puppet::PUPPETVERSION.to_f >= 5.4 && Puppet::PUPPETVERSION.to_f < 6.5 - next PuppetCheck.settings[:error_files].push("#{file}:\n#{errors.map(&:to_s).join("\n").gsub(/file: #{File.absolute_path(file)}(, |\))/, '').gsub(/Could not parse.*: /, '')}") + if Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.4') && Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('6.5') + message = errors.map(&:to_s).join("\n").gsub(/file: #{File.absolute_path(file)}(, |\))/, '').gsub(/Could not parse.*: /, '') # puppet 5.0-5.2 can only do one error per line and outputs fake dir env info - elsif Puppet::PUPPETVERSION.to_f >= 5.0 && Puppet::PUPPETVERSION.to_f < 5.3 - next PuppetCheck.settings[:error_files].push("#{file}:\n#{errors.map(&:to_s).join("\n").gsub("#{File.absolute_path(file)}:", '').gsub(/Could not parse.*: /, '')}") + elsif Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.0') && Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new('5.3') + message = errors.map(&:to_s).join("\n").gsub("#{File.absolute_path(file)}:", '').gsub(/Could not parse.*: /, '') end # puppet < 5 and 5.3 parser output style - next PuppetCheck.settings[:error_files].push("#{file}:\n#{errors.map(&:to_s).join("\n").gsub("#{File.absolute_path(file)}:", '')}") + message = errors.map(&:to_s).join("\n").gsub("#{File.absolute_path(file)}:", '') end + # output message + next PuppetCheck.settings[:error_files].push("#{file}:\n#{message}") unless message.empty? # initialize warnings with output from the parser if it exists, since the output is warnings if Puppet::Face did not trigger a SystemExit warnings = "#{file}:" unless errors.empty? # puppet 5.4-5.x has a new validator output format - warnings << if Puppet::PUPPETVERSION.to_f >= 5.4 + warnings << if Gem::Version.new(Puppet::PUPPETVERSION) >= Gem::Version.new('5.4') "\n#{errors.map(&:to_s).join("\n").gsub("file: #{File.absolute_path(file)}, ", '')}" # puppet <= 5.3 validator output format else "\n#{errors.map(&:to_s).join("\n").gsub("#{File.absolute_path(file)}:", '')}" end