Class: RubyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-check/ruby_parser.rb

Overview

executes diagnostics on ruby files

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.librarian(files) ⇒ Object

checks librarian puppet (Puppetfile/Modulefile) and misc ruby (Rakefile/Gemfile)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet-check/ruby_parser.rb', line 64

def self.librarian(files)
  files.each do |file|
    # check librarian puppet syntax
    begin
      # prevents ruby code from actually executing
      catch(:good) { instance_eval("BEGIN {throw :good}; #{File.read(file)}") }
    rescue SyntaxError, LoadError, ArgumentError => err
      PuppetCheck.error_files.push("-- #{file}:\n#{err}")
    # check librarian puppet style
    else
      if PuppetCheck.style_check
        require 'rubocop'

        # check Rubocop
        rubocop_args = PuppetCheck.rubocop_args.clone
        # RuboCop is grumpy about non-snake_case filenames so disable the Style/FileName check
        rubocop_args.include?('--except') ? rubocop_args[rubocop_args.index('--except') + 1] = "#{rubocop_args[rubocop_args.index('--except') + 1]},Style/FileName" : rubocop_args.concat(['--except', 'Style/FileName'])
        warnings = Utils.capture_stdout { RuboCop::CLI.new.run(rubocop_args + ['--format', 'emacs', file]) }

        # collect style warnings
        next PuppetCheck.warning_files.push("-- #{file}:\n#{warnings.split("#{File.absolute_path(file)}:").join('')}") unless warnings.empty?
      end
      PuppetCheck.clean_files.push("-- #{file}")
    end
  end
end

.ruby(files) ⇒ Object

checks ruby (.rb)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/puppet-check/ruby_parser.rb', line 7

def self.ruby(files)
  files.each do |file|
    # check ruby syntax
    begin
      # prevents ruby code from actually executing
      catch(:good) { instance_eval("BEGIN {throw :good}; #{File.read(file)}") }
    rescue ScriptError, StandardError => err
      PuppetCheck.error_files.push("-- #{file}:\n#{err}")
    else
      # check ruby style
      if PuppetCheck.style_check
        require 'rubocop'

        # check RuboCop and collect warnings
        rubocop_warnings = Utils.capture_stdout { RuboCop::CLI.new.run(PuppetCheck.rubocop_args + ['--format', 'emacs', file]) }
        warnings = rubocop_warnings == '' ? '' : rubocop_warnings.split("#{File.absolute_path(file)}:").join('')

        # check Reek and collect warnings
        if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.1.0')
          require 'reek'
          require 'reek/cli/application'
          reek_warnings = Utils.capture_stdout { Reek::CLI::Application.new([file]).execute }
          warnings += reek_warnings.split("\n")[1..-1].map(&:strip).join("\n") unless reek_warnings == ''
        end

        # return warnings
        next PuppetCheck.warning_files.push("-- #{file}:\n#{warnings.strip}") unless warnings == ''
      end
      PuppetCheck.clean_files.push("-- #{file}")
    end
  end
end

.template(files) ⇒ Object

checks ruby template (.erb)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet-check/ruby_parser.rb', line 41

def self.template(files)
  require 'erb'

  files.each do |file|
    # check ruby template syntax
    begin
      # need to eventually have this associated with a different binding during each iteration
      # warnings = Util.capture_stderr { ERB.new(File.read(file), nil, '-').result(RubyParser.new.binding) }
      warnings = Utils.capture_stderr { ERB.new(File.read(file), nil, '-').result }
    # credits to gds-operations/puppet-syntax for errors to ignore
    rescue NameError, TypeError
      # empty out warnings since it would contain an error if this pass triggers
      warnings = ''
    rescue ScriptError => err
      next PuppetCheck.error_files.push("-- #{file}:\n#{err}")
    end
    # return warnings from the check if there were any
    next PuppetCheck.warning_files.push("-- #{file}:\n#{warnings.gsub('warning: ', '').split('(erb):').join('').strip}") unless warnings == ''
    PuppetCheck.clean_files.push("-- #{file}")
  end
end

Instance Method Details

#bindingObject

potentially for unique erb bindings



92
93
94
# File 'lib/puppet-check/ruby_parser.rb', line 92

def binding
  binding
end