Browse code

add an exception case and map changge to struct with expect error

Signed-off-by: chchliang <chen.chuanliang@zte.com.cn>

add an exception case and map changge to struct with expect error

Signed-off-by: chchliang <chen.chuanliang@zte.com.cn>

chchliang authored on 2017/08/31 12:36:37
Showing 1 changed files
... ...
@@ -8,35 +8,117 @@ import (
8 8
 )
9 9
 
10 10
 func TestValidateEnv(t *testing.T) {
11
-	valids := map[string]string{
12
-		"a":                   "a",
13
-		"something":           "something",
14
-		"_=a":                 "_=a",
15
-		"env1=value1":         "env1=value1",
16
-		"_env1=value1":        "_env1=value1",
17
-		"env2=value2=value3":  "env2=value2=value3",
18
-		"env3=abc!qwe":        "env3=abc!qwe",
19
-		"env_4=value 4":       "env_4=value 4",
20
-		"PATH":                fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
21
-		"PATH=something":      "PATH=something",
22
-		"asd!qwe":             "asd!qwe",
23
-		"1asd":                "1asd",
24
-		"123":                 "123",
25
-		"some space":          "some space",
26
-		"  some space before": "  some space before",
27
-		"some space after  ":  "some space after  ",
11
+	testcase := []struct {
12
+		value    string
13
+		expected string
14
+		err      error
15
+	}{
16
+		{
17
+			value:    "a",
18
+			expected: "a",
19
+		},
20
+		{
21
+			value:    "something",
22
+			expected: "something",
23
+		},
24
+		{
25
+			value:    "_=a",
26
+			expected: "_=a",
27
+		},
28
+		{
29
+			value:    "env1=value1",
30
+			expected: "env1=value1",
31
+		},
32
+		{
33
+			value:    "_env1=value1",
34
+			expected: "_env1=value1",
35
+		},
36
+		{
37
+			value:    "env2=value2=value3",
38
+			expected: "env2=value2=value3",
39
+		},
40
+		{
41
+			value:    "env3=abc!qwe",
42
+			expected: "env3=abc!qwe",
43
+		},
44
+		{
45
+			value:    "env_4=value 4",
46
+			expected: "env_4=value 4",
47
+		},
48
+		{
49
+			value:    "PATH",
50
+			expected: fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
51
+		},
52
+		{
53
+			value: "=a",
54
+			err:   fmt.Errorf(fmt.Sprintf("invalid environment variable: %s", "=a")),
55
+		},
56
+		{
57
+			value:    "PATH=something",
58
+			expected: "PATH=something",
59
+		},
60
+		{
61
+			value:    "asd!qwe",
62
+			expected: "asd!qwe",
63
+		},
64
+		{
65
+			value:    "1asd",
66
+			expected: "1asd",
67
+		},
68
+		{
69
+			value:    "123",
70
+			expected: "123",
71
+		},
72
+		{
73
+			value:    "some space",
74
+			expected: "some space",
75
+		},
76
+		{
77
+			value:    "  some space before",
78
+			expected: "  some space before",
79
+		},
80
+		{
81
+			value:    "some space after  ",
82
+			expected: "some space after  ",
83
+		},
84
+		{
85
+			value: "=",
86
+			err:   fmt.Errorf(fmt.Sprintf("invalid environment variable: %s", "=")),
87
+		},
28 88
 	}
89
+
29 90
 	// Environment variables are case in-sensitive on Windows
30 91
 	if runtime.GOOS == "windows" {
31
-		valids["PaTh"] = fmt.Sprintf("PaTh=%v", os.Getenv("PATH"))
92
+		tmp := struct {
93
+			value    string
94
+			expected string
95
+			err      error
96
+		}{
97
+			value:    "PaTh",
98
+			expected: fmt.Sprintf("PaTh=%v", os.Getenv("PATH")),
99
+		}
100
+		testcase = append(testcase, tmp)
101
+
32 102
 	}
33
-	for value, expected := range valids {
34
-		actual, err := ValidateEnv(value)
103
+
104
+	for _, r := range testcase {
105
+		actual, err := ValidateEnv(r.value)
106
+
35 107
 		if err != nil {
36
-			t.Fatal(err)
108
+			if r.err == nil {
109
+				t.Fatalf("Expected err is nil, got err[%v]", err)
110
+			}
111
+			if err.Error() != r.err.Error() {
112
+				t.Fatalf("Expected err[%v], got err[%v]", r.err, err)
113
+			}
114
+		}
115
+
116
+		if err == nil && r.err != nil {
117
+			t.Fatalf("Expected err[%v], but err is nil", r.err)
37 118
 		}
38
-		if actual != expected {
39
-			t.Fatalf("Expected [%v], got [%v]", expected, actual)
119
+
120
+		if actual != r.expected {
121
+			t.Fatalf("Expected [%v], got [%v]", r.expected, actual)
40 122
 		}
41 123
 	}
42 124
 }