Commit 88eb8fe0 authored by Kirill Smelkov's avatar Kirill Smelkov

golang_test: Split pyrun into -> pyrun & pyout

pyrun runs command without piping output.
pyout runs command and returns its output.
parent f812faa2
...@@ -1017,15 +1017,19 @@ def bench_defer(b): ...@@ -1017,15 +1017,19 @@ def bench_defer(b):
# ---- misc ---- # ---- misc ----
# pyrun runs `sys.executable argv... <stdin` and returns its output. # pyrun runs `sys.executable argv... <stdin`.
def pyrun(argv, stdin=None, **kw): def pyrun(argv, stdin=None, stdout=None, stderr=None, **kw):
argv = [sys.executable] + argv argv = [sys.executable] + argv
p = Popen(argv, stdin=(PIPE if stdin else None), stdout=PIPE, stderr=PIPE, **kw) p = Popen(argv, stdin=(PIPE if stdin else None), stdout=stdout, stderr=stderr, **kw)
stdout, stderr = p.communicate(stdin) stdout, stderr = p.communicate(stdin)
if p.returncode: if p.returncode:
raise RuntimeError(' '.join(argv) + '\n' + (stderr and str(stderr) or '(failed)')) raise RuntimeError(' '.join(argv) + '\n' + (stderr and str(stderr) or '(failed)'))
return stdout return stdout
# pyout runs `sys.executable argv... <stdin` and returns its output.
def pyout(argv, stdin=None, stdout=PIPE, stderr=None, **kw):
return pyrun(argv, stdin=stdin, stdout=stdout, stderr=stderr, **kw)
# panics is similar to pytest.raises and asserts that wrapped code panics with arg. # panics is similar to pytest.raises and asserts that wrapped code panics with arg.
class panics: class panics:
def __init__(self, arg): def __init__(self, arg):
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
from __future__ import print_function, absolute_import from __future__ import print_function, absolute_import
import sys, os, golang import sys, os, golang
from golang.golang_test import pyrun from golang.golang_test import pyout
from six import PY2 from six import PY2
from six.moves import builtins from six.moves import builtins
import pytest import pytest
...@@ -89,7 +89,7 @@ def test_gevent_activated(): ...@@ -89,7 +89,7 @@ def test_gevent_activated():
def test_executable(): def test_executable():
# sys.executable must point to gpython and we must be able to execute it. # sys.executable must point to gpython and we must be able to execute it.
assert 'gpython' in sys.executable assert 'gpython' in sys.executable
out = pyrun(['-c', 'import sys; print(sys.version)']) out = pyout(['-c', 'import sys; print(sys.version)'])
assert ('[GPython %s]' % golang.__version__) in str(out) assert ('[GPython %s]' % golang.__version__) in str(out)
# b converts s to UTF-8 encoded bytes. # b converts s to UTF-8 encoded bytes.
...@@ -108,19 +108,19 @@ def test_pymain(): ...@@ -108,19 +108,19 @@ def test_pymain():
testdata = join(dirname(__file__), 'testdata') testdata = join(dirname(__file__), 'testdata')
# interactive # interactive
_ = pyrun([], stdin=b'import hello\n', cwd=testdata) _ = pyout([], stdin=b'import hello\n', cwd=testdata)
assert _ == b"hello\nworld\n['']\n" assert _ == b"hello\nworld\n['']\n"
# -c # -c
_ = pyrun(['-c', 'import hello', 'abc', 'def'], cwd=testdata) _ = pyout(['-c', 'import hello', 'abc', 'def'], cwd=testdata)
assert _ == b"hello\nworld\n['-c', 'abc', 'def']\n" assert _ == b"hello\nworld\n['-c', 'abc', 'def']\n"
# -m # -m
_ = pyrun(['-m', 'hello', 'abc', 'def'], cwd=testdata) _ = pyout(['-m', 'hello', 'abc', 'def'], cwd=testdata)
# realpath rewrites e.g. `local/lib -> lib` if local/lib is symlink # realpath rewrites e.g. `local/lib -> lib` if local/lib is symlink
hellopy = realpath(join(testdata, 'hello.py')) hellopy = realpath(join(testdata, 'hello.py'))
assert _ == b"hello\nworld\n['%s', 'abc', 'def']\n" % b(hellopy) assert _ == b"hello\nworld\n['%s', 'abc', 'def']\n" % b(hellopy)
# file # file
_ = pyrun(['testdata/hello.py', 'abc', 'def'], cwd=here) _ = pyout(['testdata/hello.py', 'abc', 'def'], cwd=here)
assert _ == b"hello\nworld\n['testdata/hello.py', 'abc', 'def']\n" assert _ == b"hello\nworld\n['testdata/hello.py', 'abc', 'def']\n"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment