-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(SDK-137) Add puppet syntax validation #105
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1c209df
(maint) switches run order of Puppet syntax and puppet lint validations
bmjen 9a200ea
(maint) Switches metadata-json-lint to not fail on warning as default…
bmjen 93a881a
(maint) Fixes bug where report target of nil throws an error
bmjen f85c3b5
(SDK-137) Adds Puppet Syntax validation.
bmjen a5252b7
(fixup) rename Report.to_(format) methods to .write_(format)
bmjen bce9290
(maint) Improves formatting of puppet-syntax events
bmjen 037c206
(maint) Adds acceptance tests for base validate all invocation
bmjen e34284b
(SDK-137) Adds acceptance tests for puppet syntax validation
bmjen 15e00be
(maint) Enable AppVeyor RDP access to debug build failure
rodjek 9c744c3
(maint) Suppress warnings from spawned ruby processes on Windows
rodjek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
require 'pdk' | ||
require 'pdk/cli/exec' | ||
require 'pdk/validators/base_validator' | ||
|
||
module PDK | ||
module Validate | ||
class PuppetSyntax < BaseValidator | ||
def self.name | ||
'puppet-syntax' | ||
end | ||
|
||
def self.cmd | ||
'puppet' | ||
end | ||
|
||
def self.pattern | ||
'**/**.pp' | ||
end | ||
|
||
def self.spinner_text | ||
_('Checking Puppet manifest syntax') | ||
end | ||
|
||
def self.parse_options(_options, targets) | ||
%w[parser validate].concat(targets) | ||
end | ||
|
||
def self.parse_output(report, result, targets) | ||
# Due to PUP-7504, we will have to programmatically construct the json | ||
# object from the text output for now. | ||
output = result[:stderr].split("\n") | ||
|
||
results_data = [] | ||
output.each do |offense| | ||
sanitize_console_output(offense) | ||
message, _at, location_raw = offense.partition(' at ') | ||
|
||
# Parse the offense type and msg | ||
severity, _colon, message = message.rpartition(': ') | ||
|
||
# Parse the offense location info | ||
location = location_raw.strip.match(%r{\A(?<file>.+):(?<line>\d+):(?<column>\d+)\Z}) unless location_raw.nil? | ||
|
||
attributes = { | ||
source: name, | ||
message: message.strip, | ||
state: 'failure', | ||
} | ||
attributes[:severity] = severity.strip unless severity.nil? | ||
|
||
unless location.nil? | ||
attributes[:file] = location[:file] | ||
attributes[:line] = location[:line] | ||
attributes[:column] = location[:column] | ||
end | ||
|
||
results_data << attributes | ||
end | ||
|
||
# puppet-lint does not include files without problems in its JSON | ||
# output, so we need to go through the list of targets and add passing | ||
# events to the report for any target not listed in the JSON output. | ||
targets.reject { |target| results_data.any? { |j| j[:file] == target } }.each do |target| | ||
report.add_event( | ||
file: target, | ||
source: 'puppet-syntax', | ||
severity: 'ok', | ||
state: :passed, | ||
) | ||
end | ||
|
||
results_data.each do |offense| | ||
report.add_event(offense) | ||
end | ||
end | ||
|
||
def self.sanitize_console_output(line) | ||
line.gsub!(%r{\e\[([;\d]+)?m}, '') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper_acceptance' | ||
|
||
describe 'Running all validations' do | ||
let(:junit_xsd) { File.join(RSpec.configuration.fixtures_path, 'JUnit.xsd') } | ||
|
||
context 'with a fresh module' do | ||
include_context 'in a new module', 'foo' | ||
|
||
init_pp = File.join('manifests', 'init.pp') | ||
|
||
before(:all) do | ||
File.open(init_pp, 'w') do |f| | ||
f.puts <<-EOS | ||
# foo | ||
class foo { } | ||
EOS | ||
end | ||
end | ||
|
||
describe command('pdk validate --list') do | ||
its(:exit_status) { is_expected.to eq(0) } | ||
its(:stdout) { is_expected.to match(%r{Available validators: metadata, puppet, ruby}i) } | ||
end | ||
|
||
describe command('pdk validate') do | ||
its(:exit_status) { is_expected.to eq(0) } | ||
its(:stdout) { is_expected.to match(%r{Running all available validators}i) } | ||
its(:stderr) { is_expected.to match(%r{Checking metadata.json}i) } | ||
its(:stderr) { is_expected.to match(%r{Checking Puppet manifest syntax}i) } | ||
its(:stderr) { is_expected.to match(%r{Checking Puppet manifest style}i) } | ||
its(:stderr) { is_expected.to match(%r{Checking Ruby code style}i) } | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be avoided by setting
Puppet[:color] = false
in https://github.com/voxpupuli/puppet-syntax/blob/master/lib/puppet-syntax/manifests.rb#L19 , or a similar place ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't want to change the default behavior of
puppet-syntax
itself and strip the colors, since users of this tool outside of PDK may still want the distinct console colors. I could add a Rakefile option to disable the colors in puppet-syntax and toggle it on or off based on that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good enough for now.