File: prompt_test.go

package info (click to toggle)
cosign 2.6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,184 kB
  • sloc: sh: 229; makefile: 170
file content (72 lines) | stat: -rw-r--r-- 2,398 bytes parent folder | download | duplicates (2)
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
// Copyright 2023 The Sigstore Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui_test

import (
	"bytes"
	"context"
	"errors"
	"testing"

	"github.com/sigstore/cosign/v2/internal/ui"
	"github.com/stretchr/testify/assert"
)

func TestConfirm(t *testing.T) {
	cases := []struct {
		name            string
		input           string
		expected        error
		expectedMessage string
	}{
		{"no", "n\n", &ui.ErrPromptDeclined{}, "user declined"},
		{"no-upper", "N\n", &ui.ErrPromptDeclined{}, "user declined"},
		{"yes", "y\n", nil, ""},
		{"yes-upper", "Y\n", nil, ""},
		{"default", "\n", &ui.ErrPromptDeclined{}, "user declined"},
		{"empty", "", &ui.ErrPromptDeclined{}, "user declined"},
		{"invalid", "yy", &ui.ErrInvalidInput{Got: "yy", Allowed: "y, n"}, "invalid input"},
		{"no-windows", "n\r\n", &ui.ErrPromptDeclined{}, "user declined"},
		{"yes-windows", "y\r\n", nil, ""},
		{"default-windows", "\r\n", &ui.ErrPromptDeclined{}, "user declined"},
		{"invalid", "yy\r\n", &ui.ErrInvalidInput{Got: "yy", Allowed: "y, n"}, "invalid input"},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			stderr := ui.RunWithTestCtx(func(ctx context.Context, write ui.WriteFunc) {
				write(tc.input)
				err := ui.ConfirmContinue(ctx)
				assert.EqualValues(t, tc.expected, err)
				if len(tc.expectedMessage) > 0 {
					assert.ErrorContains(t, err, "")
				}
			})
			assert.Equal(t, "Are you sure you would like to continue? [y/N] ", stderr, "Bad output to STDERR")
		})
	}
}

type BadReader struct{}

// BadReader implements Reader.
func (b *BadReader) Read(p []byte) (n int, err error) { //nolint: revive
	return 0, errors.New("my error")
}

func TestConfirmError(t *testing.T) {
	var stderr bytes.Buffer
	stdin := BadReader{}
	ctx := ui.WithEnv(context.Background(), &ui.Env{&stderr, &stdin})
	assert.ErrorContains(t, ui.ConfirmContinue(ctx), "my error")
}