Closing with structured markdown templates (30-80 line summaries linking PRs, follow-ups, gates) hits shell-escaping hell on Windows and brittle quoting in bash. `bd create` already solved this with --body-file <path> and `-` for stdin; close should match. - new --reason-file flag, with `-` reading from stdin - conflicts with --reason/--resolution/--message/--comment error out before any persistence - file-not-found errors quote the offending path - whitespace-only file content errors out (no silent empty close) - existing flag behavior is unchanged resolveReasonFile is exposed as a small testable helper to keep the close Run func clean while letting tests assert the conflict and empty-file branches without poking os.Exit. Fixes #3512
171 lines
4.6 KiB
Go
171 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// newCloseLikeCmd builds a minimal cobra command with the close reason flags
|
|
// registered, so resolveReasonFile can be exercised without spinning up the
|
|
// full CLI / database layer.
|
|
func newCloseLikeCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "close",
|
|
Run: func(cmd *cobra.Command, args []string) {},
|
|
}
|
|
cmd.Flags().StringP("reason", "r", "", "Reason for closing")
|
|
cmd.Flags().String("resolution", "", "")
|
|
cmd.Flags().StringP("message", "m", "", "")
|
|
cmd.Flags().String("comment", "", "")
|
|
cmd.Flags().String("reason-file", "", "Read close reason from file (use - for stdin)")
|
|
return cmd
|
|
}
|
|
|
|
func TestCloseResolveReasonFile_FileWithContent(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
path := filepath.Join(tmp, "reason.md")
|
|
content := "## Shipped\n\nWired up the thing. See PR #123.\n"
|
|
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
|
t.Fatalf("write fixture: %v", err)
|
|
}
|
|
|
|
cmd := newCloseLikeCmd()
|
|
if err := cmd.ParseFlags([]string{"--reason-file", path}); err != nil {
|
|
t.Fatalf("parse flags: %v", err)
|
|
}
|
|
|
|
got, ok, err := resolveReasonFile(cmd, "")
|
|
if err != nil {
|
|
t.Fatalf("resolveReasonFile: %v", err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("expected ok=true")
|
|
}
|
|
if got != content {
|
|
t.Errorf("content mismatch:\nwant: %q\ngot: %q", content, got)
|
|
}
|
|
}
|
|
|
|
func TestCloseResolveReasonFile_StdinDash(t *testing.T) {
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("pipe: %v", err)
|
|
}
|
|
oldStdin := os.Stdin
|
|
os.Stdin = r
|
|
t.Cleanup(func() { os.Stdin = oldStdin })
|
|
|
|
content := "Closed via stdin\nMulti-line reason\n"
|
|
go func() {
|
|
_, _ = w.WriteString(content)
|
|
_ = w.Close()
|
|
}()
|
|
|
|
cmd := newCloseLikeCmd()
|
|
if err := cmd.ParseFlags([]string{"--reason-file", "-"}); err != nil {
|
|
t.Fatalf("parse flags: %v", err)
|
|
}
|
|
|
|
got, ok, err := resolveReasonFile(cmd, "")
|
|
if err != nil {
|
|
t.Fatalf("resolveReasonFile: %v", err)
|
|
}
|
|
if !ok {
|
|
t.Fatal("expected ok=true")
|
|
}
|
|
if got != content {
|
|
t.Errorf("content mismatch:\nwant: %q\ngot: %q", content, got)
|
|
}
|
|
}
|
|
|
|
func TestCloseResolveReasonFile_FileNotFound(t *testing.T) {
|
|
missing := filepath.Join(t.TempDir(), "does-not-exist.md")
|
|
cmd := newCloseLikeCmd()
|
|
if err := cmd.ParseFlags([]string{"--reason-file", missing}); err != nil {
|
|
t.Fatalf("parse flags: %v", err)
|
|
}
|
|
|
|
_, ok, err := resolveReasonFile(cmd, "")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file, got nil")
|
|
}
|
|
if ok {
|
|
t.Error("expected ok=false on error")
|
|
}
|
|
// Path should be quoted in the error so users can see what was attempted.
|
|
if !strings.Contains(err.Error(), missing) {
|
|
t.Errorf("expected error to reference path %q, got: %v", missing, err)
|
|
}
|
|
}
|
|
|
|
func TestCloseResolveReasonFile_ConflictWithReason(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
path := filepath.Join(tmp, "reason.md")
|
|
if err := os.WriteFile(path, []byte("from file"), 0644); err != nil {
|
|
t.Fatalf("write fixture: %v", err)
|
|
}
|
|
|
|
cmd := newCloseLikeCmd()
|
|
if err := cmd.ParseFlags([]string{"--reason-file", path, "--reason", "from flag"}); err != nil {
|
|
t.Fatalf("parse flags: %v", err)
|
|
}
|
|
|
|
// Caller has already collected the inline reason from the various aliases,
|
|
// so we pass it in directly to mirror how close.go invokes the helper.
|
|
_, ok, err := resolveReasonFile(cmd, "from flag")
|
|
if err == nil {
|
|
t.Fatal("expected conflict error, got nil")
|
|
}
|
|
if ok {
|
|
t.Error("expected ok=false on conflict")
|
|
}
|
|
if !strings.Contains(err.Error(), "--reason-file") {
|
|
t.Errorf("expected conflict error to mention --reason-file, got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCloseResolveReasonFile_EmptyFile(t *testing.T) {
|
|
tmp := t.TempDir()
|
|
path := filepath.Join(tmp, "empty.md")
|
|
if err := os.WriteFile(path, []byte(" \n\t\n"), 0644); err != nil {
|
|
t.Fatalf("write fixture: %v", err)
|
|
}
|
|
|
|
cmd := newCloseLikeCmd()
|
|
if err := cmd.ParseFlags([]string{"--reason-file", path}); err != nil {
|
|
t.Fatalf("parse flags: %v", err)
|
|
}
|
|
|
|
_, ok, err := resolveReasonFile(cmd, "")
|
|
if err == nil {
|
|
t.Fatal("expected error for whitespace-only file, got nil")
|
|
}
|
|
if ok {
|
|
t.Error("expected ok=false on empty content")
|
|
}
|
|
if !strings.Contains(err.Error(), "empty") {
|
|
t.Errorf("expected error to mention 'empty', got: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCloseResolveReasonFile_FlagNotSet(t *testing.T) {
|
|
cmd := newCloseLikeCmd()
|
|
if err := cmd.ParseFlags([]string{"--reason", "inline"}); err != nil {
|
|
t.Fatalf("parse flags: %v", err)
|
|
}
|
|
|
|
got, ok, err := resolveReasonFile(cmd, "inline")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if ok {
|
|
t.Error("expected ok=false when --reason-file not set")
|
|
}
|
|
if got != "" {
|
|
t.Errorf("expected empty content, got %q", got)
|
|
}
|
|
}
|