-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from bmjen/validation-cleanup
(SDK-137) Add puppet syntax validation
- Loading branch information
Showing
15 changed files
with
363 additions
and
77 deletions.
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.