|
4 | 4 |
|
5 | 5 | import os |
6 | 6 | import sys |
7 | | -import tempfile |
8 | | -import shutil |
9 | | -import logging |
10 | 7 | import subprocess |
11 | | -from unittest.mock import patch |
12 | 8 |
|
13 | 9 | import pytest |
14 | 10 |
|
15 | | -# Add parent directory to path to import relink module |
16 | | -sys.path.insert( |
17 | | - 0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
18 | | -) |
19 | | -# pylint: disable=wrong-import-position |
20 | | -import relink # noqa: E402 |
21 | | - |
22 | | - |
23 | | -@pytest.fixture(scope="function", autouse=True) |
24 | | -def configure_logging(): |
25 | | - """Configure logging to output to stdout for all tests.""" |
26 | | - # Configure logging before each test |
27 | | - logging.basicConfig( |
28 | | - level=logging.INFO, |
29 | | - format="%(message)s", |
30 | | - stream=sys.stdout, |
31 | | - force=True, # Force reconfiguration |
| 11 | + |
| 12 | +@pytest.fixture(name="mock_dirs") |
| 13 | +def fixture_mock_dirs(tmp_path): |
| 14 | + """Create temporary directories and files for command-line testing.""" |
| 15 | + source_dir = tmp_path / "source" |
| 16 | + target_dir = tmp_path / "target" |
| 17 | + source_dir.mkdir() |
| 18 | + target_dir.mkdir() |
| 19 | + |
| 20 | + # Create a test file |
| 21 | + source_file = source_dir / "test_file.txt" |
| 22 | + target_file = target_dir / "test_file.txt" |
| 23 | + source_file.write_text("source content") |
| 24 | + target_file.write_text("target content") |
| 25 | + |
| 26 | + return source_dir, target_dir, source_file, target_file |
| 27 | + |
| 28 | + |
| 29 | +def test_command_line_execution_dry_run(mock_dirs): |
| 30 | + """Test executing relink.py from command line with --dry-run flag.""" |
| 31 | + source_dir, target_dir, source_file, _ = mock_dirs |
| 32 | + |
| 33 | + # Get the path to relink.py |
| 34 | + relink_script = os.path.join( |
| 35 | + os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), |
| 36 | + "relink.py", |
| 37 | + ) |
| 38 | + |
| 39 | + # Build the command |
| 40 | + command = [ |
| 41 | + sys.executable, |
| 42 | + relink_script, |
| 43 | + "--source-root", |
| 44 | + str(source_dir), |
| 45 | + "--target-root", |
| 46 | + str(target_dir), |
| 47 | + "--dry-run", |
| 48 | + ] |
| 49 | + |
| 50 | + # Execute the command |
| 51 | + result = subprocess.run(command, capture_output=True, text=True, check=False) |
| 52 | + |
| 53 | + # Verify the command executed successfully |
| 54 | + assert result.returncode == 0, f"Command failed with stderr: {result.stderr}" |
| 55 | + |
| 56 | + # Verify dry-run messages in output |
| 57 | + assert "DRY RUN MODE" in result.stdout |
| 58 | + assert "[DRY RUN] Would create symbolic link:" in result.stdout |
| 59 | + |
| 60 | + # Verify no actual changes were made |
| 61 | + assert source_file.is_file() |
| 62 | + assert not source_file.is_symlink() |
| 63 | + |
| 64 | + |
| 65 | +def test_command_line_execution_actual_run(mock_dirs): |
| 66 | + """Test executing relink.py from command line without dry-run.""" |
| 67 | + source_dir, target_dir, source_file, target_file = mock_dirs |
| 68 | + |
| 69 | + # Get the path to relink.py |
| 70 | + relink_script = os.path.join( |
| 71 | + os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), |
| 72 | + "relink.py", |
32 | 73 | ) |
33 | | - yield |
34 | | - # Clean up logging handlers after each test |
35 | | - logging.getLogger().handlers.clear() |
36 | | - |
37 | | - |
38 | | -@pytest.fixture(scope="function", name="mock_default_dirs") |
39 | | -def fixture_mock_default_dirs(): |
40 | | - """Mock the default directories to use temporary directories.""" |
41 | | - source_dir = tempfile.mkdtemp(prefix="test_default_source_") |
42 | | - target_dir = tempfile.mkdtemp(prefix="test_default_target_") |
43 | | - |
44 | | - with patch.object(relink, "DEFAULT_SOURCE_ROOT", source_dir): |
45 | | - with patch.object(relink, "DEFAULT_TARGET_ROOT", target_dir): |
46 | | - yield source_dir, target_dir |
47 | | - |
48 | | - # Cleanup |
49 | | - shutil.rmtree(source_dir, ignore_errors=True) |
50 | | - shutil.rmtree(target_dir, ignore_errors=True) |
51 | | - |
52 | | - |
53 | | -@pytest.fixture(scope="function", name="temp_dirs") |
54 | | -def fixture_temp_dirs(): |
55 | | - """Create temporary source and target directories for testing.""" |
56 | | - source_dir = tempfile.mkdtemp(prefix="test_source_") |
57 | | - target_dir = tempfile.mkdtemp(prefix="test_target_") |
58 | | - |
59 | | - yield source_dir, target_dir |
60 | | - |
61 | | - # Cleanup |
62 | | - shutil.rmtree(source_dir, ignore_errors=True) |
63 | | - shutil.rmtree(target_dir, ignore_errors=True) |
64 | | - |
65 | | - |
66 | | -class TestCommandLineExecution: |
67 | | - """Test suite for command-line execution of relink.py.""" |
68 | | - |
69 | | - @pytest.fixture |
70 | | - def mock_dirs(self, tmp_path): |
71 | | - """Create temporary directories and files for command-line testing.""" |
72 | | - source_dir = tmp_path / "source" |
73 | | - target_dir = tmp_path / "target" |
74 | | - source_dir.mkdir() |
75 | | - target_dir.mkdir() |
76 | | - |
77 | | - # Create a test file |
78 | | - source_file = source_dir / "test_file.txt" |
79 | | - target_file = target_dir / "test_file.txt" |
80 | | - source_file.write_text("source content") |
81 | | - target_file.write_text("target content") |
82 | | - |
83 | | - return source_dir, target_dir, source_file, target_file |
84 | | - |
85 | | - def test_command_line_execution_dry_run(self, mock_dirs): |
86 | | - """Test executing relink.py from command line with --dry-run flag.""" |
87 | | - source_dir, target_dir, source_file, _ = mock_dirs |
88 | | - |
89 | | - # Get the path to relink.py |
90 | | - relink_script = os.path.join( |
91 | | - os.path.dirname( |
92 | | - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
93 | | - ), |
94 | | - "relink.py", |
95 | | - ) |
96 | | - |
97 | | - # Build the command |
98 | | - command = [ |
99 | | - sys.executable, |
100 | | - relink_script, |
101 | | - "--source-root", |
102 | | - str(source_dir), |
103 | | - "--target-root", |
104 | | - str(target_dir), |
105 | | - "--dry-run", |
106 | | - ] |
107 | | - |
108 | | - # Execute the command |
109 | | - result = subprocess.run(command, capture_output=True, text=True, check=False) |
110 | | - |
111 | | - # Verify the command executed successfully |
112 | | - assert result.returncode == 0, f"Command failed with stderr: {result.stderr}" |
113 | | - |
114 | | - # Verify dry-run messages in output |
115 | | - assert "DRY RUN MODE" in result.stdout |
116 | | - assert "[DRY RUN] Would create symbolic link:" in result.stdout |
117 | | - |
118 | | - # Verify no actual changes were made |
119 | | - assert source_file.is_file() |
120 | | - assert not source_file.is_symlink() |
121 | | - |
122 | | - def test_command_line_execution_actual_run(self, mock_dirs): |
123 | | - """Test executing relink.py from command line without dry-run.""" |
124 | | - source_dir, target_dir, source_file, target_file = mock_dirs |
125 | | - |
126 | | - # Get the path to relink.py |
127 | | - relink_script = os.path.join( |
128 | | - os.path.dirname( |
129 | | - os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
130 | | - ), |
131 | | - "relink.py", |
132 | | - ) |
133 | | - |
134 | | - # Build the command |
135 | | - command = [ |
136 | | - sys.executable, |
137 | | - relink_script, |
138 | | - "--source-root", |
139 | | - str(source_dir), |
140 | | - "--target-root", |
141 | | - str(target_dir), |
142 | | - ] |
143 | | - |
144 | | - # Execute the command |
145 | | - result = subprocess.run(command, capture_output=True, text=True, check=False) |
146 | | - |
147 | | - # Verify the command executed successfully |
148 | | - assert result.returncode == 0, f"Command failed with stderr: {result.stderr}" |
149 | | - |
150 | | - # Verify the file was converted to a symlink |
151 | | - assert source_file.is_symlink() |
152 | | - assert os.readlink(str(source_file)) == str(target_file) |
153 | | - |
154 | | - # Verify success messages in output |
155 | | - assert "Created symbolic link:" in result.stdout |
| 74 | + |
| 75 | + # Build the command |
| 76 | + command = [ |
| 77 | + sys.executable, |
| 78 | + relink_script, |
| 79 | + "--source-root", |
| 80 | + str(source_dir), |
| 81 | + "--target-root", |
| 82 | + str(target_dir), |
| 83 | + ] |
| 84 | + |
| 85 | + # Execute the command |
| 86 | + result = subprocess.run(command, capture_output=True, text=True, check=False) |
| 87 | + |
| 88 | + # Verify the command executed successfully |
| 89 | + assert result.returncode == 0, f"Command failed with stderr: {result.stderr}" |
| 90 | + |
| 91 | + # Verify the file was converted to a symlink |
| 92 | + assert source_file.is_symlink() |
| 93 | + assert os.readlink(str(source_file)) == str(target_file) |
| 94 | + |
| 95 | + # Verify success messages in output |
| 96 | + assert "Created symbolic link:" in result.stdout |
0 commit comments