Skip to content

agent: tests that set usethis_config attributes directly must use context manager or rely on conftest reset #1812

@nathanjmcdougall

Description

@nathanjmcdougall

Lesson learned from PR #1795 (infer dependencies from setup.cfg)

When writing tests that set usethis_config.backend or usethis_config.inferred_backend directly (e.g. usethis_config.backend = BackendEnum.poetry), the state persists to subsequent tests because there is no cleanup. This caused spurious test failures in CI:

  • Tests in TestGetProjectDeps::TestPoetry and TestGetDepGroups::TestPoetry (newly added) set usethis_config.backend = BackendEnum.poetry directly inside a with block, which left inferred_backend stale after the test completed.
  • Subsequent tests that relied on backend auto-detection (e.g. TestAddDepsToGroup::test_no_pyproject_toml) received the stale inferred_backend = poetry and tried to call the Poetry subprocess, which is broken in the 'min dependencies' CI environment.
  • TestGetDepGroups::TestPoetry::test_not_read_without_poetry_backend failed because it set backend = none directly (not via context manager), but inferred_backend was already cached as poetry from prior tests.

Root cause

usethis_config.set(backend=...) (the context manager) saves and restores both backend AND inferred_backend. Direct attribute assignment (usethis_config.backend = ...) only updates backend, leaving inferred_backend stale. get_backend() in dispatch.py checks inferred_backend first (line 21) before doing any detection, so stale inferred_backend poisons all subsequent get_backend() calls.

Fix applied

Added usethis_config.backend = BackendEnum(BACKEND_DEFAULT) and usethis_config.inferred_backend = None to the clear_functools_caches autouse fixture in tests/conftest.py. This ensures clean backend state at the start of each test, preventing cross-test pollution.

Recommendation

  • Always use usethis_config.set(backend=...) as a context manager in tests, not direct attribute assignment.
  • When that is not possible, rely on the autouse fixture reset now present in tests/conftest.py.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions