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) ⇒ SyntaxCheck

Create a new SyntaxCheck object

Parameters:

  • cookbook_path (String)

    the (on disk) path to the cookbook



85
86
87
88
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 85

def initialize(cookbook_path)
  @cookbook_path   = cookbook_path
  @validated_files = PersistentSet.new
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



90
91
92
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 90

def ruby_files
  Dir[File.join(cookbook_path, '**', '*.rb')]
end

#template_filesObject



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

def template_files
  Dir[File.join(cookbook_path, '**', '*.erb')]
end

#untested_ruby_filesObject



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

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

#untested_template_filesObject



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

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

#validate_ruby_file(ruby_file) ⇒ Object



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

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



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

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



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 128

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



121
122
123
124
125
126
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 121

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

#validated(file) ⇒ Object



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

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

#validated?(file) ⇒ Boolean

Returns:

  • (Boolean)


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

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