summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Doherty <cdoherty@getchef.com>2016-02-01 11:48:16 -0800
committerChris Doherty <cdoherty@chef.io>2016-02-03 22:11:04 -0800
commitb96cdfef5cc439fcd48e48fc6784fe1694a4796d (patch)
tree25cb59a9ff4919fdc425e36da04549c87d837d44
parenta6864e6681e2bed416376fee865f19db7ce150f9 (diff)
downloadchef-cd/choco-error-msg.tar.gz
Chocolatey provider: Raise a MissingLibrary exception if we can't find Chocolatey.cd/choco-error-msg
-rw-r--r--lib/chef/provider/package/chocolatey.rb36
-rw-r--r--spec/unit/provider/package/chocolatey_spec.rb43
2 files changed, 70 insertions, 9 deletions
diff --git a/lib/chef/provider/package/chocolatey.rb b/lib/chef/provider/package/chocolatey.rb
index 166dcc2f73..0d320a3aad 100644
--- a/lib/chef/provider/package/chocolatey.rb
+++ b/lib/chef/provider/package/chocolatey.rb
@@ -30,6 +30,15 @@ class Chef
# Declare that our arguments should be arrays
use_multipackage_api
+ PATHFINDING_POWERSHELL_COMMAND = "[System.Environment]::GetEnvironmentVariable('ChocolateyInstall', 'MACHINE')"
+ CHOCO_MISSING_MSG = <<-EOS
+Could not locate your Chocolatey install. To install chocolatey, we recommend
+the 'chocolatey' cookbook (https://github.com/chocolatey/chocolatey-cookbook).
+If Chocolatey is installed, ensure that the 'ChocolateyInstall' environment
+variable is correctly set. You can verify this with the PowerShell command
+'#{PATHFINDING_POWERSHELL_COMMAND}'.
+EOS
+
# Responsible for building the current_resource.
#
# @return [Chef::Resource::ChocolateyPackage] the current_resource
@@ -43,6 +52,13 @@ class Chef
def define_resource_requirements
super
+ requirements.assert(:all_actions) do |a|
+ # GetEnvironmentVariable returns "" on failure.
+ a.assertion { !choco_install_path.to_s.empty? }
+ a.failure_message(Chef::Exceptions::MissingLibrary, CHOCO_MISSING_MSG)
+ a.whyrun("Assuming Chocolatey is installed")
+ end
+
# Chocolatey source attribute points to an alternate feed
# and not a package specific alternate source like other providers
# so we want to assert candidates exist for the alternate source
@@ -136,14 +152,18 @@ class Chef
#
# @return [String] full path of choco.exe
def choco_exe
- @choco_exe ||=
- ::File.join(
- powershell_out!(
- "[System.Environment]::GetEnvironmentVariable('ChocolateyInstall', 'MACHINE')"
- ).stdout.chomp,
- "bin",
- "choco.exe",
- )
+ @choco_exe ||= ::File.join(
+ choco_install_path,
+ "bin",
+ "choco.exe",
+ )
+ end
+
+ # lets us mock out an incorrect value for testing.
+ def choco_install_path
+ @choco_install_path ||= powershell_out!(
+ PATHFINDING_POWERSHELL_COMMAND
+ ).stdout.chomp
end
# Helper to dispatch a choco command through shell_out using the timeout
diff --git a/spec/unit/provider/package/chocolatey_spec.rb b/spec/unit/provider/package/chocolatey_spec.rb
index bf17bfe808..d550e55445 100644
--- a/spec/unit/provider/package/chocolatey_spec.rb
+++ b/spec/unit/provider/package/chocolatey_spec.rb
@@ -30,7 +30,8 @@ describe Chef::Provider::Package::Chocolatey do
Chef::Provider::Package::Chocolatey.new(new_resource, run_context)
end
- let(:choco_exe) { 'C:\ProgramData\chocolatey\bin\choco.exe' }
+ let(:choco_install_path) { "C:\\ProgramData\\chocolatey" }
+ let(:choco_exe) { "#{choco_install_path}\\bin\\choco.exe" }
# installed packages (ConEmu is upgradable)
let(:local_list_stdout) do
@@ -41,6 +42,7 @@ ConEmu|15.10.25.0
end
before do
+ allow(provider).to receive(:choco_install_path).and_return(choco_install_path)
allow(provider).to receive(:choco_exe).and_return(choco_exe)
local_list_obj = double(:stdout => local_list_stdout)
allow(provider).to receive(:shell_out!).with("#{choco_exe} list -l -r", {:timeout => timeout}).and_return(local_list_obj)
@@ -460,4 +462,43 @@ munin-node|1.6.1.20130823
expect(new_resource).to be_updated_by_last_action
end
end
+
+ describe "#choco_exe" do
+ it "calls #choco_install_path" do
+ # un-stub #choco_exe from the before{} block.
+ allow(provider).to receive(:choco_exe).and_call_original
+ expect(provider).to receive(:choco_install_path).and_return("spork")
+ provider.instance_variable_set("@choco_exe", nil)
+
+ expect(provider.send(:choco_exe)).to match(%r{spork[/\\]bin[/\\]choco.exe})
+ end
+ end
+end
+
+describe "behavior when Chocolatey is not installed" do
+ let(:new_resource) { Chef::Resource::ChocolateyPackage.new("git") }
+
+ let(:provider) do
+ node = Chef::Node.new
+ events = Chef::EventDispatch::Dispatcher.new
+ run_context = Chef::RunContext.new(node, {}, events)
+ Chef::Provider::Package::Chocolatey.new(new_resource, run_context)
+ end
+
+ before {
+ allow(provider).to receive(:choco_install_path).and_return("")
+ provider.instance_variable_set("@choco_install_path", nil)
+
+ # we don't care what this returns, but we have to let it be called.
+ allow(provider).to receive(:shell_out!).and_return(double(:stdout => ""))
+ }
+
+ context "#define_resource_requirements" do
+ it "triggers a MissingLibrary exception when Chocolatey is not installed" do
+ provider.action = :install
+ provider.load_current_resource
+ provider.define_resource_requirements
+ expect { provider.process_resource_requirements }.to raise_error(Chef::Exceptions::MissingLibrary, /Could not locate.*install.*cookbook.*PowerShell.*GetEnvironmentVariable/m)
+ end
+ end
end