-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
gitinfo_test.go
139 lines (135 loc) · 3.16 KB
/
gitinfo_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
package main
import (
"os"
"testing"
)
func TestLoadBranchFromEnv(t *testing.T) {
t.Parallel()
var tests = []struct {
testCase string
envs map[string]string
expectedBranch string
}{
{
"all vars defined",
map[string]string{
"GIT_BRANCH": "master",
"CIRCLE_BRANCH": "circle-master",
"TRAVIS_BRANCH": "travis-master",
"CI_BRANCH": "ci-master",
"APPVEYOR_REPO_BRANCH": "appveyor-master",
"WERCKER_GIT_BRANCH": "wercker-master",
"DRONE_BRANCH": "drone-master",
"BUILDKITE_BRANCH": "buildkite-master",
"BRANCH_NAME": "jenkins-master",
"CI_COMMIT_REF_NAME": "gitlab-master",
},
"master",
},
{
"all except GIT_BRANCH",
map[string]string{
"CIRCLE_BRANCH": "circle-master",
"TRAVIS_BRANCH": "travis-master",
"CI_BRANCH": "ci-master",
"APPVEYOR_REPO_BRANCH": "appveyor-master",
"WERCKER_GIT_BRANCH": "wercker-master",
"DRONE_BRANCH": "drone-master",
"BUILDKITE_BRANCH": "buildkite-master",
"BRANCH_NAME": "jenkins-master",
"CI_COMMIT_REF_NAME": "gitlab-master",
},
"circle-master",
},
{
"all except GIT_BRANCH and CIRCLE_BRANCH",
map[string]string{
"TRAVIS_BRANCH": "travis-master",
"CI_BRANCH": "ci-master",
"APPVEYOR_REPO_BRANCH": "appveyor-master",
"WERCKER_GIT_BRANCH": "wercker-master",
"DRONE_BRANCH": "drone-master",
"BUILDKITE_BRANCH": "buildkite-master",
"BRANCH_NAME": "jenkins-master",
"CI_COMMIT_REF_NAME": "gitlab-master",
},
"travis-master",
},
{
"only CI_BRANCH defined",
map[string]string{
"CI_BRANCH": "ci-master",
},
"ci-master",
},
{
"only APPVEYOR_REPO_BRANCH defined",
map[string]string{
"APPVEYOR_REPO_BRANCH": "appveyor-master",
},
"appveyor-master",
},
{
"only WERCKER_GIT_BRANCH defined",
map[string]string{
"WERCKER_GIT_BRANCH": "wercker-master",
},
"wercker-master",
},
{
"only BRANCH_NAME defined",
map[string]string{
"BRANCH_NAME": "jenkins-master",
},
"jenkins-master",
},
{
"only BUILDKITE_BRANCH defined",
map[string]string{
"BUILDKITE_BRANCH": "buildkite-master",
},
"buildkite-master",
},
{
"only DRONE_BRANCH defined",
map[string]string{
"DRONE_BRANCH": "drone-master",
},
"drone-master",
},
{
"only CI_COMMIT_REF_NAME defined",
map[string]string{
"CI_COMMIT_REF_NAME": "gitlab-master",
},
"gitlab-master",
},
{
"GitHub Action push event",
map[string]string{
"GITHUB_REF": "refs/heads/github-master",
},
"github-master",
},
{
"no branch var defined",
map[string]string{},
"",
},
}
for _, test := range tests {
resetBranchEnvs(test.envs)
envBranch := loadBranchFromEnv()
if envBranch != test.expectedBranch {
t.Errorf("%s: wrong branch returned. Expected %q, but got %q", test.testCase, test.expectedBranch, envBranch)
}
}
}
func resetBranchEnvs(values map[string]string) {
for _, envVar := range varNames {
os.Unsetenv(envVar)
}
for k, v := range values {
os.Setenv(k, v)
}
}