Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/puppet-lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
require 'puppet-lint/checks'
require 'puppet-lint/bin'
require 'puppet-lint/monkeypatches'
require 'puppet'

class PuppetLint::NoCodeError < StandardError; end
class PuppetLint::InvalidCodeError < StandardError; end
class PuppetLint::NoFix < StandardError; end

# Public: The public interface to puppet-lint.
Expand Down Expand Up @@ -153,11 +155,33 @@ def warnings?
@statistics[:warning] != 0
end

# Public: Determine if the puppet code validates properly.
#
# Returns true if the code is valid, otherwise returns false.
def valid? (code)
minimum_puppet_version = '4.0.0'
if Gem::Version.new(Puppet::PUPPETVERSION) < Gem::Version.new(minimum_puppet_version)
return
end

begin
Puppet.settings[:app_management] = true if Gem::Version.new(Puppet.version) >= Gem::Version.new('4.3.2')
parser = Puppet::Pops::Parser::EvaluatingParser.singleton
result = parser.parser.parse_string(code, 'test.pp')
result = result.model
acceptor = parser.validate(result)

acceptor.error_count.zero?
rescue => detail
end
end

# Public: Run the loaded manifest code through the lint checks and print the
# results of the checks to stdout.
#
# Returns nothing.
# Raises PuppetLint::NoCodeError if no manifest code has been loaded.
# Raises PuppetLint::InvalidCodeError if the manifest code cannot be validate by puppet
def run
if @code.nil?
raise PuppetLint::NoCodeError
Expand All @@ -169,6 +193,10 @@ def run
return
end

unless valid?(@code)
raise PuppetLint::InvalidCodeError
end

linter = PuppetLint::Checks.new
@problems = linter.run(@path, @code)
@problems.each { |problem| @statistics[problem[:kind]] += 1 }
Expand Down
8 changes: 7 additions & 1 deletion lib/puppet-lint/bin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ def run
path.each do |f|
l = PuppetLint.new
l.file = f
l.run
begin
l.run
rescue PuppetLint::InvalidCodeError
puts "#{f}: ERROR: cannot be validated by `puppet parser validate`."
return_val = 1
next
end
l.print_problems
if l.errors? or (l.warnings? and PuppetLint.configuration.fail_on_warnings)
return_val = 1
Expand Down
2 changes: 2 additions & 0 deletions puppet-lint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Gem::Specification.new do |s|

s.add_development_dependency 'github_changelog_generator'

s.add_dependency 'puppet'

s.authors = ['Tim Sharpe']
s.email = '[email protected]'
end
6 changes: 3 additions & 3 deletions spec/fixtures/test/manifests/ignore_multiple_block.pp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# lint:ignore:double_quoted_strings lint:ignore:quoted_booleans
"true"
"false"
$x = "true"
$y = "false"
# lint:endignore

"true"
$z = "true"
4 changes: 2 additions & 2 deletions spec/fixtures/test/manifests/ignore_multiple_line.pp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"true" # lint:ignore:double_quoted_strings lint:ignore:quoted_booleans
"false" # lint:ignore:quoted_booleans lint:ignore:double_quoted_strings reason
$x = "true" # lint:ignore:double_quoted_strings lint:ignore:quoted_booleans
$y = "false" # lint:ignore:quoted_booleans lint:ignore:double_quoted_strings reason
16 changes: 15 additions & 1 deletion spec/puppet-lint/bin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'rspec/mocks'
require 'optparse'

invalid_code_error = "cannot be validated by `puppet parser validate`."

class CommandRun
attr_accessor :stdout, :stderr, :exitstatus

Expand Down Expand Up @@ -71,9 +73,21 @@ def initialize(args)
let(:args) { 'spec/fixtures/test/manifests/malformed.pp' }

its(:exitstatus) { is_expected.to eq(1) }
its(:stdout) { is_expected.to eq('ERROR: Syntax error (try running `puppet parser validate <file>`) on line 1') }
its(:stdout) { is_expected.to match(/#{invalid_code_error}/) }
its(:stdout) { is_expected.to match(/ERROR/) }
end

context 'when passed a malformed and a valid file' do
let(:args) { [
'spec/fixtures/test/manifests/malformed.pp',
'spec/fixtures/test/manifests/warning.pp',
] }

its(:exitstatus) { is_expected.to eq(1) }
its(:stdout) { is_expected.to match(/WARNING/) }
its(:stdout) { is_expected.to match(/ERROR/) }
end

context 'when limited to errors only' do
let(:args) { [
'--error-level', 'error',
Expand Down