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
|
package ci
import (
"os"
"reflect"
"strings"
"testing"
)
func getVendor(name string) Vendor {
for _, v := range Vendors {
if v.Name == name {
return v
}
}
return Vendor{}
}
func TestInfo(t *testing.T) {
tests := []struct {
name string
setEnv []string
want Vendor
}{
{
name: "AppVeyor",
setEnv: []string{"APPVEYOR"},
want: getVendor("AppVeyor"),
},
{
name: "Vercel",
setEnv: []string{"VERCEL", "NOW_BUILDER"},
want: getVendor("Vercel"),
},
{
name: "Render",
setEnv: []string{"RENDER"},
want: getVendor("Render"),
},
{
name: "Netlify",
setEnv: []string{"NETLIFY"},
want: getVendor("Netlify CI"),
},
{
name: "Jenkins",
setEnv: []string{"BUILD_ID", "JENKINS_URL"},
want: getVendor("Jenkins"),
},
{
name: "Jenkins - failing",
setEnv: []string{"BUILD_ID"},
want: getVendor(""),
},
{
name: "GitHub Actions",
setEnv: []string{"GITHUB_ACTIONS"},
want: getVendor("GitHub Actions"),
},
{
name: "Codeship",
setEnv: []string{"CI_NAME=codeship"},
want: getVendor("Codeship"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// unset existing envs
liveCi := ""
if Name() == "GitHub Actions" {
liveCi = os.Getenv("GITHUB_ACTIONS")
err := os.Unsetenv("GITHUB_ACTIONS")
if err != nil {
t.Errorf("Error un-setting GITHUB_ACTIONS env: %s", err)
}
}
// set envs
for _, env := range tt.setEnv {
envParts := strings.Split(env, "=")
val := "some value"
if len(envParts) > 1 {
val = envParts[1]
}
err := os.Setenv(envParts[0], val)
if err != nil {
t.Errorf("Error setting %s for %s test", envParts[0], tt.name)
}
defer os.Unsetenv(envParts[0]) //nolint errcheck
}
// run test
if got := Info(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Info() = %v, want %v", got, tt.want)
}
// reset env
if Name() == "GitHub Actions" {
err := os.Setenv("GITHUB_ACTIONS", liveCi)
if err != nil {
t.Errorf("Error re-setting GITHUB_ACTIONS env: %s", err)
}
}
})
}
}
|