Skip to content

Commit 0e617c5

Browse files
PSeitz-ddfulmicoton-dd
authored andcommitted
add !include functionality to integration tests (#5891)
1 parent aa096fc commit 0e617c5

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

quickwit/rest-api-tests/run_tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,31 @@
1818
from os import mkdir
1919
from os import path as osp
2020

21+
# Simple !include constructor for YAML to allow reusing fragments across files.
22+
# Usage examples:
23+
# - !include path/to/file.yaml -> includes full file content
24+
# - !include path/to/file.yaml::doc_mapping -> includes the 'doc_mapping' key
25+
# - !include path/to/file.yaml::a.b.c -> includes nested key a -> b -> c
26+
def _yaml_include(loader, node):
27+
value = loader.construct_scalar(node)
28+
if "::" in value:
29+
filepath, subpath = value.split("::", 1)
30+
else:
31+
filepath, subpath = value, None
32+
with open(filepath, "r") as f:
33+
included = yaml.load(f, Loader=yaml.Loader)
34+
if subpath:
35+
cur = included
36+
for seg in filter(None, subpath.split(".")):
37+
if not isinstance(cur, dict) or seg not in cur:
38+
raise KeyError(f"!include path '{subpath}' not found in {filepath}")
39+
cur = cur[seg]
40+
return cur
41+
return included
42+
43+
# Register the constructor on the default Loader used by this script.
44+
yaml.Loader.add_constructor("!include", _yaml_include)
45+
2146
def debug_http():
2247
old_send = http.client.HTTPConnection.send
2348
def new_send(self, data):

0 commit comments

Comments
 (0)