pydoll icon indicating copy to clipboard operation
pydoll copied to clipboard

Fix temp dir management

Open thalissonvs opened this issue 8 months ago • 3 comments

PR Type

Bug fix, Tests


Description

  • Added a delay after closing the browser to ensure proper shutdown.

  • Improved temporary directory cleanup by using cleanup() instead of shutil.rmtree.

  • Updated test cases to validate the new cleanup mechanism.

  • Refactored code for better type annotations and consistency.


Changes walkthrough 📝

Relevant files
Bug fix
base.py
Add delay and cleanup after browser shutdown                         

pydoll/browser/base.py

  • Added a 1-second delay after closing the browser.
  • Ensured temporary directories are cleaned up after browser shutdown.
  • +1/-0     
    Enhancement
    managers.py
    Refactor temp directory management and cleanup                     

    pydoll/browser/managers.py

  • Replaced shutil.rmtree with cleanup() for temp directories.
  • Added type annotations for _temp_dirs.
  • Modified temp directory creation to prevent auto-deletion.
  • +4/-6     
    Tests
    test_browser_managers.py
    Update tests for temp directory cleanup                                   

    tests/test_browser_managers.py

  • Updated tests to mock and validate cleanup() calls.
  • Removed reliance on shutil.rmtree in tests.
  • Adjusted test fixtures for new temp directory behavior.
  • +7/-6     

    Need help?
  • Type /help how to ... in the comments thread for any questions about Qodo Merge usage.
  • Check out the documentation for more information.
  • thalissonvs avatar Mar 13 '25 01:03 thalissonvs

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🧪 PR contains tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Potential Race Condition

    The change to set delete=False in the temporary directory creation prevents auto-deletion but might create a race condition if cleanup() is called before the browser is fully closed. The added delay in base.py helps, but consider adding error handling in the cleanup method.

    temp_dir = self._temp_dir_factory(delete=False)  # avoid auto-deletion
    
    Hard-coded Delay

    The 1-second delay after closing the browser is a magic number. Consider making this configurable or implementing a more robust mechanism to ensure the browser has fully shut down before cleanup.

    await asyncio.sleep(1)  # small delay to ensure browser is closed
    

    qodo-code-review[bot] avatar Mar 13 '25 01:03 qodo-code-review[bot]

    :warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

    Codecov Report

    All modified and coverable lines are covered by tests :white_check_mark:

    :exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

    Flag Coverage Δ
    tests 97.18% <100.00%> (ø)

    Flags with carried forward coverage won't be shown. Click here to find out more.

    Files with missing lines Coverage Δ
    pydoll/browser/base.py 97.22% <100.00%> (+0.01%) :arrow_up:
    pydoll/browser/managers.py 98.73% <100.00%> (-0.02%) :arrow_down:
    🚀 New features to boost your workflow:
    • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

    codecov-commenter avatar Mar 13 '25 01:03 codecov-commenter

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Fix parameter mismatch

    The temp_dir_factory parameter in init is defined without parameters, but
    here you're passing a delete parameter. This will cause a TypeError if the
    factory doesn't accept this parameter.

    pydoll/browser/managers.py [215-217]

    +# If using standard TemporaryDirectory
     temp_dir = self._temp_dir_factory(delete=False)  # avoid auto-deletion
     self._temp_dirs.append(temp_dir)
     return temp_dir
     
    +# Or modify __init__ to ensure factory accepts the parameter
    +# def __init__(self, temp_dir_factory=lambda **kwargs: TemporaryDirectory(**kwargs)):
    +
    

    [Suggestion has been applied]

    Suggestion importance[1-10]: 9

    __

    Why: This suggestion identifies a critical issue where the code would fail at runtime. The factory function is called with a parameter it might not accept, which could cause TypeError exceptions. This is a significant bug that would affect the application's stability.

    High
    General
    Fix incorrect type annotation

    The type annotation for _temp_dirs is incorrect. It's using the factory function
    as a type, but should use the return type of the factory function
    (TemporaryDirectory).

    pydoll/browser/managers.py [202-203]

     self._temp_dir_factory = temp_dir_factory
    -self._temp_dirs: list[temp_dir_factory] = []
    +self._temp_dirs: list[TemporaryDirectory] = []
    
    • [ ] Apply this suggestion
    Suggestion importance[1-10]: 7

    __

    Why: The suggestion correctly identifies that using temp_dir_factory as a type annotation is incorrect. The list should contain TemporaryDirectory instances, not factory functions. This improves type safety and code clarity.

    Medium
    Improve test fixture flexibility

    **The lambda function should accept kwargs instead of just delete to be more
    flexible and match how it's used in the implementation.

    tests/test_browser_managers.py [24-26]

     return TempDirectoryManager(
    -    temp_dir_factory=lambda delete: mock_dir
    +    temp_dir_factory=lambda **kwargs: mock_dir
     )
    

    [Suggestion has been applied]

    Suggestion importance[1-10]: 6

    __

    Why: The suggestion correctly points out that using **kwargs instead of a specific parameter name would make the test fixture more robust and aligned with how factory functions typically work. This would prevent test failures if the implementation changes.

    Low
    • [ ] Update

    qodo-code-review[bot] avatar Mar 13 '25 01:03 qodo-code-review[bot]