Class: PuppetLint::Checks

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-lint/checks.rb

Overview

Internal: Various methods that orchestrate the actions of the puppet-lint check plugins.

Constant Summary collapse

YAML_COMPATIBLE_CHECKS =
[:legacy_facts].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChecks

Public: Initialise a new PuppetLint::Checks object.



12
13
14
# File 'lib/puppet-lint/checks.rb', line 12

def initialize
  @problems = []
end

Instance Attribute Details

#problemsObject

Public: Get an Array of problem Hashes.



7
8
9
# File 'lib/puppet-lint/checks.rb', line 7

def problems
  @problems
end

Instance Method Details

#enabled_checksObject

Internal: Get a list of checks that have not been disabled.

Returns an Array of String check names.



135
136
137
138
139
# File 'lib/puppet-lint/checks.rb', line 135

def enabled_checks
  @enabled_checks ||= PuppetLint.configuration.checks.select do |check|
    PuppetLint.configuration.send(:"#{check}_enabled?")
  end
end

#load_data(path, content) ⇒ Object

Internal: Tokenise the manifest code and prepare it for checking.

path - The path to the file as passed to puppet-lint as a String. content - The String manifest code to be checked.

Returns nothing.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/puppet-lint/checks.rb', line 22

def load_data(path, content)
  lexer = PuppetLint::Lexer.new
  PuppetLint::Data.path = path
  PuppetLint::Data.manifest_lines = content.split("\n", -1)
  begin
    PuppetLint::Data.tokens = lexer.tokenise(content)
    PuppetLint::Data.parse_control_comments
  rescue PuppetLint::LexerError => e
    message = if e.reason.nil?
                'Syntax error'
              else
                "Syntax error (#{e.reason})"
              end

    problems << {
      kind: :error,
      check: :syntax,
      message:,
      line: e.line_no,
      column: e.column,
      fullpath: PuppetLint::Data.fullpath,
      path: PuppetLint::Data.path,
      filename: PuppetLint::Data.filename
    }
    PuppetLint::Data.tokens = []
  end
end

#manifestObject

Internal: Render the fixed manifest.

Returns the manifest as a String.



144
145
146
# File 'lib/puppet-lint/checks.rb', line 144

def manifest
  PuppetLint::Data.tokens.map(&:to_manifest).join
end

#run(fileinfo, data) ⇒ Object

Internal: Run the lint checks over the manifest or YAML code.

fileinfo - The path to the file as passed to puppet-lint as a String. data - The String manifest or YAML code to be checked.

Returns an Array of problem Hashes.



56
57
58
59
60
61
62
63
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/puppet-lint/checks.rb', line 56

def run(fileinfo, data)
  checks_run = []
  if File.extname(fileinfo).downcase.match?(%r{\.ya?ml$})
    PuppetLint::Data.path = fileinfo
    PuppetLint::Data.manifest_lines = data.split("\n", -1)

    enabled_checks.select { |check| YAML_COMPATIBLE_CHECKS.include?(check) }.each do |check|
      klass = PuppetLint.configuration.check_object[check].new
      # FIXME: shadowing #problems
      problems = klass.run
      checks_run << [klass, problems]
    end
  else
    load_data(fileinfo, data)

    enabled_checks.each do |check|
      klass = PuppetLint.configuration.check_object[check].new
      # FIXME: shadowing #problems
      problems = klass.run
      checks_run << [klass, problems]
    end
  end
  checks_run.each do |klass, problems|
    if PuppetLint.configuration.fix
      @problems.concat(klass.fix_problems)
    else
      @problems.concat(problems)
    end
  end

  @problems
rescue PuppetLint::SyntaxError => e
  @problems << {
    kind: :error,
    check: :syntax,
    message: 'Syntax error',
    fullpath: File.expand_path(fileinfo, ENV.fetch('PWD', nil)),
    filename: File.basename(fileinfo),
    path: fileinfo,
    line: e.token.line,
    column: e.token.column
  }

  @problems
rescue StandardError => e
  $stdout.puts <<-END.gsub(%r{^ {6}}, '')
    Whoops! It looks like puppet-lint has encountered an error that it doesn't
    know how to handle. Please open an issue at https://github.com/puppetlabs/puppet-lint
    and paste the following output into the issue description.
    ---
    puppet-lint version: #{PuppetLint::VERSION}
    ruby version: #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}
    platform: #{RUBY_PLATFORM}
    file path: #{fileinfo}
  END

  if File.readable?(fileinfo)
    $stdout.puts [
      'file contents:',
      '```',
      File.read(fileinfo),
      '```',
    ].join("\n")
  end

  $stdout.puts [
    'error:',
    '```',
    "#{e.class}: #{e.message}",
    e.backtrace.join("\n"),
    '```',
  ].join("\n")

  exit 1
end