diff --git a/internal/cli/root_command_execution_test.go b/internal/cli/root_command_execution_test.go index 1579594b..3685553a 100644 --- a/internal/cli/root_command_execution_test.go +++ b/internal/cli/root_command_execution_test.go @@ -212,6 +212,33 @@ func TestRunsPurgeCommandRemovesTerminalRunArtifactsOldestFirst(t *testing.T) { } } +func TestRunsPurgeCommandHonorsCanceledCommandContext(t *testing.T) { + homeDir := t.TempDir() + t.Setenv("HOME", homeDir) + + paths, err := productizeconfig.ResolveHomePaths() + if err != nil { + t.Fatalf("ResolveHomePaths() error = %v", err) + } + if err := productizeconfig.EnsureHomeLayout(paths); err != nil { + t.Fatalf("EnsureHomeLayout() error = %v", err) + } + withWorkingDir(t, t.TempDir()) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + cmd := NewRootCommand() + cmd.SetContext(ctx) + output, err := executeCommandCombinedOutput(cmd, nil, "runs", "purge") + if !errors.Is(err, context.Canceled) { + t.Fatalf("runs purge error = %v, want context.Canceled\noutput:\n%s", err, output) + } + if strings.Contains(output, "purged") { + t.Fatalf("expected canceled runs purge to skip the sweep, got output:\n%s", output) + } +} + func TestExecCommandWithInstalledWorkspaceExtensionStaysEphemeralWithoutFlag(t *testing.T) { workspaceRoot, recordPath := prepareWorkspaceExtensionFixtureForCLI(t, "normal") withWorkingDir(t, workspaceRoot) diff --git a/internal/cli/runs.go b/internal/cli/runs.go index 0045e89c..eb412655 100644 --- a/internal/cli/runs.go +++ b/internal/cli/runs.go @@ -1,7 +1,6 @@ package cli import ( - "context" "fmt" "strings" @@ -79,7 +78,8 @@ func newRunsPurgeCommand() *cobra.Command { Short: "Delete terminal run artifacts according to configured retention", SilenceUsage: true, RunE: func(cmd *cobra.Command, _ []string) error { - ctx := context.Background() + ctx, stop := signalCommandContext(cmd) + defer stop() settings, _, err := daemon.LoadRunLifecycleSettings(ctx) if err != nil {