|
8 | 8 | import shutil |
9 | 9 | import pwd |
10 | 10 | import logging |
| 11 | +import argparse |
11 | 12 | from unittest.mock import patch |
12 | 13 |
|
13 | 14 | import pytest |
@@ -526,6 +527,67 @@ def test_timing_logging(self, tmp_path, caplog, use_timing, should_log_timing): |
526 | 527 | assert "Execution time:" not in caplog.text |
527 | 528 |
|
528 | 529 |
|
| 530 | +class TestValidateDirectory: |
| 531 | + """Test suite for validate_directory function.""" |
| 532 | + |
| 533 | + def test_valid_directory(self, tmp_path): |
| 534 | + """Test that valid directory is accepted and returns absolute path.""" |
| 535 | + test_dir = tmp_path / "valid_dir" |
| 536 | + test_dir.mkdir() |
| 537 | + |
| 538 | + result = relink.validate_directory(str(test_dir)) |
| 539 | + assert result == str(test_dir.resolve()) |
| 540 | + |
| 541 | + def test_nonexistent_directory(self): |
| 542 | + """Test that nonexistent directory raises ArgumentTypeError.""" |
| 543 | + nonexistent = os.path.join(os.sep, "nonexistent", "directory", "12345") |
| 544 | + |
| 545 | + with pytest.raises(argparse.ArgumentTypeError) as exc_info: |
| 546 | + relink.validate_directory(nonexistent) |
| 547 | + |
| 548 | + assert "does not exist" in str(exc_info.value) |
| 549 | + assert nonexistent in str(exc_info.value) |
| 550 | + |
| 551 | + def test_file_instead_of_directory(self, tmp_path): |
| 552 | + """Test that a file path raises ArgumentTypeError.""" |
| 553 | + test_file = tmp_path / "test_file.txt" |
| 554 | + test_file.write_text("content") |
| 555 | + |
| 556 | + with pytest.raises(argparse.ArgumentTypeError) as exc_info: |
| 557 | + relink.validate_directory(str(test_file)) |
| 558 | + |
| 559 | + assert "not a directory" in str(exc_info.value) |
| 560 | + |
| 561 | + def test_relative_path_converted_to_absolute(self, tmp_path): |
| 562 | + """Test that relative paths are converted to absolute.""" |
| 563 | + test_dir = tmp_path / "relative_test" |
| 564 | + test_dir.mkdir() |
| 565 | + |
| 566 | + # Change to parent directory and use relative path |
| 567 | + cwd = os.getcwd() |
| 568 | + try: |
| 569 | + os.chdir(str(tmp_path)) |
| 570 | + result = relink.validate_directory("relative_test") |
| 571 | + assert os.path.isabs(result) |
| 572 | + assert result == str(test_dir.resolve()) |
| 573 | + finally: |
| 574 | + os.chdir(cwd) |
| 575 | + |
| 576 | + def test_symlink_to_directory(self, tmp_path): |
| 577 | + """Test that symlink to a directory is accepted.""" |
| 578 | + real_dir = tmp_path / "real_dir" |
| 579 | + real_dir.mkdir() |
| 580 | + |
| 581 | + link_dir = tmp_path / "link_dir" |
| 582 | + link_dir.symlink_to(real_dir) |
| 583 | + |
| 584 | + result = relink.validate_directory(str(link_dir)) |
| 585 | + # validate_directory returns absolute path of the symlink itself |
| 586 | + assert result == str(link_dir.absolute()) |
| 587 | + # Verify it's still a symlink |
| 588 | + assert os.path.islink(result) |
| 589 | + |
| 590 | + |
529 | 591 | class TestDryRun: |
530 | 592 | """Test suite for dry-run functionality.""" |
531 | 593 |
|
|
0 commit comments