Skip to content

Commit 4d31d37

Browse files
abhaasgoyalccarouge
andcommitted
Remove dependency on internal.CWD, test improvements on #255
Co-authored-by: Claire Carouge <[email protected]>
1 parent d26fa61 commit 4d31d37

File tree

8 files changed

+25
-46
lines changed

8 files changed

+25
-46
lines changed

benchcab/benchcab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _validate_environment(self, project: str, modules: list):
7676
self.logger.error("benchcab is currently implemented only on Gadi")
7777
sys.exit(1)
7878

79-
namelist_dir = Path(internal.CWD / internal.NAMELIST_DIR)
79+
namelist_dir = Path(Path.cwd() / internal.NAMELIST_DIR)
8080
if not namelist_dir.exists():
8181
self.logger.error(
8282
"Cannot find 'namelists' directory in current working directory"

benchcab/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ def generate_parser(app: Benchcab) -> argparse.ArgumentParser:
240240
choices=["all", "realisations", "submissions"],
241241
help="""Can be one of three options:
242242
243-
submissions: deletes src/ and revision log files
244-
realisations: deletes runs/ and benchmark submission files
243+
realisations: deletes src/
244+
submissions: deletes runs/ and benchmark submission files
245245
all: deletes in both stages of submissions and realisations""",
246246
)
247247

benchcab/internal.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424

2525
# DIRECTORY PATHS/STRUCTURE:
2626

27-
# Path to the user's current working directory
28-
CWD = Path.cwd()
29-
3027
# Default system paths in Unix
3128
SYSTEM_PATHS = ["/bin", "/usr/bin", "/usr/local/bin"]
3229

