diff options
author | Akira Kitada <akitada@gmail.com> | 2017-04-26 03:10:28 +0900 |
---|---|---|
committer | Akira Kitada <akitada@gmail.com> | 2017-04-26 04:07:45 +0900 |
commit | 7c969e1e80c2b86eb0583aebb4098e6a693ffba0 (patch) | |
tree | 8a30fa6eed708a904365d155db3f413ad16849b9 | |
parent | d531bcc83c390ecbe323b6cf3d26ea01ed60495e (diff) | |
download | ohai-7c969e1e80c2b86eb0583aebb4098e6a693ffba0.tar.gz |
Inject sane paths into shell_out
To ensure shell_out can find binaries.
Signed-off-by: Akira Kitada <akitada@gmail.com>
-rw-r--r-- | lib/ohai/mixin/command.rb | 5 | ||||
-rw-r--r-- | spec/unit/mixin/command_spec.rb | 17 |
2 files changed, 18 insertions, 4 deletions
diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb index 10c93baf..01a00edc 100644 --- a/lib/ohai/mixin/command.rb +++ b/lib/ohai/mixin/command.rb @@ -27,8 +27,13 @@ module Ohai # DISCLAIMER: Logging only works in the context of a plugin!! # accept a command and any of the mixlib-shellout options def shell_out(cmd, **options) + options = options.dup # unless specified by the caller timeout after 30 seconds options[:timeout] ||= 30 + unless RUBY_PLATFORM =~ /mswin|mingw32|windows/ + options[:env] = options.key?(:env) ? options[:env].dup : {} + options[:env]["PATH"] ||= ((ENV["PATH"] || "").split(":") + %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}).join(":") + end so = Mixlib::ShellOut.new(cmd, options) begin so.run_command diff --git a/spec/unit/mixin/command_spec.rb b/spec/unit/mixin/command_spec.rb index 7b2385d0..4fc656b1 100644 --- a/spec/unit/mixin/command_spec.rb +++ b/spec/unit/mixin/command_spec.rb @@ -26,15 +26,24 @@ describe Ohai::Mixin::Command, "shell_out" do let(:plugin_name) { :OSSparkleDream } + let(:options) { windows? ? { timeout: 30 } : { timeout: 30, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } } + before(:each) do allow(Ohai::Mixin::Command).to receive(:name).and_return(plugin_name) + @original_env = ENV.to_hash + ENV.clear + end + + after(:each) do + ENV.clear + ENV.update(@original_env) end describe "when the command runs" do it "logs the command and exitstatus" do expect(Mixlib::ShellOut). to receive(:new). - with(cmd, { timeout: 30 }). + with(cmd, options). and_return(shell_out) expect(shell_out). @@ -55,7 +64,7 @@ describe Ohai::Mixin::Command, "shell_out" do it "logs the command and error message" do expect(Mixlib::ShellOut). to receive(:new). - with(cmd, { timeout: 30 }). + with(cmd, options). and_return(shell_out) expect(shell_out). @@ -76,7 +85,7 @@ describe Ohai::Mixin::Command, "shell_out" do it "logs the command an timeout error message" do expect(Mixlib::ShellOut). to receive(:new). - with(cmd, { timeout: 30 }). + with(cmd, options). and_return(shell_out) expect(shell_out). @@ -94,7 +103,7 @@ describe Ohai::Mixin::Command, "shell_out" do end describe "when a timeout option is provided" do - let(:options) { { timeout: 10 } } + let(:options) { windows? ? { timeout: 10 } : { timeout: 10, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } } it "runs the command with the provided timeout" do expect(Mixlib::ShellOut). |