Class: PuppetLint::CheckPlugin

Inherits:
Object
  • Object
show all
Includes:
Plugin
Defined in:
lib/puppet-lint/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Plugin

included

Constructor Details

#initializeCheckPlugin

Returns a new instance of CheckPlugin.



24
25
26
27
28
# File 'lib/puppet-lint/plugin.rb', line 24

def initialize
  @problems = []
  @checks = []
  @default_info = {:check => 'unknown', :linenumber => 0}
end

Instance Attribute Details

#checksObject (readonly)

Returns the value of attribute checks.



22
23
24
# File 'lib/puppet-lint/plugin.rb', line 22

def checks
  @checks
end

#problemsObject (readonly)

Returns the value of attribute problems.



22
23
24
# File 'lib/puppet-lint/plugin.rb', line 22

def problems
  @problems
end

Class Method Details

.check(name, &b) ⇒ Object



157
158
159
160
# File 'lib/puppet-lint/plugin.rb', line 157

def self.check(name, &b)
  PuppetLint.configuration.add_check name
  define_method("lint_check_#{name}", b)
end

Instance Method Details

#class_indexesObject



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

def class_indexes
  filter_tokens if @class_indexes.nil?
  @class_indexes
end

#dataObject



129
130
131
# File 'lib/puppet-lint/plugin.rb', line 129

def data
  @data
end

#defined_type_indexesObject



148
149
150
151
# File 'lib/puppet-lint/plugin.rb', line 148

def defined_type_indexes
  filter_tokens if @defined_type_indexes.nil?
  @defined_type_indexes
end

#filter_tokensObject



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
# File 'lib/puppet-lint/plugin.rb', line 74

def filter_tokens
  @title_tokens = []
  @resource_indexes = []
  @class_indexes = []
  @defined_type_indexes = []

  @tokens.each_index do |token_idx|
    if @tokens[token_idx].first == :COLON
      # gather a list of tokens that are resource titles
      if @tokens[token_idx-1].first == :RBRACK
        title_array_tokens = @tokens[@tokens.rindex { |r| r.first == :LBRACK }+1..token_idx-2]
        @title_tokens += title_array_tokens.select { |token| [:STRING, :NAME].include? token.first }
      else
        if @tokens[token_idx + 1].first != :LBRACE
          @title_tokens << @tokens[token_idx-1]
        end
      end

      # gather a list of start and end indexes for resource attribute blocks
      if @tokens[token_idx+1].first != :LBRACE
        @resource_indexes << {:start => token_idx+1, :end => @tokens[token_idx+1..-1].index { |r| [:SEMIC, :RBRACE].include? r.first }+token_idx}
      end
    elsif [:CLASS, :DEFINE].include? @tokens[token_idx].first
      lbrace_count = 0
      @tokens[token_idx+1..-1].each_index do |class_token_idx|
        idx = class_token_idx + token_idx
        if @tokens[idx].first == :LBRACE
          lbrace_count += 1
        elsif @tokens[idx].first == :RBRACE
          lbrace_count -= 1
          if lbrace_count == 0
            if @tokens[token_idx].first == :CLASS and @tokens[token_idx + 1].first != :LBRACE
              @class_indexes << {:start => token_idx, :end => idx}
            end
            @defined_type_indexes << {:start => token_idx, :end => idx} if @tokens[token_idx].first == :DEFINE
            break
          end
        end
      end
    end
  end
end

#fullpathObject



125
126
127
# File 'lib/puppet-lint/plugin.rb', line 125

def fullpath
  @fileinfo[:fullpath]
end

#manifest_linesObject



153
154
155
# File 'lib/puppet-lint/plugin.rb', line 153

def manifest_lines
  @manifest_lines ||= @data.split("\n")
end

#notify(kind, message_hash) ⇒ Object

notify(kind, message_hash) #=> nil

Adds the message to the problems array. The kind gets added to the message_hash by setting the key :kind. Typically, the message_hash should contain following keys:

message

which contains a string value describing the problem

linenumber

which contains the line number on which the problem occurs.

Besides the :kind value that is being set, some other key/values are also added. Typically, this is

check

which contains the name of the check that is being executed.

linenumber

which defaults to 0 if the message does not already contain one.

notify :warning, :message => "Something happened", :linenumber => 4
=> {:kind=>:warning, :message=>"Something happened", :linenumber=>4, :check=>'unknown'}


49
50
51
52
53
54
# File 'lib/puppet-lint/plugin.rb', line 49

def notify(kind, message_hash)
  message_hash[:kind] = kind
  message_hash.merge!(@default_info) {|key, v1, v2| v1 }
  @problems << message_hash
  message_hash
end

#pathObject



121
122
123
# File 'lib/puppet-lint/plugin.rb', line 121

def path
  @fileinfo[:path]
end

#register_check(check) ⇒ Object



30
31
32
# File 'lib/puppet-lint/plugin.rb', line 30

def register_check(check)
  @checks << check
end

#resource_indexesObject



138
139
140
141
# File 'lib/puppet-lint/plugin.rb', line 138

def resource_indexes
  filter_tokens if @resource_indexes.nil?
  @resource_indexes
end

#run(fileinfo, data) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puppet-lint/plugin.rb', line 56

def run(fileinfo, data)
  lexer = Puppet::Parser::Lexer.new
  lexer.string = data
  @tokens = lexer.fullscan
  @fileinfo = fileinfo
  @data = data

  self.public_methods.select { |method|
    method.to_s.start_with? 'lint_check_'
  }.each { |method|
    name = method.to_s[11..-1]
    @default_info[:check] = name
    self.send(method) if PuppetLint.configuration.send("#{name}_enabled?")
  }

  @problems
end

#title_tokensObject



133
134
135
136
# File 'lib/puppet-lint/plugin.rb', line 133

def title_tokens
  filter_tokens if @title_tokens.nil?
  @title_tokens
end

#tokensObject



117
118
119
# File 'lib/puppet-lint/plugin.rb', line 117

def tokens
  @tokens
end