Skip to content

Commit a9f9638

Browse files
committed
Incorporate requested changes related to testing/Repo in #255
1 parent 9e86846 commit a9f9638

File tree

4 files changed

+81
-32
lines changed

4 files changed

+81
-32
lines changed

benchcab/benchcab.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,11 @@ def checkout(self, config_path: str):
230230
try:
231231
model.repo.checkout()
232232
except Exception:
233-
self.logger.error("Try using `benchcab clean realisations` first")
234-
sys.exit(1)
233+
msg = "Try using `benchcab clean realisations` first"
234+
self.logger.error(
235+
"Model checkout failed, probably due to existing realisation name"
236+
)
237+
raise FileExistsError(msg)
235238
rev_number_log += f"{model.name}: {model.repo.get_revision()}\n"
236239

237240
rev_number_log_path = next_path("rev_number-*.log")

benchcab/data/test/integration.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mkdir -p $TEST_DIR
1818
# Clone local checkout for CABLE
1919
git clone $CABLE_REPO $CABLE_DIR
2020
cd $CABLE_DIR
21+
# Note: This is temporary, to be removed once #258 is fixed
2122
git reset --hard 67a52dc5721f0da78ee7d61798c0e8a804dcaaeb
2223

2324
# Clone the example repo
@@ -35,8 +36,7 @@ realisations:
3536
- repo:
3637
git:
3738
branch: main
38-
commit: 67a52dc5721f0da78ee7d61798c0e8a804dcaaeb
39-
39+
commit: 67a52dc5721f0da78ee7d61798c0e8a804dcaaeb # Note: This is temporary, to be removed once #258 is fixed
4040
modules: [
4141
intel-compiler/2021.1.1,
4242
netcdf/4.7.4,

benchcab/utils/repo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def get_revision(self) -> str:
8585
Human readable string describing the latest revision.
8686
8787
"""
88-
return f"Local CABLE build: {self.name}"
88+
return f"Local CABLE build: {Path(self.local_path).absolute().as_posix}"
8989

9090
def get_branch_name(self) -> str:
9191
"""Return the branch name of the source code.

tests/test_workdir.py

+73-27
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from pathlib import Path
9+
from typing import List
910

1011
import pytest
1112

@@ -60,52 +61,97 @@ def test_directory_structure_generated(self, spatial_directory_list):
6061

6162

6263
class TestCleanFiles:
63-
"""Tests for `clean_directory_tree()` and `clean_cwd_logfiles)."""
64+
"""Tests for `clean_realisation_files()` and `clean_submission_files()`."""
6465

66+
# Reset internal.CWD to suit pytest testing infrastructure
6567
@pytest.fixture(autouse=True)
66-
def set_internal_cwd(self, monkeypatch):
68+
def _set_internal_cwd(self, monkeypatch):
6769
"""Sets internal.CWD to pytest's working directory."""
6870
monkeypatch.setattr(internal, "CWD", Path.cwd())
6971

72+
# Helper functions
73+
def _create_files_in_cwd(self, filenames: List[str]):
74+
"""Given a list of filenames, create files in current working directory."""
75+
for filename in filenames:
76+
filename_path = internal.CWD / filename
77+
filename_path.touch()
78+
79+
def _check_if_any_files_exist(self, filenames: List[str]):
80+
"""Given a list of filenames, check if any of them exist w.r.t. current working directory."""
81+
return any((internal.CWD / filename).exists() for filename in filenames)
82+
7083
@pytest.fixture()
7184
def src_path(self) -> Path:
72-
return Path("src")
85+
"""Mock internal.SRC_DIR."""
86+
src_path = internal.CWD / Path("src")
87+
src_path.mkdir()
88+
return src_path
7389

7490
@pytest.fixture()
7591
def runs_path(self) -> Path:
76-
return Path("runs")
92+
"""Mock internal.RUN_DIR."""
93+
runs_path = internal.CWD / Path("runs")
94+
runs_path.mkdir()
95+
return runs_path
7796

78-
@pytest.fixture(params=["rev_number-0.log", "rev_number-200.log"])
79-
def revision_log_file(self, request) -> Path:
80-
return Path(request.param)
97+
@pytest.fixture()
98+
def revision_log_files(self) -> List[Path]:
99+
"""Create sample files of the form rev_number-*.log."""
100+
rev_log_files = ["rev_number-0.log", "rev_number-200.log"]
101+
self._create_files_in_cwd(rev_log_files)
102+
return rev_log_files
81103

82-
@pytest.fixture(
83-
params=["benchmark_cable_qsub.sh.o21871", "benchmark_cable_qsub.sh"]
84-
)
85-
def pbs_job_file(self, request) -> Path:
86-
return Path(request.param)
104+
@pytest.fixture()
105+
def pbs_job_files(self) -> List[Path]:
106+
"""Create sample files of the form benchmark_cable_qsub.sh*."""
107+
pbs_job_files = ["benchmark_cable_qsub.sh.o21871", "benchmark_cable_qsub.sh"]
108+
self._create_files_in_cwd(pbs_job_files)
109+
return pbs_job_files
87110

88-
def test_clean_realisation_files(
89-
self, src_path: Path, tmp_path: Path, revision_log_file: Path
90-
):
91-
"""Success case: Realisation files created by benchcab are removed after clean."""
92-
src_path.mkdir()
93-
cable_symlink = src_path / "main"
94-
# tmp_path contains the path being symlinked
111+
@pytest.fixture()
112+
def local_cable_src_path(self, tmp_path) -> Path:
113+
"""Local sample path for CABLE checkout."""
114+
# Temporary directory of CABLE unique to test invocation, independent of CWD
95115
local_cable_src_path = tmp_path / "CABLE"
96116
local_cable_src_path.mkdir()
117+
return local_cable_src_path
118+
119+
@pytest.fixture()
120+
def src_path_with_local(self, src_path, local_cable_src_path) -> Path:
121+
"""Local path where CABLE checkout is symlinked."""
122+
cable_symlink = src_path / "cable_local"
97123
cable_symlink.symlink_to(local_cable_src_path)
98-
revision_log_file.touch()
99-
clean_realisation_files()
124+
return src_path
100125

126+
@pytest.fixture()
127+
def src_path_with_git(self, src_path) -> Path:
128+
"""Local path where CABLE checkout from git is present."""
129+
cable_git = src_path / "cable_git"
130+
cable_git.mkdir()
131+
return src_path
132+
133+
def test_clean_realisation_files_local(
134+
self,
135+
local_cable_src_path: Path,
136+
src_path_with_local: Path,
137+
revision_log_files: List[Path],
138+
):
139+
"""Success case: Local realisation files created by benchcab are removed after clean."""
140+
clean_realisation_files()
101141
assert local_cable_src_path.exists()
102-
assert not src_path.exists()
103-
assert not revision_log_file.exists()
142+
assert not src_path_with_local.exists()
143+
assert not self._check_if_any_files_exist(revision_log_files)
104144

105-
def test_clean_submission_files(self, runs_path, pbs_job_file):
145+
def test_clean_realisation_files_git(
146+
self, src_path_with_git: Path, revision_log_files: Path
147+
):
148+
"""Success case: Git realisation files created by benchcab are removed after clean."""
149+
clean_realisation_files()
150+
assert not src_path_with_git.exists()
151+
assert not self._check_if_any_files_exist(revision_log_files)
152+
153+
def test_clean_submission_files(self, runs_path, pbs_job_files: List[Path]):
106154
"""Success case: Submission files created by benchcab are removed after clean."""
107-
runs_path.mkdir()
108-
pbs_job_file.touch()
109155
clean_submission_files()
110156
assert not runs_path.exists()
111-
assert not pbs_job_file.exists()
157+
assert not self._check_if_any_files_exist(pbs_job_files)

0 commit comments

Comments
 (0)