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

Inherits:
Object
  • Object
show all
Includes:
Logging, Mixin::Checksum, Mixin::ShellOut
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.

Author:

Defined Under Namespace

Classes: PersistentSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Checksum

#checksum

Methods included from Mixin::ShellOut

#shell_out, #shell_out!

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


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_pathObject (readonly)

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_filesObject (readonly)

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_filesObject


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

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

#template_filesObject


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

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

#untested_ruby_filesObject


93
94
95
96
97
98
99
100
101
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 93

def untested_ruby_files
  ruby_files.reject do |file|
    if validated?(file)
      true
    else
      false
    end
  end
end

#untested_template_filesObject


107
108
109
110
111
112
113
114
115
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 107

def untested_template_files
  template_files.reject do |file| 
    if validated?(file)
      true
    else
      false
    end
  end
end

#validate_ruby_file(ruby_file) ⇒ Object


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

def validate_ruby_file(ruby_file)
  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]
  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_filesObject


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

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


139
140
141
142
143
144
145
146
147
148
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 139

def validate_template(erb_file)
  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]
  log.error { "Erb template #{file_relative_path} has a syntax error:" }
  result.stderr.each_line { |l| Ridley.log..fatal(l.chomp) }
  false
end

#validate_templatesObject


132
133
134
135
136
137
# File 'lib/ridley/chef/cookbook/syntax_check.rb', line 132

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

#validated(file) ⇒ Object


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

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

#validated?(file) ⇒ Boolean

Returns:

  • (Boolean)

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

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