diff --git a/knife-solo.gemspec b/knife-solo.gemspec index 0bad3af0..a41a4455 100644 --- a/knife-solo.gemspec +++ b/knife-solo.gemspec @@ -24,14 +24,14 @@ Gem::Specification.new do |s| s.post_install_message = KnifeSolo.post_install_message s.add_development_dependency 'bundler' - s.add_development_dependency 'rake' - s.add_development_dependency 'mocha' - s.add_development_dependency 'rdoc' s.add_development_dependency 'fog' + s.add_development_dependency 'librarian', '~> 0.0.26' s.add_development_dependency 'minitest' + s.add_development_dependency 'mocha' s.add_development_dependency 'parallel' + s.add_development_dependency 'rake' + s.add_development_dependency 'rdoc' s.add_dependency 'chef', chef_version s.add_dependency 'net-ssh', '>= 2.2.2', '< 3.0' - s.add_dependency 'librarian', '~> 0.0.20' end diff --git a/lib/chef/knife/solo_cook.rb b/lib/chef/knife/solo_cook.rb index eaa60a4a..36a5599a 100644 --- a/lib/chef/knife/solo_cook.rb +++ b/lib/chef/knife/solo_cook.rb @@ -19,8 +19,6 @@ class SoloCook < Knife deps do require 'chef/cookbook/chefignore' - require 'librarian/action' - require 'librarian/chef' require 'pathname' KnifeSolo::SshCommand.load_deps KnifeSolo::NodeConfigCommand.load_deps @@ -133,10 +131,27 @@ def time(msg) end def librarian_install - return unless File.exist? 'Cheffile' - ui.msg "Installing Librarian cookbooks..." - Librarian::Action::Resolve.new(librarian_env).run - Librarian::Action::Install.new(librarian_env).run + if !File.exist? 'Cheffile' + Chef::Log.debug "Cheffile not found" + elsif !load_librarian + ui.warn "Librarian-Chef could not be loaded" + ui.warn "Please add the librarian gem to your Gemfile or install it manually with `gem install librarian`" + else + ui.msg "Installing Librarian cookbooks..." + Librarian::Action::Resolve.new(librarian_env).run + Librarian::Action::Install.new(librarian_env).run + end + end + + def load_librarian + begin + require 'librarian/action' + require 'librarian/chef' + rescue LoadError + false + else + true + end end def librarian_env diff --git a/test/solo_cook_test.rb b/test/solo_cook_test.rb index b99b654c..9d651ac7 100644 --- a/test/solo_cook_test.rb +++ b/test/solo_cook_test.rb @@ -38,7 +38,7 @@ def test_does_not_run_librarian_if_no_cheffile def test_runs_librarian_if_cheffile_found in_kitchen do - File.open("Cheffile", 'w') {} + FileUtils.touch "Cheffile" Librarian::Action::Install.any_instance.expects(:run) command("somehost").run end @@ -46,12 +46,33 @@ def test_runs_librarian_if_cheffile_found def test_does_not_run_librarian_if_denied_by_option in_kitchen do - File.open("Cheffile", 'w') {} + FileUtils.touch "Cheffile" Librarian::Action::Install.any_instance.expects(:run).never command("somehost", "--no-librarian").run end end + def test_complains_if_librarian_gem_missing + in_kitchen do + FileUtils.touch "Cheffile" + cmd = command("somehost") + cmd.expects(:load_librarian).returns(false) + cmd.ui.expects(:err).with(regexp_matches(/librarian gem/)) + Librarian::Action::Install.any_instance.expects(:run).never + cmd.run + end + end + + def test_wont_complain_if_librarian_gem_missing_but_no_cheffile + in_kitchen do + cmd = command("somehost") + cmd.expects(:load_librarian).never + cmd.ui.expects(:err).never + Librarian::Action::Install.any_instance.expects(:run).never + cmd.run + end + end + def test_validates_chef_version in_kitchen do cmd = command("somehost")