9
9
"github.com/stretchr/testify/assert"
10
10
)
11
11
12
- func TestSetRefAndSha (t * testing.T ) {
12
+ func TestSetRef (t * testing.T ) {
13
13
log .SetLevel (log .DebugLevel )
14
14
15
15
oldFindGitRef := findGitRef
@@ -29,38 +29,27 @@ func TestSetRefAndSha(t *testing.T) {
29
29
eventName string
30
30
event map [string ]interface {}
31
31
ref string
32
- sha string
33
32
}{
34
33
{
35
34
eventName : "pull_request_target" ,
36
- event : map [string ]interface {}{
37
- "pull_request" : map [string ]interface {}{
38
- "base" : map [string ]interface {}{
39
- "sha" : "pr-base-sha" ,
40
- },
41
- },
42
- },
43
- ref : "refs/heads/master" ,
44
- sha : "pr-base-sha" ,
35
+ event : map [string ]interface {}{},
36
+ ref : "refs/heads/master" ,
45
37
},
46
38
{
47
39
eventName : "pull_request" ,
48
40
event : map [string ]interface {}{
49
41
"number" : 1234. ,
50
42
},
51
43
ref : "refs/pull/1234/merge" ,
52
- sha : "1234fakesha" ,
53
44
},
54
45
{
55
46
eventName : "deployment" ,
56
47
event : map [string ]interface {}{
57
48
"deployment" : map [string ]interface {}{
58
49
"ref" : "refs/heads/somebranch" ,
59
- "sha" : "deployment-sha" ,
60
50
},
61
51
},
62
52
ref : "refs/heads/somebranch" ,
63
- sha : "deployment-sha" ,
64
53
},
65
54
{
66
55
eventName : "release" ,
@@ -70,17 +59,13 @@ func TestSetRefAndSha(t *testing.T) {
70
59
},
71
60
},
72
61
ref : "v1.0.0" ,
73
- sha : "1234fakesha" ,
74
62
},
75
63
{
76
64
eventName : "push" ,
77
65
event : map [string ]interface {}{
78
- "ref" : "refs/heads/somebranch" ,
79
- "after" : "push-sha" ,
80
- "deleted" : false ,
66
+ "ref" : "refs/heads/somebranch" ,
81
67
},
82
68
ref : "refs/heads/somebranch" ,
83
- sha : "push-sha" ,
84
69
},
85
70
{
86
71
eventName : "unknown" ,
@@ -90,13 +75,11 @@ func TestSetRefAndSha(t *testing.T) {
90
75
},
91
76
},
92
77
ref : "refs/heads/main" ,
93
- sha : "1234fakesha" ,
94
78
},
95
79
{
96
80
eventName : "no-event" ,
97
81
event : map [string ]interface {}{},
98
82
ref : "refs/heads/master" ,
99
- sha : "1234fakesha" ,
100
83
},
101
84
}
102
85
@@ -108,10 +91,9 @@ func TestSetRefAndSha(t *testing.T) {
108
91
Event : table .event ,
109
92
}
110
93
111
- ghc .SetRefAndSha (context .Background (), "main" , "/some/dir" )
94
+ ghc .SetRef (context .Background (), "main" , "/some/dir" )
112
95
113
96
assert .Equal (t , table .ref , ghc .Ref )
114
- assert .Equal (t , table .sha , ghc .Sha )
115
97
})
116
98
}
117
99
@@ -125,9 +107,96 @@ func TestSetRefAndSha(t *testing.T) {
125
107
Event : map [string ]interface {}{},
126
108
}
127
109
128
- ghc .SetRefAndSha (context .Background (), "" , "/some/dir" )
110
+ ghc .SetRef (context .Background (), "" , "/some/dir" )
129
111
130
112
assert .Equal (t , "refs/heads/master" , ghc .Ref )
131
- assert .Equal (t , "1234fakesha" , ghc .Sha )
132
113
})
133
114
}
115
+
116
+ func TestSetSha (t * testing.T ) {
117
+ log .SetLevel (log .DebugLevel )
118
+
119
+ oldFindGitRef := findGitRef
120
+ oldFindGitRevision := findGitRevision
121
+ defer func () { findGitRef = oldFindGitRef }()
122
+ defer func () { findGitRevision = oldFindGitRevision }()
123
+
124
+ findGitRef = func (ctx context.Context , file string ) (string , error ) {
125
+ return "refs/heads/master" , nil
126
+ }
127
+
128
+ findGitRevision = func (ctx context.Context , file string ) (string , string , error ) {
129
+ return "" , "1234fakesha" , nil
130
+ }
131
+
132
+ tables := []struct {
133
+ eventName string
134
+ event map [string ]interface {}
135
+ sha string
136
+ }{
137
+ {
138
+ eventName : "pull_request_target" ,
139
+ event : map [string ]interface {}{
140
+ "pull_request" : map [string ]interface {}{
141
+ "base" : map [string ]interface {}{
142
+ "sha" : "pr-base-sha" ,
143
+ },
144
+ },
145
+ },
146
+ sha : "pr-base-sha" ,
147
+ },
148
+ {
149
+ eventName : "pull_request" ,
150
+ event : map [string ]interface {}{
151
+ "number" : 1234. ,
152
+ },
153
+ sha : "1234fakesha" ,
154
+ },
155
+ {
156
+ eventName : "deployment" ,
157
+ event : map [string ]interface {}{
158
+ "deployment" : map [string ]interface {}{
159
+ "sha" : "deployment-sha" ,
160
+ },
161
+ },
162
+ sha : "deployment-sha" ,
163
+ },
164
+ {
165
+ eventName : "release" ,
166
+ event : map [string ]interface {}{},
167
+ sha : "1234fakesha" ,
168
+ },
169
+ {
170
+ eventName : "push" ,
171
+ event : map [string ]interface {}{
172
+ "after" : "push-sha" ,
173
+ "deleted" : false ,
174
+ },
175
+ sha : "push-sha" ,
176
+ },
177
+ {
178
+ eventName : "unknown" ,
179
+ event : map [string ]interface {}{},
180
+ sha : "1234fakesha" ,
181
+ },
182
+ {
183
+ eventName : "no-event" ,
184
+ event : map [string ]interface {}{},
185
+ sha : "1234fakesha" ,
186
+ },
187
+ }
188
+
189
+ for _ , table := range tables {
190
+ t .Run (table .eventName , func (t * testing.T ) {
191
+ ghc := & GithubContext {
192
+ EventName : table .eventName ,
193
+ BaseRef : "master" ,
194
+ Event : table .event ,
195
+ }
196
+
197
+ ghc .SetSha (context .Background (), "/some/dir" )
198
+
199
+ assert .Equal (t , table .sha , ghc .Sha )
200
+ })
201
+ }
202
+ }
0 commit comments