diff --git a/cmd/bd/defer.go b/cmd/bd/defer.go index 0a22b18be..762e02ccd 100644 --- a/cmd/bd/defer.go +++ b/cmd/bd/defer.go @@ -82,7 +82,10 @@ Examples: ctx := rootCtx - _, err := utils.ResolvePartialIDs(ctx, store, args) + // Resolve every argument before any write so one bad ID aborts the + // command. The loop reuses these IDs: resolving again would repeat + // the partial-ID notice (mc-qec7j). + resolvedIDs, err := utils.ResolvePartialIDs(ctx, store, args) if err != nil { return HandleError("%v", err) } @@ -93,13 +96,7 @@ Examples: return HandleErrorWithHint("database not initialized", diagHint()) } - for _, id := range args { - fullID, err := utils.ResolvePartialID(ctx, store, id) - if err != nil { - fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err) - continue - } - + for _, fullID := range resolvedIDs { updates := map[string]interface{}{ "status": string(types.StatusDeferred), } diff --git a/cmd/bd/defer_embedded_test.go b/cmd/bd/defer_embedded_test.go index 95f50afb6..7f5056749 100644 --- a/cmd/bd/defer_embedded_test.go +++ b/cmd/bd/defer_embedded_test.go @@ -40,6 +40,18 @@ func bdUndefer(t *testing.T, bd, dir string, args ...string) string { return stdout.String() } +// bdStderrAllowError runs bd with the given args and returns stderr and the +// exit error without failing the test. BD_NO_PARTIAL_ID_NOTICE is dropped so +// an inherited opt-out cannot hide the partial-ID notice a caller counts. +func bdStderrAllowError(t *testing.T, bd, dir string, args ...string) (string, error) { + t.Helper() + cmd := exec.Command(bd, args...) + cmd.Dir = dir + cmd.Env = envWithout(bdEnv(dir), "BD_NO_PARTIAL_ID_NOTICE") + _, stderr, err := runCommandBuffers(t, cmd) + return stderr.String(), err +} + // getIssueStatus returns the status of an issue via bd show --json. func getIssueStatus(t *testing.T, bd, dir, id string) string { t.Helper() @@ -140,6 +152,33 @@ func TestEmbeddedDefer(t *testing.T) { t.Errorf("expected 'Deferred' on second defer: %s", out) } }) + + // ===== Partial ID (mc-qec7j) ===== + + t.Run("defer_partial_id_notice_once", func(t *testing.T) { + // An explicit ID keeps the abbreviation unambiguous in this workspace. + issue := bdCreate(t, bd, dir, "Defer partial ID", "--type", "task", "--id", "df-qec7jnotice") + + // Every argument is resolved before any write, so one unresolvable + // argument still aborts the whole command. + if stderr, err := bdStderrAllowError(t, bd, dir, "defer", "qec7jnot", "df-qec7jmissing"); err == nil { + t.Errorf("bd defer with an unresolvable ID succeeded; want a non-zero exit\nstderr:\n%s", stderr) + } + if status := getIssueStatus(t, bd, dir, issue.ID); status != "open" { + t.Errorf("aborted bd defer changed %s to %q; want it left open", issue.ID, status) + } + + stderr, err := bdStderrAllowError(t, bd, dir, "defer", "qec7jnot") + if err != nil { + t.Fatalf("bd defer qec7jnot failed: %v\nstderr:\n%s", err, stderr) + } + if n := strings.Count(stderr, `"qec7jnot" is not an exact issue ID`); n != 1 { + t.Errorf("partial-ID notice printed %d times; want exactly once\nstderr:\n%s", n, stderr) + } + if status := getIssueStatus(t, bd, dir, issue.ID); status != "deferred" { + t.Errorf("expected status=deferred, got %q", status) + } + }) } // TestEmbeddedDeferConcurrent exercises defer operations concurrently. diff --git a/cmd/bd/undefer.go b/cmd/bd/undefer.go index b89907be8..b4b1b313b 100644 --- a/cmd/bd/undefer.go +++ b/cmd/bd/undefer.go @@ -45,7 +45,10 @@ Examples: ctx := rootCtx - _, err := utils.ResolvePartialIDs(ctx, store, args) + // Resolve every argument before any write so one bad ID aborts the + // command. The loop reuses these IDs: resolving again would repeat + // the partial-ID notice (mc-qec7j). + resolvedIDs, err := utils.ResolvePartialIDs(ctx, store, args) if err != nil { return HandleError("%v", err) } @@ -56,13 +59,7 @@ Examples: return HandleErrorWithHint("database not initialized", diagHint()) } - for _, id := range args { - fullID, err := utils.ResolvePartialID(ctx, store, id) - if err != nil { - fmt.Fprintf(os.Stderr, "Error resolving %s: %v\n", id, err) - continue - } - + for _, fullID := range resolvedIDs { issue, err := store.GetIssue(ctx, fullID) if err != nil { fmt.Fprintf(os.Stderr, "Error getting %s: %v\n", fullID, err) diff --git a/cmd/bd/undefer_embedded_test.go b/cmd/bd/undefer_embedded_test.go index 088044a95..4d7bcac6d 100644 --- a/cmd/bd/undefer_embedded_test.go +++ b/cmd/bd/undefer_embedded_test.go @@ -142,6 +142,34 @@ func TestEmbeddedUndefer(t *testing.T) { t.Errorf("expected defer_until cleared after undefer, got %v", deferUntil) } }) + + // ===== Partial ID (mc-qec7j) ===== + + t.Run("undefer_partial_id_notice_once", func(t *testing.T) { + // An explicit ID keeps the abbreviation unambiguous in this workspace. + issue := bdCreate(t, bd, dir, "Undefer partial ID", "--type", "task", "--id", "ud-qec7jnotice") + bdDefer(t, bd, dir, issue.ID) + + // Every argument is resolved before any write, so one unresolvable + // argument still aborts the whole command. + if stderr, err := bdStderrAllowError(t, bd, dir, "undefer", "qec7jnot", "ud-qec7jmissing"); err == nil { + t.Errorf("bd undefer with an unresolvable ID succeeded; want a non-zero exit\nstderr:\n%s", stderr) + } + if s := getIssueStatus(t, bd, dir, issue.ID); s != "deferred" { + t.Errorf("aborted bd undefer changed %s to %q; want it left deferred", issue.ID, s) + } + + stderr, err := bdStderrAllowError(t, bd, dir, "undefer", "qec7jnot") + if err != nil { + t.Fatalf("bd undefer qec7jnot failed: %v\nstderr:\n%s", err, stderr) + } + if n := strings.Count(stderr, `"qec7jnot" is not an exact issue ID`); n != 1 { + t.Errorf("partial-ID notice printed %d times; want exactly once\nstderr:\n%s", n, stderr) + } + if s := getIssueStatus(t, bd, dir, issue.ID); s != "open" { + t.Errorf("expected status=open after undefer, got %q", s) + } + }) } // TestEmbeddedUndeferConcurrent exercises undefer operations concurrently.