Skip to content

[Test Improver] test: add unit tests for prune command (14% -> 91%)#415

Merged
danielmeppiel merged 5 commits intomainfrom
test-assist/prune-command-coverage-23417039631-f0756e2a8bb295bf
Mar 23, 2026
Merged

[Test Improver] test: add unit tests for prune command (14% -> 91%)#415
danielmeppiel merged 5 commits intomainfrom
test-assist/prune-command-coverage-23417039631-f0756e2a8bb295bf

Conversation

@danielmeppiel
Copy link
Collaborator

🤖 Test Improver - automated AI assistant for test improvements.

Goal and Rationale

The apm prune command had only 14% test coverage despite being a user-facing CLI command that mutates the filesystem (removes package directories, cleans deployed integration files, and updates/deletes the lockfile). Low coverage on destructive commands is a high-risk gap.

Approach

Added tests/unit/test_prune_command.py with 20 tests using the Click test runner and real temp directories (no mocking of filesystem operations, following the pattern used in test_deps_clean_command.py). Tests exercise:

  • Missing apm.yml → exit 1 with error message
  • Missing apm_modules/ → clean exit 0 with info
  • Clean state (all installed packages declared) → "No orphaned packages" message
  • --dry-run with orphans → lists them but leaves files on disk; confirms "Dry run" in output
  • --dry-run with multiple orphans → all listed
  • Actual removal of orphaned packages (single and multiple)
  • Declared packages preserved when orphans are removed
  • Count reported in success message
  • Invalid apm.yml → exit 1 with error
  • safe_rmtree failure → error reported, no crash
  • Lockfile cleanup: entry removed for pruned package
  • Deployed files cleanup: integration files listed in lockfile are deleted
  • Lockfile deleted when all entries are pruned
  • Lockfile preserved with correct entries when only some packages pruned
  • Works without lockfile present

Key discovery: the current lockfile filename is apm.lock.yaml (not apm.lock); tests use _write_lockfile() helper that writes to the correct path.

Coverage Impact

File Before After
src/apm_cli/commands/prune.py 14% 91%

Remaining uncovered lines (9/103): directory-cleanup branch inside deployed-file deletion loop, and the outer exception handler at line 148.

Test Status

All 2613 unit tests pass (20 new + 2593 existing).

uv run pytest tests/unit/test_prune_command.py -v
# 20 passed in 0.31s

uv run pytest tests/unit/ -q
# 2613 passed in 10.08s

Trade-offs

  • Uses real temp directories (slower than pure mocks, but catches integration issues)
  • clear_apm_yml_cache() called in teardown to prevent cache leakage between tests
  • No new dependencies introduced

Generated by Daily Test Improver ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/daily-test-improver.md@b87234850bf9664d198f28a02df0f937d0447295

20 tests covering:
- Missing apm.yml (error + exit 1)
- Missing apm_modules/ (no-op exit 0)
- Clean state reporting
- --dry-run shows orphans without removing
- Orphan removal (single and multiple packages)
- Declared packages preserved during prune
- apm.yml parse error handling
- safe_rmtree failure graceful handling
- Lockfile entry cleanup for pruned packages
- Deployed files cleanup from lockfile
- Lockfile deletion when empty after prune
- Lockfile preservation for remaining packages
- Works without lockfile present

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@danielmeppiel danielmeppiel marked this pull request as ready for review March 23, 2026 08:04
Copilot AI review requested due to automatic review settings March 23, 2026 08:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new unit test suite for the user-facing apm prune CLI command to substantially increase coverage around orphan removal, dry-run behavior, lockfile updates, and deployed-file cleanup.

Changes:

  • Introduces tests/unit/test_prune_command.py with Click CliRunner tests using real temp directories.
  • Covers key prune scenarios: missing apm.yml, missing apm_modules/, clean state, dry-run, actual removal, YAML parse failure, and safe_rmtree failure handling.
  • Adds lockfile/deployed-file cleanup assertions for pruned dependencies.
Comments suppressed due to low confidence (1)

tests/unit/test_prune_command.py:393

  • Docstring says "no apm.lock exists", but the current lockfile name is apm.lock.yaml. Please update the wording so the test documentation matches the behavior under test.
    def test_prune_works_without_lockfile(self):
        """prune removes orphaned packages even when no apm.lock exists."""
        with self._chdir_tmp() as tmp:
            (tmp / "apm.yml").write_text(_APM_YML_NO_DEPS)
            orphan_dir = _make_package_dir(tmp, "orphan-org", "orphan-repo")
            # No apm.lock created

@danielmeppiel
Copy link
Collaborator Author

@copilot address pr review comments

Copy link
Contributor

Copilot AI commented Mar 23, 2026

@danielmeppiel I've opened a new pull request, #419, to work on those changes. Once the pull request is ready, I'll request review from you.

Copilot AI and others added 3 commits March 23, 2026 10:44
* Initial plan

* fix: address PR review comments on prune command tests

Co-authored-by: danielmeppiel <51440732+danielmeppiel@users.noreply.github.com>
Agent-Logs-Url: https://github.com/microsoft/apm/sessions/cad00612-e8f7-4f03-83f7-23a5cbdd84dc

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: danielmeppiel <51440732+danielmeppiel@users.noreply.github.com>
@danielmeppiel danielmeppiel merged commit 0408a51 into main Mar 23, 2026
6 checks passed
@danielmeppiel danielmeppiel deleted the test-assist/prune-command-coverage-23417039631-f0756e2a8bb295bf branch March 23, 2026 15:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants