-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add unit tests for helpers #10312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add unit tests for helpers #10312
Conversation
…hRepo. Organize isolated_environment tests into test class
Reviewer's Guide by SourceryThis pull request adds comprehensive unit tests for several helper functions, including No diagrams generated as the changes look simple and do not need a visual representation. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @j7an - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider adding a helper function to create temporary files/directories with content to reduce duplication in
TestCopyPath
. - The tests look good, but it would be helpful to add a brief description of what each helper function does in the docstring.
Here's what I looked at during the review
- 🟢 General issues: all looks good
- 🟢 Security: all looks good
- 🟡 Testing: 1 issue found
- 🟢 Complexity: all looks good
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
source = setup_files["source_file"] | ||
dest = setup_files["tmp_path"] / "new_file.txt" | ||
|
||
copy_path(source, dest) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Suggest adding tests for edge cases in copy_path
It would be beneficial to add tests for cases such as copying a file to a destination where the parent directory doesn't exist, copying an empty directory, copying a directory to a file when the file is a symlink, and handling potential exceptions during the copy process (e.g., permission errors).
Suggested implementation:
def test_copy_file_to_existing_file(self, setup_files: dict[str, Path]) -> None:
source = setup_files["source_file"]
dest = setup_files["dest_file"]
copy_path(source, dest)
assert dest.exists()
assert dest.read_text() == "source content"
def test_copy_file_to_destination_missing_parent(self, setup_files: dict[str, Path], tmp_path: Path) -> None:
source = setup_files["source_file"]
dest = tmp_path / "non_existent_dir" / "copied_file.txt"
# Ensure the parent directory does not exist
if dest.parent.exists():
import shutil
shutil.rmtree(dest.parent)
copy_path(source, dest)
assert dest.exists()
assert dest.read_text() == "source content"
def test_copy_empty_directory(self, tmp_path: Path) -> None:
# Create an empty source directory
source_dir = tmp_path / "empty_src"
source_dir.mkdir()
dest_dir = tmp_path / "copied_empty_dir"
copy_path(source_dir, dest_dir)
assert dest_dir.exists()
# Ensure the destination directory is empty
assert len(list(dest_dir.iterdir())) == 0
def test_copy_directory_to_symlink(self, tmp_path: Path) -> None:
# Create a source directory with a file
source_dir = tmp_path / "src_dir"
source_dir.mkdir()
(source_dir / "file.txt").write_text("123")
# Create a target file and a symlink for the destination
target_file = tmp_path / "target.txt"
target_file.write_text("old content")
dest_symlink = tmp_path / "symlink_file"
dest_symlink.symlink_to(target_file)
import pytest
# Expecting an exception because copying a directory to a symlinked file is invalid
with pytest.raises(Exception):
copy_path(source_dir, dest_symlink)
def test_copy_path_permission_error(self, setup_files: dict[str, Path], tmp_path: Path) -> None:
source = setup_files["source_file"]
dest = tmp_path / "restricted_dir" / "file.txt"
dest.parent.mkdir()
import os
# Change permissions to simulate a permission error (read-only directory)
os.chmod(dest.parent, 0o400)
import pytest
with pytest.raises(PermissionError):
copy_path(source, dest)
# Revert permissions for cleanup
os.chmod(dest.parent, 0o700)
These changes assume that your copy_path function is designed to raise appropriate exceptions when errors occur, and that it can handle directory copying. You might need to import modules like shutil, os, or pytest at the top of the file if they are not already imported.
Add unit tests for get_package, get_dependency, copy_path, MockDulwichRepo. Organize isolated_environment tests into test class
Pull Request Check List
Partially resolves: #9161
Summary by Sourcery
Add comprehensive unit tests for helper functions in the test suite
New Features:
Tests: