def output_fixture_dependency_tree(fixture: Fixture, show_scopes: bool, max_depth: Optional[int], depth: int = 0): if max_depth is not None and depth >= max_depth: return deps = [Fixture(par.default) for par in fixture.deps().values()] if not deps: return if depth == 0: yield indent(f"depends on", INDENT) for idx, dep in enumerate(deps): prefix = "├─" if idx != len(deps) - 1 else "└─" yield indent( f"{prefix} {format_fixture_header(dep, show_scopes)}", make_indent(depth + 1), ) child_lines = list( output_fixture_dependency_tree( dep, show_scopes, max_depth, depth=depth + 1, )) for child_idx, line in enumerate(child_lines): if idx < len(deps) - 1: line = line.replace(" └─", "│ └─") yield " " + line
def _resolve_single_fixture( self, fixture_fn: Callable, cache: FixtureCache ) -> Fixture: fixture = Fixture(fixture_fn) if fixture.key in cache: cached_fixture = cache[fixture.key] if fixture.scope == Scope.Global: return cached_fixture elif fixture.scope == Scope.Module: if cached_fixture.last_resolved_module_name == self.module_name: return cached_fixture elif fixture.scope == Scope.Test: if cached_fixture.last_resolved_test_id == self.id: return cached_fixture # Cache miss, so update the fixture metadata before we resolve and cache it fixture.last_resolved_test_id = self.id fixture.last_resolved_module_name = self.module_name has_deps = len(fixture.deps()) > 0 is_generator = fixture.is_generator_fixture if not has_deps: try: if is_generator: fixture.gen = fixture_fn() fixture.resolved_val = next(fixture.gen) else: fixture.resolved_val = fixture_fn() except Exception as e: raise FixtureError(f"Unable to resolve fixture '{fixture.name}'") from e cache.cache_fixture(fixture) return fixture signature = inspect.signature(fixture_fn) children_defaults = signature.bind_partial() children_defaults.apply_defaults() children_resolved = {} for name, child_fixture in children_defaults.arguments.items(): child_resolved = self._resolve_single_fixture(child_fixture, cache) children_resolved[name] = child_resolved try: if is_generator: fixture.gen = fixture_fn( **self._resolve_fixture_values(children_resolved) ) fixture.resolved_val = next(fixture.gen) else: fixture.resolved_val = fixture_fn( **self._resolve_fixture_values(children_resolved) ) except Exception as e: raise FixtureError(f"Unable to resolve fixture '{fixture.name}'") from e cache.cache_fixture(fixture) return fixture
def _resolve_single_arg(self, arg: Callable, cache: FixtureCache) -> Union[Any, Fixture]: """ Get the fixture return value If the fixture has been cached, return the value from the cache. Otherwise, call the fixture function and return the value. """ if not hasattr(arg, "ward_meta"): return arg fixture = Fixture(arg) if cache.contains(fixture, fixture.scope, self.scope_key_from(fixture.scope)): return cache.get(fixture.key, fixture.scope, self.scope_key_from(fixture.scope)) has_deps = len(fixture.deps()) > 0 is_generator = fixture.is_generator_fixture if not has_deps: try: if is_generator: fixture.gen = arg() fixture.resolved_val = next(fixture.gen) else: fixture.resolved_val = arg() except Exception as e: raise FixtureError( f"Unable to resolve fixture '{fixture.name}'") from e scope_key = self.scope_key_from(fixture.scope) cache.cache_fixture(fixture, scope_key) return fixture children_defaults = self._get_default_args(func=arg) children_resolved = {} for name, child_fixture in children_defaults.items(): child_resolved = self._resolve_single_arg(child_fixture, cache) children_resolved[name] = child_resolved try: args_to_inject = self._unpack_resolved(children_resolved) if is_generator: fixture.gen = arg(**args_to_inject) fixture.resolved_val = next(fixture.gen) else: fixture.resolved_val = arg(**args_to_inject) except Exception as e: raise FixtureError( f"Unable to resolve fixture '{fixture.name}'") from e scope_key = self.scope_key_from(fixture.scope) cache.cache_fixture(fixture, scope_key) return fixture