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
Methods included from Logging
logger, #logger, set_logger
Constructor Details
#initialize(cookbook_path) ⇒ SyntaxCheck
Create a new SyntaxCheck object
84
85
86
87
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 84
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.
74
75
76
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 74
def cookbook_path
@cookbook_path
end
|
#validated_files ⇒ Object
A PersistentSet object that tracks which files have already been validated.
78
79
80
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 78
def validated_files
@validated_files
end
|
Instance Method Details
#ruby_files ⇒ Object
89
90
91
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 89
def ruby_files
Dir[File.join(cookbook_path, '**', '*.rb')]
end
|
#template_files ⇒ Object
97
98
99
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 97
def template_files
Dir[File.join(cookbook_path, '**', '*.erb')]
end
|
#untested_ruby_files ⇒ Object
93
94
95
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 93
def untested_ruby_files
ruby_files.reject { |file| validated?(file) }
end
|
#untested_template_files ⇒ Object
101
102
103
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 101
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
150
151
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 140
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_files ⇒ Object
113
114
115
116
117
118
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 113
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
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 127
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_templates ⇒ Object
120
121
122
123
124
125
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 120
def validate_templates
untested_template_files.each do |template|
return false unless validate_template(template)
validated(template)
end
end
|
#validated(file) ⇒ Object
109
110
111
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 109
def validated(file)
validated_files.add(checksum(file))
end
|
#validated?(file) ⇒ Boolean
105
106
107
|
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 105
def validated?(file)
validated_files.include?(checksum(file))
end
|