-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathconsole_test.go
55 lines (49 loc) · 1.25 KB
/
console_test.go
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
package utils
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/supabase/cli/internal/testing/fstest"
)
func TestPromptYesNo(t *testing.T) {
t.Run("defaults on closed stdin", func(t *testing.T) {
c := NewConsole()
c.IsTTY = false
// Run test
val, err := c.PromptYesNo(context.Background(), "test", false)
// Check error
assert.NoError(t, err)
assert.False(t, val)
})
t.Run("parses piped stdin", func(t *testing.T) {
t.Cleanup(fstest.MockStdin(t, "y"))
c := NewConsole()
// Run test
val, err := c.PromptYesNo(context.Background(), "test", false)
// Check error
assert.NoError(t, err)
assert.True(t, val)
})
}
func TestPromptText(t *testing.T) {
t.Run("defaults on timeout", func(t *testing.T) {
t.Cleanup(fstest.MockStdin(t, ""))
c := NewConsole()
// Run test
val, err := c.PromptText(context.Background(), "test")
// Check error
assert.NoError(t, err)
assert.Empty(t, val)
})
t.Run("throws error on cancel", func(t *testing.T) {
c := NewConsole()
// Setup cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
// Run test
val, err := c.PromptText(ctx, "test")
// Check error
assert.ErrorIs(t, err, context.Canceled)
assert.Empty(t, val)
})
}