Skip to content

Commit

Permalink
Go back to W32 for the registry and add a helper with better failure …
Browse files Browse the repository at this point in the history
…messaging

Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 committed Nov 16, 2016
1 parent 14da869 commit 3bdfb63
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
22 changes: 16 additions & 6 deletions libraries/sevenzip_command_builder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Ark
class SevenZipCommandBuilder
include Chef::DSL::RegistryHelper # for the registry helper method

def unpack
sevenzip_command
end
Expand All @@ -22,6 +20,10 @@ def initialize(resource)

attr_reader :resource

def node
resource.run_context.node
end

def sevenzip_command
if resource.strip_components <= 0
return sevenzip_command_builder(resource.path, 'x')
Expand All @@ -42,10 +44,18 @@ def sevenzip_command
end

def sevenzip_binary
@tar_binary ||= resource.run_context.node['ark']['sevenzip_binary'] ||
if registry_key_exists?('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe\Path')
"\"#{registry_get_values('HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe\Path')}\\7z.exe\""
end
@tar_binary ||= (node['ark']['sevenzip_binary'] || sevenzip_path_from_registry)
end

def sevenzip_path_from_registry
begin
basepath = ::Win32::Registry::HKEY_LOCAL_MACHINE.open('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe').read_s('Path')

# users like pretty errors
rescue ::Win32::Registry::Error
raise 'Failed to find the path of 7zip binary by searching checking HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe\Path. Make sure to install 7zip before using this resource. If 7zip is installed and you still receive this message you can also specify the 7zip binary path by setting node["ark"]["sevenzip_binary"]'
end
"#{basepath}7z.exe"
end

def sevenzip_command_builder(dir, command)
Expand Down
22 changes: 11 additions & 11 deletions libraries/tar_command_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ def initialize(resource)

private

def tar_binary
@tar_binary ||= resource.run_context.node['ark']['tar'] || case node['platform_family']
when 'mac_os_x', 'freebsd'
'/usr/bin/tar'
when 'smartos'
'/bin/gtar'
else
'/bin/tar'
end
end

attr_reader :resource

def node
resource.run_context.node
end

def tar_binary
@tar_binary ||= node['ark']['tar'] || case node['platform_family']
when 'mac_os_x', 'freebsd'
'/usr/bin/tar'
when 'smartos'
'/bin/gtar'
else
'/bin/tar'
end
end

def args
case resource.extension
when /^(tar)$/ then 'xf'
Expand Down
4 changes: 3 additions & 1 deletion spec/libraries/default_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
end

describe '#cherry_pick_command' do
include_context 'seven zip installed'

context "when the node's platform_family is windows" do
it 'generates a 7-zip command' do
with_node_attributes(platform_family: 'windows')
Expand All @@ -132,7 +134,7 @@
run_context: double(node: { 'ark' => { 'tar' => 'sevenzip_command' } })
)

expect(cherry_pick_command).to eq('sevenzip_command e "/resource/release_file" -o"/resource/path" -uy -r /resource/creates')
expect(cherry_pick_command).to eq('C:\\Program Files\\7-Zip7z.exe e "/resource/release_file" -o"/resource/path" -uy -r /resource/creates')
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def recipe_name
fake_hkey_local_machine = double('fake_hkey_local_machine')
seven_zip_win32_registry = double('seven_zip_registry')
allow(seven_zip_win32_registry).to receive(:read_s).with('Path').and_return('C:\\Program Files\\7-Zip')
allow(fake_hkey_local_machine).to receive(:open).with('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe', ::Win32::Registry::KEY_READ).and_return(seven_zip_win32_registry)
allow(fake_hkey_local_machine).to receive(:open).with('SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\7zFM.exe').and_return(seven_zip_win32_registry)
fake_hkey_local_machine
end

Expand All @@ -125,7 +125,7 @@ class Registry
end
end
end
stub_const('::Win32::Registry::KEY_READ', double('win32_registry_key_read')) unless defined? ::Win32::Registry::KEY_READ
stub_const('::Win32::Registry::HKEY_LOCAL_MACHINE', fake_hkey_local_machine)
stub_const('::Win32::Registry::Error', double('win32_registry_error')) unless defined? ::Win32::Registry::Error
end
end

0 comments on commit 3bdfb63

Please sign in to comment.