Merge pull request #7778 from justjake/subprocess-closable-stdin

Allow closing a Vagrant::Util::Subprocess's STDIN
This commit is contained in:
Chris Roberts 2016-09-28 08:22:28 -07:00 committed by GitHub
commit a8cddf399d
2 changed files with 61 additions and 4 deletions

View File

@ -144,10 +144,11 @@ module Vagrant
# Record the start time for timeout purposes
start_time = Time.now.to_i
open_readers = [stdout, stderr]
open_writers = notify_stdin ? [process.io.stdin] : []
@logger.debug("Selecting on IO")
while true
writers = notify_stdin ? [process.io.stdin] : []
results = ::IO.select([stdout, stderr], writers, nil, 0.1)
results = ::IO.select(open_readers, open_writers, nil, 0.1)
results ||= []
readers = results[0]
writers = results[1]
@ -178,8 +179,14 @@ module Vagrant
break if process.exited?
# Check the writers to see if they're ready, and notify any listeners
if writers && !writers.empty?
yield :stdin, process.io.stdin if block_given?
if writers && !writers.empty? && 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

View File

@ -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