-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
131 lines (120 loc) · 3.02 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
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
package main
import (
"fmt"
"os"
"strings"
"testing"
)
func TestExpandPathFromGitRoot(t *testing.T) {
testCases := []struct {
paths string
wd, gitRoot string
want string
}{
{
"env/production",
"/home/user/repo/terraform",
"/home/user/repo",
"terraform/env/production",
},
{
"terraform/env/production",
"/home/user/repo",
"/home/user/repo",
"terraform/env/production",
},
{
"/home/user/repo/terraform/env/production",
"/home/user/repo",
"/home/user/repo",
"terraform/env/production",
},
}
for i, testCase := range testCases {
path := expandPathFromGitRoot(testCase.paths, testCase.wd, testCase.gitRoot)
if path != testCase.want {
t.Errorf("failed test %d: got %s, wanted %s\n", i, path, testCase.want)
}
}
}
func TestGetFoldersToPlan(t *testing.T) {
testCases := []struct {
deps map[string][]string
changedFolders []string
want []string
}{
{
map[string][]string{
"terraform/staging": []string{"modules/1", "modules/2"},
"terraform/production": []string{"modules/1", "modules/2", "modules/3"},
},
[]string{"modules/3"},
[]string{"terraform/production"},
},
{
map[string][]string{
"terraform/staging": []string{"modules/1", "modules/2"},
"terraform/production": []string{"modules/1", "modules/2", "modules/3"},
},
[]string{"modules/3", "modules/1"},
[]string{"terraform/production", "terraform/staging"},
},
{
map[string][]string{
"terraform/staging": []string{"modules/1", "modules/2"},
"terraform/production": []string{"modules/1", "modules/2", "modules/3"},
},
[]string{},
[]string{},
},
{
map[string][]string{
"terraform/staging": []string{"modules/1", "modules/2"},
"terraform/production": []string{"modules/1", "modules/2", "modules/3"},
},
[]string{"modules/5"},
[]string{},
},
{
map[string][]string{
"terraform/staging": []string{"modules/1", "modules/2"},
"terraform/production": []string{"modules/1", "modules/2", "modules/3"},
},
[]string{"modules/1", "modules/2", "modules/3"},
[]string{"terraform/staging", "terraform/production"},
},
}
for testI, testCase := range testCases {
toPlan := getFoldersToPlan(testCase.deps, testCase.changedFolders)
for _, folder := range toPlan {
found := false
for _, f := range testCase.want {
if f == folder {
found = true
}
}
if found == false {
t.Errorf("failed test %d, got %+v, wanted %+v", testI, toPlan, testCase.want)
}
}
}
}
func TestGetTerraformDependencies(t *testing.T) {
wd, _ := os.Getwd()
path := fmt.Sprintf("%s/%s", wd, "./fixtures")
got, _ := getTerraformDependencies(path)
want := "fixtures/module"
doNotWant := "github.com/something/module"
found := false
for key, _ := range got {
if strings.Contains(key, doNotWant) {
t.Errorf("Found unwanted remote module name %s", doNotWant)
}
if strings.Contains(key, want) {
found = true
}
}
if found == false {
t.Errorf("Did not find local module name %s", want)
}
}