diff --git a/docs/source/package_commands.rst b/docs/source/package_commands.rst index 7e54f2b89..85f71d056 100644 --- a/docs/source/package_commands.rst +++ b/docs/source/package_commands.rst @@ -376,6 +376,20 @@ Following is a list of the objects and functions available. if building: env.FOO_INCLUDE_PATH = "{root}/include" +.. py:attribute:: testing + :type: bool + + This boolean variable is ``True`` if a test is occurring (typically done via the :ref:`rez-test` tool), + and ``False`` otherwise. + + Typically a package will use this variable to set environment variables that are only relevant during test + execution. + + .. code-block:: python + + if testing: + env.FOO_TEST_DATA_PATH = "{root}/tests/data" + .. py:function:: command(arg: str) Run an arbitrary shell command. diff --git a/docs/source/package_definition.rst b/docs/source/package_definition.rst index b4ad4ab3d..53f83a3c6 100644 --- a/docs/source/package_definition.rst +++ b/docs/source/package_definition.rst @@ -290,6 +290,7 @@ is ``True``: * **context**: the :class:`~rez.resolved_context.ResolvedContext` instance this package belongs to; * **system**: see :attr:`system`; * **building**: see :attr:`building`; +* **testing**: see :attr:`testing`; * **request**: see :attr:`request`; * **implicits**: see :attr:`implicits`. diff --git a/src/rez/package_test.py b/src/rez/package_test.py index 3c85882d1..b00e00f98 100644 --- a/src/rez/package_test.py +++ b/src/rez/package_test.py @@ -601,6 +601,7 @@ def _get_context(self, requires, quiet=False): package_paths=self.package_paths, buf=(f if quiet else None), timestamp=self.timestamp, + testing=True, **self.context_kwargs ) diff --git a/src/rez/resolved_context.py b/src/rez/resolved_context.py index 7e1586b41..76c06ebc4 100644 --- a/src/rez/resolved_context.py +++ b/src/rez/resolved_context.py @@ -163,7 +163,7 @@ def __call__(self, state): return SolverCallbackReturn.keep_going, '' def __init__(self, package_requests, verbosity=0, timestamp=None, - building=False, caching=None, package_paths=None, + building=False, testing=False, caching=None, package_paths=None, package_filter=None, package_orderers=None, max_fails=-1, add_implicit_packages=True, time_limit=-1, callback=None, package_load_callback=None, buf=None, suppress_passive=False, @@ -176,6 +176,7 @@ def __init__(self, package_requests, verbosity=0, timestamp=None, timestamp (float): Ignore packages released after this epoch time. Packages released at exactly this time will not be ignored. building (bool): True if we're resolving for a build. + testing (bool): True if we're resolving for a test (rez-test). caching (bool): If True, cache(s) may be used to speed the resolve. If False, caches will not be used. If None, :data:`resolve_caching` is used. @@ -214,6 +215,7 @@ def __init__(self, package_requests, verbosity=0, timestamp=None, self.requested_timestamp = timestamp self.timestamp = self.requested_timestamp or int(time.time()) self.building = building + self.testing = testing self.implicit_packages = [] self.caching = config.resolve_caching if caching is None else caching self.verbosity = verbosity @@ -1553,6 +1555,7 @@ def _add(field): timestamp=self.timestamp, requested_timestamp=self.requested_timestamp, building=self.building, + testing=self.testing, caching=self.caching, implicit_packages=list(map(str, self.implicit_packages)), package_requests=list(map(str, self._package_requests)), @@ -1626,6 +1629,7 @@ def _print_version(value): r.timestamp = d["timestamp"] r.building = d["building"] + r.testing = d["testing"] r.caching = d["caching"] r.implicit_packages = [PackageRequest(x) for x in d["implicit_packages"]] r._package_requests = [PackageRequest(x) for x in d["package_requests"]] @@ -1954,6 +1958,7 @@ def _get_pre_resolve_bindings(self): self.pre_resolve_bindings = { "system": system, "building": self.building, + "testing": self.testing, "request": RequirementsBinding(self._package_requests), "implicits": RequirementsBinding(self.implicit_packages), "intersects": intersects diff --git a/src/rez/resolver.py b/src/rez/resolver.py index 921871ce6..91580b4bb 100644 --- a/src/rez/resolver.py +++ b/src/rez/resolver.py @@ -37,8 +37,8 @@ class Resolver(object): """ def __init__(self, context, package_requests, package_paths, package_filter=None, package_orderers=None, timestamp=0, callback=None, building=False, - verbosity=False, buf=None, package_load_callback=None, caching=True, - suppress_passive=False, print_stats=False): + testing=False, verbosity=False, buf=None, package_load_callback=None, + caching=True, suppress_passive=False, print_stats=False): """Create a Resolver. Args: @@ -52,6 +52,7 @@ def __init__(self, context, package_requests, package_paths, package_filter=None prior to each package being loaded. It is passed a single `Package` object. building: True if we're resolving for a build. + testing: True if we're resolving for a rez (rez-test). caching: If True, cache(s) may be used to speed the resolve. If False, caches will not be used. print_stats (bool): If true, print advanced solver stats at the end. @@ -64,6 +65,7 @@ def __init__(self, context, package_requests, package_paths, package_filter=None self.package_orderers = package_orderers self.package_load_callback = package_load_callback self.building = building + self.testing = testing self.verbosity = verbosity self.caching = caching self.buf = buf @@ -384,6 +386,7 @@ def _memcache_key(self, timestamped=False): self.package_filter_hash, self.package_orderers_hash, self.building, + self.testing, config.prune_failed_graph] if timestamped and self.timestamp: diff --git a/src/rez/serialise.py b/src/rez/serialise.py index fe82421ea..dac33a38d 100644 --- a/src/rez/serialise.py +++ b/src/rez/serialise.py @@ -184,6 +184,7 @@ def _load_file(filepath, format_, update_data_callback, original_filepath=None): # Default variables to avoid not-defined errors in early-bound attribs default_objects = { "building": False, + "testing": False, "build_variant_index": 0, "build_variant_requires": [] }