Permalink
Browse files

Merge branch 'amoslanka-master'

  • Loading branch information...
2 parents 5d7ddef + 4b578cf commit bbfa3333a08c0ac027ee17558f716a037e1040f4 @matschaffer committed Apr 14, 2012
Showing with 84 additions and 4 deletions.
  1. +12 −3 lib/chef/knife/cook.rb
  2. +72 −1 test/cook_test.rb
@@ -2,6 +2,7 @@
require 'chef/knife'
require 'chef/config'
+require 'chef/cookbook/chefignore'
require 'knife-solo/ssh_command'
require 'knife-solo/kitchen_command'
@@ -45,12 +46,12 @@ def run
def check_syntax
ui.msg('Checking cookbook syntax...')
- Dir["**/*.rb"].each do |recipe|
+ chefignore.remove_ignores_from(Dir["**/*.rb"]).each do |recipe|
ok = system "ruby -c #{recipe} >/dev/null 2>&1"
raise "Syntax error in #{recipe}" if not ok
end
- Dir["**/*.json"].each do |json|
+ chefignore.remove_ignores_from(Dir["**/*.json"]).each do |json|
begin
require 'json'
# parse without instantiating Chef classes
@@ -68,6 +69,10 @@ def node_config
def chef_path
Chef::Config.file_cache_path
end
+
+ def chefignore
+ @chefignore ||= ::Chef::Cookbook::Chefignore.new("./")
+ end
# cygwin rsync path must be adjusted to work
def adjust_rsync_path(path)
@@ -79,8 +84,12 @@ def patch_path
Array(Chef::Config.cookbook_path).first + "/chef_solo_patches/libraries"
end
+ def rsync_exclude
+ (%w{revision-deploys tmp '.*'} + chefignore.ignores).uniq
+ end
+
def rsync_kitchen
- system! %Q{rsync -rl --rsh="ssh #{ssh_args}" --delete --exclude revision-deploys --exclude tmp --exclude '.*' ./ :#{adjust_rsync_path(chef_path)}}
+ system! %Q{rsync -rl --rsh="ssh #{ssh_args}" --delete #{rsync_exclude.collect{ |ignore| "--exclude #{ignore} " }.join} ./ :#{adjust_rsync_path(chef_path)}}
end
def add_patches
View
@@ -1,8 +1,10 @@
require 'test_helper'
require 'chef/knife'
require 'chef/knife/cook'
+require 'chef/knife/kitchen'
class CookTest < TestCase
+
def test_takes_config_as_second_arg
cmd = command("host", "nodes/myhost.json")
assert_equal "nodes/myhost.json", cmd.node_config
@@ -18,7 +20,76 @@ def test_gets_destination_path_from_chef_config
assert_equal "/tmp/chef-solo", command.chef_path
end
+ def test_check_syntax_raises_error
+ Dir.chdir("/tmp/cook_kitchen_test") do
+ assert File.exist?("syntax_error.rb")
+ assert !check_syntax('syntax_error.rb')
+ assert_raises RuntimeError do
+ command.check_syntax
+ end
+ end
+ end
+
+ def test_chefignore_is_valid_object
+ assert_instance_of Chef::Cookbook::Chefignore, command.chefignore
+ end
+
+ def test_check_syntax_ignores_files_in_chefignore
+ Dir.chdir("/tmp/cook_kitchen_test") do
+ assert File.exist?("syntax_error.rb")
+ assert !check_syntax('syntax_error.rb')
+
+ assert_raises RuntimeError do
+ command.check_syntax
+ end
+
+ File.open("chefignore", 'w') do |f|
+ f << "syntax_error.rb"
+ end
+
+ command.check_syntax
+ end
+ end
+
+ def test_rsync_exclude_sources_chefignore
+ Dir.chdir("/tmp/cook_kitchen_test") do
+ assert File.exist?("syntax_error.rb")
+ File.open("chefignore", 'w') do |f|
+ f << "syntax_error.rb"
+ end
+ assert command.rsync_exclude.include?("syntax_error.rb")
+ end
+ end
+
+ def setup
+ Dir.chdir("/tmp") do
+ kitchen("cook_kitchen_test").run
+ end
+ Dir.chdir("/tmp/cook_kitchen_test") do
+ File.open("syntax_error.rb", 'w') do |f|
+ f << "this is a blatant ruby syntax error."
+ end
+ end
+ end
+
+ def teardown
+ FileUtils.rm_r("/tmp/cook_kitchen_test")
+ end
+
def command(*args)
- Chef::Knife::Cook.new(args)
+ command = Chef::Knife::Cook.new(args)
+ command.ui.stubs(:msg)
+ command
end
+
+ def kitchen(*args)
+ Chef::Knife::Kitchen.load_deps
+ Chef::Knife::Kitchen.new(args)
+ end
+
+ def check_syntax(file)
+ `ruby -c #{file} >/dev/null 2>&1 && echo 'true'`.strip == 'true'
+ end
+
+
end

0 comments on commit bbfa333

Please sign in to comment.