summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-09-09 15:26:16 -0700
committerTim Smith <tsmith84@gmail.com>2020-09-09 15:26:16 -0700
commite722fe8ca23533f83c3c6979c95bf2a160069c41 (patch)
tree68a05425d120612df21d0c26225895d0035bba31
parente2a43f613ff1830175716acd541cf3bc921183d6 (diff)
downloadmixlib-shellout-safe.tar.gz
Simplify things a bit with &.safe
Use the latest and greatest ruby-isms to cut down on some code. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/mixlib/shellout.rb2
-rw-r--r--lib/mixlib/shellout/unix.rb4
-rw-r--r--lib/mixlib/shellout/windows.rb12
-rw-r--r--lib/mixlib/shellout/windows/core_ext.rb15
-rw-r--r--spec/mixlib/shellout_spec.rb6
5 files changed, 16 insertions, 23 deletions
diff --git a/lib/mixlib/shellout.rb b/lib/mixlib/shellout.rb
index dc186a2..19be8f2 100644
--- a/lib/mixlib/shellout.rb
+++ b/lib/mixlib/shellout.rb
@@ -248,7 +248,7 @@ module Mixlib
# running or died without setting an exit status (e.g., terminated by
# `kill -9`).
def exitstatus
- @status && @status.exitstatus
+ @status&.exitstatus
end
# Run the command, writing the command's standard out and standard error
diff --git a/lib/mixlib/shellout/unix.rb b/lib/mixlib/shellout/unix.rb
index b8f42e0..5900302 100644
--- a/lib/mixlib/shellout/unix.rb
+++ b/lib/mixlib/shellout/unix.rb
@@ -370,11 +370,11 @@ module Mixlib
return if attempt_reap
@terminate_reason = "Command exceeded allowed execution time, process terminated"
- logger.error("Command exceeded allowed execution time, sending TERM") if logger
+ logger&.error("Command exceeded allowed execution time, sending TERM")
Process.kill(:TERM, child_pgid)
sleep 3
attempt_reap
- logger.error("Command exceeded allowed execution time, sending KILL") if logger
+ logger&.error("Command exceeded allowed execution time, sending KILL")
Process.kill(:KILL, child_pgid)
reap
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb
index 7ece3d1..822d8a6 100644
--- a/lib/mixlib/shellout/windows.rb
+++ b/lib/mixlib/shellout/windows.rb
@@ -89,7 +89,7 @@ module Mixlib
# Start the process
#
process, profile, token = Process.create3(create_process_args)
- logger.debug(format_process(process, app_name, command_line, timeout)) if logger
+ logger&.debug(format_process(process, app_name, command_line, timeout))
begin
# Start pushing data into input
stdin_write << input if input
@@ -124,7 +124,7 @@ module Mixlib
kill_process_tree(process.process_id, wmi, logger)
Process.kill(:KILL, process.process_id)
rescue SystemCallError
- logger.warn("Failed to kill timed out process #{process.process_id}") if logger
+ logger&.warn("Failed to kill timed out process #{process.process_id}")
end
raise Mixlib::ShellOut::CommandTimeout, [
@@ -398,20 +398,16 @@ module Mixlib
def kill_process(instance, logger)
child_pid = instance.wmi_ole_object.processid
- if logger
- logger.debug([
+ logger&.debug([
"killing child process #{child_pid}::",
"#{instance.wmi_ole_object.Name} of parent #{pid}",
].join)
- end
Process.kill(:KILL, instance.wmi_ole_object.processid)
rescue SystemCallError
- if logger
- logger.debug([
+ logger&.debug([
"Failed to kill child process #{child_pid}::",
"#{instance.wmi_ole_object.Name} of parent #{pid}",
].join)
- end
end
def format_process(process, app_name, command_line, timeout)
diff --git a/lib/mixlib/shellout/windows/core_ext.rb b/lib/mixlib/shellout/windows/core_ext.rb
index 621efb9..1c2830b 100644
--- a/lib/mixlib/shellout/windows/core_ext.rb
+++ b/lib/mixlib/shellout/windows/core_ext.rb
@@ -21,7 +21,6 @@ require "win32/process"
# Add new constants for Logon
module Process::Constants
- private
LOGON32_LOGON_INTERACTIVE = 0x00000002
LOGON32_LOGON_BATCH = 0x00000004
@@ -148,15 +147,13 @@ module Process
si_hash = {}
# If the startup_info key is present, validate its subkeys
- if hash["startup_info"]
- hash["startup_info"].each do |key, val|
- key = key.to_s.downcase
- unless valid_si_keys.include?(key)
- raise ArgumentError, "invalid startup_info key '#{key}'"
- end
-
- si_hash[key] = val
+ hash["startup_info"]&.each do |key, val|
+ key = key.to_s.downcase
+ unless valid_si_keys.include?(key)
+ raise ArgumentError, "invalid startup_info key '#{key}'"
end
+
+ si_hash[key] = val
end
# The +command_line+ key is mandatory unless the +app_name+ key
diff --git a/spec/mixlib/shellout_spec.rb b/spec/mixlib/shellout_spec.rb
index 0d444a3..9e79fa8 100644
--- a/spec/mixlib/shellout_spec.rb
+++ b/spec/mixlib/shellout_spec.rb
@@ -1095,7 +1095,7 @@ describe Mixlib::ShellOut do
end
after do
- @test_file.close if @test_file
+ @test_file&.close
end
let(:ruby_code) { "fd = File.for_fd(#{@test_file.to_i}) rescue nil; if fd; fd.seek(0); puts fd.read(5); end" }
@@ -1380,7 +1380,7 @@ describe Mixlib::ShellOut do
end
context "with subprocess writing lots of data to both stdout and stderr" do
- let(:expected_output_with) { lambda { |chr| (chr * 20_000) + (LINE_ENDING).to_s + (chr * 20_000) + (LINE_ENDING).to_s } }
+ let(:expected_output_with) { lambda { |chr| (chr * 20_000) + LINE_ENDING.to_s + (chr * 20_000) + LINE_ENDING.to_s } }
context "when writing to STDOUT first" do
let(:ruby_code) { %q{puts "f" * 20_000; STDERR.puts "u" * 20_000; puts "f" * 20_000; STDERR.puts "u" * 20_000} }
@@ -1543,7 +1543,7 @@ describe Mixlib::ShellOut do
let(:options) { { user: user } }
it "should run as specified user" do
- expect(running_user).to eql((user).to_s)
+ expect(running_user).to eql(user.to_s)
end
end
end