diff options
| author | Jack Nagel | 2014-07-05 13:50:54 -0500 |
|---|---|---|
| committer | Jack Nagel | 2014-07-05 13:50:54 -0500 |
| commit | 6833266bb0bfbc7628026176bd54ebd998e663d4 (patch) | |
| tree | d824cee1766f353dafb6e2ec3e552da73dbbdf0d | |
| parent | ff41c90da6581f30c3289dff22b614e5a2a6f1b4 (diff) | |
| download | homebrew-6833266bb0bfbc7628026176bd54ebd998e663d4.tar.bz2 | |
Add popen wrapper that does not invoke the shell
| -rw-r--r-- | Library/Homebrew/test/test_utils.rb | 6 | ||||
| -rw-r--r-- | Library/Homebrew/utils.rb | 1 | ||||
| -rw-r--r-- | Library/Homebrew/utils/popen.rb | 20 |
3 files changed, 27 insertions, 0 deletions
diff --git a/Library/Homebrew/test/test_utils.rb b/Library/Homebrew/test/test_utils.rb index 4a4d96076..f9dce3a8c 100644 --- a/Library/Homebrew/test/test_utils.rb +++ b/Library/Homebrew/test/test_utils.rb @@ -5,4 +5,10 @@ class UtilTests < Homebrew::TestCase # Issue #217 put columns with new results fails. assert_silent { puts_columns [] } end + + def test_popen_read + out = Utils.popen_read("/bin/sh", "-c", "echo success", &:read).chomp + assert_equal "success", out + assert_predicate $?, :success? + end end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index eae5c922e..9d39a72a5 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -3,6 +3,7 @@ require 'exceptions' require 'os/mac' require 'utils/json' require 'utils/inreplace' +require 'utils/popen' require 'open-uri' class Tty diff --git a/Library/Homebrew/utils/popen.rb b/Library/Homebrew/utils/popen.rb new file mode 100644 index 000000000..83f140163 --- /dev/null +++ b/Library/Homebrew/utils/popen.rb @@ -0,0 +1,20 @@ +module Utils + def self.popen_read(*args, &block) + popen(args, "rb", &block) + end + + def self.popen_write(*args, &block) + popen(args, "wb", &block) + end + + def self.popen(args, mode) + IO.popen("-", mode) do |pipe| + if pipe + yield pipe + else + STDERR.reopen("/dev/null", "w") + exec(*args) + end + end + end +end |
