summaryrefslogtreecommitdiff
path: root/internal/release/release_prompt_test.go
blob: fdb1d2748678e465b97bcc8c19b8d964dba99068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package release

import (
	"os"
	"testing"
)

// withStdin temporarily replaces os.Stdin with r for the duration of fn, then
// restores the original value. Not run in parallel with other tests since
// os.Stdin is a shared global.
func withStdin(t *testing.T, r *os.File, fn func()) {
	t.Helper()

	original := os.Stdin
	os.Stdin = r
	defer func() { os.Stdin = original }()

	fn()
}

// TestPromptConfirmation_EmptyInputDeclines verifies that when Scanln
// returns an error (e.g. a bare newline, which fmt.Scanln reports as
// "unexpected newline"), PromptConfirmation still safely defaults to
// declining rather than panicking or hanging - this is the discarded-error
// behavior that PromptConfirmation's fmt.Scanln call relies on.
func TestPromptConfirmation_EmptyInputDeclines(t *testing.T) {
	r, w, err := os.Pipe()
	if err != nil {
		t.Fatalf("failed to create pipe: %v", err)
	}
	defer func() { _ = r.Close() }()

	if _, err := w.WriteString("\n"); err != nil {
		t.Fatalf("failed to write to pipe: %v", err)
	}
	if err := w.Close(); err != nil {
		t.Fatalf("failed to close pipe writer: %v", err)
	}

	var got bool
	withStdin(t, r, func() {
		got = PromptConfirmation("Proceed?")
	})

	if got {
		t.Fatal("PromptConfirmation() = true for empty input, want false")
	}
}

// TestPromptConfirmation_YesInputConfirms is a regression check that the
// happy path (explicit "y") still works after touching the Scanln call.
func TestPromptConfirmation_YesInputConfirms(t *testing.T) {
	r, w, err := os.Pipe()
	if err != nil {
		t.Fatalf("failed to create pipe: %v", err)
	}
	defer func() { _ = r.Close() }()

	if _, err := w.WriteString("y\n"); err != nil {
		t.Fatalf("failed to write to pipe: %v", err)
	}
	if err := w.Close(); err != nil {
		t.Fatalf("failed to close pipe writer: %v", err)
	}

	var got bool
	withStdin(t, r, func() {
		got = PromptConfirmation("Proceed?")
	})

	if !got {
		t.Fatal("PromptConfirmation() = false for \"y\" input, want true")
	}
}