Skip to content

Commit 07334fb

Browse files
authored
Cache reference MFiles for regression tests (#4069)
* Cache reference mfiles for regression tests * Add platformdirs to test requirements
1 parent 8466c1c commit 07334fb

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ test = [
5656
"testbook>=0.4",
5757
"pytest-cov>=3.0.0",
5858
"pytest-xdist>=2.5.0",
59+
"platformdirs~=4.5.0",
5960
"process[examples]"
6061
]
6162
examples = ["jupyter==1.0.0"]

tests/regression/regression_test_assets.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
import logging
77
import re
88
import subprocess
9-
from pathlib import Path
109

1110
import requests
11+
from platformdirs import user_cache_path
1212

1313
logger = logging.getLogger(__name__)
1414

15+
TEST_ASSET_CACHE_DIR = user_cache_path("PROCESS-regression-tests", "ukaea")
16+
1517

1618
@dataclasses.dataclass
1719
class TrackedMFile:
@@ -28,9 +30,7 @@ def __init__(self) -> None:
2830
self._hashes = self._git_commit_hashes()
2931
self._tracked_mfiles = self._get_tracked_mfiles()
3032

31-
def get_reference_mfile(
32-
self, scenario_name: str, directory: Path, target_hash: str | None = None
33-
):
33+
def get_reference_mfile(self, scenario_name: str, target_hash: str | None = None):
3434
"""Finds the most recent reference MFile for `<scenario_name>.IN.DAT`
3535
and downloads it to the `directory` with the name `ref.<scenario_name>.MFILE.DAT`.
3636
@@ -50,17 +50,29 @@ def get_reference_mfile(
5050
`None` is returned.
5151
:rtype: Path
5252
"""
53-
reference_mfile_location = directory / f"ref.{scenario_name}.MFILE.DAT"
53+
5454
for mf in self._tracked_mfiles:
5555
if (mf.scenario_name == scenario_name and target_hash is None) or (
5656
mf.scenario_name == scenario_name and target_hash == mf.hash
5757
):
58-
Path(reference_mfile_location).write_text(
58+
cache_directory = TEST_ASSET_CACHE_DIR / mf.hash
59+
cached_location = cache_directory / f"ref.{scenario_name}.MFILE.DAT"
60+
61+
if cached_location.exists():
62+
logger.info(
63+
f"Using cached reference MFile ({cached_location}) found for commit {mf.hash}."
64+
)
65+
return cached_location
66+
67+
cache_directory.mkdir(parents=True, exist_ok=True)
68+
cached_location.write_text(
5969
requests.get(mf.download_link).content.decode()
6070
)
6171

62-
logger.info(f"Reference MFile found for commit {mf.hash}")
63-
return reference_mfile_location
72+
logger.info(
73+
f"Reference MFile found for commit {mf.hash}. Writing to {cached_location}"
74+
)
75+
return cached_location
6476

6577
return None
6678

tests/regression/test_process_input_files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def test_input_file(
286286
scenario = RegressionTestScenario(new_input_file)
287287

288288
reference_mfile = tracked_regression_test_assets.get_reference_mfile(
289-
scenario.scenario_name, tmp_path
289+
scenario.scenario_name
290290
)
291291

292292
scenario.run(solver_name)

0 commit comments

Comments
 (0)