diff options
author | Chris Doherty <cdoherty@getchef.com> | 2016-02-01 11:48:16 -0800 |
---|---|---|
committer | Chris Doherty <cdoherty@chef.io> | 2016-02-03 22:11:04 -0800 |
commit | b96cdfef5cc439fcd48e48fc6784fe1694a4796d (patch) | |
tree | 25cb59a9ff4919fdc425e36da04549c87d837d44 /lib | |
parent | a6864e6681e2bed416376fee865f19db7ce150f9 (diff) | |
download | chef-b96cdfef5cc439fcd48e48fc6784fe1694a4796d.tar.gz |
Chocolatey provider: Raise a MissingLibrary exception if we can't find Chocolatey.cd/choco-error-msg
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/provider/package/chocolatey.rb | 36 |
1 files changed, 28 insertions, 8 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 |