Class: Ridley::Chef::Cookbook::SyntaxCheck

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

Overview

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

Borrowed and modified from: https://github.com/opscode/chef/blob/11.4.0/lib/chef/cookbook/syntax_check.rb

Copyright

Copyright © 2010 Opscode, Inc.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Classes: PersistentSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Checksum

#checksum

Methods included from Logging

logger, #logger, set_logger

Constructor Details

#initialize(cookbook_path, chefignore = nil) ⇒ SyntaxCheck

Create a new SyntaxCheck object

Parameters:

  • cookbook_path (String)

    the (on disk) path to the cookbook

  • chefignore (Ridley::Chef::Chefignore) (defaults to: nil)

    the instance of R::C::Chefignore to filter out



87
88
89
90
91
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 87

def initialize(cookbook_path, chefignore = nil)
  @cookbook_path   = cookbook_path
  @validated_files = PersistentSet.new
  @chefignore      = chefignore
end

Instance Attribute Details

#cookbook_pathObject (readonly)

Returns the value of attribute cookbook_path.



75
76
77
# File 'lib/ridley/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/ridley/chef/cookbook/syntax_check.rb', line 79

def validated_files
  @validated_files
end

Instance Method Details

#ruby_filesObject



94
95
96
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 94

def ruby_files
  Dir[File.join(cookbook_path, '**', '*.rb')].reject { |f| ignored?(f) }
end

#template_filesObject



102
103
104
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 102

def template_files
  Dir[File.join(cookbook_path, '**', '*.erb')].reject { |f| ignored?(f) }
end

#untested_ruby_filesObject



98
99
100
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 98

def untested_ruby_files
  ruby_files.reject { |file| validated?(file) }
end

#untested_template_filesObject



106
107
108
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 106

def untested_template_files
  template_files.reject { |file| validated?(file) }
end

#validate_ruby_file(ruby_file) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 147

def validate_ruby_file(ruby_file)
  result = shell_out("ruby -c #{ruby_file.shellescape}")

  if result.error?
    file_relative_path = ruby_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1]
    log.error { "Cookbook file #{file_relative_path} has a ruby syntax error:" }
    result.stderr.each_line { |l| Ridley.log.error(l.chomp) }
    return false
  end

  true
end

#validate_ruby_filesObject



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

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

#validate_template(erb_file) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 134

def validate_template(erb_file)
  result = shell_out("erubis -x #{erb_file.shellescape} | ruby -c")

  if result.error?
    file_relative_path = erb_file[/^#{Regexp.escape(cookbook_path+File::Separator)}(.*)/, 1]
    log.error { "Erb template #{file_relative_path} has a syntax error:" }
    result.stderr.each_line { |l| Ridley.log.fatal(l.chomp) }
    return false
  end

  true
end

#validate_templatesObject



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

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

#validated(file) ⇒ Object



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

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

#validated?(file) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 110

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