From fe12a6d849f36cb1faa8d9bf0647a980b1c0fe7d Mon Sep 17 00:00:00 2001 From: Sergey Shepelev Date: Tue, 9 Jul 2013 22:16:21 +0400 Subject: [PATCH] tests: extract run_python() utility function Proposal is to use it for new subprocess tests where module is commited to repo --- tests/__init__.py | 21 +++++++++++++++++++++ tests/patcher_test.py | 19 +++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index b52d6bc..43e558a 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -6,6 +6,8 @@ try: except ImportError: resource = None import signal +import subprocess +import sys import unittest import warnings @@ -290,5 +292,24 @@ def get_database_auth(): pass return retval + +def run_python(path): + if not path.endswith('.py'): + path += '.py' + path = os.path.abspath(path) + dir_ = os.path.dirname(path) + new_env = os.environ.copy() + new_env['PYTHONPATH'] = os.pathsep.join(sys.path + [dir_]) + p = subprocess.Popen( + [sys.executable, path], + env=new_env, + stderr=subprocess.STDOUT, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + ) + output, _ = p.communicate() + return output + + certificate_file = os.path.join(os.path.dirname(__file__), 'test_server.crt') private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key') diff --git a/tests/patcher_test.py b/tests/patcher_test.py index 901c7cb..e083ae5 100644 --- a/tests/patcher_test.py +++ b/tests/patcher_test.py @@ -4,7 +4,7 @@ import subprocess import sys import tempfile -from tests import LimitedTestCase, main, skip_with_pyevent +from tests import LimitedTestCase, main, run_python, skip_with_pyevent base_module_contents = """ @@ -43,21 +43,16 @@ class ProcessBase(LimitedTestCase): shutil.rmtree(self.tempdir) def write_to_tempfile(self, name, contents): - filename = os.path.join(self.tempdir, name + '.py') - fd = open(filename, "w") + filename = os.path.join(self.tempdir, name) + if not filename.endswith('.py'): + filename = filename + '.py' + fd = open(filename, "wb") fd.write(contents) fd.close() def launch_subprocess(self, filename): - python_path = os.pathsep.join(sys.path + [self.tempdir]) - new_env = os.environ.copy() - new_env['PYTHONPATH'] = python_path - if not filename.endswith('.py'): - filename = filename + '.py' - p = subprocess.Popen( - [sys.executable, os.path.join(self.tempdir, filename)], - stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=new_env) - output, _ = p.communicate() + path = os.path.join(self.tempdir, filename) + output = run_python(path) lines = output.split("\n") return output, lines