From da56a08f742d5163ca80466542d73ee99f22cd77 Mon Sep 17 00:00:00 2001 From: Adam Leff Date: Wed, 12 Apr 2017 11:22:58 -0400 Subject: [PATCH] Fix gem resource on Windows RubyGems on windows comes with a batch file that wraps the `gem` command so it executes correctly. This change uses that batch file for windows for our `gem` resource, and also properly handles when we receive no output from the command. Signed-off-by: Adam Leff --- lib/resources/gem.rb | 5 ++++- test/helper.rb | 2 +- test/unit/resources/gem_test.rb | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/resources/gem.rb b/lib/resources/gem.rb index 5d7dac5c7..281f3036f 100644 --- a/lib/resources/gem.rb +++ b/lib/resources/gem.rb @@ -23,7 +23,7 @@ module Inspec::Resources 'gem' when :chef if inspec.os.windows? - 'c:\opscode\chef\embedded\bin\gem' + 'c:\opscode\chef\embedded\bin\gem.bat' else '/opt/chef/embedded/bin/gem' end @@ -32,6 +32,7 @@ module Inspec::Resources else gem_binary end + skip_resource 'Unable to retrieve gem information' if info.empty? end def info @@ -47,6 +48,8 @@ module Inspec::Resources # extract package name and version # parses data like winrm (1.3.4, 1.3.3) params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp) + return {} if params.nil? + versions = params[2].split(',') @info[:name] = params[1] @info[:version] = versions[0] diff --git a/test/helper.rb b/test/helper.rb index b1d1af813..c408e4034 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -175,7 +175,7 @@ class MockLoader 'gem list --local -a -q ^rubocop$' => cmd.call('gem-list-local-a-q-rubocop'), '/opt/ruby-2.3.1/embedded/bin/gem list --local -a -q ^pry$' => cmd.call('gem-list-local-a-q-pry'), '/opt/chef/embedded/bin/gem list --local -a -q ^chef-sugar$' => cmd.call('gem-list-local-a-q-chef-sugar'), - 'c:\opscode\chef\embedded\bin\gem list --local -a -q ^json$' => cmd.call('gem-list-local-a-q-json'), + 'c:\opscode\chef\embedded\bin\gem.bat list --local -a -q ^json$' => cmd.call('gem-list-local-a-q-json'), '/opt/opscode/embedded/bin/gem list --local -a -q ^knife-backup$' => cmd.call('gem-list-local-a-q-knife-backup'), 'npm ls -g --json bower' => cmd.call('npm-ls-g--json-bower'), 'pip show jinja2' => cmd.call('pip-show-jinja2'), diff --git a/test/unit/resources/gem_test.rb b/test/unit/resources/gem_test.rb index a41b946d0..32f5f58bf 100644 --- a/test/unit/resources/gem_test.rb +++ b/test/unit/resources/gem_test.rb @@ -56,7 +56,7 @@ describe 'Inspec::Resources::Gem' do } _(resource.installed?).must_equal true _(resource.info).must_equal pkg - _(resource.gem_binary).must_equal 'c:\opscode\chef\embedded\bin\gem' + _(resource.gem_binary).must_equal 'c:\opscode\chef\embedded\bin\gem.bat' end it 'verify gem in :chef_server' do