Class: Ridley::Chef::Cookbook::SyntaxCheck
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
#checksum
#shell_out, #shell_out!
Methods included from Logging
logger, #logger, set_logger
Constructor Details
#initialize(cookbook_path) ⇒ SyntaxCheck
Create a new SyntaxCheck object
86
87
88
89
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 86
def initialize(cookbook_path)
@cookbook_path = cookbook_path
@validated_files = PersistentSet.new
end
|
Instance Attribute Details
#cookbook_path ⇒ Object
Returns the value of attribute cookbook_path.
76
77
78
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 76
def cookbook_path
@cookbook_path
end
|
#validated_files ⇒ Object
A PersistentSet object that tracks which files have already been validated.
80
81
82
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 80
def validated_files
@validated_files
end
|
Instance Method Details
#ruby_files ⇒ Object
91
92
93
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 91
def ruby_files
Dir[File.join(cookbook_path, '**', '*.rb')]
end
|
#template_files ⇒ Object
99
100
101
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 99
def template_files
Dir[File.join(cookbook_path, '**', '*.erb')]
end
|
#untested_ruby_files ⇒ Object
95
96
97
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 95
def untested_ruby_files
ruby_files.reject { |file| validated?(file) }
end
|
#untested_template_files ⇒ Object
103
104
105
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 103
def untested_template_files
template_files.reject { |file| validated?(file) }
end
|
#validate_ruby_file(ruby_file) ⇒ Object
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 140
def validate_ruby_file(ruby_file)
result = quietly { shell_out("ruby -c #{ruby_file.shellescape}") }
result.error!
true
rescue Mixlib::ShellOut::ShellCommandFailed
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) }
false
end
|
#validate_ruby_files ⇒ Object
115
116
117
118
119
120
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 115
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
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 129
def validate_template(erb_file)
result = quietly { shell_out("erubis -x #{erb_file.shellescape} | ruby -c") }
result.error!
true
rescue Mixlib::ShellOut::ShellCommandFailed
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) }
false
end
|
#validate_templates ⇒ Object
122
123
124
125
126
127
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 122
def validate_templates
untested_template_files.each do |template|
return false unless validate_template(template)
validated(template)
end
end
|
#validated(file) ⇒ Object
111
112
113
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 111
def validated(file)
validated_files.add(checksum(file))
end
|
#validated?(file) ⇒ Boolean
107
108
109
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 107
def validated?(file)
validated_files.include?(checksum(file))
end
|