aboutsummaryrefslogtreecommitdiffstats
path: root/library/python/testing/yatest_common/yatest/common/runtime.py
diff options
context:
space:
mode:
authorprettyboy <prettyboy@yandex-team.com>2022-09-25 13:06:29 +0300
committerprettyboy <prettyboy@yandex-team.com>2022-09-25 13:06:29 +0300
commita56f9bf1dd89a3bf9b12b3075311c0c93e84fbb2 (patch)
tree1471279703beb18e586d8aad8d1fabe5ded1182b /library/python/testing/yatest_common/yatest/common/runtime.py
parente14c5ad0a3b4044dab6d6c9aad1d5d76360494a7 (diff)
downloadydb-a56f9bf1dd89a3bf9b12b3075311c0c93e84fbb2.tar.gz
[library/python/testing/yatest_common] Relaxed runtime methods for non-standalone runs
Diffstat (limited to 'library/python/testing/yatest_common/yatest/common/runtime.py')
-rw-r--r--library/python/testing/yatest_common/yatest/common/runtime.py94
1 files changed, 88 insertions, 6 deletions
diff --git a/library/python/testing/yatest_common/yatest/common/runtime.py b/library/python/testing/yatest_common/yatest/common/runtime.py
index b8d5964d1d..aaae9bb873 100644
--- a/library/python/testing/yatest_common/yatest/common/runtime.py
+++ b/library/python/testing/yatest_common/yatest/common/runtime.py
@@ -2,6 +2,7 @@ import errno
import functools
import json
import os
+import sys
import threading
import six
@@ -12,14 +13,20 @@ _lock = threading.Lock()
_config = None
+class NoRuntimeFormed(NotImplementedError):
+ pass
+
+
def _set_ya_config(config=None, ya=None):
global _config
if config:
_config = config
elif ya:
+
class Config:
def __init__(self):
self.ya = None
+
_config = Config()
_config.ya = ya
@@ -30,9 +37,10 @@ def _get_ya_config():
else:
try:
import pytest
+
return pytest.config
except (ImportError, AttributeError):
- raise NotImplementedError("yatest.common.* is only available from the testing runtime")
+ raise NoRuntimeFormed("yatest.common.* is only available from the testing runtime")
def _get_ya_plugin_instance():
@@ -48,6 +56,52 @@ def _norm_path(path):
return os.path.normpath(path)
+def _is_binary():
+ return getattr(sys, 'is_standalone_binary', False)
+
+
+def default_arg0(func):
+ return default_arg(func, 0)
+
+
+def default_arg1(func):
+ return default_arg(func, 1)
+
+
+def default_arg(func, narg):
+ # Always try to call func, before checking standaloneness.
+ # The context file might be provided and func might return
+ # result even if it's not a standalone binary run.
+ @functools.wraps(func)
+ def wrapper(*args, **kw):
+ try:
+ return func(*args, **kw)
+ except NoRuntimeFormed:
+ if not _is_binary():
+ if len(args) > narg:
+ return args[narg]
+ return None
+ raise
+
+ return wrapper
+
+
+def default_value(value):
+ def decorator(func):
+ @functools.wraps(func)
+ def wrapper(*args, **kw):
+ try:
+ return func(*args, **kw)
+ except NoRuntimeFormed:
+ if not _is_binary():
+ return value
+ raise
+
+ return wrapper
+
+ return decorator
+
+
def _join_path(main_path, path):
if not path:
return main_path
@@ -60,13 +114,16 @@ def not_test(func):
:param func:
:return:
"""
+
@functools.wraps(func)
def wrapper(*args, **kwds):
return func(*args, **kwds)
+
setattr(wrapper, '__test__', False)
return wrapper
+@default_arg0
def source_path(path=None):
"""
Get source path inside arcadia
@@ -76,6 +133,7 @@ def source_path(path=None):
return _join_path(_get_ya_plugin_instance().source_root, path)
+@default_arg0
def build_path(path=None):
"""
Get path inside build directory
@@ -91,6 +149,7 @@ def java_path():
:return: absolute path to java
"""
from . import runtime_java
+
return runtime_java.get_java_path(binary_path(os.path.join('contrib', 'tools', 'jdk')))
@@ -99,9 +158,12 @@ def java_home():
Get jdk directory path
"""
from . import runtime_java
+
jdk_dir = runtime_java.get_build_java_dir(binary_path('jdk'))
if not jdk_dir:
- raise Exception("Cannot find jdk - make sure 'jdk' is added to the DEPENDS section and exists for the current platform")
+ raise Exception(
+ "Cannot find jdk - make sure 'jdk' is added to the DEPENDS section and exists for the current platform"
+ )
return jdk_dir
@@ -113,6 +175,7 @@ def java_bin():
return os.path.join(java_home(), "bin", "java")
+@default_arg0
def data_path(path=None):
"""
Get path inside arcadia_tests_data directory
@@ -122,6 +185,7 @@ def data_path(path=None):
return _join_path(_get_ya_plugin_instance().data_root, path)
+@default_arg0
def output_path(path=None):
"""
Get path inside the current test suite output dir.
@@ -132,6 +196,7 @@ def output_path(path=None):
return _join_path(_get_ya_plugin_instance().output_dir, path)
+@default_arg0
def ram_drive_path(path=None):
"""
:param path: path relative to the ram drive.
@@ -143,6 +208,7 @@ def ram_drive_path(path=None):
return _join_path(get_param("ram_drive_path"), path)
+@default_arg0
def output_ram_drive_path(path=None):
"""
Returns path inside ram drive directory which will be saved in the testing_out_stuff directory after testing.
@@ -155,6 +221,7 @@ def output_ram_drive_path(path=None):
return _join_path(_get_ya_plugin_instance().get_context("test_output_ram_drive_path"), path)
+@default_arg0
def binary_path(path=None):
"""
Get path to the built binary
@@ -165,6 +232,7 @@ def binary_path(path=None):
return _get_ya_plugin_instance().get_binary(path)
+@default_arg0
def work_path(path=None):
"""
Get path inside the current test suite working directory. Creating files in the work directory does not guarantee
@@ -173,12 +241,11 @@ def work_path(path=None):
:return: absolute path inside the test suite working dir
"""
return _join_path(
- os.environ.get("TEST_WORK_PATH") or
- _get_ya_plugin_instance().get_context("work_path") or
- os.getcwd(),
- path)
+ os.environ.get("TEST_WORK_PATH") or _get_ya_plugin_instance().get_context("work_path") or os.getcwd(), path
+ )
+@default_value("python")
def python_path():
"""
Get path to the arcadia python.
@@ -192,6 +259,7 @@ def python_path():
return _get_ya_plugin_instance().python_path
+@default_value("valgrind")
def valgrind_path():
"""
Get path to valgrind
@@ -200,6 +268,7 @@ def valgrind_path():
return _get_ya_plugin_instance().valgrind_path
+@default_arg1
def get_param(key, default=None):
"""
Get arbitrary parameter passed via command line
@@ -210,6 +279,7 @@ def get_param(key, default=None):
return _get_ya_plugin_instance().get_param(key, default)
+@default_value(lambda _: {})
def get_param_dict_copy():
"""
Return copy of dictionary with all parameters. Changes to this dictionary do *not* change parameters.
@@ -240,10 +310,13 @@ def project_path(path=None):
return _join_path(os.path.join(build_path(), context.project_path), path)
+@default_value("gdb")
def gdb_path():
"""
Get path to the gdb
"""
+ if not _is_binary():
+ return "gdb"
return _get_ya_plugin_instance().gdb_path
@@ -307,34 +380,42 @@ class Context(object):
"""
@property
+ @default_value(None)
def build_type(self):
return _get_ya_plugin_instance().get_context("build_type")
@property
+ @default_value(None)
def project_path(self):
return _get_ya_plugin_instance().get_context("project_path")
@property
+ @default_value(False)
def test_stderr(self):
return _get_ya_plugin_instance().get_context("test_stderr")
@property
+ @default_value(False)
def test_debug(self):
return _get_ya_plugin_instance().get_context("test_debug")
@property
+ @default_value(None)
def test_traceback(self):
return _get_ya_plugin_instance().get_context("test_traceback")
@property
+ @default_value(None)
def test_name(self):
return _get_ya_config().current_test_name
@property
+ @default_value("test_tool")
def test_tool_path(self):
return _get_ya_plugin_instance().get_context("test_tool_path")
@property
+ @default_value(False)
def sanitize(self):
"""
Detect if current test run is under sanitizer
@@ -344,6 +425,7 @@ class Context(object):
return _get_ya_plugin_instance().get_context("sanitize")
@property
+ @default_value(lambda _: {})
def flags(self):
_flags = _get_ya_plugin_instance().get_context("flags")
if _flags: