Skip to content

Add comprehensive unit tests for Host class#10

Open
Copilot wants to merge 27 commits intomasterfrom
copilot/add-unit-tests-host-class
Open

Add comprehensive unit tests for Host class#10
Copilot wants to merge 27 commits intomasterfrom
copilot/add-unit-tests-host-class

Conversation

Copy link
Contributor

Copilot AI commented Oct 18, 2025

Add comprehensive unit tests for the Host class (rebased to latest master):

  • Rebase branch to latest master
  • Include new commits from master:
    • GitHub Actions build and test workflow
    • Windows hosts file path fix
    • README documentation improvements
    • Copilot instructions enhancements
    • Build workflow security improvements
  • Verify all 64 tests still pass
  • Verify code style compliance
  • Address code review feedback: Remove unnecessary temp_fd = None initialization in parser.py

Summary

Successfully rebased to the latest master branch. The PR is now properly aligned with master's commit history, ensuring a clean merge. All 64 tests continue to pass after the rebase.

Previously addressed code review feedback by removing the redundant temp_fd = None initialization in parser.py. The variable is assigned from tempfile.mkstemp() and immediately used, so the initial None assignment was unnecessary.

Test Results

  • test/test_base.py: 6 tests ✓
  • test/test_host.py: 44 tests ✓
  • test/test_new_features.py: 14 tests ✓
  • Total: 64 tests - All passing

Code style compliance verified with pycodestyle.

Original prompt

Suggest adding unit tests for the Host class. Tests should cover valid and invalid inputs, string representations, and equality checks to ensure robustness and reliability.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Copilot AI changed the title [WIP] Add unit tests for Host class functionality Add comprehensive unit tests for Host class Oct 18, 2025
Copilot AI requested a review from igormilovanovic October 18, 2025 20:58
@igormilovanovic
Copy link
Owner

@copilot rebase to master

Copilot AI and others added 9 commits October 27, 2025 23:12
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ity, and test specificity

Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 27, 2025

@copilot rebase to master

Rebased to master successfully. Updated tests to work with the modernized Host class (frozen dataclass with ipaddress module). All 64 tests passing. (commit b5a36c1 and updates in current commit)

@igormilovanovic igormilovanovic marked this pull request as ready for review October 28, 2025 10:01
@igormilovanovic igormilovanovic requested review from Copilot and removed request for igormilovanovic November 8, 2025 08:31
@igormilovanovic
Copy link
Owner

@copilot Rebase

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modernizes the pyhosts library by migrating from Python 3.8/netaddr to Python 3.10+ with built-in modules. The main changes include:

  • Replaced netaddr dependency with Python's built-in ipaddress module
  • Refactored code into a modular architecture with separate files for models, parser, and platform resolution
  • Updated Host class to use immutable dataclass with type hints
  • Implemented lazy loading and improved the Hosts class with MutableSequence protocol
  • Added comprehensive test coverage for new features

Reviewed Changes

Copilot reviewed 11 out of 13 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
test/test_new_features.py New comprehensive test suite for modernized features including immutability, lazy loading, and searching
test/test_host.py New detailed test suite for Host class covering validation, parsing, and string representations
test/test_base.py Updated existing tests to use temporary files and new API (ip_address, Path, new attribute names)
pyhosts/init.py Simplified to export public API with proper type hints and version management
pyhosts/models.py New Host dataclass implementation with validation and parsing logic
pyhosts/hosts.py New Hosts manager implementing MutableSequence protocol with lazy loading
pyhosts/parser.py New parser module for reading/writing hosts files with atomic operations
pyhosts/platform_resolver.py New platform-specific path resolution with factory pattern
pyproject.toml New modern project configuration replacing setup.py
requirements.txt Updated to reflect removal of netaddr dependency
setup.py Removed in favor of pyproject.toml
pyhosts/host.py Removed old Host implementation
pyhosts/py.typed Added PEP 561 marker for type hints

igormilovanovic and others added 2 commits November 8, 2025 08:33
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 8, 2025

@copilot Rebase

Rebased to latest master successfully. The branch now includes 7 new commits from master (CI/CD workflows, documentation updates, bug fixes). All 64 tests passing. (commit cbdf44c)

igormilovanovic and others added 7 commits November 8, 2025 08:34
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
Co-authored-by: igormilovanovic <51771+igormilovanovic@users.noreply.github.com>
@igormilovanovic
Copy link
Owner

@copilot Rebase

Copy link
Contributor Author

Copilot AI commented Nov 8, 2025

@copilot Rebase

Rebased to latest master successfully. The branch is now properly aligned with master's commit history. All 64 tests passing. (commit 0d8e395)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 15 out of 17 changed files in this pull request and generated 4 comments.

### Error Handling
- Use custom exceptions: `PlatformNotSupportedException`, `DuplicateEntryError`
- Use custom exceptions: `PlatformNotSupportedError`, `DuplicateEntryError`
- Handle `AddrFormatError` from netaddr for invalid IP addresses
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling section references AddrFormatError from netaddr, but the codebase now uses Python's built-in ipaddress module which raises ValueError for invalid IP addresses (as seen in models.py line 87). Update this to reflect the current implementation:

Change "Handle AddrFormatError from netaddr for invalid IP addresses" to "Handle ValueError from ipaddress module for invalid IP addresses"

Copilot uses AI. Check for mistakes.
Comment on lines +226 to +227
- **Symptom**: `AddrFormatError` from netaddr
- **Solution**: Validate IP addresses before creating Host objects; use `netaddr.IPAddress()` for validation
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This troubleshooting section references AddrFormatError from netaddr, but the codebase now uses Python's built-in ipaddress module which raises ValueError for invalid IP addresses. Update to:

  • Symptom: ValueError when parsing invalid IP addresses
  • Solution: Validate IP addresses before creating Host objects; use ipaddress.ip_address() for validation
Suggested change
- **Symptom**: `AddrFormatError` from netaddr
- **Solution**: Validate IP addresses before creating Host objects; use `netaddr.IPAddress()` for validation
- **Symptom**: `ValueError` when parsing invalid IP addresses
- **Solution**: Validate IP addresses before creating Host objects; use `ipaddress.ip_address()` for validation

Copilot uses AI. Check for mistakes.
- Each `Host` object has: `ipaddress` (IPAddress), `hostname` (str), `aliases` (list or None), `comments` (str or None)
- The `Hosts` class provides iteration and attribute-based access to entries
- Host entries can be accessed by hostname, alias, or IP address
- Each `Host` object is a dataclass with: `ipaddress` (IPAddress from netaddr), `hostname` (str), `aliases` (list[str] or None), `comments` (str or None)
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation incorrectly describes Host attributes. According to the actual implementation in models.py:

  • The attribute is ip_address (not ipaddress)
  • It's of type IPv4Address | IPv6Address from Python's built-in ipaddress module (not IPAddress from netaddr)
  • aliases is tuple[str, ...] (not list[str] or None)
  • The attribute is comment (not comments), and it's Optional[str] (not str or None)

Update line 157 to: "Each Host object is a dataclass with: ip_address (IPv4Address | IPv6Address from ipaddress), hostname (str), aliases (tuple[str, ...]), comment (Optional[str])"

Suggested change
- Each `Host` object is a dataclass with: `ipaddress` (IPAddress from netaddr), `hostname` (str), `aliases` (list[str] or None), `comments` (str or None)
- Each `Host` object is a dataclass with: `ip_address` (IPv4Address | IPv6Address from ipaddress), `hostname` (str), `aliases` (tuple[str, ...]), `comment` (Optional[str])

Copilot uses AI. Check for mistakes.
Comment on lines +59 to +61
- Core dependencies: See `requirements.txt` (currently: netaddr)
- Test dependencies: See `test-requirements.txt` (pytest, pytest-cov, pycodestyle, pylint)
- Key dependency: `netaddr` for IP address handling and validation
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copilot instructions still reference netaddr as a key dependency (lines 59, 61), but the actual implementation uses Python's built-in ipaddress module and requirements.txt indicates no external dependencies. This documentation should be updated to reflect the current architecture.

Suggested changes:

  • Line 59: Update "(currently: netaddr)" to "(currently: none - uses built-in modules)"
  • Line 61: Update to "Key dependency: Python's built-in ipaddress module for IP address handling and validation"
Suggested change
- Core dependencies: See `requirements.txt` (currently: netaddr)
- Test dependencies: See `test-requirements.txt` (pytest, pytest-cov, pycodestyle, pylint)
- Key dependency: `netaddr` for IP address handling and validation
- Core dependencies: See `requirements.txt` (currently: none - uses built-in modules)
- Test dependencies: See `test-requirements.txt` (pytest, pytest-cov, pycodestyle, pylint)
- Key dependency: Python's built-in `ipaddress` module for IP address handling and validation

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 17 changed files in this pull request and generated 2 comments.

Comment on lines +59 to +61
- Core dependencies: See `requirements.txt` (currently: netaddr)
- Test dependencies: See `test-requirements.txt` (pytest, pytest-cov, pycodestyle, pylint)
- Key dependency: `netaddr` for IP address handling and validation
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation still references 'netaddr' as a dependency, but the project has been migrated to use Python's built-in 'ipaddress' module (as seen in requirements.txt which now states "No external dependencies"). This section should be updated to reflect that the project uses Python 3.10+ built-in ipaddress module instead of the external netaddr library.

Copilot uses AI. Check for mistakes.
### Error Handling
- Use custom exceptions: `PlatformNotSupportedException`, `DuplicateEntryError`
- Use custom exceptions: `PlatformNotSupportedError`, `DuplicateEntryError`
- Handle `AddrFormatError` from netaddr for invalid IP addresses
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

References to 'AddrFormatError from netaddr' are outdated. The project now uses Python's built-in ipaddress module, which raises ValueError for invalid IP addresses (not AddrFormatError). This documentation should be updated to reference ValueError from the ipaddress module instead.

Copilot uses AI. Check for mistakes.
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.

2 participants