|
| 1 | +"""Tests for CLI main() function and execution paths. |
| 2 | +
|
| 3 | +This module tests the main entry point and error handling. |
| 4 | +""" |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import MagicMock, Mock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from shadowfs.cli import CLIError, main |
| 12 | + |
| 13 | + |
| 14 | +class TestMainFunction: |
| 15 | + """Tests for main() function execution.""" |
| 16 | + |
| 17 | + def test_main_with_config_file(self, tmpdir): |
| 18 | + """Test main() with config file specified.""" |
| 19 | + config_file = tmpdir / "config.yaml" |
| 20 | + config_file.write_text("shadowfs:\n version: '1.0'\n", encoding="utf-8") |
| 21 | + |
| 22 | + mount_point = tmpdir / "mount" |
| 23 | + mount_point.mkdir() |
| 24 | + source_dir = tmpdir / "source" |
| 25 | + source_dir.mkdir() |
| 26 | + |
| 27 | + test_args = [ |
| 28 | + "shadowfs", |
| 29 | + str(source_dir), |
| 30 | + str(mount_point), |
| 31 | + "--config", |
| 32 | + str(config_file), |
| 33 | + "--foreground", |
| 34 | + ] |
| 35 | + |
| 36 | + with patch.object(sys, "argv", test_args): |
| 37 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 38 | + mock_run.return_value = 0 |
| 39 | + result = main() |
| 40 | + |
| 41 | + assert result == 0 |
| 42 | + assert mock_run.called |
| 43 | + |
| 44 | + def test_main_without_config_file(self, tmpdir): |
| 45 | + """Test main() without config file (build from args).""" |
| 46 | + mount_point = tmpdir / "mount" |
| 47 | + mount_point.mkdir() |
| 48 | + source_dir = tmpdir / "source" |
| 49 | + source_dir.mkdir() |
| 50 | + |
| 51 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "--foreground"] |
| 52 | + |
| 53 | + with patch.object(sys, "argv", test_args): |
| 54 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 55 | + mock_run.return_value = 0 |
| 56 | + result = main() |
| 57 | + |
| 58 | + assert result == 0 |
| 59 | + assert mock_run.called |
| 60 | + |
| 61 | + def test_main_cli_error(self, tmpdir): |
| 62 | + """Test main() handles CLIError.""" |
| 63 | + test_args = ["shadowfs", "--invalid-arg"] |
| 64 | + |
| 65 | + with patch.object(sys, "argv", test_args): |
| 66 | + with patch("shadowfs.cli.parse_arguments") as mock_parse: |
| 67 | + mock_parse.side_effect = CLIError("Invalid argument") |
| 68 | + result = main() |
| 69 | + |
| 70 | + assert result == 1 |
| 71 | + |
| 72 | + def test_main_keyboard_interrupt(self, tmpdir): |
| 73 | + """Test main() handles KeyboardInterrupt.""" |
| 74 | + mount_point = tmpdir / "mount" |
| 75 | + mount_point.mkdir() |
| 76 | + source_dir = tmpdir / "source" |
| 77 | + source_dir.mkdir() |
| 78 | + |
| 79 | + test_args = ["shadowfs", str(source_dir), str(mount_point)] |
| 80 | + |
| 81 | + with patch.object(sys, "argv", test_args): |
| 82 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 83 | + mock_run.side_effect = KeyboardInterrupt() |
| 84 | + result = main() |
| 85 | + |
| 86 | + assert result == 130 |
| 87 | + |
| 88 | + def test_main_unexpected_error(self, tmpdir): |
| 89 | + """Test main() handles unexpected exceptions.""" |
| 90 | + mount_point = tmpdir / "mount" |
| 91 | + mount_point.mkdir() |
| 92 | + source_dir = tmpdir / "source" |
| 93 | + source_dir.mkdir() |
| 94 | + |
| 95 | + test_args = ["shadowfs", str(source_dir), str(mount_point)] |
| 96 | + |
| 97 | + with patch.object(sys, "argv", test_args): |
| 98 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 99 | + mock_run.side_effect = RuntimeError("Unexpected error") |
| 100 | + result = main() |
| 101 | + |
| 102 | + assert result == 1 |
| 103 | + |
| 104 | + |
| 105 | +class TestArgumentParsingEdgeCases: |
| 106 | + """Tests for argument parsing edge cases.""" |
| 107 | + |
| 108 | + def test_positional_source_takes_precedence(self, tmpdir): |
| 109 | + """Test positional source overrides --sources.""" |
| 110 | + source1 = tmpdir / "source1" |
| 111 | + source1.mkdir() |
| 112 | + source2 = tmpdir / "source2" |
| 113 | + source2.mkdir() |
| 114 | + mount_point = tmpdir / "mount" |
| 115 | + mount_point.mkdir() |
| 116 | + |
| 117 | + test_args = [ |
| 118 | + "shadowfs", |
| 119 | + str(source1), # positional |
| 120 | + str(mount_point), |
| 121 | + "--sources", |
| 122 | + str(source2), # flag |
| 123 | + ] |
| 124 | + |
| 125 | + with patch.object(sys, "argv", test_args): |
| 126 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 127 | + mock_run.return_value = 0 |
| 128 | + main() |
| 129 | + |
| 130 | + # Should use positional source1 |
| 131 | + call_args = mock_run.call_args |
| 132 | + config = call_args[0][1] |
| 133 | + assert config["sources"][0]["path"] == str(source1) |
| 134 | + |
| 135 | + def test_sources_flag_only(self, tmpdir): |
| 136 | + """Test --sources flag without positional.""" |
| 137 | + source_dir = tmpdir / "source" |
| 138 | + source_dir.mkdir() |
| 139 | + mount_point = tmpdir / "mount" |
| 140 | + mount_point.mkdir() |
| 141 | + |
| 142 | + test_args = ["shadowfs", "--sources", str(source_dir), "--mount-point", str(mount_point)] |
| 143 | + |
| 144 | + with patch.object(sys, "argv", test_args): |
| 145 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 146 | + mock_run.return_value = 0 |
| 147 | + main() |
| 148 | + |
| 149 | + assert mock_run.called |
| 150 | + |
| 151 | + def test_positional_mount_takes_precedence(self, tmpdir): |
| 152 | + """Test positional mount overrides --mount-point.""" |
| 153 | + source_dir = tmpdir / "source" |
| 154 | + source_dir.mkdir() |
| 155 | + mount1 = tmpdir / "mount1" |
| 156 | + mount1.mkdir() |
| 157 | + mount2 = tmpdir / "mount2" |
| 158 | + mount2.mkdir() |
| 159 | + |
| 160 | + test_args = [ |
| 161 | + "shadowfs", |
| 162 | + str(source_dir), |
| 163 | + str(mount1), # positional |
| 164 | + "--mount-point", |
| 165 | + str(mount2), # flag |
| 166 | + ] |
| 167 | + |
| 168 | + with patch.object(sys, "argv", test_args): |
| 169 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 170 | + mock_run.return_value = 0 |
| 171 | + main() |
| 172 | + |
| 173 | + # Should use positional mount1 |
| 174 | + call_args = mock_run.call_args |
| 175 | + args = call_args[0][0] |
| 176 | + assert str(mount1) in str(args.mount_point) |
| 177 | + |
| 178 | + def test_mount_options_ro(self, tmpdir): |
| 179 | + """Test -o ro option sets read-only.""" |
| 180 | + source_dir = tmpdir / "source" |
| 181 | + source_dir.mkdir() |
| 182 | + mount_point = tmpdir / "mount" |
| 183 | + mount_point.mkdir() |
| 184 | + |
| 185 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "ro"] |
| 186 | + |
| 187 | + with patch.object(sys, "argv", test_args): |
| 188 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 189 | + mock_run.return_value = 0 |
| 190 | + main() |
| 191 | + |
| 192 | + call_args = mock_run.call_args |
| 193 | + args = call_args[0][0] |
| 194 | + assert args.read_write is False |
| 195 | + |
| 196 | + def test_mount_options_rw(self, tmpdir): |
| 197 | + """Test -o rw option sets read-write.""" |
| 198 | + source_dir = tmpdir / "source" |
| 199 | + source_dir.mkdir() |
| 200 | + mount_point = tmpdir / "mount" |
| 201 | + mount_point.mkdir() |
| 202 | + |
| 203 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "rw"] |
| 204 | + |
| 205 | + with patch.object(sys, "argv", test_args): |
| 206 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 207 | + mock_run.return_value = 0 |
| 208 | + main() |
| 209 | + |
| 210 | + call_args = mock_run.call_args |
| 211 | + args = call_args[0][0] |
| 212 | + assert args.read_write is True |
| 213 | + |
| 214 | + def test_mount_options_allow_other(self, tmpdir): |
| 215 | + """Test -o allow_other option.""" |
| 216 | + source_dir = tmpdir / "source" |
| 217 | + source_dir.mkdir() |
| 218 | + mount_point = tmpdir / "mount" |
| 219 | + mount_point.mkdir() |
| 220 | + |
| 221 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "allow_other"] |
| 222 | + |
| 223 | + with patch.object(sys, "argv", test_args): |
| 224 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 225 | + mock_run.return_value = 0 |
| 226 | + main() |
| 227 | + |
| 228 | + call_args = mock_run.call_args |
| 229 | + args = call_args[0][0] |
| 230 | + assert args.allow_other is True |
| 231 | + |
| 232 | + def test_mount_options_debug(self, tmpdir): |
| 233 | + """Test -o debug option.""" |
| 234 | + source_dir = tmpdir / "source" |
| 235 | + source_dir.mkdir() |
| 236 | + mount_point = tmpdir / "mount" |
| 237 | + mount_point.mkdir() |
| 238 | + |
| 239 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "debug"] |
| 240 | + |
| 241 | + with patch.object(sys, "argv", test_args): |
| 242 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 243 | + mock_run.return_value = 0 |
| 244 | + main() |
| 245 | + |
| 246 | + call_args = mock_run.call_args |
| 247 | + args = call_args[0][0] |
| 248 | + assert args.debug is True |
| 249 | + |
| 250 | + def test_mount_options_foreground(self, tmpdir): |
| 251 | + """Test -o foreground option.""" |
| 252 | + source_dir = tmpdir / "source" |
| 253 | + source_dir.mkdir() |
| 254 | + mount_point = tmpdir / "mount" |
| 255 | + mount_point.mkdir() |
| 256 | + |
| 257 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "foreground"] |
| 258 | + |
| 259 | + with patch.object(sys, "argv", test_args): |
| 260 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 261 | + mock_run.return_value = 0 |
| 262 | + main() |
| 263 | + |
| 264 | + call_args = mock_run.call_args |
| 265 | + args = call_args[0][0] |
| 266 | + assert args.foreground is True |
| 267 | + |
| 268 | + def test_mount_options_f_shorthand(self, tmpdir): |
| 269 | + """Test -o f (shorthand for foreground) option.""" |
| 270 | + source_dir = tmpdir / "source" |
| 271 | + source_dir.mkdir() |
| 272 | + mount_point = tmpdir / "mount" |
| 273 | + mount_point.mkdir() |
| 274 | + |
| 275 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "f"] |
| 276 | + |
| 277 | + with patch.object(sys, "argv", test_args): |
| 278 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 279 | + mock_run.return_value = 0 |
| 280 | + main() |
| 281 | + |
| 282 | + call_args = mock_run.call_args |
| 283 | + args = call_args[0][0] |
| 284 | + assert args.foreground is True |
| 285 | + |
| 286 | + def test_mount_options_multiple(self, tmpdir): |
| 287 | + """Test multiple mount options.""" |
| 288 | + source_dir = tmpdir / "source" |
| 289 | + source_dir.mkdir() |
| 290 | + mount_point = tmpdir / "mount" |
| 291 | + mount_point.mkdir() |
| 292 | + |
| 293 | + test_args = ["shadowfs", str(source_dir), str(mount_point), "-o", "ro,allow_other,debug"] |
| 294 | + |
| 295 | + with patch.object(sys, "argv", test_args): |
| 296 | + with patch("shadowfs.main.run_shadowfs") as mock_run: |
| 297 | + mock_run.return_value = 0 |
| 298 | + main() |
| 299 | + |
| 300 | + call_args = mock_run.call_args |
| 301 | + args = call_args[0][0] |
| 302 | + assert args.read_write is False |
| 303 | + assert args.allow_other is True |
| 304 | + assert args.debug is True |
| 305 | + |
| 306 | + |
| 307 | +class TestDiscoverConfigSystemPath: |
| 308 | + """Tests for system config discovery.""" |
| 309 | + |
| 310 | + def test_discovers_system_config_if_exists(self): |
| 311 | + """Test discovers system config at /etc/shadowfs/config.yaml.""" |
| 312 | + from shadowfs.cli import discover_config |
| 313 | + |
| 314 | + with patch("pathlib.Path.exists") as mock_exists: |
| 315 | + # System config exists |
| 316 | + mock_exists.return_value = True |
| 317 | + |
| 318 | + with patch("pathlib.Path.__new__") as mock_path_new: |
| 319 | + # Make Path("/etc/shadowfs/config.yaml").exists() return True |
| 320 | + def path_new(cls, *args): |
| 321 | + if args and args[0] == "/etc/shadowfs/config.yaml": |
| 322 | + mock_path = Mock() |
| 323 | + mock_path.exists.return_value = True |
| 324 | + mock_path.__str__ = lambda self: "/etc/shadowfs/config.yaml" |
| 325 | + return mock_path |
| 326 | + return object.__new__(cls) |
| 327 | + |
| 328 | + mock_path_new.side_effect = path_new |
| 329 | + |
| 330 | + # This is complex to test without actual filesystem |
| 331 | + # The function will check /etc first, which we can't easily mock |
| 332 | + # For now, accept this limitation |
| 333 | + result = discover_config() |
| 334 | + |
| 335 | + # Result depends on actual filesystem |
| 336 | + assert result is None or "/etc/shadowfs/config.yaml" in str(result) |
0 commit comments