Skip to content

Commit cb1b51d

Browse files
committed
Add tests for printed messages.
1 parent 872de08 commit cb1b51d

1 file changed

Lines changed: 118 additions & 1 deletion

File tree

tests/test_relink.py

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_nested_directory_structure(self, temp_dirs, current_user):
8686
assert os.path.islink(source_file), "Nested file should be a symlink"
8787
assert os.readlink(source_file) == target_file
8888

89-
def test_skip_existing_symlinks(self, temp_dirs, current_user):
89+
def test_skip_existing_symlinks(self, temp_dirs, current_user, capsys):
9090
"""Test that existing symlinks are skipped."""
9191
source_dir, target_dir = temp_dirs
9292
username = current_user
@@ -119,6 +119,11 @@ def test_skip_existing_symlinks(self, temp_dirs, current_user):
119119
os.readlink(source_link) == dummy_target
120120
), "Symlink target should be unchanged"
121121

122+
# Check that "Skipping symlink" message was printed
123+
captured = capsys.readouterr()
124+
assert "Skipping symlink:" in captured.out
125+
assert source_link in captured.out
126+
122127
def test_missing_target_file(self, temp_dirs, current_user, capsys):
123128
"""Test behavior when target file doesn't exist."""
124129
source_dir, target_dir = temp_dirs
@@ -215,6 +220,64 @@ def test_absolute_paths(self, temp_dirs, current_user):
215220
finally:
216221
os.chdir(cwd)
217222

223+
def test_print_searching_message(self, temp_dirs, current_user, capsys):
224+
"""Test that searching message is printed."""
225+
source_dir, target_dir = temp_dirs
226+
username = current_user
227+
228+
# Run the function
229+
relink.find_and_replace_owned_files(source_dir, target_dir, username)
230+
231+
# Check that searching message was printed
232+
captured = capsys.readouterr()
233+
assert f"Searching for files owned by '{username}'" in captured.out
234+
assert f"in '{os.path.abspath(source_dir)}'" in captured.out
235+
236+
def test_print_found_owned_file(self, temp_dirs, current_user, capsys):
237+
"""Test that 'Found owned file' message is printed."""
238+
source_dir, target_dir = temp_dirs
239+
username = current_user
240+
241+
# Create a file owned by current user
242+
source_file = os.path.join(source_dir, "owned_file.txt")
243+
target_file = os.path.join(target_dir, "owned_file.txt")
244+
245+
with open(source_file, "w", encoding="utf-8") as f:
246+
f.write("content")
247+
with open(target_file, "w", encoding="utf-8") as f:
248+
f.write("target content")
249+
250+
# Run the function
251+
relink.find_and_replace_owned_files(source_dir, target_dir, username)
252+
253+
# Check that "Found owned file" message was printed
254+
captured = capsys.readouterr()
255+
assert "Found owned file:" in captured.out
256+
assert source_file in captured.out
257+
258+
def test_print_deleted_and_created_messages(self, temp_dirs, current_user, capsys):
259+
"""Test that deleted and created symlink messages are printed."""
260+
source_dir, target_dir = temp_dirs
261+
username = current_user
262+
263+
# Create files
264+
source_file = os.path.join(source_dir, "test_file.txt")
265+
target_file = os.path.join(target_dir, "test_file.txt")
266+
267+
with open(source_file, "w", encoding="utf-8") as f:
268+
f.write("source")
269+
with open(target_file, "w", encoding="utf-8") as f:
270+
f.write("target")
271+
272+
# Run the function
273+
relink.find_and_replace_owned_files(source_dir, target_dir, username)
274+
275+
# Check messages
276+
captured = capsys.readouterr()
277+
assert "Deleted original file:" in captured.out
278+
assert "Created symbolic link:" in captured.out
279+
assert f"{source_file} -> {target_file}" in captured.out
280+
218281

219282
class TestParseArguments:
220283
"""Test suite for parse_arguments function."""
@@ -323,3 +386,57 @@ def test_file_with_special_characters(self, temp_dirs):
323386
# Verify
324387
assert os.path.islink(source_file)
325388
assert os.readlink(source_file) == target_file
389+
390+
def test_error_deleting_file(self, temp_dirs, capsys):
391+
"""Test error message when file deletion fails."""
392+
source_dir, target_dir = temp_dirs
393+
username = os.environ["USER"]
394+
395+
# Create files
396+
source_file = os.path.join(source_dir, "test.txt")
397+
target_file = os.path.join(target_dir, "test.txt")
398+
399+
with open(source_file, "w", encoding="utf-8") as f:
400+
f.write("source")
401+
with open(target_file, "w", encoding="utf-8") as f:
402+
f.write("target")
403+
404+
# Mock os.rename to raise an error
405+
def mock_rename(src, dst):
406+
raise OSError("Simulated rename error")
407+
408+
with patch("os.rename", side_effect=mock_rename):
409+
# Run the function
410+
relink.find_and_replace_owned_files(source_dir, target_dir, username)
411+
412+
# Check error message
413+
captured = capsys.readouterr()
414+
assert "Error deleting file" in captured.out
415+
assert source_file in captured.out
416+
417+
def test_error_creating_symlink(self, temp_dirs, capsys):
418+
"""Test error message when symlink creation fails."""
419+
source_dir, target_dir = temp_dirs
420+
username = os.environ["USER"]
421+
422+
# Create source file
423+
source_file = os.path.join(source_dir, "test.txt")
424+
target_file = os.path.join(target_dir, "test.txt")
425+
426+
with open(source_file, "w", encoding="utf-8") as f:
427+
f.write("source")
428+
with open(target_file, "w", encoding="utf-8") as f:
429+
f.write("target")
430+
431+
# Mock os.symlink to raise an error
432+
def mock_symlink(src, dst):
433+
raise OSError("Simulated symlink error")
434+
435+
with patch("os.symlink", side_effect=mock_symlink):
436+
# Run the function
437+
relink.find_and_replace_owned_files(source_dir, target_dir, username)
438+
439+
# Check error message
440+
captured = capsys.readouterr()
441+
assert "Error creating symlink" in captured.out
442+
assert source_file in captured.out

0 commit comments

Comments
 (0)