|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# TODO: move tests one out of src to project root. |
| 3 | +# TODO: travis has numpy on their workers. Maybe add tests? |
| 4 | + |
| 5 | +"""Helpers for testing.""" |
| 6 | + |
| 7 | +import ctypes |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import sysconfig |
| 11 | +from subprocess import check_call |
| 12 | +from tempfile import mkdtemp |
| 13 | +import shutil |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from pythonnet import set_runtime |
| 18 | + |
| 19 | +# Add path for `Python.Test` |
| 20 | +cwd = os.path.dirname(__file__) |
| 21 | +fixtures_path = os.path.join(cwd, "fixtures") |
| 22 | +sys.path.append(fixtures_path) |
| 23 | + |
| 24 | +def pytest_addoption(parser): |
| 25 | + parser.addoption( |
| 26 | + "--runtime", |
| 27 | + action="store", |
| 28 | + default="default", |
| 29 | + help="Must be one of default, netcore, netfx and mono" |
| 30 | + ) |
| 31 | + |
| 32 | +def pytest_configure(config): |
| 33 | + global bin_path |
| 34 | + runtime_opt = config.getoption("runtime") |
| 35 | + |
| 36 | + test_proj_path = os.path.join(cwd, "..", "src", "testing") |
| 37 | + |
| 38 | + if runtime_opt not in ["netcore", "netfx", "mono", "default"]: |
| 39 | + raise RuntimeError(f"Invalid runtime: {runtime_opt}") |
| 40 | + |
| 41 | + bin_path = mkdtemp() |
| 42 | + |
| 43 | + # tmpdir_factory.mktemp(f"pythonnet-{runtime_opt}") |
| 44 | + |
| 45 | + fw = "net5.0" if runtime_opt == "netcore" else "netstandard2.0" |
| 46 | + |
| 47 | + check_call(["dotnet", "publish", "-f", fw, "-o", bin_path, test_proj_path]) |
| 48 | + |
| 49 | + sys.path.append(bin_path) |
| 50 | + |
| 51 | + if runtime_opt == "default": |
| 52 | + pass |
| 53 | + elif runtime_opt == "netfx": |
| 54 | + from clr_loader import get_netfx |
| 55 | + runtime = get_netfx() |
| 56 | + set_runtime(runtime) |
| 57 | + elif runtime_opt == "mono": |
| 58 | + from clr_loader import get_mono |
| 59 | + runtime = get_mono() |
| 60 | + set_runtime(runtime) |
| 61 | + elif runtime_opt == "netcore": |
| 62 | + from clr_loader import get_coreclr |
| 63 | + rt_config_path = os.path.join(bin_path, "Python.Test.runtimeconfig.json") |
| 64 | + runtime = get_coreclr(rt_config_path) |
| 65 | + set_runtime(runtime) |
| 66 | + |
| 67 | + import clr |
| 68 | + clr.AddReference("Python.Test") |
| 69 | + clr.AddReference("System") |
| 70 | + clr.AddReference("System.Collections") |
| 71 | + clr.AddReference("System.Data") |
| 72 | + clr.AddReference("System.Xml") |
| 73 | + |
| 74 | + |
| 75 | +def pytest_unconfigure(config): |
| 76 | + global bin_path |
| 77 | + shutil.rmtree(bin_path) |
| 78 | + |
| 79 | +def pytest_report_header(config): |
| 80 | + """Generate extra report headers""" |
| 81 | + # FIXME: https://github.com/pytest-dev/pytest/issues/2257 |
| 82 | + is_64bits = sys.maxsize > 2**32 |
| 83 | + arch = "x64" if is_64bits else "x86" |
| 84 | + ucs = ctypes.sizeof(ctypes.c_wchar) |
| 85 | + libdir = sysconfig.get_config_var("LIBDIR") |
| 86 | + shared = bool(sysconfig.get_config_var("Py_ENABLE_SHARED")) |
| 87 | + |
| 88 | + header = ("Arch: {arch}, UCS: {ucs}, LIBDIR: {libdir}, " |
| 89 | + "Py_ENABLE_SHARED: {shared}".format(**locals())) |
| 90 | + return header |
| 91 | + |
| 92 | + |
| 93 | +@pytest.fixture() |
| 94 | +def filepath(): |
| 95 | + """Returns full filepath for file in `fixtures` directory.""" |
| 96 | + |
| 97 | + def make_filepath(filename): |
| 98 | + # http://stackoverflow.com/questions/18011902/parameter-to-a-fixture |
| 99 | + return os.path.join(fixtures_path, filename) |
| 100 | + |
| 101 | + return make_filepath |
0 commit comments