Skip to content

Commit c5c08d1

Browse files
author
wildleo91
committed
test(limits): patch wl_limits.fcntl in 2 mocked-open write tests
Both test_set_limit_config_valid and test_write_daily_limits_success mock builtins.open() with mock_open(). On Python 3.9 the resulting Mock fd was leniently accepted by fcntl.flock; on Python 3.11+ fcntl.flock raises TypeError: fileno() returned a non-integer. The bug was masked for the entire round-7 window because the install step itself failed (pytest 9.0.3 requires Python >=3.10 but CI used 3.9), so these tests never executed. Phase 0.2's Python bump unmasked them. Fix: patch `wl_limits.fcntl` to a MagicMock in both tests so the inline flock calls become no-ops. Test semantics unchanged — neither test asserts on what was written to disk; they only verify the "happy path" doesn't raise and (for the second) that open() was invoked. Other tests in test_limits.py use mock_open(read_data=...) or side_effect=OSError, neither of which reaches a flock call, so no other tests need this patch. Verified locally: - tests/unit/test_limits.py::test_set_limit_config_valid PASS - tests/unit/test_limits.py::test_write_daily_limits_success PASS - Full test_limits.py: 45/45 pass CI on Linux/Python 3.11 will exercise the actual fcntl path and is the real gate; local Windows skips the path because fcntl is None on Windows.
1 parent b4757ec commit c5c08d1

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

tests/unit/test_limits.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,21 @@ def test_set_limit_config_valid(mock_limit_config):
443443
"""Test: valid config written successfully.
444444
445445
set_limit_config delegates to write_limit_config which uses
446-
fcntl directly (not a file_lock context manager). We only need
447-
to mock open + os.replace to keep the test off-disk.
446+
fcntl directly (not a file_lock context manager). We mock
447+
open + os.replace to keep the test off-disk, AND patch
448+
wl_limits.fcntl to a MagicMock so the inline flock calls do
449+
not attempt to lock a Mock file descriptor. The latter is
450+
Python-3.11-mandatory: fcntl.flock rejects non-int fds with
451+
TypeError on 3.11+, while 3.9 happened to be more lenient.
448452
"""
449453
with patch('wl_limits._get_limit_config_path',
450454
return_value="/tmp/limit_config.json"):
451455
with patch('builtins.open', mock_open()):
452456
with patch('os.replace'):
453-
success, error = wl_limits.set_limit_config(mock_limit_config)
454-
assert success is True
455-
assert error == ""
457+
with patch('wl_limits.fcntl'):
458+
success, error = wl_limits.set_limit_config(mock_limit_config)
459+
assert success is True
460+
assert error == ""
456461

457462

458463
@pytest.mark.unit
@@ -654,14 +659,19 @@ def test_write_daily_limits_success():
654659
"""Test: write_daily_limits writes atomically.
655660
656661
The new API returns None (raises on failure). Verify it
657-
completes without raising and that open() was called."""
662+
completes without raising and that open() was called.
663+
664+
Also patches wl_limits.fcntl so the inline flock call does
665+
not attempt to lock a Mock fd — same Python-3.11-mandatory
666+
fix as test_set_limit_config_valid above."""
658667
test_data = {"2026-04-01": {"jsmith": {"row_removal": 5}}}
659668
with patch('wl_limits._get_daily_limits_path',
660669
return_value="/tmp/daily_limits.json"):
661670
with patch('builtins.open', mock_open()) as mocked_open:
662671
with patch('os.replace'):
663-
wl_limits.write_daily_limits(test_data)
664-
assert mocked_open.called
672+
with patch('wl_limits.fcntl'):
673+
wl_limits.write_daily_limits(test_data)
674+
assert mocked_open.called
665675

666676

667677
@pytest.mark.unit

0 commit comments

Comments
 (0)