-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvsync_test.go
194 lines (163 loc) · 4.45 KB
/
envsync_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package envsync
import (
"io/ioutil"
"math/rand"
"os"
"reflect"
"testing"
)
func TestGetMapKeysSorted(t *testing.T) {
k := []string{"abc", "acb", "bac", "bca", "cab", "cba"}
kShuffled := make([]string, len(k))
for {
for i, v := range rand.Perm(len(k)) {
kShuffled[v] = k[i]
}
if reflect.DeepEqual(kShuffled, k) {
break
}
}
m := make(map[string]string, len(k))
for _, v := range kShuffled {
m[v] = "a"
}
sorted := getMapKeysSorted(m)
eq := reflect.DeepEqual(sorted, k)
if !eq {
t.Error("getMapKeysSorted failed")
}
}
func TestGetKeysDiff(t *testing.T) {
a := []string{"one", "two", "three", "four", "five"}
b := []string{"two", "three", "five"}
diff := getKeysDiff(a, b)
eq := reflect.DeepEqual(diff, []string{"one", "four"})
if !eq {
t.Error("getKeysDiff must return true")
}
a = []string{"one", "two", "three", "four", "five"}
b = []string{"one", "two", "three", "four", "five"}
diff = getKeysDiff(a, b)
eq = reflect.DeepEqual(diff, []string{})
if eq {
t.Error("getKeysDiff must return false")
}
}
func TestCallWithOneNonExistingFilename(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
filename := dir + "/.env"
err = ioutil.WriteFile(filename, []byte{}, 0600)
if err != nil {
panic(err)
}
defer os.Remove(filename)
_, err = EnvSync(".env", "random_file_name_789")
// get just first 26 characters from error message because the rest differ linux&mac vs windows :|
if err != nil && err.Error()[0:26] == "open random_file_name_789:" {
return
}
t.Error("Calling with one non-existing filename must return first 26 characters 'open random_file_name_789:'")
}
func TestCallWithTwoNonExistingFilenames(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
filename := dir + "/.env"
err = ioutil.WriteFile(filename, []byte{}, 0600)
if err != nil {
panic(err)
}
defer os.Remove(filename)
_, err = EnvSync("random_file_name_789", "789_random_file_name")
// get just first 26 characters from error message because the rest differ linux&mac vs windows :|
if err != nil && err.Error()[0:26] == "open random_file_name_789:" {
return
}
t.Error("Calling with one non-existing filename must return first 26 characters 'open 789_random_file_name:'")
}
func TestCallWithExampleFileExistingAndEnvFileDefault(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
filename := dir + "/.env"
err = ioutil.WriteFile(filename, []byte{}, 0600)
if err != nil {
panic(err)
}
tmpfile := createFile([]byte{}, ".env.example")
defer os.Remove(filename)
defer os.Remove(tmpfile.Name())
_, err = EnvSync(filename, tmpfile.Name())
if err != nil {
t.Error("Calling with example file existing must return nil")
}
}
func TestCallWithExampleAndEnvFileSynced(t *testing.T) {
tmpfileEnv := createFile(
[]byte("APP_1=a\nAPP_2=a\nAPP_3=a"),
".environment",
)
tmpfileEnvExample := createFile(
[]byte("APP_1=a\nAPP_2=a\nAPP_3=a"),
".environment.example",
)
// cleanup
defer os.Remove(tmpfileEnv.Name())
defer os.Remove(tmpfileEnvExample.Name())
r, err := EnvSync(tmpfileEnv.Name(), tmpfileEnvExample.Name())
if r && err != nil {
t.Error("Calling with example and environment synced must return true, nil")
}
}
func TestCallWithEnvFileNotSynced(t *testing.T) {
tmpfileEnv := createFile(
[]byte("APP_1=a\nAPP_2=a\n"),
".environment",
)
tmpfileEnvExample := createFile(
[]byte("APP_1=a\nAPP_2=a\nAPP_3=a"),
".environment.example",
)
// cleanup
defer os.Remove(tmpfileEnv.Name())
defer os.Remove(tmpfileEnvExample.Name())
r, err := EnvSync(tmpfileEnv.Name(), tmpfileEnvExample.Name())
if r || err == nil {
t.Error("Calling with example and environment not synced must return false, err")
}
}
func TestCallWithEnvExampleFileNotSynced(t *testing.T) {
tmpfileEnv := createFile(
[]byte("APP_1=a\nAPP_2=a\nAPP_3=a"),
".environment",
)
tmpfileEnvExample := createFile(
[]byte("APP_1=a\nAPP_2=a\n"),
".environment.example",
)
// cleanup
defer os.Remove(tmpfileEnv.Name())
defer os.Remove(tmpfileEnvExample.Name())
r, err := EnvSync(tmpfileEnv.Name(), tmpfileEnvExample.Name())
if r || err == nil {
t.Error("Calling with example and environment not synced must return false, err")
}
}
func createFile(content []byte, filename string) *os.File {
tmpfile, err := ioutil.TempFile("", filename)
if err != nil {
panic(err)
}
if _, err := tmpfile.Write(content); err != nil {
panic(err)
}
if err := tmpfile.Close(); err != nil {
panic(err)
}
return tmpfile
}