Skip to content
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) Adds Puppet Parser syntax validation #94

Merged
merged 8 commits into from
Jun 21, 2017
Merged
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
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in pdk.gemspec
gemspec

gem 'metadata-json-lint'
gem 'puppet-lint'

# avoid newer versions that do not support ruby 2.1 anymore
gem 'nokogiri', '1.7.2'

Expand Down
7 changes: 5 additions & 2 deletions lib/pdk/cli/validate.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'pdk/util/bundler'

module PDK::CLI
@validate_cmd = @base_cmd.define_command do
name 'validate'
Expand All @@ -8,8 +10,6 @@ module PDK::CLI
flag nil, :list, _('list all available validators')

run do |opts, args, _cmd|
require 'pdk/validate'

validator_names = PDK::Validate.validators.map { |v| v.name }
validators = PDK::Validate.validators
targets = []
Expand Down Expand Up @@ -63,6 +63,9 @@ module PDK::CLI

exit_code = 0

# Ensure that the bundle is installed and tools are available before running any validations.
PDK::Util::Bundler.ensure_bundle!

validators.each do |validator|
exit_code = validator.invoke(report, options)
break if exit_code != 0
Expand Down
6 changes: 3 additions & 3 deletions lib/pdk/generators/module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def self.invoke(opts = {})
'name' => "#{Etc.getlogin}-#{opts[:name]}",
'version' => '0.1.0',
'dependencies' => [
{ 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 1.0.0' },
{ 'name' => 'puppetlabs-stdlib', 'version_requirement' => '>= 4.13.1 < 5.0.0' },
],
}

Expand Down Expand Up @@ -112,11 +112,11 @@ def self.module_interview(metadata, opts = {})
end

puts ''
module_summary = PDK::CLI::Input.get(_('How would you describe this module in a single sentence?'))
module_summary = PDK::CLI::Input.get(_('How would you describe this module in a single sentence?'), metadata.data['summary'])
metadata.update!('summary' => module_summary)

puts ''
module_source = PDK::CLI::Input.get(_("Where is this module's source code repository?"))
module_source = PDK::CLI::Input.get(_("Where is this module's source code repository?"), metadata.data['source'])
metadata.update!('source' => module_source)

puts ''
Expand Down
12 changes: 8 additions & 4 deletions lib/pdk/validators/base_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
module PDK
module Validate
class BaseValidator
def self.cmd_path
File.join(PDK::Util.module_root, 'bin', cmd)
end

def self.parse_targets(options)
# If no targets are specified, then we will run validations from the
# base module directory.
Expand All @@ -16,7 +20,8 @@ def self.parse_targets(options)
targets.map { |target|
if respond_to?(:pattern)
if File.directory?(target)
Array[pattern].flatten.map { |p| Dir.glob(File.join(target, p)) }
files_glob = Array[pattern].flatten.map { |p| Dir.glob(File.join(target, p)) }
files_glob.flatten.empty? ? target : files_glob
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return target if there are no files that match the pattern?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

puppet-lint and puppet parser validate require a path input to work. The alternative is to not call them at all if no files match.

else
target
end
Expand All @@ -35,11 +40,10 @@ def self.spinner_text
end

def self.invoke(report, options = {})
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!(name)
PDK::Util::Bundler.ensure_binstubs!(cmd)

targets = parse_targets(options)
cmd_argv = parse_options(options, targets).unshift(cmd)
cmd_argv = parse_options(options, targets).unshift(cmd_path)
cmd_argv.unshift('ruby') if Gem.win_platform?

PDK.logger.debug(_('Running %{cmd}') % { cmd: cmd_argv.join(' ') })
Expand Down
34 changes: 24 additions & 10 deletions lib/pdk/validators/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,35 @@ def self.name
end

def self.cmd
File.join(PDK::Util.module_root, 'bin', 'metadata-json-lint')
'metadata-json-lint'
end

def self.invoke(_report, options = {})
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!('metadata-json-lint')
def self.parse_targets(_options)
[File.join(PDK::Util.module_root, 'metadata.json')]
end

def self.parse_options(_options, targets)
cmd_options = ['--format', 'json']

options[:targets] = [File.join(PDK::Util.module_root, 'metadata.json')]
cmd_options.concat(targets)
end

# result = super
def self.parse_output(report, json_data)
return if json_data.empty?

# FIXME: this is weird so that it complies with the format
# of the other validators which are nested
# { 'metadata' => result }
0
json_data.delete('result')
json_data.keys.each do |type|
json_data[type].each do |offense|
report.add_event(
file: 'metadata.json',
source: cmd,
message: offense['msg'],
test: offense['check'],
severity: type,
state: :failure,
)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/validators/puppet/puppet_lint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.name
end

def self.cmd
File.join(PDK::Util.module_root, 'bin', 'puppet-lint')
'puppet-lint'
end

def self.pattern
Expand Down
19 changes: 18 additions & 1 deletion lib/pdk/validators/puppet/puppet_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,24 @@ def self.name
end

def self.cmd
'pwd'
'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, _json_data)
# TODO: handle outputs
report.add_event(result.merge(state: :passed, severity: :ok))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pdk/validators/puppet_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.name
end

def self.puppet_validators
[PuppetLint]
[PuppetLint, PuppetParser]
end

def self.invoke(report, options = {})
Expand Down
9 changes: 1 addition & 8 deletions lib/pdk/validators/ruby/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.name
end

def self.cmd
File.join(PDK::Util.module_root, 'bin', 'rubocop')
'rubocop'
end

def self.spinner_text
Expand All @@ -26,13 +26,6 @@ def self.parse_options(_options, targets)
cmd_options.concat(targets)
end

def self.invoke(report, options = {})
PDK::Util::Bundler.ensure_bundle!
PDK::Util::Bundler.ensure_binstubs!('rubocop')

super
end

def self.parse_output(report, json_data)
return unless json_data.key?('files')

Expand Down
4 changes: 4 additions & 0 deletions spec/cli/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
let(:validator_names) { validators.map(&:name).join(', ') }
let(:validator_success) { { exit_code: 0, stdout: 'success', stderr: '' } }

before(:each) do
allow(PDK::Util::Bundler).to receive(:ensure_bundle!)
end

context 'when no arguments or options are provided' do
it 'invokes each validator with no report and no options and exits zero' do
validators.each do |validator|
Expand Down