Skip to content

Conversation

j7an
Copy link

@j7an j7an commented Mar 30, 2025

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

  • Added tests for changed code.
  • Updated documentation for changed code.

Summary by Sourcery

Add comprehensive unit tests for helper functions in the test suite

New Features:

  • Created detailed test coverage for helper utility functions in the test suite

Tests:

  • Introduced new test classes for get_package, get_dependency, copy_path, and MockDulwichRepo helper functions
  • Reorganized isolated_environment tests into a dedicated test class
  • Added parameterized tests to cover various input scenarios and edge cases for helper functions

…hRepo. Organize isolated_environment tests into test class
Copy link

sourcery-ai bot commented Mar 30, 2025

Reviewer's Guide by Sourcery

This pull request adds comprehensive unit tests for several helper functions, including get_package, get_dependency, copy_path, and MockDulwichRepo. It also organizes the isolated environment tests into a dedicated test class. The tests cover various scenarios and input types to ensure the correct behavior of these helper functions.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Added unit tests for the get_package helper function, covering different scenarios such as string and Version object inputs, and yanked package handling.
  • Created a test class TestGetPackage to encapsulate tests for get_package.
  • Added a fixture base_package for common setup.
  • Added test test_get_package_with_string_name to verify package name.
  • Added test test_get_package_version_handling to test version handling with string and Version object inputs.
  • Added test test_get_package_yanked_handling to test yanked package handling.
tests/test_helpers.py
Added unit tests for the get_dependency helper function, covering name, constraint, optional, and prerelease scenarios.
  • Created a test class TestGetDependency to encapsulate tests for get_dependency.
  • Added a fixture base_dependency for common setup.
  • Added test test_get_dependency_name to verify dependency name.
  • Added test test_get_dependency_default_constraint_value to verify default constraint value.
  • Added test test_get_dependency_defaults_to_false_optional to verify default optional value.
  • Added test test_get_dependency_default_to_false_allow_prereleases to verify default allow prereleases value.
  • Added test test_get_dependency_constraint_handling to test constraint handling with different input types.
  • Added test test_get_dependency_with_complex_dict_constraint to test constraint handling with complex dictionary input.
  • Added test test_get_dependency_with_groups to test groups handling.
  • Added test test_get_dependency_boolean_params to test boolean parameters.
tests/test_helpers.py
Added unit tests for the copy_path helper function, covering file and directory copy scenarios to new and existing destinations.
  • Created a test class TestCopyPath to encapsulate tests for copy_path.
  • Added a fixture setup_files to create source and destination files and directories.
  • Added test test_copy_file_to_new_destination to test copying a file to a new destination.
  • Added test test_copy_file_to_existing_file to test copying a file to an existing file.
  • Added test test_copy_file_to_existing_directory to test copying a file to an existing directory.
  • Added test test_copy_directory_to_new_destination to test copying a directory to a new destination.
  • Added test test_copy_directory_to_existing_directory to test copying a directory to an existing directory.
  • Added test test_copy_directory_to_existing_file to test copying a directory to an existing file.
tests/test_helpers.py
Added unit tests for the MockDulwichRepo helper class, covering initialization with different path types and head method behavior.
  • Created a test class TestMockDulwichRepo to encapsulate tests for MockDulwichRepo.
  • Added a fixture repo_path to create a temporary repository path.
  • Added test test_init_with_path_object to test initialization with a Path object.
  • Added test test_init_with_string_path to test initialization with a string path.
  • Added test test_head_returns_default_revision_encoded to test the head method returns the default revision encoded.
  • Added test test_init_ignores_extra_kwargs to test that extra keyword arguments are ignored during initialization.
tests/test_helpers.py
Organized the isolated environment tests into a test class.
  • Created a test class TestIsolatedEnvironment to encapsulate tests for isolated_environment.
  • Moved the test_isolated_environment_restores_original_environ test into the new class.
  • Moved the test_isolated_environment_clears_environ test into the new class.
  • Moved the test_isolated_environment_updates_environ test into the new class.
tests/test_helpers.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a 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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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)
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add test coverage for tests/helpers.py
1 participant