Class: Chef::Cookbook::SyntaxCheck

Inherits:
Object
  • Object
show all
Includes:
Mixin::Checksum, Mixin::ShellOut
Defined in:
lib/chef/cookbook/syntax_check.rb

Overview

Chef::Cookbook::SyntaxCheck

Encapsulates the process of validating the ruby syntax of files in Chef cookbooks.

Defined Under Namespace

Classes: PersistentSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Checksum

#checksum, #checksum_match?, #short_cksum

Constructor Details

#initialize(cookbook_path) ⇒ SyntaxCheck

Create a new SyntaxCheck object

Arguments

cookbook_path:

the (on disk) path to the cookbook



99
100
101
102
103
104
# File 'lib/chef/cookbook/syntax_check.rb', line 99

def initialize(cookbook_path)
  @cookbook_path = cookbook_path
  @chefignore ||= Chefignore.new(cookbook_path)

  @validated_files = PersistentSet.new
end

Instance Attribute Details

#chefignoreObject (readonly)

Returns the value of attribute chefignore.



81
82
83
# File 'lib/chef/cookbook/syntax_check.rb', line 81

def chefignore
  @chefignore
end

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



75
76
77
# File 'lib/chef/cookbook/syntax_check.rb', line 75

def cookbook_path
  @cookbook_path
end

#validated_filesObject (readonly)

A PersistentSet object that tracks which files have already been validated.



79
80
81
# File 'lib/chef/cookbook/syntax_check.rb', line 79

def validated_files
  @validated_files
end

Class Method Details

.for_cookbook(cookbook_name, cookbook_path = nil) ⇒ Object

Creates a new SyntaxCheck given the cookbook_name and a cookbook_path. If no cookbook_path is given, Chef::Config.cookbook_path is used.

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
# File 'lib/chef/cookbook/syntax_check.rb', line 85

def self.for_cookbook(cookbook_name, cookbook_path = nil)
  cookbook_path ||= Chef::Config.cookbook_path

  Array(cookbook_path).each do |entry|
    path = File.expand_path(File.join(entry, cookbook_name.to_s))
    return new(path) if Dir.exist?(path)
  end

  raise ArgumentError, "Cannot find cookbook #{cookbook_name} unless Chef::Config.cookbook_path is set or an explicit cookbook path is given"
end

Instance Method Details

#invalid_erb_file(erb_file, error_message) ⇒ Object

Debug a syntax error in a template.



215
216
217
218
219
220
# File 'lib/chef/cookbook/syntax_check.rb', line 215

def invalid_erb_file(erb_file, error_message)
  file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path + File::Separator)}(.*)/, 1]
  Chef::Log.fatal("Erb template #{file_relative_path} has a syntax error:")
  error_message.each_line { |l| Chef::Log.fatal(l.chomp) }
  nil
end

#invalid_ruby_file(ruby_file, error_message) ⇒ Object

Debugs ruby syntax errors by printing the path to the file and any diagnostic info given in error_message



250
251
252
253
254
255
# File 'lib/chef/cookbook/syntax_check.rb', line 250

def invalid_ruby_file(ruby_file, error_message)
  file_relative_path = ruby_file[ruby_file.index(cookbook_path.split("/").last), ruby_file.length]
  Chef::Log.fatal("Cookbook file #{file_relative_path} has a ruby syntax error.")
  error_message.each_line { |l| Chef::Log.fatal(l.chomp) }
  false
end

#remove_ignored_files(file_list) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/chef/cookbook/syntax_check.rb', line 106

def remove_ignored_files(file_list)
  return file_list if chefignore.ignores.empty?

  file_list.reject do |full_path|
    relative_pn = Chef::Util::PathHelper.relative_path_from(cookbook_path, full_path)
    chefignore.ignored?(relative_pn.to_s)
  end
end

#remove_uninteresting_ruby_files(file_list) ⇒ Object



115
116
117
# File 'lib/chef/cookbook/syntax_check.rb', line 115

