Skip to content

fix: Skip test_returns_none_for_unreadable_file when running as root#2001

Open
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/test-skip-root-chmod
Open

fix: Skip test_returns_none_for_unreadable_file when running as root#2001
mohammedahmed18 wants to merge 1 commit intomainfrom
fix/test-skip-root-chmod

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Problem

Test test_returns_none_for_unreadable_file in tests/code_utils/test_config_js.py fails when running as root because the root user can read files regardless of chmod permissions.

Root Cause

On Linux, the root user (uid=0) bypasses file permission checks. When the test creates a file with chmod(0o000) to make it unreadable, root can still open and read it, causing no PermissionError to be raised.

Evidence:

# Test creates file and removes read permission
package_json.write_text("{}")
package_json.chmod(0o000)

# When running as root, this still succeeds:
result = get_package_json_data(package_json)
# Expected: None (file should be unreadable)
# Got: {} (root can read any file)

Impact

  • Severity: LOW (test-only bug, doesn't affect production code)
  • Affects: CI/Docker environments running as root
  • Test already skips on Windows for the same reason

Fix

Added os.getuid() == 0 check to the @pytest.mark.skipif decorator:

@pytest.mark.skipif(
    sys.platform == "win32" or os.getuid() == 0,
    reason="chmod doesn't restrict read access on Windows or when running as root"
)
def test_returns_none_for_unreadable_file(self, tmp_path: Path) -> None:
    ...

Testing

  • ✅ When running as root: Test skips (expected behavior)
  • ✅ When running as non-root: Test should pass (verifies permission handling)
  • ✅ All 80 other tests in the file pass
  • ✅ Linting passes (uv run prek)

Trace ID

Discovered during autoresearch:debug iteration 4/20 while investigating failing test suite.

**Problem:**
Test `test_returns_none_for_unreadable_file` fails when running as root
because the root user can read files regardless of `chmod` permissions.

**Root Cause:**
On Linux, root (uid=0) bypasses file permission checks. When the test
creates a file with `chmod(0o000)` to make it unreadable, root can still
open and read it, causing no `PermissionError` to be raised.

**Evidence:**
- Test expects `get_package_json_data()` to return `None` for unreadable file
- When running as root, file is successfully read and returns `{}`
- Running `python3 -m pytest tests/code_utils/test_config_js.py` as root fails

**Impact:**
- Severity: LOW (test-only bug, doesn't affect production code)
- Affects CI/Docker environments running as root
- Test already skips on Windows for similar reason

**Fix:**
Added `os.getuid() == 0` check to the `@pytest.mark.skipif` decorator.
Test now skips on both Windows and when running as root.

**Testing:**
- When running as root: Test skips (expected)
- When running as non-root: Test should pass (verifies permission handling)
- All 80 other tests in the file pass

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 6, 2026

Claude finished @mohammedahmed18's task in 1m 23s —— View job


PR Review Summary

Prek Checks

All checks pass — ruff lint and format both clean.

Code Review

Size: SMALL (1 test file, +5/-1 lines)

The fix is correct. On Linux, the root user bypasses DAC (discretionary access control), so chmod(0o000) does not prevent root from reading a file. Skipping the test when os.getuid() == 0 is the standard approach.

Windows safety: os.getuid does not exist on Windows, but since Python short-circuits or, os.getuid() == 0 is only evaluated when sys.platform == "win32" is False — so this is safe on Windows. ✅

No bugs, no security issues, no breaking changes.

Duplicate Detection

No duplicates detected. This pattern (skipping on Windows) already existed; the PR consistently extends it.

Test Coverage

N/A — only test code changed.


Verdict: LGTM. The fix is minimal, correct, and follows the same pattern already used in the skipif decorator.


Last updated: 2026-04-06T10:35Z

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.

1 participant