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



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

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

Instance Method Details

#class_indexesObject



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

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

#dataObject



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

def data
  @data
end

#defined_type_indexesObject



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

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

#filter_tokensObject



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

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
            class_indexes << {:start => token_idx, :end => idx} if @tokens[token_idx].first == :CLASS
            defined_type_indexes << {:start => token_idx, :end => idx} if @tokens[token_idx].first == :DEFINE
            break
          end
        end
      end
    end
  end
end

#manifest_linesObject



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

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



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

def path
  @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



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

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

#run(path, data) ⇒ Object



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

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

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

  @problems
end

#title_tokensObject



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

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

#tokensObject



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

def tokens
  @tokens
end