1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
import os
import sys
import subprocess as sp
import pytest
def pytest_configure(config):
config.addinivalue_line(
"markers", "embedded: the test create an embedded executable"
)
skip_if_win32 = pytest.mark.skipif(
"sys.platform == 'win32'", reason="skipping Posix tests on Windows"
)
skip_if_macos = pytest.mark.skipif(
"sys.platform == 'darwin'", reason="skipping test on macOS"
)
skip_if_pypy = pytest.mark.skipif(
"'__pypy__' in sys.builtin_module_names", reason="skipping test on pypy"
)
skip_if_no_proc_env = pytest.mark.skipif(
"not os.path.exists('/proc/self/environ')",
reason="'/proc/self/environ' not available",
)
skip_if_no_proc_cmdline = pytest.mark.skipif(
"not os.path.exists('/proc/%s/cmdline' % os.getpid())",
reason="'/proc/PID/cmdline' not available",
)
skip_if_no_proc_tasks = pytest.mark.skipif(
"not os.path.exists('/proc/self/task')",
reason="'/proc/self/task' not available",
)
@pytest.fixture(scope="session")
def pyrun(pyconfig):
"""
Build the pyrun executable and return its path
"""
# poor man's make
here = os.path.abspath(os.path.dirname(__file__))
ver2 = "%s.%s" % sys.version_info[:2]
source = os.path.join(here, "pyrun.c")
target = os.path.join(here, f"pyrun{ver2}")
if (
os.path.exists(target)
and os.stat(target).st_mtime > os.stat(source).st_mtime
):
return target
cmdline = ["cc"] # big punt
cmdline.extend(pyconfig("includes"))
cmdline.extend(["-o", target, source])
cmdline.extend(pyconfig("ldflags"))
cmdline.append(f"-L{pyconfig('prefix')[0]}/lib")
sp.check_call(cmdline)
return target
@pytest.fixture(scope="session")
def pyconfig():
"""Return the result of 'python-config --opt' as a list of strings"""
pyexe = os.path.realpath(sys.executable)
ver2 = "%s.%s" % sys.version_info[:2]
for name in (f"python{ver2}-config", "python3-config", "python-config"):
pyconfexe = os.path.join(os.path.dirname(pyexe), name)
if os.path.exists(pyconfexe):
break
else:
pytest.fail(
f"can't find python-config from executable {sys.executable}"
)
# Travis' Python 3.8 is not built with --embed
help = sp.check_output([pyconfexe, "--help"])
has_embed = b"--embed" in help
def pyconfig_func(opt):
cmdline = [pyconfexe, f"--{opt}"]
if has_embed:
cmdline.append("--embed")
bout = sp.check_output(cmdline)
out = bout.decode(
sys.getfilesystemencoding() # sounds like a good bet
)
return out.split()
return pyconfig_func
@pytest.fixture(scope="session")
def spt_directory():
"""
Where is the setproctitle module installed?
"""
rv = run_script(
"""
import os
import setproctitle
print(os.path.dirname(os.path.dirname(setproctitle.__file__)))
"""
)
return rv.rstrip()
@pytest.fixture(scope="function")
def tmp_pypath(monkeypatch, tmp_path):
"""
return a tmp directory which has been added to the python path
"""
monkeypatch.setenv(
"PYTHONPATH",
str(tmp_path) + os.pathsep + os.environ.get("PYTHONPATH", ""),
)
return tmp_path
def run_script(script=None, args=None, executable=None, env=None):
"""run a script in a separate process.
if the script completes successfully, return its ``stdout``,
else fail the test.
"""
if executable is None:
executable = sys.executable
cmdline = str(executable)
if args:
cmdline = cmdline + " " + args
proc = sp.Popen(
cmdline,
stdin=sp.PIPE,
stdout=sp.PIPE,
stderr=sp.PIPE,
env=env,
shell=True,
close_fds=True,
)
out, err = proc.communicate(script and script.encode())
if 0 != proc.returncode:
if out:
print(out.decode("utf8", "replace"), file=sys.stdout)
if err:
print(err.decode("utf8", "replace"), file=sys.stderr)
pytest.fail("test script failed")
# Py3 subprocess generates bytes strings.
out = out.decode()
return out
|