|
9 | 9 | import pwd |
10 | 10 | import logging |
11 | 11 | import argparse |
| 12 | +import subprocess |
12 | 13 | from unittest.mock import patch |
13 | 14 |
|
14 | 15 | import pytest |
@@ -902,3 +903,89 @@ def test_dry_run_no_delete_or_create_messages(self, dry_run_setup, caplog): |
902 | 903 | assert "Created symbolic link:" not in caplog.text |
903 | 904 | # But the dry-run message should be there |
904 | 905 | assert "[DRY RUN] Would create symbolic link: " in caplog.text |
| 906 | + |
| 907 | + |
| 908 | +class TestCommandLineExecution: |
| 909 | + """Test suite for command-line execution of relink.py.""" |
| 910 | + |
| 911 | + @pytest.fixture |
| 912 | + def mock_dirs(self, tmp_path): |
| 913 | + """Create temporary directories and files for command-line testing.""" |
| 914 | + source_dir = tmp_path / "source" |
| 915 | + target_dir = tmp_path / "target" |
| 916 | + source_dir.mkdir() |
| 917 | + target_dir.mkdir() |
| 918 | + |
| 919 | + # Create a test file |
| 920 | + source_file = source_dir / "test_file.txt" |
| 921 | + target_file = target_dir / "test_file.txt" |
| 922 | + source_file.write_text("source content") |
| 923 | + target_file.write_text("target content") |
| 924 | + |
| 925 | + return source_dir, target_dir, source_file, target_file |
| 926 | + |
| 927 | + def test_command_line_execution_dry_run(self, mock_dirs): |
| 928 | + """Test executing relink.py from command line with --dry-run flag.""" |
| 929 | + source_dir, target_dir, source_file, _ = mock_dirs |
| 930 | + |
| 931 | + # Get the path to relink.py |
| 932 | + relink_script = os.path.join( |
| 933 | + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "relink.py" |
| 934 | + ) |
| 935 | + |
| 936 | + # Build the command |
| 937 | + command = [ |
| 938 | + sys.executable, |
| 939 | + relink_script, |
| 940 | + "--source-root", |
| 941 | + str(source_dir), |
| 942 | + "--target-root", |
| 943 | + str(target_dir), |
| 944 | + "--dry-run", |
| 945 | + ] |
| 946 | + |
| 947 | + # Execute the command |
| 948 | + result = subprocess.run(command, capture_output=True, text=True, check=False) |
| 949 | + |
| 950 | + # Verify the command executed successfully |
| 951 | + assert result.returncode == 0, f"Command failed with stderr: {result.stderr}" |
| 952 | + |
| 953 | + # Verify dry-run messages in output |
| 954 | + assert "DRY RUN MODE" in result.stdout |
| 955 | + assert "[DRY RUN] Would create symbolic link:" in result.stdout |
| 956 | + |
| 957 | + # Verify no actual changes were made |
| 958 | + assert source_file.is_file() |
| 959 | + assert not source_file.is_symlink() |
| 960 | + |
| 961 | + def test_command_line_execution_actual_run(self, mock_dirs): |
| 962 | + """Test executing relink.py from command line without dry-run.""" |
| 963 | + source_dir, target_dir, source_file, target_file = mock_dirs |
| 964 | + |
| 965 | + # Get the path to relink.py |
| 966 | + relink_script = os.path.join( |
| 967 | + os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "relink.py" |
| 968 | + ) |
| 969 | + |
| 970 | + # Build the command |
| 971 | + command = [ |
| 972 | + sys.executable, |
| 973 | + relink_script, |
| 974 | + "--source-root", |
| 975 | + str(source_dir), |
| 976 | + "--target-root", |
| 977 | + str(target_dir), |
| 978 | + ] |
| 979 | + |
| 980 | + # Execute the command |
| 981 | + result = subprocess.run(command, capture_output=True, text=True, check=False) |
| 982 | + |
| 983 | + # Verify the command executed successfully |
| 984 | + assert result.returncode == 0, f"Command failed with stderr: {result.stderr}" |
| 985 | + |
| 986 | + # Verify the file was converted to a symlink |
| 987 | + assert source_file.is_symlink() |
| 988 | + assert os.readlink(str(source_file)) == str(target_file) |
| 989 | + |
| 990 | + # Verify success messages in output |
| 991 | + assert "Created symbolic link:" in result.stdout |
0 commit comments