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:
|
except ImportError:
|
||||||
resource = None
|
resource = None
|
||||||
import signal
|
import signal
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
import warnings
|
import warnings
|
||||||
|
|
||||||
@@ -290,5 +292,24 @@ def get_database_auth():
|
|||||||
pass
|
pass
|
||||||
return retval
|
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')
|
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')
|
private_key_file = os.path.join(os.path.dirname(__file__), 'test_server.key')
|
||||||
|
@@ -4,7 +4,7 @@ import subprocess
|
|||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
|
||||||
from tests import LimitedTestCase, main, skip_with_pyevent
|
from tests import LimitedTestCase, main, run_python, skip_with_pyevent
|
||||||
|
|
||||||
|
|
||||||
base_module_contents = """
|
base_module_contents = """
|
||||||
@@ -43,21 +43,16 @@ class ProcessBase(LimitedTestCase):
|
|||||||
shutil.rmtree(self.tempdir)
|
shutil.rmtree(self.tempdir)
|
||||||
|
|
||||||
def write_to_tempfile(self, name, contents):
|
def write_to_tempfile(self, name, contents):
|
||||||
filename = os.path.join(self.tempdir, name + '.py')
|
filename = os.path.join(self.tempdir, name)
|
||||||
fd = open(filename, "w")
|
if not filename.endswith('.py'):
|
||||||
|
filename = filename + '.py'
|
||||||
|
fd = open(filename, "wb")
|
||||||
fd.write(contents)
|
fd.write(contents)
|
||||||
fd.close()
|
fd.close()
|
||||||
|
|
||||||
def launch_subprocess(self, filename):
|
def launch_subprocess(self, filename):
|
||||||
python_path = os.pathsep.join(sys.path + [self.tempdir])
|
path = os.path.join(self.tempdir, filename)
|
||||||
new_env = os.environ.copy()
|
output = run_python(path)
|
||||||
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()
|
|
||||||
lines = output.split("\n")
|
lines = output.split("\n")
|
||||||
return output, lines
|
return output, lines
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user