| 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 |
import pytest |
| 26 |
import os |
| 27 |
import sys |
| 28 |
|
| 29 |
try: |
| 30 |
import xdist |
| 31 |
except ImportError: |
| 32 |
@pytest.fixture(scope="session") |
| 33 |
def worker_id(): |
| 34 |
return None |
| 35 |
|
| 36 |
testdir = os.path.dirname(__file__) |
| 37 |
moddir = os.path.dirname(testdir) |
| 38 |
rootdir = os.path.dirname(moddir) |
| 39 |
sys.path.append(rootdir) |
| 40 |
|
| 41 |
def pytest_addoption(parser): |
| 42 |
parser.addoption("--tensile-options") |
| 43 |
parser.addoption("--global-parameters") |
| 44 |
parser.addoption("--prebuilt-client") |
| 45 |
parser.addoption("--no-common-build", action="store_true") |
| 46 |
parser.addoption("--builddir", "--client-dir") |
| 47 |
parser.addoption("--timing-file", default=None) |
| 48 |
|
| 49 |
@pytest.fixture(scope="session") |
| 50 |
def timing_path(pytestconfig, tmpdir_factory): |
| 51 |
userDir = pytestconfig.getoption("--timing-file") |
| 52 |
if userDir is not None: |
| 53 |
return userDir |
| 54 |
return str(tmpdir_factory.mktemp("results") / "timing.csv") |
| 55 |
|
| 56 |
@pytest.fixture(autouse=True) |
| 57 |
def incremental_timer(request, worker_lock_instance, timing_path): |
| 58 |
|
| 59 |
|
| 60 |
import datetime |
| 61 |
start = datetime.datetime.now() |
| 62 |
yield |
| 63 |
stop = datetime.datetime.now() |
| 64 |
|
| 65 |
testtime = stop - start |
| 66 |
with worker_lock_instance: |
| 67 |
with open(timing_path, "a") as f: |
| 68 |
f.write("{},{}\n".format(request.node.name, testtime.total_seconds())) |
| 69 |
|
| 70 |
@pytest.fixture(scope="session") |
| 71 |
def builddir(pytestconfig, tmpdir_factory): |
| 72 |
userDir = pytestconfig.getoption("--builddir") |
| 73 |
if userDir is not None: |
| 74 |
return userDir |
| 75 |
return str(tmpdir_factory.mktemp("0_Build")) |
| 76 |
|
| 77 |
@pytest.fixture(scope="session") |
| 78 |
def worker_lock_path(tmp_path_factory, worker_id): |
| 79 |
if not worker_id: |
| 80 |
return None |
| 81 |
|
| 82 |
return tmp_path_factory.getbasetemp().parent / "client_execution.lock" |
| 83 |
|
| 84 |
@pytest.fixture |
| 85 |
def tensile_script_path(): |
| 86 |
return os.path.join(moddir, 'bin', 'Tensile') |
| 87 |
|
| 88 |
@pytest.fixture |
| 89 |
def worker_lock_instance(worker_lock_path): |
| 90 |
if not worker_lock_path: |
| 91 |
return open(os.devnull) |
| 92 |
|
| 93 |
import filelock |
| 94 |
return filelock.FileLock(str(worker_lock_path)) |
| 95 |
|
| 96 |
@pytest.fixture |
| 97 |
def tensile_args(pytestconfig, builddir, worker_lock_path): |
| 98 |
rv = [] |
| 99 |
if worker_lock_path: |
| 100 |
rv += ["--client-lock", str(worker_lock_path)] |
| 101 |
|
| 102 |
extraOptions = pytestconfig.getoption("--tensile-options") |
| 103 |
if extraOptions is not None: |
| 104 |
rv += extraOptions.split(",") |
| 105 |
if pytestconfig.getoption("--global-parameters"): |
| 106 |
rv += ["--global-parameters", pytestconfig.getoption("--global-parameters")] |
| 107 |
if not pytestconfig.getoption("--no-common-build"): |
| 108 |
if pytestconfig.getoption("--prebuilt-client"): |
| 109 |
rv += ["--prebuilt-client", pytestconfig.getoption("--prebuilt-client")] |
| 110 |
|
| 111 |
return rv |
| 112 |
|
| 113 |
def pytest_collection_modifyitems(items): |
| 114 |
""" |
| 115 |
Mainly for tests that aren't simple YAML files (including unit tests). |
| 116 |
Adds a mark for the root directory name to each test. |
| 117 |
""" |
| 118 |
for item in items: |
| 119 |
|
| 120 |
relpath = item.fspath.relto(testdir) |
| 121 |
components = relpath.split(os.path.sep) |
| 122 |
|
| 123 |
if len(components) > 0 and len(components[0]) > 0: |
| 124 |
item.add_marker(getattr(pytest.mark, components[0])) |
| 125 |
|
| 126 |
@pytest.fixture |
| 127 |
def useGlobalParameters(tensile_args): |
| 128 |
from Tensile import Common |
| 129 |
from Tensile import Tensile |
| 130 |
import argparse |
| 131 |
|
| 132 |
class gpUpdater: |
| 133 |
def __init__(self, **params): |
| 134 |
self.additionalParams = params |
| 135 |
|
| 136 |
def __enter__(self): |
| 137 |
argParser = argparse.ArgumentParser() |
| 138 |
Tensile.addCommonArguments(argParser) |
| 139 |
args = argParser.parse_args(tensile_args) |
| 140 |
|
| 141 |
Common.restoreDefaultGlobalParameters() |
| 142 |
if args.CxxCompiler: |
| 143 |
Common.globalParameters["CxxCompiler"] = args.CxxCompiler |
| 144 |
isa = Common.detectGlobalCurrentISA(args.device) |
| 145 |
Common.assignGlobalParameters({}, isa) |
| 146 |
|
| 147 |
overrideParameters = Tensile.argUpdatedGlobalParameters(args) |
| 148 |
for key, value in overrideParameters.items(): |
| 149 |
Common.globalParameters[key] = value |
| 150 |
|
| 151 |
for key, value in self.additionalParams.items(): |
| 152 |
Common.globalParameters[key] = value |
| 153 |
|
| 154 |
return Common.globalParameters |
| 155 |
|
| 156 |
def __exit__(self, exc_type, exc_value, traceback): |
| 157 |
Common.restoreDefaultGlobalParameters() |
| 158 |
|
| 159 |
return gpUpdater |
| 160 |
|