Skip to content

[Test Improver] test: expand coverage for update command (64% -> ~95%)#657

Merged
danielmeppiel merged 5 commits intomainfrom
test-assist/update-command-coverage-24221207307-9cbf3fb30797a8ce
Apr 21, 2026
Merged

[Test Improver] test: expand coverage for update command (64% -> ~95%)#657
danielmeppiel merged 5 commits intomainfrom
test-assist/update-command-coverage-24221207307-9cbf3fb30797a8ce

Conversation

@danielmeppiel
Copy link
Copy Markdown
Collaborator

🤖 Test Improver automated PR — an AI assistant focused on improving test quality for this repository.

Goal and Rationale

The apm update command had only 3 tests (64% coverage) covering the success-path installer logic. Many important code paths were untested:

  • Platform detection helpers (_is_windows_platform, _get_update_installer_url, etc.)
  • Error paths: dev version detection, failed version fetch, already-up-to-date check
  • Edge cases: PowerShell not found on Windows, /bin/sh fallback on Unix, requests not installed
  • Temp file cleanup verification

These are real user-facing code paths that can silently fail (e.g., wrong installer URL served to the wrong platform, missing PowerShell erroring without a clear message).

Approach

Added 21 new tests in two new test classes alongside the existing TestUpdateCommand:

  • TestUpdatePlatformHelpers: Pure unit tests for all platform-detection helper functions. No mocking of subprocess or network — just platform patches.
  • TestUpdateCommandLogic: Integration-style tests via CliRunner covering the full command flow for each error/success branch.

Coverage Impact

File Before After
src/apm_cli/commands/update.py 64% (32 miss) ~95% (few lines in outer except)

Total tests: 3790 (main) → 3811 (branch, +21)

Trade-offs

  • Tests mock requests.get, subprocess.run, and get_version — standard patterns for CLI command testing in this repo.
  • The requests ImportError test uses builtins.__import__ patching, which is slightly more involved but accurately tests the fallback path.

Reproducibility

uv run pytest tests/unit/test_update_command.py -v
uv run pytest tests/unit tests/test_console.py -x -q

Test Status

All 3811 tests pass (3790 pre-existing + 21 new).

Generated by Daily Test Improver ·

To install this agentic workflow, run

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

Add 21 new tests covering previously untested paths in update.py:

- Platform helper tests: _is_windows_platform(), _get_update_installer_url(),
  _get_update_installer_suffix(), _get_manual_update_command() (Unix path)
- _get_installer_run_command(): /bin/sh fallback, pwsh fallback, no PowerShell error
- Update command logic: dev version (with/without --check), can't fetch latest,
  already on latest, update available with --check, installer failure (exit 1),
  requests not available (exit 1), network error (exit 1), temp file cleanup

Co-authored-by: Copilot <[email protected]>
@danielmeppiel danielmeppiel added automation Deprecated: use type/automation. Kept for issue history; will be removed in milestone 0.10.0. testing Deprecated: use area/testing. Kept for issue history; will be removed in milestone 0.10.0. labels Apr 10, 2026
@danielmeppiel danielmeppiel marked this pull request as ready for review April 18, 2026 02:56
Copilot AI review requested due to automatic review settings April 18, 2026 02:56
Copy link
Copy Markdown
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

This PR expands unit/integration-style test coverage for the apm update command, aiming to exercise platform helper functions and additional success/error branches in src/apm_cli/commands/update.py.

Changes:

  • Added unit tests for platform detection + installer URL/suffix/runner command helpers.
  • Added CLI-driven tests for dev-version behavior, version-fetch failures, already-up-to-date, --check behavior, installer failures, network errors, missing requests, and temp-file cleanup.
Show a summary per file
File Description
tests/unit/test_update_command.py Adds new test classes to cover platform helper utilities and more apm update command branches via CliRunner.

Copilot's findings

  • Files reviewed: 1/1 changed files
  • Comments generated: 2

Comment on lines +169 to +170
def setUp(self):
self.runner = CliRunner()
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

apm update writes the installer script into get_apm_temp_dir() (env var / user config dependent). These tests invoke the command without pinning that temp dir, so they can fail on developer machines where APM_TEMP_DIR or ~/.apm/config.json points to a non-writable/non-existent location. Consider setting APM_TEMP_DIR to a temporary directory (or otherwise patching get_apm_temp_dir) in setUp so the tests are hermetic across environments.

Copilot uses AI. Check for mistakes.
original_unlink = update_module.os.unlink

def tracking_unlink(path):
deleted_paths.append(path)
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

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

original_unlink is assigned but never used, and tracking_unlink does not call through to the real os.unlink. That means this test will leave the temporary installer script on disk, which can pollute the configured/system temp directory over repeated runs. Consider calling the original unlink inside the tracking wrapper (or avoid patching unlink and assert cleanup via filesystem state in an isolated temp dir).

Suggested change
deleted_paths.append(path)
deleted_paths.append(path)
original_unlink(path)

Copilot uses AI. Check for mistakes.
@danielmeppiel danielmeppiel added CI/CD Deprecated: use area/ci-cd. Kept for issue history; will be removed in milestone 0.10.0. and removed CI/CD Deprecated: use area/ci-cd. Kept for issue history; will be removed in milestone 0.10.0. labels Apr 19, 2026
danielmeppiel and others added 3 commits April 20, 2026 00:03
Two findings on tests/unit/test_update_command.py:

- Both TestUpdateCommand and TestUpdateCommandLogic now pin
  APM_TEMP_DIR to a per-test tempfile.TemporaryDirectory in setUp and
  restore the previous value in tearDown. apm update writes its
  installer script via tempfile.NamedTemporaryFile(dir=
  get_apm_temp_dir()), so without this pin the tests inherit whatever
  APM_TEMP_DIR / ~/.apm/config.json point to on the developer machine
  and can fail or leak files into a non-hermetic location.
- test_update_temp_file_cleanup_on_success now calls the real
  os.unlink from tracking_unlink so the temporary installer script is
  actually removed instead of being left on disk every run.

All 24 tests in the file pass.

Co-authored-by: Copilot <[email protected]>
@danielmeppiel
Copy link
Copy Markdown
Collaborator Author

Addressed both Copilot review findings in 26023f6:

  • Both TestUpdateCommand and TestUpdateCommandLogic now pin APM_TEMP_DIR to a per-test tempfile.TemporaryDirectory in setUp and restore the previous value in tearDown, so the installer script written by apm update via tempfile.NamedTemporaryFile(dir=get_apm_temp_dir()) lands in a hermetic, writable location regardless of the developer machine's environment or ~/.apm/config.json.
  • test_update_temp_file_cleanup_on_success now calls the real os.unlink from tracking_unlink, so the temporary installer script is actually removed instead of leaking onto disk every run.

All 24 tests in the file pass.

@danielmeppiel danielmeppiel merged commit da1cbfd into main Apr 21, 2026
9 checks passed
@danielmeppiel danielmeppiel deleted the test-assist/update-command-coverage-24221207307-9cbf3fb30797a8ce branch April 21, 2026 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automation Deprecated: use type/automation. Kept for issue history; will be removed in milestone 0.10.0. testing Deprecated: use area/testing. Kept for issue history; will be removed in milestone 0.10.0.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants