Skip to content
This repository has been archived by the owner on Mar 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #226 from tmatilai/auto-add-librarian-dir
Browse files Browse the repository at this point in the history
Automatically add Librarian's install directory to cookbook_paths
  • Loading branch information
tmatilai committed Apr 9, 2013
2 parents b13fac0 + 476a9f9 commit 02e923b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/chef/knife/solo_cook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,20 @@ def sync_kitchen
upload_to_provision_path(:encrypted_data_bag_secret, 'data_bag_key')
end

def expanded_config_paths(key)
Array(Chef::Config[key]).map { |path| Pathname.new(path).expand_path }
end

def cookbook_paths
Array(Chef::Config[:cookbook_path]) + [KnifeSolo.resource('patch_cookbooks').to_s]
@cookbook_paths ||= expanded_config_paths(:cookbook_path) + [patch_cookbooks_path]
end

def add_cookbook_path(path)
cookbook_paths.unshift(path) unless cookbook_paths.include?(path)
end

def patch_cookbooks_path
KnifeSolo.resource('patch_cookbooks')
end

def nodes_path
Expand Down Expand Up @@ -166,6 +178,7 @@ def librarian_install
ui.msg "Installing Librarian cookbooks..."
Librarian::Action::Resolve.new(librarian_env).run
Librarian::Action::Install.new(librarian_env).run
add_cookbook_path librarian_env.install_path
end
end

Expand Down Expand Up @@ -205,8 +218,7 @@ def upload_to_provision_path(src, dest, key_name = 'path')
elsif !File.exist?(src)
ui.warn "Local #{key_name} '#{src}' does not exist"
else
src << '/' if File.directory? src
upload(src, File.join(provisioning_path, dest))
upload("#{src}#{'/' if File.directory?(src)}", File.join(provisioning_path, dest))
end
end

Expand Down
54 changes: 54 additions & 0 deletions test/solo_cook_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,50 @@ def test_rsync_exclude_sources_chefignore
end
end

def test_expanded_config_paths_returns_empty_array_for_nil
Chef::Config[:foo] = nil
assert_equal [], command.expanded_config_paths(:foo)
end

def test_expanded_config_paths_returns_pathnames
Chef::Config[:foo] = ["foo"]
assert_instance_of Pathname, command.expanded_config_paths(:foo).first
end

def test_expanded_config_paths_expands_paths
Chef::Config[:foo] = ["foo", "/absolute/path"]
paths = command.expanded_config_paths(:foo)
assert_equal File.join(Dir.pwd, "foo"), paths[0].to_s
assert_equal "/absolute/path", paths[1].to_s
end

def test_patch_cookbooks_paths_exists
path = command.patch_cookbooks_path
refute_nil path, "patch_cookbooks_path should not be nil"
assert Dir.exist?(path), "patch_cookbooks_path is not a directory"
end

def test_cookbook_paths_includes_patch_cookbooks
cmd = command
assert_equal cmd.patch_cookbooks_path, cmd.cookbook_paths.last, "patch_cookbooks is not included"
end

def test_cookbook_paths_expands_paths
cmd = command
Chef::Config.cookbook_path = ["mycookbooks", "/some/other/path"]
assert_equal File.join(Dir.pwd, "mycookbooks"), cmd.cookbook_paths[0].to_s
assert_equal "/some/other/path", cmd.cookbook_paths[1].to_s
end

def test_add_cookbook_path_prepends_the_path
cmd = command
Chef::Config.cookbook_path = ["mycookbooks", "/some/other/path"]
cmd.add_cookbook_path "/new/path"
assert_equal "/new/path", cmd.cookbook_paths[0].to_s
assert_equal File.join(Dir.pwd, "mycookbooks"), cmd.cookbook_paths[1].to_s
assert_equal "/some/other/path", cmd.cookbook_paths[2].to_s
end

def test_does_not_run_librarian_if_no_cheffile
in_kitchen do
Librarian::Action::Install.any_instance.expects(:run).never
Expand Down Expand Up @@ -73,6 +117,16 @@ def test_wont_complain_if_librarian_gem_missing_but_no_cheffile
end
end

def test_adds_librarian_path_to_cookbooks
ENV['LIBRARIAN_CHEF_PATH'] = "librarian/path"
in_kitchen do
FileUtils.touch "Cheffile"
cmd = command("somehost")
cmd.run
assert_equal File.join(Dir.pwd, "librarian/path"), cmd.cookbook_paths[0].to_s
end
end

def test_validates_chef_version
in_kitchen do
cmd = command("somehost")
Expand Down

0 comments on commit 02e923b

Please sign in to comment.