diff options
Diffstat (limited to 'spec/unit/mixin/shell_out_spec.rb')
-rw-r--r-- | spec/unit/mixin/shell_out_spec.rb | 238 |
1 files changed, 120 insertions, 118 deletions
diff --git a/spec/unit/mixin/shell_out_spec.rb b/spec/unit/mixin/shell_out_spec.rb index 66dfbd3491..df35960cc9 100644 --- a/spec/unit/mixin/shell_out_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -48,128 +48,130 @@ describe Chef::Mixin::ShellOut do ENV.update(@original_env) end + let(:retobj) { instance_double(Mixlib::ShellOut, "error!" => false) } let(:cmd) { "echo '#{rand(1000)}'" } - describe "#shell_out" do - - describe "when the last argument is a Hash" do - describe "and environment is an option" do - it "should not change environment language settings when they are set to nil" do - options = { :environment => { "LC_ALL" => nil, "LANGUAGE" => nil, "LANG" => nil, env_path => nil } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) - shell_out_obj.shell_out(cmd, options) + [ :shell_out, :shell_out_compact, :shell_out_compact_timeout, :shell_out!, :shell_out_compact!, :shell_out_compact_timeout! ].each do |method| + describe "##{method}" do + + describe "when the last argument is a Hash" do + describe "and environment is an option" do + it "should not change environment language settings when they are set to nil" do + options = { :environment => { "LC_ALL" => nil, "LANGUAGE" => nil, "LANG" => nil, env_path => nil } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end + + it "should not change environment language settings when they are set to non-nil" do + options = { :environment => { "LC_ALL" => "en_US.UTF-8", "LANGUAGE" => "en_US.UTF-8", "LANG" => "en_US.UTF-8", env_path => "foo:bar:baz" } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end + + it "should set environment language settings to the configured internal locale when they are not present" do + options = { :environment => { "HOME" => "/Users/morty" } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, { + :environment => { + "HOME" => "/Users/morty", + "LC_ALL" => Chef::Config[:internal_locale], + "LANG" => Chef::Config[:internal_locale], + "LANGUAGE" => Chef::Config[:internal_locale], + env_path => sanitized_path, + }, + }).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end + + it "should not mutate the options hash when it adds language settings" do + options = { :environment => { "HOME" => "/Users/morty" } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, { + :environment => { + "HOME" => "/Users/morty", + "LC_ALL" => Chef::Config[:internal_locale], + "LANG" => Chef::Config[:internal_locale], + "LANGUAGE" => Chef::Config[:internal_locale], + env_path => sanitized_path, + }, + }).and_return(retobj) + shell_out_obj.send(method, cmd, options) + expect(options[:environment].has_key?("LC_ALL")).to be false + end end - it "should not change environment language settings when they are set to non-nil" do - options = { :environment => { "LC_ALL" => "en_US.UTF-8", "LANGUAGE" => "en_US.UTF-8", "LANG" => "en_US.UTF-8", env_path => "foo:bar:baz" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) - shell_out_obj.shell_out(cmd, options) + describe "and env is an option" do + it "should not change env when langauge options are set to nil" do + options = { :env => { "LC_ALL" => nil, "LANG" => nil, "LANGUAGE" => nil, env_path => nil } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end + + it "should not change env when language options are set to non-nil" do + options = { :env => { "LC_ALL" => "de_DE.UTF-8", "LANG" => "de_DE.UTF-8", "LANGUAGE" => "de_DE.UTF-8", env_path => "foo:bar:baz" } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end + + it "should set environment language settings to the configured internal locale when they are not present" do + options = { :env => { "HOME" => "/Users/morty" } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, { + :env => { + "HOME" => "/Users/morty", + "LC_ALL" => Chef::Config[:internal_locale], + "LANG" => Chef::Config[:internal_locale], + "LANGUAGE" => Chef::Config[:internal_locale], + env_path => sanitized_path, + }, + }).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end + + it "should not mutate the options hash when it adds language settings" do + options = { :env => { "HOME" => "/Users/morty" } } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, { + :env => { + "HOME" => "/Users/morty", + "LC_ALL" => Chef::Config[:internal_locale], + "LANG" => Chef::Config[:internal_locale], + "LANGUAGE" => Chef::Config[:internal_locale], + env_path => sanitized_path, + }, + }).and_return(retobj) + shell_out_obj.send(method, cmd, options) + expect(options[:env].has_key?("LC_ALL")).to be false + end end - it "should set environment language settings to the configured internal locale when they are not present" do - options = { :environment => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, { - :environment => { - "HOME" => "/Users/morty", - "LC_ALL" => Chef::Config[:internal_locale], - "LANG" => Chef::Config[:internal_locale], - "LANGUAGE" => Chef::Config[:internal_locale], - env_path => sanitized_path, - }, - }).and_return(true) - shell_out_obj.shell_out(cmd, options) - end - - it "should not mutate the options hash when it adds language settings" do - options = { :environment => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, { - :environment => { - "HOME" => "/Users/morty", - "LC_ALL" => Chef::Config[:internal_locale], - "LANG" => Chef::Config[:internal_locale], - "LANGUAGE" => Chef::Config[:internal_locale], - env_path => sanitized_path, - }, - }).and_return(true) - shell_out_obj.shell_out(cmd, options) - expect(options[:environment].has_key?("LC_ALL")).to be false - end - end - - describe "and env is an option" do - it "should not change env when langauge options are set to nil" do - options = { :env => { "LC_ALL" => nil, "LANG" => nil, "LANGUAGE" => nil, env_path => nil } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) - shell_out_obj.shell_out(cmd, options) - end - - it "should not change env when language options are set to non-nil" do - options = { :env => { "LC_ALL" => "de_DE.UTF-8", "LANG" => "de_DE.UTF-8", "LANGUAGE" => "de_DE.UTF-8", env_path => "foo:bar:baz" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) - shell_out_obj.shell_out(cmd, options) - end - - it "should set environment language settings to the configured internal locale when they are not present" do - options = { :env => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, { - :env => { - "HOME" => "/Users/morty", - "LC_ALL" => Chef::Config[:internal_locale], - "LANG" => Chef::Config[:internal_locale], - "LANGUAGE" => Chef::Config[:internal_locale], - env_path => sanitized_path, - }, - }).and_return(true) - shell_out_obj.shell_out(cmd, options) - end - - it "should not mutate the options hash when it adds language settings" do - options = { :env => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, { - :env => { - "HOME" => "/Users/morty", - "LC_ALL" => Chef::Config[:internal_locale], - "LANG" => Chef::Config[:internal_locale], - "LANGUAGE" => Chef::Config[:internal_locale], - env_path => sanitized_path, - }, - }).and_return(true) - shell_out_obj.shell_out(cmd, options) - expect(options[:env].has_key?("LC_ALL")).to be false - end - end - - describe "and no env/environment option is present" do - it "should set environment language settings to the configured internal locale" do - options = { :user => "morty" } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, { - :user => "morty", - :environment => { - "LC_ALL" => Chef::Config[:internal_locale], - "LANG" => Chef::Config[:internal_locale], - "LANGUAGE" => Chef::Config[:internal_locale], - env_path => sanitized_path, - }, - }).and_return(true) - shell_out_obj.shell_out(cmd, options) + describe "and no env/environment option is present" do + it "should set environment language settings to the configured internal locale" do + options = { :user => "morty" } + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, { + :user => "morty", + :environment => { + "LC_ALL" => Chef::Config[:internal_locale], + "LANG" => Chef::Config[:internal_locale], + "LANGUAGE" => Chef::Config[:internal_locale], + env_path => sanitized_path, + }, + }).and_return(retobj) + shell_out_obj.send(method, cmd, options) + end end end end describe "when the last argument is not a Hash" do it "should set environment language settings to the configured internal locale" do - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, { + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, { :environment => { "LC_ALL" => Chef::Config[:internal_locale], "LANG" => Chef::Config[:internal_locale], "LANGUAGE" => Chef::Config[:internal_locale], env_path => sanitized_path, }, - }).and_return(true) - shell_out_obj.shell_out(cmd) + }).and_return(retobj) + shell_out_obj.send(method, cmd) end end - end describe "#shell_out_with_systems_locale" do @@ -178,19 +180,19 @@ describe Chef::Mixin::ShellOut do describe "and environment is an option" do it "should not change environment['LC_ALL'] when set to nil" do options = { :environment => { "LC_ALL" => nil } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end it "should not change environment['LC_ALL'] when set to non-nil" do options = { :environment => { "LC_ALL" => "en_US.UTF-8" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end it "should no longer set environment['LC_ALL'] to nil when 'LC_ALL' not present" do options = { :environment => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end end @@ -198,19 +200,19 @@ describe Chef::Mixin::ShellOut do describe "and env is an option" do it "should not change env when set to nil" do options = { :env => { "LC_ALL" => nil } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end it "should not change env when set to non-nil" do options = { :env => { "LC_ALL" => "en_US.UTF-8" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end it "should no longer set env['LC_ALL'] to nil when 'LC_ALL' not present" do options = { :env => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end end @@ -218,7 +220,7 @@ describe Chef::Mixin::ShellOut do describe "and no env/environment option is present" do it "should no longer add environment option and set environment['LC_ALL'] to nil" do options = { :user => "morty" } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd, options) end end @@ -226,7 +228,7 @@ describe Chef::Mixin::ShellOut do describe "when the last argument is not a Hash" do it "should no longer add environment options and set environment['LC_ALL'] to nil" do - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, {}).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd).and_return(true) shell_out_obj.shell_out_with_systems_locale(cmd) end end @@ -238,19 +240,19 @@ describe Chef::Mixin::ShellOut do describe "and environment is an option" do it "should not change environment['LC_ALL'] when set to nil" do options = { :environment => { "LC_ALL" => nil } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end it "should not change environment['LC_ALL'] when set to non-nil" do options = { :environment => { "LC_ALL" => "en_US.UTF-8" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end it "should no longer set environment['LC_ALL'] to nil when 'LC_ALL' not present" do options = { :environment => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end end @@ -258,19 +260,19 @@ describe Chef::Mixin::ShellOut do describe "and env is an option" do it "should not change env when set to nil" do options = { :env => { "LC_ALL" => nil } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end it "should not change env when set to non-nil" do options = { :env => { "LC_ALL" => "en_US.UTF-8" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end it "should no longer set env['LC_ALL'] to nil when 'LC_ALL' not present" do options = { :env => { "HOME" => "/Users/morty" } } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end end @@ -278,7 +280,7 @@ describe Chef::Mixin::ShellOut do describe "and no env/environment option is present" do it "should no longer add environment option and set environment['LC_ALL'] to nil" do options = { :user => "morty" } - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, options).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd, options).and_return(true) shell_out_obj.shell_out(cmd, **options, default_env: false) end end @@ -286,7 +288,7 @@ describe Chef::Mixin::ShellOut do describe "when the last argument is not a Hash" do it "should no longer add environment options and set environment['LC_ALL'] to nil" do - expect(shell_out_obj).to receive(:shell_out_command).with(cmd, {}).and_return(true) + expect(Chef::Mixin::ShellOut).to receive(:shell_out_command).with(cmd).and_return(true) shell_out_obj.shell_out(cmd, default_env: false) end end |