forked from ESMCI/inputdataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_replace_one_file_with_symlink.py
More file actions
259 lines (192 loc) · 8.61 KB
/
test_replace_one_file_with_symlink.py
File metadata and controls
259 lines (192 loc) · 8.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Tests of replace_one_file_with_symlink() in relink.py
"""
import os
import sys
import logging
from unittest.mock import patch
# Add parent directory to path to import relink module
sys.path.insert(
0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
)
# pylint: disable=wrong-import-position
import relink # noqa: E402
from shared import INDENT
def test_basic_file_replacement(temp_dirs):
"""Test basic functionality: replace owned file with symlink."""
source_dir, target_dir = temp_dirs
# Create a file in source directory
source_file = os.path.join(source_dir, "test_file.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("source content")
# Create corresponding file in target directory
target_file = os.path.join(target_dir, "test_file.txt")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target content")
# Run the function
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Verify the source file is now a symlink
assert os.path.islink(source_file), "Source file should be a symlink"
assert (
os.readlink(source_file) == target_file
), "Symlink should point to target file"
def test_nested_directory_structure(temp_dirs):
"""Test with nested directory structures."""
source_dir, target_dir = temp_dirs
# Create nested directories
nested_path = os.path.join("subdir1", "subdir2")
os.makedirs(os.path.join(source_dir, nested_path))
os.makedirs(os.path.join(target_dir, nested_path))
# Create files in nested directories
source_file = os.path.join(source_dir, nested_path, "nested_file.txt")
target_file = os.path.join(target_dir, nested_path, "nested_file.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("nested source")
with open(target_file, "w", encoding="utf-8") as f:
f.write("nested target")
# Run the function
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Verify
assert os.path.islink(source_file), "Nested file should be a symlink"
assert os.readlink(source_file) == target_file
def test_missing_target_file(temp_dirs, caplog):
"""Test behavior when target file doesn't exist."""
source_dir, target_dir = temp_dirs
# Create only source file (no corresponding target)
source_file = os.path.join(source_dir, "orphan.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("orphan content")
# Run the function
with caplog.at_level(logging.INFO):
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Verify the file is NOT converted to symlink
assert not os.path.islink(source_file), "File should not be a symlink"
assert os.path.isfile(source_file), "Original file should still exist"
# Check warning message
assert f"{INDENT}Warning: Corresponding file " in caplog.text
assert " not found" in caplog.text
def test_absolute_paths(temp_dirs):
"""Test that function handles relative paths by converting to absolute."""
source_dir, target_dir = temp_dirs
# Create test files
source_file = os.path.join(source_dir, "test.txt")
target_file = os.path.join(target_dir, "test.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("test")
with open(target_file, "w", encoding="utf-8") as f:
f.write("test target")
# Use relative paths (if possible)
cwd = os.getcwd()
try:
os.chdir(os.path.dirname(source_dir))
rel_source = os.path.basename(source_dir)
rel_target = os.path.basename(target_dir)
# Run with relative paths
relink.replace_one_file_with_symlink(rel_source, rel_target, source_file)
# Verify it still works
assert os.path.islink(source_file)
finally:
os.chdir(cwd)
def test_no_print_found_owned_file(temp_dirs, caplog):
"""Test that message with filename is printed."""
source_dir, target_dir = temp_dirs
# Create a file owned by current user
source_file = os.path.join(source_dir, "owned_file.txt")
target_file = os.path.join(target_dir, "owned_file.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("content")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target content")
# Run the function
with caplog.at_level(logging.INFO):
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Check that message was NOT logged (should happen in replace_files_with_symlinks instead)
assert f"'{source_file}':" not in caplog.text
def test_print_deleted_and_created_messages(temp_dirs, caplog):
"""Test that deleted and created symlink messages are printed."""
source_dir, target_dir = temp_dirs
# Create files
source_file = os.path.join(source_dir, "test_file.txt")
target_file = os.path.join(target_dir, "test_file.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("source")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target")
# Run the function
with caplog.at_level(logging.INFO):
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Check messages
assert f"{INDENT}Deleted original file:" in caplog.text
assert f"{INDENT}Created symbolic link:" in caplog.text
assert f"{source_file} -> {target_file}" in caplog.text
def test_error_creating_symlink(temp_dirs, caplog):
"""Test error message when symlink creation fails."""
source_dir, target_dir = temp_dirs
# Create source file
source_file = os.path.join(source_dir, "test.txt")
target_file = os.path.join(target_dir, "test.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("source")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target")
# Mock os.symlink to raise an error
def mock_symlink(src, dst):
raise OSError("Simulated symlink error")
with patch("os.symlink", side_effect=mock_symlink):
# Run the function
with caplog.at_level(logging.INFO):
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Check error message
assert f"{INDENT}Error creating symlink" in caplog.text
assert source_file in caplog.text
def test_file_with_spaces_in_name(temp_dirs):
"""Test files with spaces in their names."""
source_dir, target_dir = temp_dirs
# Create files with spaces
source_file = os.path.join(source_dir, "file with spaces.txt")
target_file = os.path.join(target_dir, "file with spaces.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("content")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target content")
# Run the function
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Verify
assert os.path.islink(source_file)
assert os.readlink(source_file) == target_file
def test_file_with_special_characters(temp_dirs):
"""Test files with special characters in names."""
source_dir, target_dir = temp_dirs
# Create files with special chars (that are valid in filenames)
filename = "file-with_special.chars@123.txt"
source_file = os.path.join(source_dir, filename)
target_file = os.path.join(target_dir, filename)
with open(source_file, "w", encoding="utf-8") as f:
f.write("content")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target content")
# Run the function
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Verify
assert os.path.islink(source_file)
assert os.readlink(source_file) == target_file
def test_error_deleting_file(temp_dirs, caplog):
"""Test error message when file deletion fails."""
source_dir, target_dir = temp_dirs
# Create files
source_file = os.path.join(source_dir, "test.txt")
target_file = os.path.join(target_dir, "test.txt")
with open(source_file, "w", encoding="utf-8") as f:
f.write("source")
with open(target_file, "w", encoding="utf-8") as f:
f.write("target")
# Mock os.rename to raise an error
def mock_rename(src, dst):
raise OSError("Simulated rename error")
with patch("os.rename", side_effect=mock_rename):
# Run the function
with caplog.at_level(logging.INFO):
relink.replace_one_file_with_symlink(source_dir, target_dir, source_file)
# Check error message
assert f"{INDENT}Error deleting file" in caplog.text
assert source_file in caplog.text