forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
177 lines (157 loc) · 3.69 KB
/
config_test.go
File metadata and controls
177 lines (157 loc) · 3.69 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package config
import (
"errors"
"testing"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type configStub map[string]string
func genKey(host, key string) string {
if host != "" {
return host + ":" + key
}
return key
}
func (c configStub) Get(host, key string) (string, error) {
val, _, err := c.GetWithSource(host, key)
return val, err
}
func (c configStub) GetWithSource(host, key string) (string, string, error) {
if v, found := c[genKey(host, key)]; found {
return v, "(memory)", nil
}
return "", "", errors.New("not found")
}
func (c configStub) Set(host, key, value string) error {
c[genKey(host, key)] = value
return nil
}
func (c configStub) Aliases() (*config.AliasConfig, error) {
return nil, nil
}
func (c configStub) Hosts() ([]string, error) {
return nil, nil
}
func (c configStub) UnsetHost(hostname string) {
}
func (c configStub) CheckWriteable(host, key string) error {
return nil
}
func (c configStub) Write() error {
c["_written"] = "true"
return nil
}
func TestConfigGet(t *testing.T) {
tests := []struct {
name string
config configStub
args []string
stdout string
stderr string
}{
{
name: "get key",
config: configStub{
"editor": "ed",
},
args: []string{"editor"},
stdout: "ed\n",
stderr: "",
},
{
name: "get key scoped by host",
config: configStub{
"editor": "ed",
"github.com:editor": "vim",
},
args: []string{"editor", "-h", "github.com"},
stdout: "vim\n",
stderr: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, stdout, stderr := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: io,
Config: func() (config.Config, error) {
return tt.config, nil
},
}
cmd := NewCmdConfigGet(f)
cmd.Flags().BoolP("help", "x", false, "")
cmd.SetArgs(tt.args)
cmd.SetOut(stdout)
cmd.SetErr(stderr)
_, err := cmd.ExecuteC()
require.NoError(t, err)
assert.Equal(t, tt.stdout, stdout.String())
assert.Equal(t, tt.stderr, stderr.String())
assert.Equal(t, "", tt.config["_written"])
})
}
}
func TestConfigSet(t *testing.T) {
tests := []struct {
name string
config configStub
args []string
expectKey string
expectVal string
stdout string
stderr string
}{
{
name: "set key",
config: configStub{},
args: []string{"editor", "vim"},
expectKey: "editor",
expectVal: "vim",
stdout: "",
stderr: "",
},
{
name: "set key scoped by host",
config: configStub{},
args: []string{"editor", "vim", "-h", "github.com"},
expectKey: "github.com:editor",
expectVal: "vim",
stdout: "",
stderr: "",
},
{
name: "set key",
config: configStub{},
args: []string{"unknownKey", "someValue"},
expectKey: "unknownKey",
expectVal: "someValue",
stdout: "",
stderr: "! warning: 'unknownKey' is not a known configuration key\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, stdout, stderr := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: io,
Config: func() (config.Config, error) {
return tt.config, nil
},
}
cmd := NewCmdConfigSet(f)
cmd.Flags().BoolP("help", "x", false, "")
cmd.SetArgs(tt.args)
cmd.SetOut(stdout)
cmd.SetErr(stderr)
_, err := cmd.ExecuteC()
require.NoError(t, err)
assert.Equal(t, tt.stdout, stdout.String())
assert.Equal(t, tt.stderr, stderr.String())
assert.Equal(t, tt.expectVal, tt.config[tt.expectKey])
assert.Equal(t, "true", tt.config["_written"])
})
}
}