-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdRoot_test.go
167 lines (145 loc) · 4.49 KB
/
cmdRoot_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
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
package main
import (
"context"
"errors"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlagResolution(t *testing.T) {
testCases := []struct {
name string
envVar string
flagArgs []string
wantOutput string
}{
{
name: "If no flag value is provided, the default value (gino_keva) should be used",
envVar: "",
flagArgs: []string{},
wantOutput: "gino_keva",
},
{
name: "Set notes ref flag via command line",
envVar: "",
flagArgs: []string{"--ref", "From_Command_Line"},
wantOutput: "From_Command_Line",
},
{
name: "Set notes ref flag with an environment variable",
envVar: "From_Environment",
flagArgs: []string{},
wantOutput: "From_Environment",
},
{
name: "Overwrite notes ref flag set in environment via command line",
envVar: "From_Environment",
flagArgs: []string{"--ref", "From_Command_Line"},
wantOutput: "From_Command_Line",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
root := NewRootCommand()
os.Setenv("GINO_KEVA_REF", tc.envVar)
defer os.Unsetenv("GINO_KEVA_REF")
listFlagArgs := []string{"show-flag", "ref"}
args := append(listFlagArgs, tc.flagArgs...)
ctx := ContextWithGitWrapper(context.Background(), ¬esStub{})
gotOutput, err := executeCommandContext(ctx, root, args...)
assert.NoError(t, err)
assert.Equal(t, tc.wantOutput, gotOutput)
})
}
}
func TestFetchFlag(t *testing.T) {
testCases := []struct {
name string
args []string
wantFetchCalled bool
}{
{
name: "Fetch is called when calling list (default)",
args: []string{"list"},
wantFetchCalled: true,
},
{
name: "Fetch is NOT called if set to false",
args: disableFetch([]string{"list"}),
wantFetchCalled: false,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var fetchCalled bool
gitWrapper := ¬esStub{
fetchNotesImplementation: spyArgsString(&fetchCalled, nil),
logCommitsImplementation: dummyStubArgsNone,
notesListImplementation: dummyStubArgsString,
notesShowImplementation: dummyStubArgsStringString,
}
ctx := ContextWithGitWrapper(context.Background(), gitWrapper)
root := NewRootCommand()
_, err := executeCommandContext(ctx, root, tc.args...)
assert.NoError(t, err)
assert.Equal(t, tc.wantFetchCalled, fetchCalled)
})
}
}
func TestPushFlag(t *testing.T) {
testCases := []struct {
name string
args []string
wantPushCalled bool
}{
{
name: "Push is NOT called when flag is unset (default)",
args: []string{"set", "foo", "bar"},
wantPushCalled: false,
},
{
name: "Push is called when flag is set",
args: enablePush([]string{"set", "foo", "bar"}),
wantPushCalled: true,
}}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var pushCalled bool
gitWrapper := ¬esStub{
pushNotesImplementation: spyArgsString(&pushCalled, nil),
revParseHeadImplementation: responseStubArgsNone(TestDataDummyHash),
logCommitsImplementation: dummyStubArgsNone,
notesAddImplementation: dummyStubArgsStringString,
notesListImplementation: dummyStubArgsString,
notesShowImplementation: dummyStubArgsStringString,
}
ctx := ContextWithGitWrapper(context.Background(), gitWrapper)
root := NewRootCommand()
args := disableFetch(tc.args)
_, err := executeCommandContext(ctx, root, args...)
assert.NoError(t, err)
assert.Equal(t, tc.wantPushCalled, pushCalled)
})
}
}
func TestFetchNoUpstreamRef(t *testing.T) {
var fetchStubNoUpstreamRef = func(string) (response string, err error) {
// Mimic error when there's no upstream ref present yet
err = errors.New("exit status 128")
response = "fatal: couldn't find remote ref refs/notes/foo"
return response, err
}
t.Run("Fetch without upstream notesref doesn't result in error", func(t *testing.T) {
root := NewRootCommand()
gitWrapper := ¬esStub{
fetchNotesImplementation: fetchStubNoUpstreamRef,
logCommitsImplementation: dummyStubArgsNone,
notesListImplementation: dummyStubArgsString,
notesShowImplementation: dummyStubArgsStringString,
}
ctx := ContextWithGitWrapper(context.Background(), gitWrapper)
args := []string{"list"}
gotOutput, err := executeCommandContext(ctx, root, args...)
assert.NoError(t, err)
assert.Equal(t, TestDataEmptyString, gotOutput)
})
}