Skip to content

Commit 9845675

Browse files
committed
Add tests of validate_directory().
1 parent efebdda commit 9845675

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

tests/test_relink.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import shutil
99
import pwd
1010
import logging
11+
import argparse
1112
from unittest.mock import patch
1213

1314
import pytest
@@ -526,6 +527,67 @@ def test_timing_logging(self, tmp_path, caplog, use_timing, should_log_timing):
526527
assert "Execution time:" not in caplog.text
527528

528529

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+
529591
class TestDryRun:
530592
"""Test suite for dry-run functionality."""
531593

0 commit comments

Comments
 (0)