|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | from pathlib import Path |
| 9 | +from typing import List |
9 | 10 |
|
10 | 11 | import pytest |
11 | 12 |
|
@@ -60,52 +61,97 @@ def test_directory_structure_generated(self, spatial_directory_list): |
60 | 61 |
|
61 | 62 |
|
62 | 63 | class TestCleanFiles: |
63 | | - """Tests for `clean_directory_tree()` and `clean_cwd_logfiles).""" |
| 64 | + """Tests for `clean_realisation_files()` and `clean_submission_files()`.""" |
64 | 65 |
|
| 66 | + # Reset internal.CWD to suit pytest testing infrastructure |
65 | 67 | @pytest.fixture(autouse=True) |
66 | | - def set_internal_cwd(self, monkeypatch): |
| 68 | + def _set_internal_cwd(self, monkeypatch): |
67 | 69 | """Sets internal.CWD to pytest's working directory.""" |
68 | 70 | monkeypatch.setattr(internal, "CWD", Path.cwd()) |
69 | 71 |
|
| 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 | + |
70 | 83 | @pytest.fixture() |
71 | 84 | 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 |
73 | 89 |
|
74 | 90 | @pytest.fixture() |
75 | 91 | 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 |
77 | 96 |
|
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 |
81 | 103 |
|
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 |
87 | 110 |
|
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 |
95 | 115 | local_cable_src_path = tmp_path / "CABLE" |
96 | 116 | 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" |
97 | 123 | cable_symlink.symlink_to(local_cable_src_path) |
98 | | - revision_log_file.touch() |
99 | | - clean_realisation_files() |
| 124 | + return src_path |
100 | 125 |
|
| 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() |
101 | 141 | 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) |
104 | 144 |
|
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]): |
106 | 154 | """Success case: Submission files created by benchcab are removed after clean.""" |
107 | | - runs_path.mkdir() |
108 | | - pbs_job_file.touch() |
109 | 155 | clean_submission_files() |
110 | 156 | 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