From 70b93f65eab70cd84d221059a2864a52202df141 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Wed, 27 Mar 2019 21:15:20 +0000 Subject: [PATCH] *cmd/*: use *system instead of exec for easier testing. This improves both test coverage, makes it easier to use a profiler and a debugger. --- Library/Homebrew/cmd/cat.rb | 2 +- Library/Homebrew/cmd/list.rb | 9 ++++----- Library/Homebrew/cmd/log.rb | 2 +- Library/Homebrew/cmd/sh.rb | 2 +- Library/Homebrew/dev-cmd/prof.rb | 2 +- Library/Homebrew/dev-cmd/ruby.rb | 5 ++++- Library/Homebrew/utils.rb | 10 ++-------- 7 files changed, 14 insertions(+), 18 deletions(-) diff --git a/Library/Homebrew/cmd/cat.rb b/Library/Homebrew/cmd/cat.rb index b7d3976555..89b5304c40 100644 --- a/Library/Homebrew/cmd/cat.rb +++ b/Library/Homebrew/cmd/cat.rb @@ -23,6 +23,6 @@ module Homebrew raise "`brew cat` doesn't support multiple arguments" if args.remaining.size > 1 cd HOMEBREW_REPOSITORY - exec "cat", formulae.first.path, *ARGV.options_only + safe_system "cat", formulae.first.path, *ARGV.options_only end end diff --git a/Library/Homebrew/cmd/list.rb b/Library/Homebrew/cmd/list.rb index bc25766dfb..a0540d25b4 100644 --- a/Library/Homebrew/cmd/list.rb +++ b/Library/Homebrew/cmd/list.rb @@ -46,8 +46,7 @@ module Homebrew def list list_args.parse - # Use of exec means we don't explicitly exit - list_unbrewed if args.unbrewed? + return list_unbrewed if args.unbrewed? # Unbrewed uses the PREFIX, which will exist # Things below use the CELLAR, which doesn't until the first formula is installed. @@ -67,10 +66,10 @@ module Homebrew puts Formatter.columns(full_names) else ENV["CLICOLOR"] = nil - exec "ls", *ARGV.options_only << HOMEBREW_CELLAR + safe_system "ls", *ARGV.options_only << HOMEBREW_CELLAR end elsif args.verbose? || !$stdout.tty? - exec "find", *ARGV.kegs.map(&:to_s) + %w[-not -type d -print] + safe_system "find", *ARGV.kegs.map(&:to_s) + %w[-not -type d -print] else ARGV.kegs.each { |keg| PrettyListing.new keg } end @@ -116,7 +115,7 @@ module Homebrew arguments.concat %w[)] cd HOMEBREW_PREFIX - exec "find", *arguments + safe_system "find", *arguments end def filtered_list diff --git a/Library/Homebrew/cmd/log.rb b/Library/Homebrew/cmd/log.rb index e1fd65b09c..8b0a526b25 100644 --- a/Library/Homebrew/cmd/log.rb +++ b/Library/Homebrew/cmd/log.rb @@ -56,6 +56,6 @@ module Homebrew end args = ARGV.options_only args += ["--follow", "--", path] unless path.nil? - exec "git", "log", *args + system "git", "log", *args end end diff --git a/Library/Homebrew/cmd/sh.rb b/Library/Homebrew/cmd/sh.rb index 90d1f6895b..724f30b19e 100644 --- a/Library/Homebrew/cmd/sh.rb +++ b/Library/Homebrew/cmd/sh.rb @@ -51,6 +51,6 @@ module Homebrew When done, type `exit'. EOS $stdout.flush - exec ENV["SHELL"] + safe_system ENV["SHELL"] end end diff --git a/Library/Homebrew/dev-cmd/prof.rb b/Library/Homebrew/dev-cmd/prof.rb index 80b8ae2f8d..ec3b206ca7 100644 --- a/Library/Homebrew/dev-cmd/prof.rb +++ b/Library/Homebrew/dev-cmd/prof.rb @@ -19,6 +19,6 @@ module Homebrew Homebrew.install_gem_setup_path! "ruby-prof" FileUtils.mkdir_p "prof" brew_rb = (HOMEBREW_LIBRARY_PATH/"brew.rb").resolved_path - exec "ruby-prof", "--printer=multi", "--file=prof", brew_rb, "--", *ARGV + safe_system "ruby-prof", "--printer=multi", "--file=prof", brew_rb, "--", *ARGV end end diff --git a/Library/Homebrew/dev-cmd/ruby.rb b/Library/Homebrew/dev-cmd/ruby.rb index f6373ac833..a1c42f4d78 100644 --- a/Library/Homebrew/dev-cmd/ruby.rb +++ b/Library/Homebrew/dev-cmd/ruby.rb @@ -21,6 +21,9 @@ module Homebrew def ruby ruby_args.parse - exec ENV["HOMEBREW_RUBY_PATH"], "-I", $LOAD_PATH.join(File::PATH_SEPARATOR), "-rglobal", "-rdev-cmd/irb", *ARGV + safe_system ENV["HOMEBREW_RUBY_PATH"], + "-I", $LOAD_PATH.join(File::PATH_SEPARATOR), + "-rglobal", "-rdev-cmd/irb", + *ARGV end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 05962a3365..d4fe1385cd 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -321,7 +321,7 @@ end def exec_editor(*args) puts "Editing #{args.join "\n"}" - with_homebrew_path { safe_exec(which_editor, *args) } + with_homebrew_path { safe_system(which_editor, *args) } end def exec_browser(*args) @@ -331,13 +331,7 @@ def exec_browser(*args) ENV["DISPLAY"] = ENV["HOMEBREW_DISPLAY"] - safe_exec(browser, *args) -end - -def safe_exec(cmd, *args) - # This buys us proper argument quoting and evaluation - # of environment variables in the cmd parameter. - exec "/bin/sh", "-c", "#{cmd} \"$@\"", "--", *args + safe_system(browser, *args) end # GZips the given paths, and returns the gzipped paths