-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathflag_text_test.go
More file actions
158 lines (128 loc) · 3.94 KB
/
Copy pathflag_text_test.go
File metadata and controls
158 lines (128 loc) · 3.94 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package cli
import (
"errors"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// errText is a TextMarshalUnmarshaler whose MarshalText always fails, used to
// exercise the error branches of textValue.ToString and textValue.String.
type errText struct{}
func (errText) MarshalText() ([]byte, error) { return nil, errors.New("marshal boom") }
func (errText) UnmarshalText([]byte) error { return nil }
func TestTextFlagSetFromArg(t *testing.T) {
lv := &slog.LevelVar{}
cmd := &Command{
Flags: []Flag{
&TextFlag{Name: "level", Value: lv},
},
}
require.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--level", "WARN"}))
assert.Equal(t, slog.LevelWarn, lv.Level())
}
func TestTextFlagDefaultValue(t *testing.T) {
lv := &slog.LevelVar{}
lv.Set(slog.LevelError)
cmd := &Command{
Flags: []Flag{
&TextFlag{Name: "level", Value: lv},
},
}
// Without the flag being passed, the value keeps its default.
require.NoError(t, cmd.Run(buildTestContext(t), []string{""}))
assert.Equal(t, slog.LevelError, lv.Level())
}
func TestTextFlagDestination(t *testing.T) {
lv := &slog.LevelVar{}
var dest TextMarshalUnmarshaler = lv
cmd := &Command{
Flags: []Flag{
&TextFlag{Name: "level", Destination: &dest},
},
}
require.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--level", "DEBUG"}))
assert.Equal(t, slog.LevelDebug, lv.Level())
}
func TestTextFlagTrimSpace(t *testing.T) {
lv := &slog.LevelVar{}
cmd := &Command{
Flags: []Flag{
&TextFlag{Name: "level", Value: lv, Config: StringConfig{TrimSpace: true}},
},
}
require.NoError(t, cmd.Run(buildTestContext(t), []string{"", "--level", " INFO "}))
assert.Equal(t, slog.LevelInfo, lv.Level())
}
func TestTextFlagFromEnvSource(t *testing.T) {
t.Setenv("LOG_LEVEL", "ERROR")
lv := &slog.LevelVar{}
cmd := &Command{
Flags: []Flag{
&TextFlag{Name: "level", Value: lv, Sources: EnvVars("LOG_LEVEL")},
},
}
require.NoError(t, cmd.Run(buildTestContext(t), []string{""}))
assert.Equal(t, slog.LevelError, lv.Level())
}
func TestTextFlagInvalidValue(t *testing.T) {
lv := &slog.LevelVar{}
cmd := &Command{
Flags: []Flag{
&TextFlag{Name: "level", Value: lv},
},
}
err := cmd.Run(buildTestContext(t), []string{"", "--level", "NOPE"})
require.Error(t, err)
}
func TestTextFlagValueFromCommand(t *testing.T) {
lv := &slog.LevelVar{}
f := &TextFlag{Name: "level", Value: lv}
cmd := &Command{
Flags: []Flag{f},
}
require.NoError(t, cmd.Set("level", "WARN"))
require.Equal(t, lv, cmd.Text(f.Name))
}
func TestTextFlagTextNotAvailable(t *testing.T) {
cmd := &Command{
Flags: []Flag{
&StringFlag{Name: "str"},
},
}
// The flag exists but its value is a string, not a
// TextMarshalUnmarshaler, so Text returns nil.
require.NoError(t, cmd.Set("str", "value"))
assert.Nil(t, cmd.Text("str"))
// An unknown flag name also returns nil.
assert.Nil(t, cmd.Text("missing"))
}
func TestTextValueToString(t *testing.T) {
var tv textValue
// A nil value marshals to the empty string.
assert.Equal(t, "", tv.ToString(nil))
// A MarshalText error is swallowed and yields the empty string.
assert.Equal(t, "", tv.ToString(errText{}))
// A value that marshals cleanly is rendered.
lv := &slog.LevelVar{}
lv.Set(slog.LevelWarn)
assert.Equal(t, "WARN", tv.ToString(lv))
}
func TestTextValueSetNilDestination(t *testing.T) {
// Set is a no-op when there is no destination to unmarshal into.
tv := &textValue{}
require.NoError(t, tv.Set("anything"))
}
func TestTextValueString(t *testing.T) {
// A nil destination stringifies to the empty string.
tv := &textValue{}
assert.Equal(t, "", tv.String())
// A MarshalText error is swallowed and yields the empty string.
tvErr := &textValue{destination: errText{}}
assert.Equal(t, "", tvErr.String())
// A destination that marshals cleanly is rendered.
lv := &slog.LevelVar{}
lv.Set(slog.LevelWarn)
tvOK := &textValue{destination: lv}
assert.Equal(t, "WARN", tvOK.String())
}