tests: extract run_python() utility function
Proposal is to use it for new subprocess tests where module is commited to repo
This commit is contained in:
@@ -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')
|
||||
|
@@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user