test: autouse-stub get_adapters_from_hci to prevent host adapter leaks#280
Draft
bluetoothbot wants to merge 1 commit into
Draft
test: autouse-stub get_adapters_from_hci to prevent host adapter leaks#280bluetoothbot wants to merge 1 commit into
bluetoothbot wants to merge 1 commit into
Conversation
Closed issue Bluetooth-Devices#118 ("Tests fail when run on hardware with a bluetooth adaptor") because LinuxAdapters.refresh() pulls from two sources — BlueZ via D-Bus AND a direct kernel HCI socket query. Each test had to remember to patch get_adapters_from_hci or the kernel call leaked real host adapters into the result. Move that defense into a tests/conftest.py autouse fixture so it can't be forgotten, and drop the now-redundant per-test patches. The one test that needed a non-empty HCI payload keeps its explicit nested patch (overrides the autouse and restores it on exit). No behavior change. 51 tests pass.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #280 +/- ##
=======================================
Coverage 99.61% 99.61%
=======================================
Files 14 14
Lines 522 522
Branches 48 48
=======================================
Hits 520 520
Partials 2 2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Add
tests/conftest.pywith an autouse fixture that stubsget_adapters_from_hcito{}for every test, and drop the 11 per-test patches that did the same thing.Why
Issue #118 was closed by teaching every Linux-backend test to
patch(\"bluetooth_adapters.systems.linux.get_adapters_from_hci\", return_value={}). That works, but the defense lives at every call site — any new test that forgets the patch lets the kernel HCI socket query leak real host adapters into the result.LinuxAdapters.refresh()has two independent adapter sources (BlueZ via D-Bus and the direct kernel HCI socket query). Mocking only the D-Bus side is not enough, and that asymmetry is exactly what #118 documented.How
tests/conftest.py: a single@pytest.fixture(autouse=True)patchesbluetooth_adapters.systems.linux.get_adapters_from_hcifor the duration of every test.patch(... return_value={})blocks fromtests/test_init.py.test_get_adapters_linux_hci_only_*) keeps its explicitpatch(...)—unittest.mockstacks, so the inner patch wins and restores the autouse stub on exit.tests/test_linux_hci.pyis unaffected: it exercisessystems.linux_hci.get_adapters_from_hcidirectly (different module ref), not the re-import insystems.linux.Testing
poetry run pytest tests/— 51 passed, coverage unchanged at 98%. Verified no remainingget_adapters_from_hci", return_value={}site in the test suite (only the intentional non-empty override survives).🤖 Generated with Claude Code
Quality Report
Changes: 2 files changed, 30 insertions(+), 33 deletions(-)
Code scan: clean
Tests: passed (1
PASSED)
Branch hygiene: clean
Generated by Kōan post-mission quality pipeline