Skip to content

Commit 1725400

Browse files
fix: move InstallLogger creation before scope init to prevent UnboundLocalError, add exit code assertion to --global test
Co-authored-by: sergio-sisternes-epam <207026618+sergio-sisternes-epam@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/apm/sessions/0920712b-bd70-4b27-a30e-55b6c1fe6700
1 parent 4d465a0 commit 1725400

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

src/apm_cli/commands/install.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,12 @@ def install(ctx, packages, runtime, exclude, only, update, dry_run, force, verbo
543543
apm install -g org/pkg1 # Install to user scope (~/.apm/)
544544
"""
545545
try:
546+
# Create structured logger for install output early so exception
547+
# handlers can always reference it (avoids UnboundLocalError if
548+
# scope initialisation below throws).
549+
is_partial = bool(packages)
550+
logger = InstallLogger(verbose=verbose, dry_run=dry_run, partial=is_partial)
551+
546552
# Resolve scope
547553
from ..core.scope import InstallScope, get_deploy_root, get_apm_dir, get_manifest_path, get_modules_dir, ensure_user_dirs, warn_unsupported_user_scope
548554
scope = InstallScope.USER if global_ else InstallScope.PROJECT
@@ -561,10 +567,6 @@ def install(ctx, packages, runtime, exclude, only, update, dry_run, force, verbo
561567
# Display name for messages (short for project scope, full for user scope)
562568
manifest_display = str(manifest_path) if scope is InstallScope.USER else APM_YML_FILENAME
563569

564-
# Create structured logger for install output
565-
is_partial = bool(packages)
566-
logger = InstallLogger(verbose=verbose, dry_run=dry_run, partial=is_partial)
567-
568570
# Check if apm.yml exists
569571
apm_yml_exists = manifest_path.exists()
570572

tests/unit/test_install_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ def test_global_flag_shows_scope_info(self):
635635
fake_home.mkdir()
636636
with patch.object(Path, "home", return_value=fake_home):
637637
result = self.runner.invoke(cli, ["install", "--global"])
638+
assert result.exit_code == 1
638639
assert "user scope" in result.output.lower() or "~/.apm/" in result.output
639640
# Should warn about unsupported targets
640641
assert "copilot" in result.output.lower()

0 commit comments

Comments
 (0)