Summary
Tests fail when the libmagic system library is not installed, despite the book_parser.py code gracefully handling the missing dependency via try/except and runtime error messages. This creates a poor developer experience where 17 tests fail unnecessarily when libmagic is absent, even though the core functionality correctly falls back to extension-based file type detection.
What Does NOT Work
Patterns that FAIL:
-
Current test approach without conditional skipping - Tests that depend on python-magic fail hard when libmagic system library is not installed, even though the production code handles this gracefully with fallback logic.
-
Mock patching system-level dependencies - Attempting to mock libmagic at the system level doesn't work well because the dependency is loaded at module import time, and the missing C library causes import failures before tests can mock anything.
-
Assuming all developers have libmagic installed - This creates friction for new contributors who may not have the system library, leading to confusing test failures that don't reflect actual functionality problems.
Scenarios
Fresh Install
- Developer installs autonomous-dev: May not have libmagic system library installed
- Runs tests: 17 tests fail even though book_parser.py works correctly with extension-based fallback
- Expected behavior: Tests should skip cleanly with clear message about optional dependency
- Actual behavior: Tests fail with ImportError/AttributeError traces
Update/Upgrade
- Existing users with working book_parser.py: May have been using extension-based detection without issues
- Pull new test changes: Tests suddenly fail when libmagic not present
- Expected behavior: Tests pass or skip based on dependency availability
- Actual behavior: Test failures block development workflow
User Customizations
- Not applicable: This is testing infrastructure, no user customizations involved
Implementation Approach
Files to modify:
-
Tests using python-magic (identify via grep for `import magic` or `magic.from_file`):
- Add `pytest.importorskip('magic')` at module level or in fixtures
- Separate tests into magic-dependent and magic-independent suites
-
pyproject.toml:
```toml
[project.optional-dependencies]
mime-detection = ["python-magic>=0.4.27"]
```
-
plugins/autonomous-dev/lib/book_parser.py (enhance error messages):
- Add platform-specific installation instructions to ImportError message
- Consider adding puremagic as pure-Python fallback (optional enhancement)
-
Documentation:
- Create or update `docs/DEPENDENCIES.md` with libmagic installation instructions
- Document optional dependencies in README.md or INSTALLATION.md
Technical approach:
```python
In test files that use magic
magic = pytest.importorskip('magic', reason="python-magic not installed - install libmagic system library")
Or for specific test functions
@pytest.mark.skipif(not has_magic, reason="Requires python-magic and libmagic")
def test_magic_based_detection():
...
```
Test Scenarios
Multiple test paths to verify:
-
With libmagic installed:
-
Without libmagic (fresh install):
-
Mock approach (optional):
-
CI/CD environments:
-
Error message verification:
Acceptance Criteria
Tests
Documentation
Error Messages
Validation
Dependencies
Python package: `python-magic>=0.4.27`
System library: `libmagic` (file type identification library)
- Part of the `file` command utilities
- Platform-specific installation:
- macOS: `brew install libmagic`
- Ubuntu/Debian: `apt-get install libmagic1`
- RHEL/CentOS: `yum install file-libs`
- Windows: Use `python-magic-bin` package (bundles libmagic DLL)
Alternative (pure Python): `puremagic` (optional consideration)
- No system dependencies
- Less accurate than libmagic but works everywhere
- Could be used as fallback layer before extension-based detection
Environment Requirements
Verified environments:
- Python 3.8+ (assumed based on autonomous-dev requirements)
- pytest 7.0+ (for importorskip functionality)
System library availability:
- libmagic is available on all major platforms but requires separate installation
- Not included in Python standard library or PyPI packages (except python-magic-bin for Windows)
Testing without libmagic:
- Should work on fresh Python environments
- Should work in minimal Docker containers
- Should work in CI/CD without apt-get/brew install steps
Source of Truth
Solution verified from:
-
pytest documentation - `pytest.importorskip()` pattern for optional dependencies
-
python-magic GitHub - Installation instructions and error handling patterns
-
autonomous-dev book_parser.py implementation - Current try/except pattern
- File: `plugins/autonomous-dev/lib/book_parser.py`
- Already handles ImportError gracefully at runtime
- Tests need to match this graceful handling
-
Best practices - setuptools optional-dependencies for optional features
- PEP 621 specification for pyproject.toml
- Allows `pip install autonomous-dev[mime-detection]` syntax
Related Issues
Summary
Tests fail when the libmagic system library is not installed, despite the book_parser.py code gracefully handling the missing dependency via try/except and runtime error messages. This creates a poor developer experience where 17 tests fail unnecessarily when libmagic is absent, even though the core functionality correctly falls back to extension-based file type detection.
What Does NOT Work
Patterns that FAIL:
Current test approach without conditional skipping - Tests that depend on python-magic fail hard when libmagic system library is not installed, even though the production code handles this gracefully with fallback logic.
Mock patching system-level dependencies - Attempting to mock libmagic at the system level doesn't work well because the dependency is loaded at module import time, and the missing C library causes import failures before tests can mock anything.
Assuming all developers have libmagic installed - This creates friction for new contributors who may not have the system library, leading to confusing test failures that don't reflect actual functionality problems.
Scenarios
Fresh Install
Update/Upgrade
User Customizations
Implementation Approach
Files to modify:
Tests using python-magic (identify via grep for `import magic` or `magic.from_file`):
pyproject.toml:
```toml
[project.optional-dependencies]
mime-detection = ["python-magic>=0.4.27"]
```
plugins/autonomous-dev/lib/book_parser.py (enhance error messages):
Documentation:
Technical approach:
```python
In test files that use magic
magic = pytest.importorskip('magic', reason="python-magic not installed - install libmagic system library")
Or for specific test functions
@pytest.mark.skipif(not has_magic, reason="Requires python-magic and libmagic")
def test_magic_based_detection():
...
```
Test Scenarios
Multiple test paths to verify:
With libmagic installed:
Without libmagic (fresh install):
Mock approach (optional):
CI/CD environments:
Error message verification:
Acceptance Criteria
Tests
Documentation
Error Messages
Validation
Dependencies
Python package: `python-magic>=0.4.27`
System library: `libmagic` (file type identification library)
Alternative (pure Python): `puremagic` (optional consideration)
Environment Requirements
Verified environments:
System library availability:
Testing without libmagic:
Source of Truth
Solution verified from:
pytest documentation - `pytest.importorskip()` pattern for optional dependencies
python-magic GitHub - Installation instructions and error handling patterns
autonomous-dev book_parser.py implementation - Current try/except pattern
Best practices - setuptools optional-dependencies for optional features
Related Issues