Skip to content

Commit 2cdcd41

Browse files
EliEli
authored andcommitted
Trial and error case location to avoid $SRC expansion bug
Test directory was being located with code that had $SRC in it unexpanded. This new way is less elegant -- it looks at a couple expected places in the hierarchy.
1 parent 3a99655 commit 2cdcd41

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

tests/test_read_yaml_header.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,25 @@ def load_cases():
1616
cases = {}
1717
current_name = None
1818
current_lines = []
19-
20-
for line in DATA_FILE.read_text(encoding="utf-8").splitlines(keepends=True):
19+
# This tedious way of doing things is to avoid some GiHub-side substitutions of $SRC that were not expanded
20+
_candidates = [
21+
Path("tests/data/header_data.txt"),
22+
Path("data/header_data.txt"),
23+
Path(__file__).parent.resolve() / "data" / "header_data.txt",
24+
]
25+
26+
for candidate in _candidates:
27+
if candidate.is_file():
28+
DATA_FILE = candidate
29+
break
30+
else: # else with a for loop executes if the loop completes without hitting a break
31+
raise FileNotFoundError(
32+
"Could not find header_data.txt. Tried:\n" +
33+
"\n".join(str(p) for p in _candidates)
34+
)
35+
36+
37+
for line in DATA_FILE.read_text().splitlines(keepends=True):
2138
if line.startswith("!"):
2239
if current_name is not None:
2340
cases[current_name] = "".join(current_lines)
@@ -32,6 +49,7 @@ def load_cases():
3249
return cases
3350

3451

52+
3553
def split_header_and_body(text: str, comment: str = "#") -> tuple[str, str]:
3654
lines = text.splitlines(keepends=True)
3755
header = []

tests/test_yaml_headers.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,24 @@ def load_header_cases():
2323
cases = {}
2424
current_name = None
2525
current_lines = []
26-
DATA_FILE = Path(__file__).parent.resolve() / "data" / "header_data.txt"
26+
# This tedious way of doing things is to avoid some GiHub-side substitutions of $SRC that were not expanded
27+
_candidates = [
28+
Path("tests/data/header_data.txt"),
29+
Path("data/header_data.txt"),
30+
Path(__file__).parent.resolve() / "data" / "header_data.txt",
31+
]
32+
33+
for candidate in _candidates:
34+
if candidate.is_file():
35+
DATA_FILE = candidate
36+
break
37+
else: # else with a for loop executes if the loop completes without hitting a break
38+
raise FileNotFoundError(
39+
"Could not find header_data.txt. Tried:\n" +
40+
"\n".join(str(p) for p in _candidates)
41+
)
42+
43+
2744
for line in DATA_FILE.read_text().splitlines(keepends=True):
2845
if line.startswith("!"):
2946
if current_name is not None:
@@ -38,14 +55,6 @@ def load_header_cases():
3855

3956
return cases
4057

41-
from pathlib import Path
42-
print("DEBUG __file__ =", __file__)
43-
print("DEBUG parent =", Path(__file__).parent)
44-
print("DEBUG resolved =", Path(__file__).parent.resolve())
45-
DATA_FILE = Path(__file__).parent.resolve() / "data" / "header_data.txt"
46-
print("DEBUG DATA_FILE =", DATA_FILE)
47-
print("DEBUG exists =", DATA_FILE.exists())
48-
4958

5059
CASES = load_header_cases()
5160

0 commit comments

Comments
 (0)