Skip to content

test: autouse-stub get_adapters_from_hci to prevent host adapter leaks#280

Draft
bluetoothbot wants to merge 1 commit into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/conftest-stub-hci
Draft

test: autouse-stub get_adapters_from_hci to prevent host adapter leaks#280
bluetoothbot wants to merge 1 commit into
Bluetooth-Devices:mainfrom
bluetoothbot:koan/conftest-stub-hci

Conversation

@bluetoothbot

@bluetoothbot bluetoothbot commented May 27, 2026

Copy link
Copy Markdown
Contributor

What

Add tests/conftest.py with an autouse fixture that stubs get_adapters_from_hci to {} 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

  • New tests/conftest.py: a single @pytest.fixture(autouse=True) patches bluetooth_adapters.systems.linux.get_adapters_from_hci for the duration of every test.
  • Removed 11 redundant patch(... return_value={}) blocks from tests/test_init.py.
  • The one test that does need a non-empty HCI payload (test_get_adapters_linux_hci_only_*) keeps its explicit patch(...)unittest.mock stacks, so the inner patch wins and restores the autouse stub on exit.
  • tests/test_linux_hci.py is unaffected: it exercises systems.linux_hci.get_adapters_from_hci directly (different module ref), not the re-import in systems.linux.

Testing

poetry run pytest tests/ — 51 passed, coverage unchanged at 98%. Verified no remaining get_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

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

codecov Bot commented May 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.61%. Comparing base (ece711c) to head (cae612d).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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