Skip to content

Commit

Permalink
reuse puppetlint and rubocop objects for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Dec 7, 2023
1 parent cce38ba commit 849fbd1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
13 changes: 8 additions & 5 deletions lib/puppet-check/puppet_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def self.manifest(files, style, pl_args)
# prepare the Puppet settings for the error checking
Puppet.initialize_settings unless Puppet.settings.app_defaults_initialized?

# prepare the PuppetLint object for style checks
if style
require 'puppet-lint'
require 'puppet-lint/optparser'
puppet_lint = PuppetLint.new
end

files.each do |file|
# setup error logging and collection; warnings logged for all versions, but errors for only puppet < 6.5
errors = []
Expand Down Expand Up @@ -48,18 +55,14 @@ def self.manifest(files, style, pl_args)

# check puppet style
if style
require 'puppet-lint'
require 'puppet-lint/optparser'

# check for invalid arguments to PuppetLint
begin
PuppetLint::OptParser.build.parse!(pl_args.clone)
rescue OptionParser::InvalidOption
raise "puppet-lint: invalid option supplied among #{pl_args.join(' ')}"
end

# prepare the PuppetLint object for style checks
puppet_lint = PuppetLint.new
# execute puppet-lint style checks
puppet_lint.file = file
puppet_lint.run

Expand Down
20 changes: 15 additions & 5 deletions lib/puppet-check/ruby_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
class RubyParser
# checks ruby (.rb)
def self.ruby(files, style, rc_args)
# prepare rubocop object for style checks
if style
require 'json'
require 'rubocop'

rubocop_cli = RuboCop::CLI.new
end

files.each do |file|
# check ruby syntax
# prevents ruby code from actually executing
Expand All @@ -15,10 +23,7 @@ def self.ruby(files, style, rc_args)
# check ruby style
if style
# check RuboCop and parse warnings' JSON output
require 'json'
require 'rubocop'

rubocop_warnings = Utils.capture_stdout { RuboCop::CLI.new.run(rc_args + ['--enable-pending-cops', '--require', 'rubocop-performance', '--format', 'json', file]) }
rubocop_warnings = Utils.capture_stdout { rubocop_cli.run(rc_args + ['--enable-pending-cops', '--require', 'rubocop-performance', '--format', 'json', file]) }
rubocop_offenses = JSON.parse(rubocop_warnings)['files'][0]['offenses'].map { |warning| "#{warning['location']['line']}:#{warning['location']['column']} #{warning['message']}" }

# check Reek and parse warnings' JSON output
Expand Down Expand Up @@ -65,6 +70,11 @@ def self.template(files)
def self.librarian(files, style, rc_args)
# efficient var assignment prior to iterator
if style
require 'json'
require 'rubocop'

rubocop_cli = RuboCop::CLI.new

# RuboCop is grumpy about non-snake_case filenames so disable the FileName check
rc_args.include?('--except') ? rc_args[rc_args.index('--except') + 1] = "#{rc_args[rc_args.index('--except') + 1]},Naming/FileName" : rc_args.push('--except', 'Naming/FileName')
end
Expand All @@ -82,7 +92,7 @@ def self.librarian(files, style, rc_args)
require 'json'
require 'rubocop'

warnings = Utils.capture_stdout { RuboCop::CLI.new.run(rc_args + ['--enable-pending-cops', '--require', 'rubocop-performance', '--format', 'json', file]) }
warnings = Utils.capture_stdout { rubocop_cli.run(rc_args + ['--enable-pending-cops', '--require', 'rubocop-performance', '--format', 'json', file]) }
offenses = JSON.parse(warnings)['files'][0]['offenses'].map { |warning| "#{warning['location']['line']}:#{warning['location']['column']} #{warning['message']}" }

# collect style warnings
Expand Down

0 comments on commit 849fbd1

Please sign in to comment.