Parent

Files

Class/Module Index [+]

Quicksearch

Chef::Cookbook::SyntaxCheck

Chef::Cookbook::SyntaxCheck

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

Attributes

cookbook_path[R]
validated_files[R]

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

Public Class Methods

for_cookbook(cookbook_name, cookbook_path=nil) click to toggle source

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

# File lib/chef/cookbook/syntax_check.rb, line 89
def self.for_cookbook(cookbook_name, cookbook_path=nil)
  cookbook_path ||= Chef::Config.cookbook_path
  unless cookbook_path
    raise ArgumentError, "Cannot find cookbook #{cookbook_name} unless Chef::Config.cookbook_path is set or an explicit cookbook path is given"
  end
  new(File.join(cookbook_path, cookbook_name.to_s))
end
new(cookbook_path) click to toggle source

Create a new SyntaxCheck object

Arguments

cookbook_path:

the (on disk) path to the cookbook

# File lib/chef/cookbook/syntax_check.rb, line 100
def initialize(cookbook_path)
  @cookbook_path = cookbook_path
  @validated_files = PersistentSet.new
end

Public Instance Methods

ruby_files() click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 105
def ruby_files
  Dir[File.join(cookbook_path, '**', '*.rb')]
end
template_files() click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 120
def template_files
  Dir[File.join(cookbook_path, '**', '*.erb')]
end
untested_ruby_files() click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 109
def untested_ruby_files
  ruby_files.reject do |file|
    if validated?(file)
      Chef::Log.debug("Ruby file #{file} is unchanged, skipping syntax check")
      true
    else
      false
    end
  end
end
untested_template_files() click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 124
def untested_template_files
  template_files.reject do |file| 
    if validated?(file)
      Chef::Log.debug("Template #{file} is unchanged, skipping syntax check")
      true
    else
      false
    end
  end
end
validate_ruby_file(ruby_file) click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 169
def validate_ruby_file(ruby_file)
  Chef::Log.debug("Testing #{ruby_file} for syntax errors...")
  result = shell_out("ruby -c #{ruby_file}")
  result.error!
  true
rescue Mixlib::ShellOut::ShellCommandFailed
  file_relative_path = ruby_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1]
  Chef::Log.fatal("Cookbook file #{file_relative_path} has a ruby syntax error:")
  result.stderr.each_line { |l| Chef::Log.fatal(l.chomp) }
  false
end
validate_ruby_files() click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 143
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) click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 157
def validate_template(erb_file)
  Chef::Log.debug("Testing template #{erb_file} for syntax errors...")
  result = shell_out("erubis -x #{erb_file} | ruby -c")
  result.error!
  true
rescue Mixlib::ShellOut::ShellCommandFailed
  file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1]
  Chef::Log.fatal("Erb template #{file_relative_path} has a syntax error:")
  result.stderr.each_line { |l| Chef::Log.fatal(l.chomp) }
  false
end
validate_templates() click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 150
def validate_templates
  untested_template_files.each do |template|
    return false unless validate_template(template)
    validated(template)
  end
end
validated(file) click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 139
def validated(file)
  validated_files.add(checksum(file))
end
validated?(file) click to toggle source
# File lib/chef/cookbook/syntax_check.rb, line 135
def validated?(file)
  validated_files.include?(checksum(file))
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.