benchcab/utils/repo.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def __init__(self, path: str, realisation_path: str) -> None:
5757
realisation_path : str
5858
Path for local checkout of CABLE
5959
path : str
60-
Directory where CABLE is symlinked from
60+
Directory where CABLE is symlinked from, assigned as `local_path`
6161
6262
"""
6363
self.name = Path(path).name
@@ -137,7 +137,7 @@ def __init__(
137137
"""
138138
self.url = url
139139
self.branch = branch
140-
self.path = (
140+
self.realisation_path = (
141141
realisation_path / branch if realisation_path.is_dir() else realisation_path
142142
)
143143
self.commit = commit
@@ -149,11 +149,11 @@ def checkout(self):
149149
# remote progress. See
150150
# https://gitpython.readthedocs.io/en/stable/reference.html#git.remote.RemoteProgress
151151
self.subprocess_handler.run_cmd(
152-
f"git clone --branch {self.branch} -- {self.url} {self.path}"
152+
f"git clone --branch {self.branch} -- {self.url} {self.realisation_path}"
153153
)
154154
if self.commit:
155155
self.logger.debug(f"Reset to commit {self.commit} (hard reset)")
156-
repo = git.Repo(self.path)
156+
repo = git.Repo(self.realisation_path)
157157
repo.head.reset(self.commit, working_tree=True)
158158
self.logger.info(
159159
f"Successfully checked out {self.branch} - {self.get_revision()}"
@@ -168,7 +168,7 @@ def get_revision(self) -> str:
168168
Human readable string describing the latest revision.
169169
170170
"""
171-
repo = git.Repo(self.path)
171+
repo = git.Repo(self.realisation_path)
172172
return f"commit {repo.head.commit.hexsha}"
173173

174174
def get_branch_name(self) -> str:
@@ -223,7 +223,7 @@ def __init__(
223223
self.svn_root = svn_root
224224
self.branch_path = branch_path
225225
self.revision = revision
226-
self.path = (
226+
self.realisation_path = (
227227
realisation_path / Path(branch_path).name
228228
if realisation_path.is_dir()
229229
else realisation_path
@@ -237,12 +237,12 @@ def checkout(self):
237237
if self.revision:
238238
cmd += f" -r {self.revision}"
239239

240-
cmd += f" {internal.CABLE_SVN_ROOT}/{self.branch_path} {self.path}"
240+
cmd += f" {internal.CABLE_SVN_ROOT}/{self.branch_path} {self.realisation_path}"
241241

242242
self.subprocess_handler.run_cmd(cmd)
243243

244244
self.logger.info(
245-
f"Successfully checked out {self.path.name} - {self.get_revision()}"
245+
f"Successfully checked out {self.realisation_path.name} - {self.get_revision()}"
246246
)
247247

248248
def get_revision(self) -> str:
@@ -255,7 +255,7 @@ def get_revision(self) -> str:
255255
256256
"""
257257
proc = self.subprocess_handler.run_cmd(
258-
f"svn info --show-item last-changed-revision {self.path}",
258+
f"svn info --show-item last-changed-revision {self.realisation_path}",
259259
capture_output=True,
260260
)
261261
return f"last-changed-revision {proc.stdout.strip()}"

benchcab/workdir.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"""Functions for generating the directory structure used for `benchcab`."""
55

66
import shutil
7+
from pathlib import Path
78

89
from benchcab import internal
910
from benchcab.utils.fs import mkdir
@@ -17,16 +18,13 @@ def clean_realisation_files():
1718
realisation.unlink()
1819
shutil.rmtree(internal.SRC_DIR)
1920

20-
for rev_log_file in internal.CWD.glob("rev_number-*.log"):
21-
rev_log_file.unlink()
22-
2321

2422
def clean_submission_files():
2523
"""Remove files/directories related to PBS jobs."""
2624
if internal.RUN_DIR.exists():
2725
shutil.rmtree(internal.RUN_DIR)
2826

29-
for pbs_job_file in internal.CWD.glob(f"{internal.QSUB_FNAME}*"):
27+
for pbs_job_file in Path.cwd().glob(f"{internal.QSUB_FNAME}*"):
3028
pbs_job_file.unlink()
3129

3230

docs/user_guide/config_options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ realisations:
371371

372372
### [name](#name)
373373

374-
: **Default:** base name of [branch_path](#+repo.svn.branch_path) if an SVN repository is given, for Git repositories the default is the branch name, _optional key_. :octicons-dash-24: An alias name used internally by `benchcab` for the branch. The `name` key also specifies the directory name of the source code when retrieving from SVN or GitHub.
374+
: **Default:** base name of [branch_path](#+repo.svn.branch_path) if an SVN repository is given; the branch name if a git repository is given; the folder name if a local path is given, _optional key_. :octicons-dash-24: An alias name used internally by `benchcab` for the branch. The `name` key also specifies the directory name of the source code when retrieving from SVN, GitHub or local.
375375

376376
```yaml
377377
realisations:

docs/user_guide/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ The tool will follow the steps:
9595
In case the code branches are already checked out before running Step (1) - `benchcab` will fail. This could happen on re-runs of `benchcab`. In that case, run `benchcab clean realisations` before the `checkout` step.
9696

9797
!!! warning
98-
It is dangerous to delete `src/` via `rm -rf`, since `src/` may contain symlinks to local directories that could also be affected. Use `benchcab clean realisations` instead, which would also delete unecessary log files like `rev_number-*.log`.
98+
It is dangerous to delete `src/` via `rm -rf`, since `src/` may contain symlinks to local directories that could also be affected. Use `benchcab clean realisations` instead.
9999

100100
!!! tip "Expected output"
101101

tests/test_workdir.py

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import pytest
1212

13-
from benchcab import internal
1413
from benchcab.workdir import (
1514
clean_realisation_files,
1615
clean_submission_files,
@@ -63,48 +62,38 @@ def test_directory_structure_generated(self, spatial_directory_list):
6362
class TestCleanFiles:
6463
"""Tests for `clean_realisation_files()` and `clean_submission_files()`."""
6564

66-
# Reset internal.CWD to suit pytest testing infrastructure
67-
@pytest.fixture(autouse=True)
68-
def _set_internal_cwd(self, monkeypatch):
69-
"""Sets internal.CWD to pytest's working directory."""
70-
monkeypatch.setattr(internal, "CWD", Path.cwd())
71-
7265
# Helper functions
7366
def _create_files_in_cwd(self, filenames: List[str]):
7467
"""Given a list of filenames, create files in current working directory."""
7568
for filename in filenames:
76-
filename_path = internal.CWD / filename
69+
filename_path = Path(filename)
7770
filename_path.touch()
7871

7972
def _check_if_any_files_exist(self, filenames: List[str]):
8073
"""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)
74+
return any((filename).exists() for filename in filenames)
8275

8376
@pytest.fixture()
8477
def src_path(self) -> Path:
8578
"""Mock internal.SRC_DIR."""
86-
src_path = internal.CWD / Path("src")
79+
src_path = Path("src")
8780
src_path.mkdir()
8881
return src_path
8982

9083
@pytest.fixture()
9184
def runs_path(self) -> Path:
9285
"""Mock internal.RUN_DIR."""
93-
runs_path = internal.CWD / Path("runs")
86+
runs_path = Path("runs")
9487
runs_path.mkdir()
9588
return runs_path
9689

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
103-
10490
@pytest.fixture()
10591
def pbs_job_files(self) -> List[Path]:
10692
"""Create sample files of the form benchmark_cable_qsub.sh*."""
107-
pbs_job_files = ["benchmark_cable_qsub.sh.o21871", "benchmark_cable_qsub.sh"]
93+
pbs_job_files = [
94+
Path("benchmark_cable_qsub.sh.o21871"),
95+
Path("benchmark_cable_qsub.sh"),
96+
]
10897
self._create_files_in_cwd(pbs_job_files)
10998
return pbs_job_files
11099

@@ -134,21 +123,16 @@ def test_clean_realisation_files_local(
134123
self,
135124
local_cable_src_path: Path,
136125
src_path_with_local: Path,
137-
revision_log_files: List[Path],
138126
):
139127
"""Success case: Local realisation files created by benchcab are removed after clean."""
140128
clean_realisation_files()
141129
assert local_cable_src_path.exists()
142130
assert not src_path_with_local.exists()
143-
assert not self._check_if_any_files_exist(revision_log_files)
144131

145-
def test_clean_realisation_files_git(
146-
self, src_path_with_git: Path, revision_log_files: Path
147-
):
132+
def test_clean_realisation_files_git(self, src_path_with_git: Path):
148133
"""Success case: Git realisation files created by benchcab are removed after clean."""
149134
clean_realisation_files()
150135
assert not src_path_with_git.exists()
151-
assert not self._check_if_any_files_exist(revision_log_files)
152136

153137
def test_clean_submission_files(self, runs_path, pbs_job_files: List[Path]):
154138
"""Success case: Submission files created by benchcab are removed after clean."""

0 commit comments

Comments
 (0)