Skip to content

Commit b5d429b

Browse files
author
Todd DeLuca
committed
Raise exception in _stream_vagrant_command
If the subprocess returns a non-zero code, raise a CalledProcessError like _run_vagrant_command.
1 parent d6d5d70 commit b5d429b

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

tests/test_vagrant.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import sys
2424
import tempfile
2525
import time
26-
from nose.tools import eq_, ok_, with_setup
26+
from nose.tools import eq_, ok_, with_setup, assert_raises
2727

2828
import vagrant
2929
from vagrant import compat
@@ -552,6 +552,9 @@ def test_streaming_output():
552552
test_string = 'Waiting for machine to boot.'
553553
v = vagrant.Vagrant(TD)
554554

555+
with assert_raises(subprocess.CalledProcessError):
556+
v.up(vm_name='incorrect-name')
557+
555558
streaming_up = False
556559
for line in v.up(stream_output=True):
557560
print('output line:', line)

vagrant/__init__.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -993,19 +993,16 @@ def _stream_vagrant_command(self, args):
993993
sp_args = dict(args=command, cwd=self.root, env=self.env,
994994
stdout=subprocess.PIPE, stderr=err_fh, bufsize=1)
995995

996-
# Method to iterate over output lines depends on version of Python.
996+
# Iterate over output lines.
997997
# See http://stackoverflow.com/questions/2715847/python-read-streaming-input-from-subprocess-communicate#17698359
998-
if not py3: # Python 2.x
999-
p = subprocess.Popen(**sp_args)
1000-
with p.stdout:
1001-
for line in iter(p.stdout.readline, b''):
1002-
yield line
1003-
p.wait()
1004-
else: # Python 3.0+
1005-
with subprocess.Popen(**sp_args) as p:
1006-
for line in p.stdout:
1007-
yield line
1008-
998+
p = subprocess.Popen(**sp_args)
999+
with p.stdout:
1000+
for line in iter(p.stdout.readline, b''):
1001+
yield compat.decode(line) # if PY3 decode bytestrings
1002+
p.wait()
1003+
# Raise CalledProcessError for consistency with _call_vagrant_command
1004+
if p.returncode != 0:
1005+
raise subprocess.CalledProcessError(p.returncode, command)
10091006

10101007

10111008
class SandboxVagrant(Vagrant):

0 commit comments

Comments
 (0)