Skip to content

Test coverage: _attach.py error-handling and partial-init-cleanup paths untested (lowest-covered file, 75%) #317

Description

@codeforester

Problem

Same coverage run as the companion _cleanup.py/_private_files.py issue. lib/python/base_cli/_attach.py -- the Typer/Click attach adapter, per project context the newest and most structurally complex piece of base-cli -- is the lowest-covered file in the repo at 75% (87 of 378 statements missing, 26 of 124 branches missing).

The uncovered lines aren't scattered randomly; they concentrate in error-handling and partial-initialization-cleanup paths in _AttachedLifecycleResource:

  • __enter__ (lines 86-89, 120-125): the ConfigurationError/RuntimeDirectoryError -> click.UsageError/click.ClickException translation when context creation fails, and the except BaseException handler that records the failure outcome and calls self._finalize() when any part of __enter__ raises after the context was partially created. This second path is specifically the "something failed halfway through setup, now clean up what did get created" branch.
  • initialize_factories (lines 143-150): the same ConfigurationError/RuntimeDirectoryError translation when an extension's context_factory or service_factory raises.
  • Roughly 15 additional smaller gaps spread through the file (dialect/command-path resolution helpers around lines 185-265, 312-325, 344-388, 403-414, 504-593) that look like edge-case branches in Click/Typer command-tree walking rather than a single coherent story -- lower priority than the two above, listed for completeness.

Why this matters

Adapter/attachment code is exactly where partial-initialization bugs are expensive: if __enter__ raises after acquiring some resources (a telemetry session was started, a context was created) but the cleanup-on-failure path (self._finalize()) has never been exercised by a test, a real failure in the field could leak the resource or mask the original error instead of surfacing it cleanly. This is the highest-risk kind of gap in an adapter that every attached Typer app depends on.

Proposed change

Add tests that force each of the four failure injection points above (a ConfigurationError/RuntimeDirectoryError from app._create_context, a BaseException after partial context creation, and the same two exception types from context_factory/service_factory), asserting: the correct Click exception type is raised, record_exception/_record_lifecycle_diagnostic is called with the right outcome, and _finalize() runs cleanup exactly once.

Acceptance criteria

  • The four failure-injection scenarios above have direct tests.
  • _attach.py coverage moves closer to the repo average (87%); the smaller edge-case gaps can be picked up opportunistically rather than requiring a specific target.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or product improvement

Type

No type

Projects

  • Status
    Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions