pytest-httpchain 0.16.0__tar.gz → 0.17.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/PKG-INFO +1 -1
  2. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/pyproject.toml +3 -2
  3. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/pyproject.toml.orig +4 -2
  4. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/cli.py +5 -5
  5. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/dataflow.py +18 -13
  6. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/importers/builder.py +8 -2
  7. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/jsonc.py +41 -5
  8. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/response_steps.py +10 -3
  9. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/scoping.py +152 -7
  10. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/discovery.py +57 -7
  11. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/loader.py +8 -55
  12. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/semantic.py +34 -17
  13. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/validate.py +25 -4
  14. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/LICENSE +0 -0
  15. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/README.md +0 -0
  16. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/__init__.py +0 -0
  17. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/body_schema.py +0 -0
  18. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/carrier.py +0 -0
  19. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/constants.py +0 -0
  20. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/errors.py +0 -0
  21. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/factory.py +0 -0
  22. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/har_writer.py +0 -0
  23. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/importers/__init__.py +0 -0
  24. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/importers/curl.py +0 -0
  25. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/importers/har.py +0 -0
  26. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/__init__.py +0 -0
  27. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/equality.py +0 -0
  28. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/exceptions.py +0 -0
  29. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/loader.py +0 -0
  30. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/plumbing/__init__.py +0 -0
  31. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/plumbing/circular.py +0 -0
  32. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/plumbing/path.py +0 -0
  33. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/jsonref/plumbing/reference.py +0 -0
  34. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/models/__init__.py +0 -0
  35. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/models/entities.py +0 -0
  36. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/models/types.py +0 -0
  37. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/parallel_stats.py +0 -0
  38. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/plugin.py +0 -0
  39. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/py.typed +0 -0
  40. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/redaction.py +0 -0
  41. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/report_formatter.py +0 -0
  42. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/request_builder.py +0 -0
  43. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/schema.py +0 -0
  44. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/templates/__init__.py +0 -0
  45. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/templates/exceptions.py +0 -0
  46. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/templates/expressions.py +0 -0
  47. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/templates/functions.py +0 -0
  48. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/templates/substitution.py +0 -0
  49. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/userfunc.py +0 -0
  50. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/utils.py +0 -0
  51. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/__init__.py +0 -0
  52. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/deep.py +0 -0
  53. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/validation/diagnostics.py +0 -0
  54. {pytest_httpchain-0.16.0 → pytest_httpchain-0.17.0}/src/pytest_httpchain/warnings.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pytest-httpchain
3
- Version: 0.16.0
3
+ Version: 0.17.0
4
4
  Summary: pytest plugin for HTTP testing using JSON files
5
5
  Keywords: testing,pytest,httpx,http,api,integration-testing
6
6
  Author: Alexander Eresov
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pytest-httpchain"
3
- version = "0.16.0"
3
+ version = "0.17.0"
4
4
  description = "pytest plugin for HTTP testing using JSON files"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -173,7 +173,8 @@ layers = [
173
173
  "carrier : dataflow : schema : importers",
174
174
  "validation : har_writer : report_formatter : request_builder : response_steps : parallel_stats",
175
175
  "body_schema",
176
- "utils : scoping : redaction",
176
+ "scoping",
177
+ "utils : redaction",
177
178
  "userfunc",
178
179
  "models",
179
180
  "templates : jsonref",
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "pytest-httpchain"
3
- version = "0.16.0"
3
+ version = "0.17.0"
4
4
  description = "pytest plugin for HTTP testing using JSON files"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.13"
@@ -217,7 +217,9 @@ layers = [
217
217
  "carrier : dataflow : schema : importers",
218
218
  "validation : har_writer : report_formatter : request_builder : response_steps : parallel_stats",
219
219
  "body_schema",
220
- "utils : scoping : redaction",
220
+ # scoping reads stage marks with utils.make_marker (scoping.SkipCause).
221
+ "scoping",
222
+ "utils : redaction",
221
223
  "userfunc",
222
224
  "models",
223
225
  "templates : jsonref",
@@ -38,7 +38,7 @@ class GraphDirection(StrEnum):
38
38
 
39
39
 
40
40
  RefParentTraversalDepth = Annotated[int, typer.Option(help="Maximum $ref parent directory traversal depth.")]
41
- RootPath = Annotated[Path | None, typer.Option("--root-path", help="Directory that constrains $ref resolution (default: auto-detected project root).")]
41
+ RootPath = Annotated[Path | None, typer.Option("--root-path", help="Directory that constrains $ref resolution (default: the rootdir pytest would use).")]
42
42
  OutputFormatOption = Annotated[OutputFormat, typer.Option("--format", help="Output format: human-readable text or machine-readable JSON.")]
43
43
 
44
44
 
@@ -216,7 +216,7 @@ def _load_for_inspection(path: Path, depth: int, root_path: Path | None = None)
216
216
 
217
217
  def _render_show_text(path: Path, scenario: Scenario, flow: DataFlow) -> list[str]:
218
218
  # Listed nearest first: a name has several producers when the nearer ones
219
- # have a skip_if (analyze_dataflow), each read only when the nearer skipped.
219
+ # may skip (analyze_dataflow), each read only when the nearer skipped.
220
220
  producers_of: dict[tuple[int, str], list[int]] = {}
221
221
  for edge in flow.edges:
222
222
  for var_name in edge.vars:
@@ -285,9 +285,9 @@ def _to_mermaid(flow: DataFlow, direction: str = "TD") -> str:
285
285
  label = f"{s.index + 1} · {s.name}" if s.name else f"{s.index + 1}"
286
286
  lines.append(f' S{s.index}["{_mermaid_label(label)}"]')
287
287
  for edge in flow.edges:
288
- # Dotted from a stage that may skip (skip_if): the values may come from
289
- # an earlier producer instead (its own edge), or never.
290
- arrow = "-->" if flow.stages[edge.producer].skip_if is False else "-.->"
288
+ # Dotted from a stage that may skip (skip_if, or a skip or xfail mark):
289
+ # the values may come from an earlier producer instead (its own edge), or never.
290
+ arrow = "-.->" if flow.stages[edge.producer].may_skip else "-->"
291
291
  lines.append(f" S{edge.producer} {arrow}|{', '.join(edge.vars)}| S{edge.consumer}")
292
292
  return "\n".join(lines)
293
293
 
@@ -31,8 +31,8 @@ def _consumed(refs: set[str], earlier_saves: frozenset[str], shadows: frozenset[
31
31
  class DataFlowEdge(BaseModel):
32
32
  """A data dependency: ``vars`` saved by stage ``producer`` are referenced by
33
33
  stage ``consumer``. A name has more than one producer when its last writer
34
- before the consumer has a ``skip_if``: one edge from each writer back to the
35
- nearest that never skips (`analyze_dataflow`)."""
34
+ before the consumer may skip: one edge from each writer back to the nearest
35
+ that never skips (`analyze_dataflow`)."""
36
36
 
37
37
  producer: int
38
38
  consumer: int
@@ -40,9 +40,12 @@ class DataFlowEdge(BaseModel):
40
40
 
41
41
 
42
42
  class StageFlow(BaseModel):
43
- """Per-stage data-flow summary. ``skip_if`` is the stage's as declared:
44
- unless it is ``false``, the stage may skip, and a later stage then reads
45
- the value an earlier stage saved under the same name, or finds none."""
43
+ """Per-stage data-flow summary. ``skip_if`` is the stage's as declared.
44
+ ``may_skip`` says whether the stage may end without its saves while the
45
+ chain goes on: a ``skip_if`` other than ``false``, or a ``skip``,
46
+ ``skipif`` or ``xfail`` mark of its own (`scoping.SkipCause`). A later
47
+ stage then reads the value an earlier stage saved under the same name, or
48
+ finds none."""
46
49
 
47
50
  index: int
48
51
  name: str
@@ -51,6 +54,7 @@ class StageFlow(BaseModel):
51
54
  fixtures: list[str]
52
55
  marks: list[str]
53
56
  skip_if: bool | str
57
+ may_skip: bool
54
58
  saves: list[str]
55
59
  consumes: list[str]
56
60
 
@@ -67,13 +71,13 @@ class DataFlow(BaseModel):
67
71
  def _producers(writers: list[int], stages: list[StageFlow]) -> list[int]:
68
72
  """The stages a consumer may read a name from, given its earlier writers in
69
73
  stage order: the last writer, as a later save shadows an earlier one — and
70
- while that writer has a ``skip_if``, the writer before it too, since a
71
- skipped stage leaves the chain running on the earlier value. The walk stops
72
- at the nearest writer that never skips; nearest first."""
74
+ while that writer may skip, the writer before it too, since a skipped
75
+ stage leaves the chain running on the earlier value. The walk stops at the
76
+ nearest writer that never skips; nearest first."""
73
77
  producers: list[int] = []
74
78
  for index in reversed(writers):
75
79
  producers.append(index)
76
- if stages[index].skip_if is False:
80
+ if not stages[index].may_skip:
77
81
  break
78
82
  return producers
79
83
 
@@ -89,10 +93,10 @@ def analyze_dataflow(scenario: Scenario, test_data: dict[str, Any]) -> DataFlow:
89
93
  never saved values.
90
94
 
91
95
  A consumed name comes from its last writer before the consumer. When that
92
- writer has a ``skip_if`` it may have skipped with the chain going on, and
93
- the consumer then reads the writer before it: so the edges run from each
94
- writer back to, and including, the nearest one that never skips — ``graph``
95
- draws those from a stage with a ``skip_if`` dotted.
96
+ writer may skip (a ``skip_if``, or a skip or xfail mark) it may have skipped
97
+ with the chain going on, and the consumer then reads the writer before it:
98
+ so the edges run from each writer back to, and including, the nearest one
99
+ that never skips — ``graph`` draws those from a stage that may skip dotted.
96
100
  """
97
101
  raws = raw_stages(test_data)
98
102
  scopes = stage_scopes(scenario)
@@ -153,6 +157,7 @@ def analyze_dataflow(scenario: Scenario, test_data: dict[str, Any]) -> DataFlow:
153
157
  fixtures=sorted(stage.fixtures),
154
158
  marks=list(stage.marks),
155
159
  skip_if=stage.skip_if,
160
+ may_skip=scope.skip_cause is not None,
156
161
  saves=sorted(scope.saves),
157
162
  consumes=sorted(consumes),
158
163
  )
@@ -638,7 +638,7 @@ class _Builder:
638
638
  """``{"fields": ..., "files": ...}``: a text field without a type or
639
639
  filename is a field, anything else a file object (a path alone
640
640
  where that says all). A secret's text, a field's or a file object's
641
- ``content`` (a field sent with a type is one), is a placeholder,
641
+ ``content`` or ``base64`` (a field sent with a type is one), is a placeholder,
642
642
  which fails the stage unset (a field or a file's only source is no
643
643
  null). A part named as a reference key cannot be written, and is
644
644
  left out with a note."""
@@ -656,7 +656,13 @@ class _Builder:
656
656
  self.files.append(part.path)
657
657
  spec["path"] = escape(part.path)
658
658
  elif part.base64 is not None:
659
- spec["base64"] = part.base64
659
+ # Bytes that are no text (a password sent in another
660
+ # encoding) are as secret as text: the placeholder holds
661
+ # them base64-encoded, as recorded.
662
+ if self.is_secret_field(part.name, part.base64):
663
+ spec["base64"] = self.secret(part.name, part.base64, f"multipart part {part.name!r}, base64-encoded", stage)
664
+ else:
665
+ spec["base64"] = part.base64
660
666
  elif self.is_secret_field(part.name, content := part.value if part.value is not None else part.content or ""):
661
667
  spec["content"] = self.secret(part.name, content, f"multipart part {part.name!r}", stage)
662
668
  else:
@@ -11,12 +11,13 @@ Strictly valid JSON is left as it is, so every reader goes through here with no
11
11
  opt-in: `loads_jsonc` is what the reference resolver parses a file with and what
12
12
  a body schema file is read with (``utils.read_json_schema_file``), so they agree
13
13
  on what a file may hold. What arrives over HTTP (a response body) is never
14
- passed through here: it stays strict JSON.
14
+ passed through here: it stays plain JSON, as ``json.loads`` reads it.
15
15
  """
16
16
 
17
17
  import json
18
+ import math
18
19
  import re
19
- from typing import Any
20
+ from typing import Any, NoReturn
20
21
 
21
22
  # JSON's whitespace, and nothing else: Python's \s would take a no-break space
22
23
  # the JSON parser refuses as whitespace.
@@ -112,8 +113,43 @@ def strip_jsonc(text: str) -> str:
112
113
  return "".join(parts)
113
114
 
114
115
 
116
+ # A string, whole, or a word ``json.loads`` reads as a number JSON cannot
117
+ # write: ``NaN`` or ``Infinity``, or a number too large for a float, which it
118
+ # reads as infinity. Words are found only outside strings, and the first one
119
+ # spelled as the parser handed it over is where the parser met it.
120
+ _WORD = re.compile(r'"[^"\\]*+(?:\\.[^"\\]*+)*+"|(?P<word>-?Infinity|NaN|-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)')
121
+
122
+
123
+ class _NotJSON(Exception):
124
+ """A word the parser read that is no JSON number, raised out of it with
125
+ why."""
126
+
127
+
128
+ def _refuse_constant(word: str) -> NoReturn:
129
+ raise _NotJSON(word, f"{word} is not valid JSON")
130
+
131
+
132
+ def _finite(word: str) -> float:
133
+ # 1e400 is JSON, but a float past the largest is infinity, which is not.
134
+ if math.isinf(value := float(word)):
135
+ raise _NotJSON(word, f"{word} is too large a number (it reads as infinity)")
136
+ return value
137
+
138
+
115
139
  def loads_jsonc(text: str, **kwargs: Any) -> Any:
116
140
  """``json.loads`` of JSONC text (`strip_jsonc`), with ``json.loads``' keyword
117
- arguments. Raises `json.JSONDecodeError` as ``json.loads`` does, for an
118
- unterminated comment too."""
119
- return json.loads(strip_jsonc(text), **kwargs)
141
+ arguments but ``parse_constant`` and ``parse_float``, which it sets.
142
+ Raises `json.JSONDecodeError` as ``json.loads`` does, for an
143
+ unterminated comment too, and for ``NaN``, ``Infinity`` and
144
+ ``-Infinity``, which ``json.loads`` reads as numbers though they are no
145
+ JSON, and a number too large for a float (``1e400``), which it reads as
146
+ infinity: a file holding one is not a JSON file, or holds a number no
147
+ JSON can write, and ``resolve``, which prints strict JSON, would print
148
+ ``Infinity``."""
149
+ stripped = strip_jsonc(text)
150
+ try:
151
+ return json.loads(stripped, parse_constant=_refuse_constant, parse_float=_finite, **kwargs)
152
+ except _NotJSON as e:
153
+ word, message = e.args
154
+ position = next(match.start("word") for match in _WORD.finditer(stripped) if match["word"] == word)
155
+ raise json.JSONDecodeError(message, stripped, position) from None
@@ -58,8 +58,12 @@ def process_save(save_model: Save, response: httpx.Response, context: ChainMap[s
58
58
  for var_name, jmespath_expr in save_model.jmespath.items():
59
59
  try:
60
60
  step_saved[var_name] = jmespath.search(jmespath_expr, response_json)
61
- except jmespath.exceptions.JMESPathError as e:
62
- raise SaveError(f"Error saving variable {var_name}: {e}") from e
61
+ except (ValueError, ArithmeticError, TypeError) as e:
62
+ # What a verify.jmespath entry fails on (`_verify_jmespath`):
63
+ # jmespath's own errors, and Python's from what it hands
64
+ # its functions unchecked (`contains(s, n)`, `ceil()` of
65
+ # inf). Another response may hold what it needs: retryable.
66
+ raise SaveError(f"Error saving variable {var_name}: {_evaluation_error(e)}") from e
63
67
 
64
68
  case RegexSave():
65
69
  for var_name, entry in save_model.regex.items():
@@ -315,7 +319,10 @@ def process_verify(
315
319
  if expression is _UNRENDERED:
316
320
  continue
317
321
  if not isinstance(expression, bool):
318
- failures.add(f"Verify expression {i} must evaluate to bool, got {type(expression).__name__} ({expression!r}), a value written where a condition belongs")
322
+ # The type alone, never the value, as for skip_if: `{{
323
+ # response.headers['Set-Cookie'] }}` renders a credential, which
324
+ # the report redacts in the headers but this line would print.
325
+ failures.add(f"Verify expression {i} must evaluate to bool, got {type(expression).__name__}, a value written where a condition belongs")
319
326
  elif not expression:
320
327
  failures.add(f"Expression {i} failed: evaluated to {expression}")
321
328
 
@@ -32,9 +32,11 @@ time against scenario substitutions only, which is why `StageScopes` exposes
32
32
 
33
33
  An earlier stage's saves are in scope because it passed: one that failed
34
34
  aborted the chain, so only an ``always_run`` stage runs without them. One
35
- that skipped (``skip_if``) leaves the chain healthy, so every later stage runs
36
- without them; `StageScopes.skippable_saves` holds the names only such stages
37
- save, and `StageScopes.when_skipped` the scope without them.
35
+ that skipped leaves the chain healthy, so every later stage runs without them:
36
+ a ``skip_if``, or a ``skip``, ``skipif`` or ``xfail`` mark of the stage's own
37
+ (a `SkipCause`; an expected failure is reported skipped too).
38
+ `StageScopes.skippable_saves` holds the names only such stages save, and
39
+ `StageScopes.when_skipped` the scope without them.
38
40
 
39
41
  The template built-ins (``now()``, ``len()``, ``true``, ...) are in scope in
40
42
  every phase, beneath the user's names: a user name shadows a built-in of the
@@ -43,12 +45,15 @@ built-in (see `extract_template_variables` and `extract_builtin_stand_ins`).
43
45
  """
44
46
 
45
47
  import ast
48
+ import warnings
46
49
  from collections import ChainMap
47
50
  from collections.abc import Iterable, Iterator, Mapping
48
51
  from collections.abc import Set as AbstractSet
49
52
  from dataclasses import dataclass, replace
50
53
  from typing import Any, NamedTuple
51
54
 
55
+ import pytest
56
+
52
57
  from pytest_httpchain.models import (
53
58
  CombinationsParameter,
54
59
  FunctionsSubstitution,
@@ -68,6 +73,7 @@ from pytest_httpchain.models import (
68
73
  normalize_list_input,
69
74
  )
70
75
  from pytest_httpchain.templates import CALL_ONLY_BUILTINS, CONTEXT_HELPERS, TEMPLATE_BUILTINS, TemplatesError, find_templates, parse_expression, template_form
76
+ from pytest_httpchain.utils import make_marker
71
77
 
72
78
  # The name under which response metadata is injected into response-step contexts.
73
79
  RESPONSE_META_NAME = "response"
@@ -567,6 +573,137 @@ def response_step_templates(stage: Stage, raw_response: Any) -> Iterator[tuple[A
567
573
  opaque_save_seen = True
568
574
 
569
575
 
576
+ # --------------------------------------------------------------------------- #
577
+ # Skip causes: which stages may end without their saves while the chain goes on.
578
+ # --------------------------------------------------------------------------- #
579
+
580
+
581
+ @dataclass(frozen=True, slots=True)
582
+ class SkipCause:
583
+ """Why a stage may end without committing its saves while the stages after
584
+ it still run: its ``skip_if``, or a ``skip``, ``skipif`` or ``xfail`` mark of
585
+ its own. pytest reports all of them skipped (an expected failure is a skip
586
+ carrying ``wasxfail``), which `plugin.pytest_runtest_makereport` leaves
587
+ chain-healthy."""
588
+
589
+ mark: str | None
590
+ """The mark as written; ``None`` for the stage's ``skip_if``."""
591
+ fails_as_expected: bool = False
592
+ """An ``xfail`` that runs the stage: its saves are lost when it fails as
593
+ expected, where the others' are lost when it skips."""
594
+ never_called: bool = False
595
+ """pytest never calls the stage: a skip at setup, or an ``xfail(run=False)``
596
+ (unless pytest runs with ``--runxfail``, which ignores xfail marks)."""
597
+
598
+
599
+ # The marks pytest's skipping plugin turns into an outcome.
600
+ _OUTCOME_MARKS = ("skip", "skipif", "xfail")
601
+
602
+
603
+ def _outcome_marks(marks: Iterable[str]) -> list[tuple[str, pytest.Mark]]:
604
+ """The ``skip``, ``skipif`` and ``xfail`` marks among ``marks``, in order,
605
+ each with the mark as written. A mark that does not parse is HTTPCHAIN019's
606
+ to report, and left out."""
607
+ parsed: list[tuple[str, pytest.Mark]] = []
608
+ for mark_str in marks:
609
+ try:
610
+ # Constructing an unregistered mark warns; only the parse matters here.
611
+ with warnings.catch_warnings():
612
+ warnings.simplefilter("ignore")
613
+ mark = make_marker(mark_str).mark
614
+ except Exception:
615
+ continue
616
+ if mark.name in _OUTCOME_MARKS:
617
+ parsed.append((mark_str, mark))
618
+ return parsed
619
+
620
+
621
+ def _conditions_apply(mark: pytest.Mark) -> bool | None:
622
+ """Whether a ``skipif``/``xfail`` mark applies, read as pytest's skipping
623
+ plugin reads it: with no condition it always does, otherwise when a
624
+ condition is true, tried in order. ``None`` is pytest's to decide at run
625
+ time: a string condition is an expression it evaluates, unless the string
626
+ is a literal. A non-string condition needs ``reason=``: without it pytest
627
+ fails the stage, true or not, and a failure aborts the chain, so from there
628
+ on the mark never skips it."""
629
+ conditions = (mark.kwargs["condition"],) if "condition" in mark.kwargs else mark.args
630
+ if not conditions:
631
+ return True
632
+ verdict: bool | None = False
633
+ for condition in conditions:
634
+ if isinstance(condition, str):
635
+ try:
636
+ condition = ast.literal_eval(condition)
637
+ except Exception:
638
+ # Broad on purpose: whatever literal_eval rejects is an
639
+ # expression only pytest can evaluate.
640
+ verdict = None
641
+ continue
642
+ elif mark.kwargs.get("reason") is None:
643
+ return verdict
644
+ if condition:
645
+ return True
646
+ return verdict
647
+
648
+
649
+ def marks_skip_cause(marks: Iterable[str]) -> SkipCause | None:
650
+ """The skip cause the marks on a stage give it, if any, read in the order
651
+ pytest's skipping plugin reads them.
652
+
653
+ At setup every ``skipif`` and ``skip`` is looked at before any ``xfail``:
654
+ one that certainly applies means pytest never calls the stage. Of the
655
+ ``xfail`` marks, the first that applies decides: with ``run=False`` pytest
656
+ never calls the stage, otherwise it runs, and an expected failure discards
657
+ its saves. One with ``raises=`` never fails as expected: a mark can give it
658
+ only a literal, never an exception type, so pytest reports every failure as
659
+ a real one. One pytest never calls is named first, then a ``skip``/``skipif``
660
+ that may apply, then the first ``xfail`` that may end the stage.
661
+ """
662
+ outcome_marks = _outcome_marks(marks)
663
+ may_skip: list[str] = []
664
+ for mark_str, mark in outcome_marks:
665
+ if mark.name == "xfail":
666
+ continue
667
+ applies = True if mark.name == "skip" else _conditions_apply(mark)
668
+ if applies:
669
+ return SkipCause(mark=mark_str, never_called=True)
670
+ if applies is None:
671
+ may_skip.append(mark_str)
672
+ xfail: SkipCause | None = None
673
+ for mark_str, mark in outcome_marks:
674
+ if mark.name != "xfail":
675
+ continue
676
+ applies = _conditions_apply(mark)
677
+ if applies is False:
678
+ continue
679
+ runs = bool(mark.kwargs.get("run", True))
680
+ if xfail is None and (not runs or mark.kwargs.get("raises") is None):
681
+ xfail = SkipCause(mark=mark_str, fails_as_expected=runs, never_called=applies is True and not runs)
682
+ if applies:
683
+ break # this one decides; pytest looks at none after it
684
+ if xfail is not None and xfail.never_called:
685
+ return xfail
686
+ return SkipCause(mark=may_skip[0]) if may_skip else xfail
687
+
688
+
689
+ def _stage_skip_cause(stage: Stage) -> SkipCause | None:
690
+ """Why ``stage`` may end without its saves, or ``None`` when it never does:
691
+ a mark that keeps pytest from calling it, else its ``skip_if``, else a mark
692
+ that may skip it (`marks_skip_cause`).
693
+
694
+ Scenario-level ``marks`` are not read. They apply to every stage, the one
695
+ that reads a save as much as the one that saves it: a scenario-wide
696
+ ``skipif`` skips both or neither, and under a scenario-wide ``xfail`` the
697
+ reader's failure is an expected one too.
698
+ """
699
+ cause = marks_skip_cause(stage.marks)
700
+ if cause is not None and cause.never_called:
701
+ return cause
702
+ if stage.skip_if is not False:
703
+ return SkipCause(mark=None)
704
+ return cause
705
+
706
+
570
707
  # --------------------------------------------------------------------------- #
571
708
  # Static scope model: per-stage, per-phase name availability.
572
709
  # --------------------------------------------------------------------------- #
@@ -635,12 +772,17 @@ class StageScopes:
635
772
  saves: frozenset[str]
636
773
  earlier_saves: frozenset[str]
637
774
  skippable_saves: frozenset[str]
638
- """The ``earlier_saves`` that only stages with a ``skip_if`` save: none
775
+ """The ``earlier_saves`` that only stages with a `SkipCause` save: none
639
776
  is there when those stages skipped."""
777
+ skip_cause: SkipCause | None
778
+ """This stage's own, if it may skip."""
779
+ never_called: bool
780
+ """pytest never calls this stage, by its own marks or the scenario's, which
781
+ apply to every stage (`marks_skip_cause`): it reads nothing."""
640
782
 
641
783
  @property
642
784
  def when_skipped(self) -> "StageScopes":
643
- """This scope as it is when every earlier stage with a ``skip_if``
785
+ """This scope as it is when every earlier stage with a `SkipCause`
644
786
  skipped: without `skippable_saves`. A name in a phase's scope here but
645
787
  not in the same phase of ``when_skipped`` is defined only when such a
646
788
  stage ran."""
@@ -703,7 +845,7 @@ class StageScopes:
703
845
  def stage_scopes(scenario: Scenario) -> list[StageScopes]:
704
846
  """Per-stage scopes in execution order. ``earlier_saves`` accumulates stage
705
847
  by stage, mirroring the runtime commit of saves after a stage passes, and
706
- ``skippable_saves`` holds those of them no stage without a ``skip_if``
848
+ ``skippable_saves`` holds those of them no stage without a `SkipCause`
707
849
  saves (``skip_if: false`` never skips)."""
708
850
  scenario_substitutions = frozenset(substitution_names(scenario.substitutions))
709
851
  scenario_fixtures = frozenset(scenario.fixtures)
@@ -713,6 +855,7 @@ def stage_scopes(scenario: Scenario) -> list[StageScopes]:
713
855
  unskippable_saves: frozenset[str] = frozenset()
714
856
  for stage in scenario.stages:
715
857
  saves = frozenset(saved_in_stage(stage))
858
+ skip_cause = _stage_skip_cause(stage)
716
859
  scopes.append(
717
860
  StageScopes(
718
861
  scenario_substitutions=scenario_substitutions,
@@ -724,10 +867,12 @@ def stage_scopes(scenario: Scenario) -> list[StageScopes]:
724
867
  saves=saves,
725
868
  earlier_saves=earlier_saves,
726
869
  skippable_saves=earlier_saves - unskippable_saves,
870
+ skip_cause=skip_cause,
871
+ never_called=(reader := marks_skip_cause([*stage.marks, *scenario.marks])) is not None and reader.never_called,
727
872
  )
728
873
  )
729
874
  earlier_saves |= saves
730
- if stage.skip_if is False:
875
+ if skip_cause is None:
731
876
  unskippable_saves |= saves
732
877
  return scopes
733
878
 
@@ -154,9 +154,39 @@ def configured_suffix(paths: Sequence[Path]) -> str:
154
154
  raise DiscoveryError(f"{file}: {ConfigOptions.SUFFIX} {e}") from None
155
155
 
156
156
 
157
+ def pytest_rootdir(paths: Sequence[Path]) -> Path:
158
+ """The rootdir pytest would determine for a run on ``paths`` from the
159
+ current directory, given no ``-c`` or ``--rootdir``: the directory of the
160
+ configuration file it would read (see `configured_suffix`); failing that,
161
+ the nearest directory holding a ``setup.py``, at or above the common
162
+ ancestor of the paths; failing that, the common ancestor of the current
163
+ directory and the paths', or the paths' alone when that is the root of
164
+ the file system, or when there is none (on Windows, paths on another
165
+ drive than the current directory, where pytest keeps the current
166
+ directory, which holds none of them).
167
+
168
+ Collection sandboxes ``$ref`` targets in pytest's rootdir, so the CLI's
169
+ default reference root is this: `validate` rejects no reference a run
170
+ on the same paths resolves, and one run's files share one root.
171
+
172
+ A configuration file that cannot be read, which pytest stops on, is
173
+ passed over here, as one holding no pytest configuration is: a run over
174
+ files needs no setting from it (`configured_suffix` reports it when a
175
+ directory needs the suffix)."""
176
+ return _setup(paths, strict=False)[0]
177
+
178
+
157
179
  def _locate_config(paths: Sequence[Path]) -> tuple[Path, dict[str, object]] | None:
158
180
  """The configuration file pytest would read for ``paths`` and the settings
159
181
  it holds, or None when it would read none (see `configured_suffix`)."""
182
+ return _setup(paths, strict=True)[1]
183
+
184
+
185
+ def _setup(paths: Sequence[Path], *, strict: bool) -> tuple[Path, tuple[Path, dict[str, object]] | None]:
186
+ """pytest's rootdir for ``paths``, and the configuration file it reads
187
+ with its settings, or None (see `pytest_rootdir`); ``strict`` raises
188
+ `DiscoveryError` for a configuration file that cannot be read, else it is
189
+ passed over."""
160
190
  dirs: list[Path] = []
161
191
  for path in paths:
162
192
  # Made absolute as pytest makes an argument absolute: `..` and `.`
@@ -165,10 +195,22 @@ def _locate_config(paths: Sequence[Path]) -> tuple[Path, dict[str, object]] | No
165
195
  if os.path.exists(absolute):
166
196
  dirs.append(absolute if os.path.isdir(absolute) else absolute.parent)
167
197
  ancestor = _common_ancestor(dirs) if dirs else Path.cwd()
168
- found = _find_config([ancestor])
169
- if found is None and dirs != [ancestor] and not any(os.path.isfile(base / "setup.py") for base in (ancestor, *ancestor.parents)):
170
- found = _find_config(dirs)
171
- return found
198
+ if (found := _find_config([ancestor], strict=strict)) is not None:
199
+ return found[0].parent, found
200
+ for base in (ancestor, *ancestor.parents):
201
+ if os.path.isfile(base / "setup.py"):
202
+ return base, None
203
+ if dirs != [ancestor] and (found := _find_config(dirs, strict=strict)) is not None:
204
+ return found[0].parent, found
205
+ try:
206
+ rootdir = Path(os.path.commonpath([Path.cwd(), ancestor]))
207
+ except ValueError:
208
+ # On another drive than the current directory (Windows): pytest keeps
209
+ # the current directory then, a rootdir that holds none of the paths,
210
+ # so their own common ancestor is taken instead.
211
+ return ancestor, None
212
+ # The root of the file system (of a drive, on Windows) is no rootdir.
213
+ return (ancestor if os.path.splitdrive(rootdir)[1] == os.sep else rootdir), None
172
214
 
173
215
 
174
216
  def _common_ancestor(dirs: list[Path]) -> Path:
@@ -182,9 +224,12 @@ def _common_ancestor(dirs: list[Path]) -> Path:
182
224
  return ancestor
183
225
 
184
226
 
185
- def _find_config(starts: Iterable[Path]) -> tuple[Path, dict[str, object]] | None:
227
+ def _find_config(starts: Iterable[Path], *, strict: bool = True) -> tuple[Path, dict[str, object]] | None:
186
228
  """The first configuration file holding pytest configuration in each of
187
- ``starts`` or above it, in turn; else the first ``pyproject.toml`` met."""
229
+ ``starts`` or above it, in turn; else the first ``pyproject.toml`` met.
230
+ Not ``strict``, one that cannot be read, or that pytest would refuse, is
231
+ passed over as one holding no pytest configuration is: a
232
+ ``pyproject.toml`` of the kind is still the one met, if first."""
188
233
  bare_pyproject: Path | None = None
189
234
  for start in starts:
190
235
  for base in (start, *start.parents):
@@ -192,7 +237,12 @@ def _find_config(starts: Iterable[Path]) -> tuple[Path, dict[str, object]] | Non
192
237
  file = base / name
193
238
  if not os.path.isfile(file):
194
239
  continue
195
- settings = _read_config(file)
240
+ try:
241
+ settings = _read_config(file)
242
+ except DiscoveryError:
243
+ if strict:
244
+ raise
245
+ settings = None
196
246
  if settings is not None:
197
247
  return file, settings
198
248
  if name == "pyproject.toml" and bare_pyproject is None:
@@ -1,8 +1,6 @@
1
1
  """The load + $ref-resolve + model-validate path shared by the CLI and pytest collection."""
2
2
 
3
- import configparser
4
3
  import json
5
- import tomllib
6
4
  import warnings
7
5
  from pathlib import Path
8
6
  from typing import Any
@@ -12,66 +10,21 @@ from pydantic import ValidationError
12
10
  from pytest_httpchain.jsonref import FileLoadError, InvalidJSONError, ReferenceResolverError, load_json
13
11
  from pytest_httpchain.models import Scenario
14
12
  from pytest_httpchain.validation.diagnostics import Diagnostic, DiagnosticCode, diag
13
+ from pytest_httpchain.validation.discovery import pytest_rootdir
15
14
  from pytest_httpchain.warnings import AmbiguousReferenceWarning
16
15
 
17
- _ROOT_MARKERS = ("pytest.ini", "pyproject.toml", "tox.ini", "setup.cfg", "setup.py", ".git")
18
-
19
-
20
- def _holds_pytest_config(directory: Path) -> bool:
21
- """True when the directory holds a file pytest would accept as its inifile.
22
-
23
- Mirrors pytest's own rule: ``pytest.ini`` always counts, while the shared
24
- files count only when they actually carry a pytest section. A bare
25
- ``pyproject.toml`` — a sub-package in a monorepo — is not a pytest rootdir.
26
- """
27
- if (directory / "pytest.ini").is_file():
28
- return True
29
-
30
- pyproject = directory / "pyproject.toml"
31
- if pyproject.is_file():
32
- try:
33
- if "pytest" in tomllib.loads(pyproject.read_text(encoding="utf-8")).get("tool", {}):
34
- return True
35
- except (OSError, ValueError):
36
- pass
37
-
38
- for filename, section in (("tox.ini", "pytest"), ("setup.cfg", "tool:pytest")):
39
- candidate = directory / filename
40
- if not candidate.is_file():
41
- continue
42
- parser = configparser.ConfigParser()
43
- try:
44
- parser.read(candidate, encoding="utf-8")
45
- except (OSError, configparser.Error, UnicodeDecodeError):
46
- continue
47
- if parser.has_section(section):
48
- return True
49
-
50
- return False
51
-
52
16
 
53
17
  def resolve_root_path(path: Path) -> Path:
54
18
  """Directory that constrains ``$ref`` resolution when no explicit root is given.
55
19
 
56
- Approximates pytest's ``rootdir`` (which collection passes explicitly): a
57
- real pytest config wins outright, even when a bare project marker sits
58
- nearer. Without that precedence a sub-package's plain ``pyproject.toml``
59
- shrinks the CLI's root below pytest's, and ``validate`` rejects ``$ref``
60
- targets that collection resolves fine — a red CI on a working scenario.
61
- Falls back to the nearest bare marker, then the nearest ``tests/`` ancestor,
62
- then the file's own parent.
20
+ pytest's rootdir for a run on ``path`` from the current directory
21
+ (`discovery.pytest_rootdir`), which collection passes explicitly: the
22
+ directory of the configuration file pytest would read, else of the
23
+ nearest ``setup.py``, else the common ancestor of the current directory
24
+ and the file's. A narrower root rejects ``$ref`` targets that collection
25
+ resolves fine, a red CI on a working scenario.
63
26
  """
64
- ancestors = path.resolve().parents
65
- for ancestor in ancestors:
66
- if _holds_pytest_config(ancestor):
67
- return ancestor
68
- for ancestor in ancestors:
69
- if any((ancestor / marker).exists() for marker in _ROOT_MARKERS):
70
- return ancestor
71
- for ancestor in ancestors:
72
- if ancestor.name == "tests":
73
- return ancestor
74
- return path.parent
27
+ return pytest_rootdir([path])
75
28
 
76
29
 
77
30
  def _verify_subpath(path: tuple[str | int, ...]) -> tuple[str | int, ...] | None:
@@ -34,6 +34,8 @@ from pytest_httpchain.scoping import (
34
34
  SCENARIO_TEMPLATE_FIELDS,
35
35
  DefinedNames,
36
36
  NameUnion,
37
+ SkipCause,
38
+ StageScopes,
37
39
  defined_names,
38
40
  extract_builtin_stand_ins,
39
41
  extract_defined_variables,
@@ -44,7 +46,6 @@ from pytest_httpchain.scoping import (
44
46
  raw_stages,
45
47
  response_step_templates,
46
48
  saved_in_response,
47
- saved_in_stage,
48
49
  stage_scopes,
49
50
  stats_name,
50
51
  substitution_names,
@@ -350,16 +351,23 @@ def _parametrize_rendered_values(raw_parametrize: Any) -> Any:
350
351
  return raw_parametrize
351
352
 
352
353
 
353
- def _skippable_save_diagnostic(scenario: Scenario, i: int, where: str, name: str, location: str) -> Diagnostic:
354
+ def _skip_cause_text(cause: SkipCause) -> str:
355
+ return "skip_if" if cause.mark is None else f"the mark {cause.mark!r}"
356
+
357
+
358
+ def _skippable_save_diagnostic(scenario: Scenario, scopes: list[StageScopes], i: int, where: str, name: str, location: str) -> Diagnostic:
354
359
  """HTTPCHAIN003 for a reference to ``name`` that only earlier stages with a
355
- ``skip_if`` save (`scoping.StageScopes.skippable_saves`): a skip leaves the
356
- chain healthy, so stage ``i`` runs, and finds the name undefined, whenever
357
- those stages skipped. ``where`` is the phase that reads it."""
358
- savers = [f"'{stage.name}'" for stage in scenario.stages[:i] if name in saved_in_stage(stage)]
360
+ `scoping.SkipCause` save (`scoping.StageScopes.skippable_saves`): a skip
361
+ leaves the chain healthy, so stage ``i`` runs, and finds the name undefined,
362
+ whenever those stages skipped. Each saver is named with its cause, its
363
+ ``skip_if`` or the mark as written. ``where`` is the phase that reads it."""
364
+ savers = [(scenario.stages[j].name, cause) for j in range(i) if name in scopes[j].saves and (cause := scopes[j].skip_cause)]
359
365
  if len(savers) == 1:
360
- saved = f"which only stage {savers[0]} saves, and it has skip_if: when it skips"
366
+ [(saver, cause)] = savers
367
+ saved = f"which only stage '{saver}' saves, and it has {_skip_cause_text(cause)}: when it {'fails as expected' if cause.fails_as_expected else 'skips'}"
361
368
  else:
362
- saved = f"which only stages {', '.join(savers)} save, and each has skip_if: when they all skip"
369
+ listed = ", ".join(f"'{saver}' ({_skip_cause_text(cause)})" for saver, cause in savers)
370
+ saved = f"which only stages {listed} save: when none of them saves it"
363
371
  return diag(
364
372
  DiagnosticCode.UNDEFINED_VAR,
365
373
  f"Stage '{scenario.stages[i].name}': {where} references '{name}', {saved}, '{name}' is undefined here — read it with get('{name}', <default>){_builtin_fallback({name})}",
@@ -374,9 +382,12 @@ def _dataflow_diagnostics(scenario: Scenario, test_data: dict[str, Any], defined
374
382
  ``scoping.stage_scopes``. An unavailable reference is a FORWARD_REF when the
375
383
  name is saved later (by a later stage, or by a later step of this stage's
376
384
  own response) or defined by a later substitution step, else an UNDEFINED_VAR.
377
- An available one is an UNDEFINED_VAR too where only earlier stages with a
378
- ``skip_if`` save it, and nothing else of that name is in scope: a skipped
379
- stage, unlike a failed one, does not stop the stages after it. A built-in's
385
+ An available one is an UNDEFINED_VAR too where only earlier stages that may
386
+ skip (a ``skip_if``, or a ``skip``, ``skipif`` or ``xfail`` mark) save it,
387
+ and nothing else of that name is in scope: a skipped stage, unlike a failed
388
+ one, does not stop the stages after it. Not in a stage a mark keeps pytest
389
+ from ever calling, which reads nothing, nor past the always_run of one whose
390
+ skip_if is true. A built-in's
380
391
  name the scenario defines too, called or passed as a function where that
381
392
  definition is not in scope, gets the built-in instead: a BUILTIN_STANDS_IN.
382
393
  """
@@ -394,8 +405,14 @@ def _dataflow_diagnostics(scenario: Scenario, test_data: dict[str, Any], defined
394
405
  for i, stage in enumerate(scenario.stages):
395
406
  scope = scopes[i]
396
407
  phases = scope.phases()
397
- # The same phases, as they are when every earlier stage with a skip_if skipped.
398
- skipped = phases if scope.when_skipped is scope else scope.when_skipped.phases()
408
+ # The same phases, as they are when every earlier stage that may skip
409
+ # skipped. Unchanged where the stage reads nothing: pytest never calls
410
+ # it, or, past always_run, its skip_if is true (the carrier skips it
411
+ # right after always_run, before anything else renders).
412
+ skipped = phases if scope.when_skipped is scope or scope.never_called else scope.when_skipped.phases()
413
+ skipped_always_run = skipped.always_run
414
+ if stage.skip_if is True:
415
+ skipped = phases
399
416
  raw = raws[i] if i < len(raws) and isinstance(raws[i], dict) else {}
400
417
 
401
418
  parametrize_refs, parametrize_stand_ins = _template_refs(_parametrize_rendered_values(raw.get("parametrize")), defined)
@@ -419,8 +436,8 @@ def _dataflow_diagnostics(scenario: Scenario, test_data: dict[str, Any], defined
419
436
  always_run_refs, always_run_stand_ins = _template_refs(raw.get("always_run"), defined)
420
437
  for name in sorted(always_run_refs):
421
438
  if name in phases.always_run:
422
- if name not in skipped.always_run:
423
- yield _skippable_save_diagnostic(scenario, i, "always_run", name, f"stages[{i}].always_run")
439
+ if name not in skipped_always_run:
440
+ yield _skippable_save_diagnostic(scenario, scopes, i, "always_run", name, f"stages[{i}].always_run")
424
441
  continue
425
442
  if name in all_saved:
426
443
  j = first_save_stage[name]
@@ -441,7 +458,7 @@ def _dataflow_diagnostics(scenario: Scenario, test_data: dict[str, Any], defined
441
458
  yield _stand_in_diagnostic(f"Stage '{stage.name}': always_run", always_run_stand_ins, f"stages[{i}].always_run")
442
459
 
443
460
  # (phase, template text, names available to it, and those of them
444
- # still available when every earlier stage with a skip_if skipped).
461
+ # still available when every earlier stage that may skip skipped).
445
462
  # Each substitution and response step is checked against its own
446
463
  # scope, not the whole stage's: checking cumulatively is what catches
447
464
  # intra-list forward references. A name referenced by several steps is
@@ -474,7 +491,7 @@ def _dataflow_diagnostics(scenario: Scenario, test_data: dict[str, Any], defined
474
491
  for name in sorted(refs):
475
492
  if name in available:
476
493
  if name not in available_when_skipped:
477
- yield _skippable_save_diagnostic(scenario, i, phase, name, f"stages[{i}].{phase}")
494
+ yield _skippable_save_diagnostic(scenario, scopes, i, phase, name, f"stages[{i}].{phase}")
478
495
  continue
479
496
  if name in scope.stage_substitutions:
480
497
  yield diag(
@@ -10,7 +10,7 @@ from pytest_httpchain.body_schema import ReferenceBounds
10
10
  from pytest_httpchain.constants import SCENARIO_FILE_EXTENSIONS
11
11
  from pytest_httpchain.validation.deep import check_scenario_deep
12
12
  from pytest_httpchain.validation.diagnostics import Diagnostic, DiagnosticCode, ValidateResult, diag, result
13
- from pytest_httpchain.validation.discovery import configured_suffix, find_scenario_files
13
+ from pytest_httpchain.validation.discovery import configured_suffix, find_scenario_files, pytest_rootdir
14
14
  from pytest_httpchain.validation.loader import load_with_diagnostics, resolve_root_path
15
15
  from pytest_httpchain.validation.semantic import check_scenario, describe_scenario
16
16
 
@@ -23,7 +23,10 @@ def validate_scenario(
23
23
  syspaths: list[Path] | None = None,
24
24
  ) -> ValidateResult:
25
25
  """Validate a scenario file: existence, JSON, ``$ref``, schema, then the
26
- semantic checks — and with ``deep``, the opt-in import/file checks."""
26
+ semantic checks — and with ``deep``, the opt-in import/file checks.
27
+
28
+ References are held to ``root_path``, by default pytest's rootdir for a
29
+ run on the file (`resolve_root_path`)."""
27
30
  diagnostics: list[Diagnostic] = []
28
31
 
29
32
  if not path.exists():
@@ -41,6 +44,9 @@ def validate_scenario(
41
44
  )
42
45
  )
43
46
 
47
+ if root_path is None:
48
+ root_path = resolve_root_path(path)
49
+
44
50
  # Load diagnostics are collected rather than returned early, so ambiguity
45
51
  # warnings earned by earlier references are still reported.
46
52
  loaded, load_diagnostics = load_with_diagnostics(path, root_path=root_path, ref_parent_traversal_depth=ref_parent_traversal_depth)
@@ -53,7 +59,7 @@ def validate_scenario(
53
59
 
54
60
  if deep:
55
61
  # The bounds the load held the scenario's references to, for its body schemas'.
56
- ref_bounds = ReferenceBounds(root_path if root_path is not None else resolve_root_path(path), ref_parent_traversal_depth)
62
+ ref_bounds = ReferenceBounds(root_path, ref_parent_traversal_depth)
57
63
  diagnostics.extend(check_scenario_deep(scenario, syspaths=syspaths, scenario_dir=path.parent, ref_bounds=ref_bounds))
58
64
 
59
65
  return result(diagnostics, describe_scenario(scenario, test_data))
@@ -87,7 +93,22 @@ def validate_paths(
87
93
  Every directory is searched before any file is validated, so the
88
94
  `DiscoveryError` of one that cannot be (or of a configuration file that
89
95
  cannot be read) stops the run before it has done any work.
96
+
97
+ References are held to ``root_path``, by default to the rootdir pytest
98
+ would determine for a run on ``paths`` (`pytest_rootdir`): one root for
99
+ every file, as a pytest run has, where a root derived for each file
100
+ alone could be narrower than the run's and reject a reference it
101
+ resolves. A file that root does not hold (files of two projects in one
102
+ run, the run's root the first one's) is held to the root pytest gives
103
+ it alone: the run's would fail every reference it makes.
90
104
  """
105
+ run_root = root_path if root_path is not None else pytest_rootdir(paths)
106
+
107
+ def file_root(path: Path) -> Path:
108
+ if root_path is not None or Path(os.path.abspath(path)).is_relative_to(run_root):
109
+ return run_root
110
+ return pytest_rootdir([path])
111
+
91
112
  targets: list[tuple[Path, ValidateResult | None]] = []
92
113
  seen: set[str] = set()
93
114
 
@@ -116,7 +137,7 @@ def validate_paths(
116
137
  return [
117
138
  (
118
139
  path,
119
- found if found is not None else validate_scenario(path, ref_parent_traversal_depth=ref_parent_traversal_depth, root_path=root_path, deep=deep, syspaths=syspaths),
140
+ found if found is not None else validate_scenario(path, ref_parent_traversal_depth=ref_parent_traversal_depth, root_path=file_root(path), deep=deep, syspaths=syspaths),
120
141
  )
121
142
  for path, found in targets
122
143
  ]