From 1701fdfec6bd917e6afe649f4269c9a501ba842d Mon Sep 17 00:00:00 2001 From: Claire Carouge Date: Thu, 5 Oct 2023 11:58:59 +1100 Subject: [PATCH 1/2] #149- Move next_path() to fs.py --- benchcab/benchcab.py | 2 +- benchcab/utils/fs.py | 23 +++++++++++++++++++++++ benchcab/utils/logging.py | 26 -------------------------- tests/{test_logging.py => test_fs.py} | 4 ++-- 4 files changed, 26 insertions(+), 29 deletions(-) delete mode 100644 benchcab/utils/logging.py rename tests/{test_logging.py => test_fs.py} (85%) diff --git a/benchcab/benchcab.py b/benchcab/benchcab.py index c6fc1fcd..25c4bf73 100644 --- a/benchcab/benchcab.py +++ b/benchcab/benchcab.py @@ -25,7 +25,7 @@ from benchcab.environment_modules import EnvironmentModules, EnvironmentModulesInterface from benchcab.utils.subprocess import SubprocessWrapper, SubprocessWrapperInterface from benchcab.utils.pbs import render_job_script -from benchcab.utils.logging import next_path +from benchcab.utils.fs import next_path class Benchcab: diff --git a/benchcab/utils/fs.py b/benchcab/utils/fs.py index 36027efe..300377e4 100644 --- a/benchcab/utils/fs.py +++ b/benchcab/utils/fs.py @@ -29,3 +29,26 @@ def copy2(src: Path, dest: Path, verbose=False): if verbose: print(f"cp -p {src} {dest}") shutil.copy2(src, dest) + + +def next_path(path: Path, path_pattern: str, sep: str = "-"): + """Finds the next free path in a sequentially named list of + files with the following pattern in the `path` directory: + + path_pattern = 'file{sep}*.suf': + + file-1.txt + file-2.txt + file-3.txt + """ + + loc_pattern = Path(path_pattern) + new_file_index = 1 + common_filename, _ = loc_pattern.stem.split(sep) + + pattern_files_sorted = sorted(path.glob(path_pattern)) + if pattern_files_sorted != []: + common_filename, last_file_index = pattern_files_sorted[-1].stem.split(sep) + new_file_index = int(last_file_index) + 1 + + return f"{common_filename}{sep}{new_file_index}{loc_pattern.suffix}" diff --git a/benchcab/utils/logging.py b/benchcab/utils/logging.py deleted file mode 100644 index 06200a5c..00000000 --- a/benchcab/utils/logging.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Contains helper functions for logging.""" - -from pathlib import Path - - -def next_path(path: Path, path_pattern: str, sep: str = "-"): - """Finds the next free path in a sequentially named list of - files with the following pattern in the `path` directory: - - path_pattern = 'file{sep}*.suf': - - file-1.txt - file-2.txt - file-3.txt - """ - - loc_pattern = Path(path_pattern) - new_file_index = 1 - common_filename, _ = loc_pattern.stem.split(sep) - - pattern_files_sorted = sorted(path.glob(path_pattern)) - if pattern_files_sorted != []: - common_filename, last_file_index = pattern_files_sorted[-1].stem.split(sep) - new_file_index = int(last_file_index) + 1 - - return f"{common_filename}{sep}{new_file_index}{loc_pattern.suffix}" diff --git a/tests/test_logging.py b/tests/test_fs.py similarity index 85% rename from tests/test_logging.py rename to tests/test_fs.py index 8af56e01..1b1634da 100644 --- a/tests/test_logging.py +++ b/tests/test_fs.py @@ -1,6 +1,6 @@ -"""`pytest` tests for utils/logging.py""" +"""`pytest` tests for utils/fs.py""" -from benchcab.utils.logging import next_path +from benchcab.utils.fs import next_path from .common import MOCK_CWD From d3cd1a1229533ab17c55058a1c2496a407591e9b Mon Sep 17 00:00:00 2001 From: Claire Carouge Date: Thu, 5 Oct 2023 12:12:17 +1100 Subject: [PATCH 2/2] #149- Change `assert all()` to individual asserts --- tests/test_repository.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_repository.py b/tests/test_repository.py index a578665e..2067d9cf 100644 --- a/tests/test_repository.py +++ b/tests/test_repository.py @@ -158,6 +158,15 @@ def test_run_build(): mock_modules = ["foo", "bar"] (MOCK_CWD / internal.SRC_DIR / "trunk" / "offline" / ".tmp").mkdir(parents=True) + environment_vars = { + "NCDIR": f"{mock_netcdf_root}/lib/Intel", + "NCMOD": f"{mock_netcdf_root}/include/Intel", + "CFLAGS": "-O2 -fp-model precise", + "LDFLAGS": f"-L{mock_netcdf_root}/lib/Intel -O0", + "LD": "-lnetcdf -lnetcdff", + "FC": "ifort", + } + # This is required so that we can use the NETCDF_ROOT environment variable # when running `make`, and `serial_cable` and `parallel_cable` scripts: os.environ["NETCDF_ROOT"] = mock_netcdf_root @@ -188,17 +197,8 @@ def test_run_build(): mock_subprocess = MockSubprocessWrapper() repo = get_mock_repo(subprocess_handler=mock_subprocess) repo.run_build(mock_modules) - assert all( - kv in mock_subprocess.env.items() - for kv in { - "NCDIR": f"{mock_netcdf_root}/lib/Intel", - "NCMOD": f"{mock_netcdf_root}/include/Intel", - "CFLAGS": "-O2 -fp-model precise", - "LDFLAGS": f"-L{mock_netcdf_root}/lib/Intel -O0", - "LD": "-lnetcdf -lnetcdff", - "FC": "ifort", - }.items() - ) + for kv in environment_vars.items(): + assert (kv in mock_subprocess.env.items()) # Success case: test non-verbose standard output repo = get_mock_repo()