| 1 | from __future__ import annotations |
| 2 | from dataclasses import asdict |
| 3 | import logging |
| 4 | import os |
| 5 | import re |
| 6 | from unittest.mock import MagicMock, call, patch |
| 7 | import pytest |
| 8 | from con_duct._models import LogPaths, Outputs |
| 9 | |
| 10 | |
| 11 | def test_log_paths_datetime_prefix() -> None: |
| 12 | log_paths = LogPaths.create("start_{datetime}") |
| 13 | pattern = r"^start_\d{4}\.\d{2}\.\d{2}T\d{2}\.\d{2}\.\d{2}.*" |
| 14 | for path in asdict(log_paths).values(): |
| 15 | assert re.match(pattern, path) is not None |
| 16 | |
| 17 | |
| 18 | def test_log_paths_deprecated_datetime_filesafe_prefix( |
| 19 | caplog: pytest.LogCaptureFixture, |
| 20 | ) -> None: |
| 21 | """Ensure deprecated {datetime_filesafe} format field still works and warns.""" |
| 22 | with caplog.at_level(logging.WARNING): |
| 23 | log_paths = LogPaths.create("start_{datetime_filesafe}") |
| 24 | pattern = r"^start_\d{4}\.\d{2}\.\d{2}T\d{2}\.\d{2}\.\d{2}.*" |
| 25 | for path in asdict(log_paths).values(): |
| 26 | assert re.match(pattern, path) is not None |
| 27 | assert "{datetime_filesafe}" in caplog.text |
| 28 | |
| 29 | |
| 30 | def test_log_paths_no_warning_for_escaped_datetime_filesafe( |
| 31 | caplog: pytest.LogCaptureFixture, |
| 32 | ) -> None: |
| 33 | """Escaped braces {{datetime_filesafe}} are literal text; no warning should fire.""" |
| 34 | with caplog.at_level(logging.WARNING): |
| 35 | LogPaths.create("prefix_{{datetime_filesafe}}_suffix_") |
| 36 | assert "{datetime_filesafe}" not in caplog.text |
| 37 | |
| 38 | |
| 39 | def test_log_paths_datetime_filesafe_warning_with_format_spec( |
| 40 | caplog: pytest.LogCaptureFixture, |
| 41 | ) -> None: |
| 42 | """Warning fires when {datetime_filesafe} has a format spec (substring check would miss this).""" |
| 43 | with caplog.at_level(logging.WARNING): |
| 44 | LogPaths.create("padded_{datetime_filesafe:>20}") |
| 45 | assert "{datetime_filesafe}" in caplog.text |
| 46 | |
| 47 | |
| 48 | def test_log_paths_pid_prefix() -> None: |
| 49 | prefix = "prefix_{pid}_" |
| 50 | log_paths = LogPaths.create(prefix, pid=123456) |
| 51 | assert log_paths.prefix == prefix.format(pid=123456) |
| 52 | |
| 53 | |
| 54 | @pytest.mark.parametrize( |
| 55 | "path", |
| 56 | [ |
| 57 | "directory/", |
| 58 | "nested/directory/", |
| 59 | "/abs/path/", |
| 60 | ], |
| 61 | ) |
| 62 | @patch("con_duct._duct_main.os.makedirs") |
| 63 | @patch("con_duct._duct_main.os.path.exists") |
| 64 | @patch("builtins.open") |
| 65 | def test_prepare_dir_paths_available( |
| 66 | _mock_open: MagicMock, mock_exists: MagicMock, mock_mkdir: MagicMock, path: str |
| 67 | ) -> None: |
| 68 | mock_exists.return_value = False |
| 69 | log_paths = LogPaths.create(path) |
| 70 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.ALL) |
| 71 | mock_mkdir.assert_called_once_with(path, exist_ok=True) |
| 72 | |
| 73 | |
| 74 | @pytest.mark.parametrize( |
| 75 | "path", |
| 76 | [ |
| 77 | "directory/pre_", |
| 78 | "nested/directory/pre_", |
| 79 | "/abs/path/pre_", |
| 80 | ], |
| 81 | ) |
| 82 | @patch("con_duct._duct_main.os.path.exists") |
| 83 | @patch("con_duct._duct_main.os.makedirs") |
| 84 | @patch("builtins.open") |
| 85 | def test_prefix_with_filepart_and_directory_part( |
| 86 | mock_open: MagicMock, mock_mkdir: MagicMock, mock_exists: MagicMock, path: str |
| 87 | ) -> None: |
| 88 | mock_exists.return_value = False |
| 89 | log_paths = LogPaths.create(path) |
| 90 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.ALL) |
| 91 | mock_mkdir.assert_called_once_with(os.path.dirname(path), exist_ok=True) |
| 92 | expected_calls = [call(each, "w") for _name, each in log_paths] |
| 93 | mock_open.assert_has_calls(expected_calls, any_order=True) |
| 94 | |
| 95 | |
| 96 | @patch("con_duct._duct_main.os.path.exists") |
| 97 | @patch("con_duct._duct_main.os.makedirs") |
| 98 | @patch("builtins.open") |
| 99 | def test_prefix_with_filepart_only( |
| 100 | mock_open: MagicMock, mock_mkdir: MagicMock, mock_exists: MagicMock |
| 101 | ) -> None: |
| 102 | mock_exists.return_value = False |
| 103 | log_paths = LogPaths.create("filepartonly") |
| 104 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.ALL) |
| 105 | mock_mkdir.assert_not_called() |
| 106 | expected_calls = [call(each, "w") for _name, each in log_paths] |
| 107 | mock_open.assert_has_calls(expected_calls, any_order=True) |
| 108 | |
| 109 | |
| 110 | @patch("con_duct._duct_main.os.path.exists") |
| 111 | @patch("con_duct._duct_main.os.makedirs") |
| 112 | @patch("builtins.open") |
| 113 | def test_prepare_file_paths_available_all( |
| 114 | mock_open: MagicMock, _mock_mkdir: MagicMock, mock_exists: MagicMock |
| 115 | ) -> None: |
| 116 | mock_exists.return_value = False |
| 117 | prefix = "prefix_" |
| 118 | log_paths = LogPaths.create(prefix) |
| 119 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.ALL) |
| 120 | expected_calls = [call(each, "w") for _name, each in log_paths] |
| 121 | mock_open.assert_has_calls(expected_calls, any_order=True) |
| 122 | |
| 123 | |
| 124 | @patch("con_duct._duct_main.os.path.exists") |
| 125 | @patch("con_duct._duct_main.os.makedirs") |
| 126 | @patch("builtins.open") |
| 127 | def test_prepare_file_paths_available_stdout( |
| 128 | mock_open: MagicMock, _mock_mkdir: MagicMock, mock_exists: MagicMock |
| 129 | ) -> None: |
| 130 | mock_exists.return_value = False |
| 131 | prefix = "prefix_" |
| 132 | log_paths = LogPaths.create(prefix) |
| 133 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.STDOUT) |
| 134 | expected_calls = [ |
| 135 | call(each, "w") for name, each in log_paths if name != Outputs.STDERR |
| 136 | ] |
| 137 | mock_open.assert_has_calls(expected_calls, any_order=True) |
| 138 | |
| 139 | |
| 140 | @patch("con_duct._duct_main.os.path.exists") |
| 141 | @patch("con_duct._duct_main.os.makedirs") |
| 142 | @patch("builtins.open") |
| 143 | def test_prepare_file_paths_available_stderr( |
| 144 | mock_open: MagicMock, _mock_mkdir: MagicMock, mock_exists: MagicMock |
| 145 | ) -> None: |
| 146 | mock_exists.return_value = False |
| 147 | prefix = "prefix_" |
| 148 | log_paths = LogPaths.create(prefix) |
| 149 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.STDERR) |
| 150 | expected_calls = [ |
| 151 | call(each, "w") for name, each in log_paths if name != Outputs.STDOUT |
| 152 | ] |
| 153 | mock_open.assert_has_calls(expected_calls, any_order=True) |
| 154 | |
| 155 | |
| 156 | @patch("con_duct._duct_main.os.path.exists") |
| 157 | @patch("con_duct._duct_main.os.makedirs") |
| 158 | @patch("builtins.open") |
| 159 | def test_prepare_file_paths_available_no_streams( |
| 160 | mock_open: MagicMock, _mock_mkdir: MagicMock, mock_exists: MagicMock |
| 161 | ) -> None: |
| 162 | mock_exists.return_value = False |
| 163 | prefix = "prefix_" |
| 164 | log_paths = LogPaths.create(prefix) |
| 165 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.NONE) |
| 166 | streams = [Outputs.STDOUT, Outputs.STDERR] |
| 167 | expected_calls = [ |
| 168 | call(each, "w") for name, each in log_paths if name not in streams |
| 169 | ] |
| 170 | mock_open.assert_has_calls(expected_calls, any_order=True) |
| 171 | |
| 172 | |
| 173 | @patch("con_duct._duct_main.os.makedirs") |
| 174 | @patch("con_duct._duct_main.os.path.exists") |
| 175 | @patch("builtins.open") |
| 176 | def test_prepare_paths_not_available_no_clobber( |
| 177 | mock_open: MagicMock, mock_exists: MagicMock, mock_mkdir: MagicMock |
| 178 | ) -> None: |
| 179 | mock_exists.return_value = True |
| 180 | log_paths = LogPaths.create("doesntmatter") |
| 181 | with pytest.raises(FileExistsError): |
| 182 | log_paths.prepare_paths(clobber=False, capture_outputs=Outputs.ALL) |
| 183 | mock_mkdir.assert_not_called() |
| 184 | mock_open.assert_not_called() |