-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
68 lines (55 loc) · 1.3 KB
/
main_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
package main
import (
"flag"
"fmt"
"os"
"testing"
)
func TestTagsWrong(t *testing.T) {
testData := []string{"", " ", "BAD", "BAD;BAD2"}
for _, v := range testData {
t.Run(fmt.Sprintf("TAGS are set to %q", v), func(t *testing.T) {
tags := tags(v)
if len(tags) != 0 {
t.Fatal()
}
})
}
}
func TestTagsCorrect(t *testing.T) {
testData := []string{"K1=V1", "K1=V1;K2=V2"}
for i := 0; i < len(testData); i++ {
t.Run(fmt.Sprintf("TAGS are set to %q", testData[i]), func(t *testing.T) {
tags := tags(testData[i])
if len(tags) != i+1 {
t.Fatal(tags)
}
})
}
}
func TestEnvOverFlags(t *testing.T) {
err := os.Setenv("URL", "http://localhost")
if err != nil {
t.Fatal(err)
}
var testFlag = flag.String("url", DEFAULT_ANODOT_URL, "url value")
err = flag.Set("url", "http://192.168.0.1")
if err != nil {
t.Fatal(err)
}
v := envOrFlag("URL", testFlag)
if v != "http://localhost" {
t.Fatal("env variable should have higher order over flags")
}
}
func TestFlagEnvNotSet(t *testing.T) {
var testFlag = flag.String("url", DEFAULT_ANODOT_URL, "url value")
err := flag.Set("url", "http://192.168.0.1")
if err != nil {
t.Fatal(err)
}
v := envOrFlag("URL", testFlag)
if v != "http://192.168.0.1" {
t.Fatal("flag value should be used is env variable is not set")
}
}