def remove_uninteresting_ruby_files(file_list)
  file_list.grep_v(%r{#{Regexp.quote(cookbook_path)}/(files|templates)/})
end

#rubyObject

Returns the full path to the running ruby.



258
259
260
# File 'lib/chef/cookbook/syntax_check.rb', line 258

def ruby
  Gem.ruby
end

#ruby_filesObject



119
120
121
122
123
124
# File 'lib/chef/cookbook/syntax_check.rb', line 119

def ruby_files
  path  = Chef::Util::PathHelper.escape_glob_dir(cookbook_path)
  files = Dir[File.join(path, "**", "*.rb")]
  files = remove_ignored_files(files)
  remove_uninteresting_ruby_files(files)
end

#template_filesObject



137
138
139
# File 'lib/chef/cookbook/syntax_check.rb', line 137

def template_files
  remove_ignored_files Dir[File.join(Chef::Util::PathHelper.escape_glob_dir(cookbook_path), "**/templates/**", "*.erb")]
end

#untested_ruby_filesObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/chef/cookbook/syntax_check.rb', line 126

def untested_ruby_files
  ruby_files.reject do |file|
    if validated?(file)
      Chef::Log.trace("Ruby file #{file} is unchanged, skipping syntax check")
      true
    else
      false
    end
  end
end

#untested_template_filesObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/cookbook/syntax_check.rb', line 141

def untested_template_files
  template_files.reject do |file|
    if validated?(file)
      Chef::Log.trace("Template #{file} is unchanged, skipping syntax check")
      true
    else
      false
    end
  end
end

#validate_erb_file_inline(erb_file) ⇒ Object

Validate the ruby code in an erb template. Uses RubyVM to do syntax checking.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/chef/cookbook/syntax_check.rb', line 188

def validate_erb_file_inline(erb_file)
  old_stderr = $stderr

  engine = Erubis::Eruby.new
  engine.convert!(IO.read(erb_file))

  ruby_code = engine.src

  # Even when we're compiling the code w/ RubyVM, syntax errors just
  # print to $stderr. We want to capture this and handle the printing
  # ourselves, so we must temporarily swap $stderr to capture the output.
  tmp_stderr = $stderr = StringIO.new

  abs_path = File.expand_path(erb_file)
  RubyVM::InstructionSequence.new(ruby_code, erb_file, abs_path, 0)

  true
rescue SyntaxError
  $stderr = old_stderr
  invalid_erb_file(erb_file, tmp_stderr.string)
  false
ensure
  # be paranoid about setting stderr back to the old value.
  $stderr = old_stderr if defined?(old_stderr) && old_stderr
end

#validate_ruby_file(ruby_file) ⇒ Object



181
182
183
184
# File 'lib/chef/cookbook/syntax_check.rb', line 181

def validate_ruby_file(ruby_file)
  Chef::Log.trace("Testing #{ruby_file} for syntax errors...")
  validate_ruby_file_inline(ruby_file)
end

#validate_ruby_file_inline(ruby_file) ⇒ Object

Validate the syntax of a ruby file. Uses (Ruby 1.9+ only) RubyVM to compile the code without evaluating it or spawning a new process.



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/chef/cookbook/syntax_check.rb', line 224

def validate_ruby_file_inline(ruby_file)
  # Even when we're compiling the code w/ RubyVM, syntax errors just
  # print to $stderr. We want to capture this and handle the printing
  # ourselves, so we must temporarily swap $stderr to capture the output.
  old_stderr = $stderr
  tmp_stderr = $stderr = StringIO.new
  abs_path = File.expand_path(ruby_file)
  file_content = IO.read(abs_path)
  # We have to wrap this in a block so the user code evaluates in a
  # similar context as what Chef does normally. Otherwise RubyVM
  # will reject some common idioms, like using `return` to end evaluation
  # of a recipe. See also CHEF-5199
  wrapped_content = "Object.new.instance_eval do\n#{file_content}\nend\n"
  RubyVM::InstructionSequence.new(wrapped_content, ruby_file, abs_path, 0)
  true
rescue SyntaxError
  $stderr = old_stderr
  invalid_ruby_file(ruby_file, tmp_stderr.string)
  false
ensure
  # be paranoid about setting stderr back to the old value.
  $stderr = old_stderr if defined?(old_stderr) && old_stderr
end

#validate_ruby_filesObject



160
161
162
163
164
165
166
# File 'lib/chef/cookbook/syntax_check.rb', line 160

def validate_ruby_files
  untested_ruby_files.each do |ruby_file|
    return false unless validate_ruby_file(ruby_file)

    validated(ruby_file)
  end
end

#validate_template(erb_file) ⇒ Object



176
177
178
179
# File 'lib/chef/cookbook/syntax_check.rb', line 176

def validate_template(erb_file)
  Chef::Log.trace("Testing template #{erb_file} for syntax errors...")
  validate_erb_file_inline(erb_file)
end

#validate_templatesObject



168
169
170
171
172
173
174
# File 'lib/chef/cookbook/syntax_check.rb', line 168

def validate_templates
  untested_template_files.each do |template|
    return false unless validate_template(template)

    validated(template)
  end
end

#validated(file) ⇒ Object



156
157
158
# File 'lib/chef/cookbook/syntax_check.rb', line 156

def validated(file)
  validated_files.add(checksum(file))
end

#validated?(file) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/chef/cookbook/syntax_check.rb', line 152

def validated?(file)
  validated_files.include?(checksum(file))
end