-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
137 lines (109 loc) · 3.58 KB
/
config_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
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
package git_test
import (
"testing"
git "github.com/purpleclay/gitz"
"github.com/purpleclay/gitz/gittest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestConfig(t *testing.T) {
gittest.InitRepository(t)
gittest.ConfigSet(t, "user.name", "joker", "user.email", "[email protected]")
client, _ := git.NewClient()
cfg, err := client.Config()
require.NoError(t, err)
assert.Equal(t, "joker", cfg["user.name"])
assert.Equal(t, "[email protected]", cfg["user.email"])
}
func TestConfigOnlyLatestValues(t *testing.T) {
gittest.InitRepository(t)
gittest.ConfigSet(t, "user.name", "joker", "user.name", "scarecrow")
client, _ := git.NewClient()
cfg, err := client.Config()
require.NoError(t, err)
assert.Equal(t, "scarecrow", cfg["user.name"])
}
func TestConfigL(t *testing.T) {
gittest.InitRepository(t)
gittest.ConfigSet(t, "user.name", "alfred")
client, _ := git.NewClient()
cfg, err := client.ConfigL("user.name", "user.email")
require.NoError(t, err)
require.Len(t, cfg["user.name"], 2)
assert.Equal(t, "alfred", cfg["user.name"][0])
assert.Equal(t, gittest.DefaultAuthorName, cfg["user.name"][1])
require.Len(t, cfg["user.email"], 1)
assert.Equal(t, gittest.DefaultAuthorEmail, cfg["user.email"][0])
}
func configEquals(t *testing.T, path, expected string) {
t.Helper()
cfg, err := gittest.Exec(t, "git config --get "+path)
require.NoError(t, err)
assert.Equal(t, expected, cfg)
}
func TestConfigSetL(t *testing.T) {
gittest.InitRepository(t)
client, _ := git.NewClient()
err := client.ConfigSetL("user.phobia", "bats", "user.birth.place", "gotham")
require.NoError(t, err)
configEquals(t, "user.phobia", "bats")
configEquals(t, "user.birth.place", "gotham")
}
func TestConfigSetLMismatchedPairsError(t *testing.T) {
gittest.InitRepository(t)
client, _ := git.NewClient()
err := client.ConfigSetL("user.hobbies")
assert.EqualError(t, err, "config paths mismatch. path: user.hobbies is missing a corresponding value")
}
func TestConfigSetLNothingSetIfError(t *testing.T) {
gittest.InitRepository(t)
client, _ := git.NewClient()
err := client.ConfigSetL("user.hobbies", "fighting crime", "user.arch.3nemy", "joker")
require.Error(t, err)
configMissing(t, "user.hobbies")
configMissing(t, "user.4rch.enemy")
}
func configMissing(t *testing.T, path string) {
t.Helper()
cfg, err := gittest.Exec(t, "git config --get "+path)
require.Error(t, err)
require.Empty(t, cfg)
}
func TestCheckConfigPathError(t *testing.T) {
tests := []struct {
name string
path string
errMsg string
}{
{
name: "InvalidMissingDot",
path: "nodot",
errMsg: "path: nodot invalid as dot separator is missing or is the last character",
},
{
name: "InvalidJustSection",
path: "section.only.",
errMsg: "path: section.only|.| invalid as dot separator is missing or is the last character",
},
{
name: "InvalidDigitAfterLastDot",
path: "a.bad.4pple",
errMsg: "path: a.bad.|4|pple invalid as first character after final dot must be a letter [a-zA-Z]",
},
{
name: "InvalidContainsNonAlphanumeric",
path: "no.$symbol.allowed",
errMsg: "path: no.|$|symbol.allowed invalid as non alphanumeric character detected [a-zA-Z0-9]",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.EqualError(t, git.CheckConfigPath(tt.path), tt.errMsg)
})
}
}
func TestToInlineConfig(t *testing.T) {
cfg, err := git.ToInlineConfig("user.name", "penguin", "user.email", "[email protected]")
require.NoError(t, err)
assert.ElementsMatch(t, []string{"-c user.name='penguin'", "-c user.email='[email protected]'"}, cfg)
}