From 5a966a1ec8662d2e0b9035675a770c6f2c739b7d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 25 Aug 2013 23:52:24 +0000 Subject: process: allow passing env to qx This should allow us to more easily transfer user-configured environment variables to helper tools (avprobe/soxi). --- lib/dtas/process.rb | 9 +++++++-- test/test_process.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 test/test_process.rb diff --git a/lib/dtas/process.rb b/lib/dtas/process.rb index 2d7dcb0..3f1dc07 100644 --- a/lib/dtas/process.rb +++ b/lib/dtas/process.rb @@ -3,6 +3,7 @@ # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) require 'shellwords' require 'io/wait' +require_relative '../dtas' module DTAS::Process # :nodoc: PIDS = {} @@ -43,7 +44,11 @@ module DTAS::Process # :nodoc: # this is like backtick, but takes an array instead of a string # This will also raise on errors - def qx(cmd, opts = {}) + def qx(env, cmd = {}, opts = {}) + unless Hash === env + cmd, opts = env, cmd + env = {} + end r, w = IO.pipe opts = opts.merge(out: w) r.binmode @@ -53,7 +58,7 @@ module DTAS::Process # :nodoc: opts[:err] = we end pid = begin - Process.spawn(*cmd, opts) + Process.spawn(env, *cmd, opts) rescue Errno::EINTR # Ruby bug? retry end diff --git a/test/test_process.rb b/test/test_process.rb new file mode 100644 index 0000000..a480312 --- /dev/null +++ b/test/test_process.rb @@ -0,0 +1,12 @@ +# -*- encoding: binary -*- +# Copyright (C) 2013, Eric Wong +# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) +require './test/helper' +require 'dtas/process' +class TestProcess < Minitest::Unit::TestCase + include DTAS::Process + + def test_qx_env + assert_equal "WORLD\n", qx({"HELLO" => "WORLD"}, 'echo $HELLO') + end +end -- cgit v1.2.3-24-ge0c7