Merge pull request #7778 from justjake/subprocess-closable-stdin
Allow closing a Vagrant::Util::Subprocess's STDIN
This commit is contained in:
commit
a8cddf399d
|
@ -144,10 +144,11 @@ module Vagrant
|
||||||
# Record the start time for timeout purposes
|
# Record the start time for timeout purposes
|
||||||
start_time = Time.now.to_i
|
start_time = Time.now.to_i
|
||||||
|
|
||||||
|
open_readers = [stdout, stderr]
|
||||||
|
open_writers = notify_stdin ? [process.io.stdin] : []
|
||||||
@logger.debug("Selecting on IO")
|
@logger.debug("Selecting on IO")
|
||||||
while true
|
while true
|
||||||
writers = notify_stdin ? [process.io.stdin] : []
|
results = ::IO.select(open_readers, open_writers, nil, 0.1)
|
||||||
results = ::IO.select([stdout, stderr], writers, nil, 0.1)
|
|
||||||
results ||= []
|
results ||= []
|
||||||
readers = results[0]
|
readers = results[0]
|
||||||
writers = results[1]
|
writers = results[1]
|
||||||
|
@ -178,8 +179,14 @@ module Vagrant
|
||||||
break if process.exited?
|
break if process.exited?
|
||||||
|
|
||||||
# Check the writers to see if they're ready, and notify any listeners
|
# Check the writers to see if they're ready, and notify any listeners
|
||||||
if writers && !writers.empty?
|
if writers && !writers.empty? && block_given?
|
||||||
yield :stdin, process.io.stdin if block_given?
|
yield :stdin, process.io.stdin
|
||||||
|
|
||||||
|
# if the callback closed stdin, we should remove it, because
|
||||||
|
# IO.select() will throw if called with a closed io.
|
||||||
|
if process.io.stdin.closed?
|
||||||
|
open_writers = []
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
require File.expand_path("../../../base", __FILE__)
|
||||||
|
require "vagrant/util/subprocess"
|
||||||
|
|
||||||
|
describe Vagrant::Util::Subprocess do
|
||||||
|
describe '#execute' do
|
||||||
|
before do
|
||||||
|
# ensure we have `cat` and `echo` in our PATH so that we can run these
|
||||||
|
# tests successfully.
|
||||||
|
['cat', 'echo'].each do |cmd|
|
||||||
|
if !Vagrant::Util::Which.which(cmd)
|
||||||
|
pending("cannot run subprocess tests without command #{cmd.inspect}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
let (:cat) { described_class.new('cat', :notify => [:stdin]) }
|
||||||
|
|
||||||
|
it 'yields the STDIN stream for the process if we set :notify => :stdin' do
|
||||||
|
echo = described_class.new('echo', 'hello world', :notify => [:stdin])
|
||||||
|
echo.execute do |type, data|
|
||||||
|
expect(type).to eq(:stdin)
|
||||||
|
expect(data).to be_a(::IO)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'can close STDIN' do
|
||||||
|
result = cat.execute do |type, stdin|
|
||||||
|
# We should be able to close STDIN without raising an exception
|
||||||
|
stdin.close
|
||||||
|
end
|
||||||
|
|
||||||
|
# we should exit successfully.
|
||||||
|
expect(result.exit_code).to eq(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'can write to STDIN correctly' do
|
||||||
|
data = "hello world\n"
|
||||||
|
result = cat.execute do |type, stdin|
|
||||||
|
stdin.write(data)
|
||||||
|
stdin.close
|
||||||
|
end
|
||||||
|
|
||||||
|
# we should exit successfully.
|
||||||
|
expect(result.exit_code).to eq(0)
|
||||||
|
|
||||||
|
# we should see our data as the output from `cat`
|
||||||
|
expect(result.stdout).to eq(data)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue