1
16
17 package util
18
19 import (
20 "strings"
21 "testing"
22 )
23
24
25
26 func Test_processEnvFileLine(t *testing.T) {
27 testCases := []struct {
28 name string
29 line []byte
30 currentLine int
31 expectedKey string
32 expectedValue string
33 expectedErr string
34 }{
35 {"the utf8bom is trimmed on the first line",
36 append(utf8bom, 'a', '=', 'c'), 0, "a", "c", ""},
37
38 {"the utf8bom is NOT trimmed on the second line",
39 append(utf8bom, 'a', '=', 'c'), 1, "", "", "not a valid key name"},
40
41 {"no key is returned on a comment line",
42 []byte{' ', '#', 'c'}, 0, "", "", ""},
43
44 {"no key is returned on a blank line",
45 []byte{' ', ' ', '\t'}, 0, "", "", ""},
46
47 {"key is returned even with no value",
48 []byte{' ', 'x', '='}, 0, "x", "", ""},
49 }
50 for _, tt := range testCases {
51 t.Run(tt.name, func(t *testing.T) {
52 key, value, err := processEnvFileLine(tt.line, `filename`, tt.currentLine)
53 t.Logf("Testing that %s.", tt.name)
54 if tt.expectedKey != key {
55 t.Errorf("\texpected key %q, received %q", tt.expectedKey, key)
56 }
57 if tt.expectedValue != value {
58 t.Errorf("\texpected value %q, received %q", tt.expectedValue, value)
59 }
60 if len(tt.expectedErr) == 0 {
61 if err != nil {
62 t.Errorf("\tunexpected err %v", err)
63 }
64 } else {
65 if !strings.Contains(err.Error(), tt.expectedErr) {
66 t.Errorf("\terr %v doesn't match expected %q", err, tt.expectedErr)
67 }
68 }
69 })
70 }
71 }
72
73
74
75
76
77
78
79
80
81
82
83 func Test_processEnvFileLine_readEnvironment(t *testing.T) {
84 const realKey = "k8s_test_env_file_key"
85 const realValue = `my_value`
86
87 t.Setenv(realKey, `my_value`)
88
89 key, value, err := processEnvFileLine([]byte(realKey), `filename`, 3)
90 if err != nil {
91 t.Fatal(err)
92 }
93 if key != realKey {
94 t.Errorf(`expected key %q, received %q`, realKey, key)
95 }
96 if value != realValue {
97 t.Errorf(`expected value %q, received %q`, realValue, value)
98 }
99 }
100
View as plain text