@@ -72,48 +72,125 @@ func Test_filterFeatures(t *testing.T) {
72
72
expectedError : nil ,
73
73
},
74
74
{
75
- title : "Every field which defines populated feature is populated" ,
75
+ title : "Every field which defines populated feature is populated for github " ,
76
76
planConfig : types.Plan {
77
- Github : types.Github {
78
- AppID : "example ID" ,
79
- PrivateKeyFile : "~/home/private_key.pem" ,
77
+ SCM : types .GitHubManager ,
78
+ TLS : true ,
79
+ TLSConfig : types.TLSConfig {
80
+ DNSService : types .Route53 ,
80
81
},
82
+ EnableOAuth : true ,
83
+ },
84
+ expectedFeatures : []string {types .DefaultFeature , types .GitHubSCM , types .Auth , types .Route53DNS },
85
+ expectedError : nil ,
86
+ },
87
+ {
88
+ title : "Every field which defines populated feature is populated for gitlab" ,
89
+ planConfig : types.Plan {
90
+ SCM : types .GitLabManager ,
81
91
TLS : true ,
82
92
TLSConfig : types.TLSConfig {
83
93
DNSService : types .Route53 ,
84
94
},
85
95
EnableOAuth : true ,
86
96
},
87
- expectedFeatures : []string {types .DefaultFeature , types .GitHub , types .Auth , types .Route53DNS },
97
+ expectedFeatures : []string {types .DefaultFeature , types .GitLabSCM , types .Auth , types .Route53DNS },
88
98
expectedError : nil ,
89
99
},
90
100
{
91
- title : "Auth and TLS are enabled " ,
101
+ title : "Example in which the function throws error in this case the SCM field is empty " ,
92
102
planConfig : types.Plan {
93
103
TLS : true ,
94
104
TLSConfig : types.TLSConfig {
95
105
DNSService : types .Route53 ,
96
106
},
97
107
EnableOAuth : true ,
98
108
},
109
+ expectedFeatures : []string {types .DefaultFeature },
110
+ expectedError : errors .New ("Error while filtering features" ),
111
+ },
112
+ {
113
+ title : "Auth and TLS are enabled along with GitLab" ,
114
+ planConfig : types.Plan {
115
+ TLS : true ,
116
+ TLSConfig : types.TLSConfig {
117
+ DNSService : types .Route53 ,
118
+ },
119
+ EnableOAuth : true ,
120
+ SCM : types .GitHubManager ,
121
+ },
99
122
expectedFeatures : []string {types .DefaultFeature , types .Auth , types .Route53DNS },
100
123
expectedError : nil ,
101
124
},
102
125
}
103
126
for _ , test := range tests {
104
127
t .Run (test .title , func (t * testing.T ) {
105
- test .planConfig , _ = filterFeatures (test .planConfig )
106
- for _ , feature := range test .planConfig .Features {
107
- t .Logf ("Searching for feature: %s, " , feature )
108
- for allFeatures , expectedFeature := range test .expectedFeatures {
109
- if feature == expectedFeature {
128
+ var filterError error
129
+ test .planConfig , filterError = filterFeatures (test .planConfig )
130
+ t .Logf ("Features in the plan: %v" , test .planConfig .Features )
131
+ if filterError != nil && test .expectedError != nil {
132
+ if ! strings .Contains (filterError .Error (), test .expectedError .Error ()) {
133
+ t .Errorf ("Expected error to contain: `%s` got: `%s`" , test .expectedError .Error (), filterError .Error ())
134
+ }
135
+ }
136
+ for _ , expectedFeature := range test .expectedFeatures {
137
+ for allPlanFeatures , enabledFeature := range test .planConfig .Features {
138
+ if len (test .planConfig .Features ) == 0 {
139
+ t .Errorf ("Feature 'default' should always be present" )
140
+ }
141
+ if expectedFeature == enabledFeature {
110
142
break
111
143
}
112
- if allFeatures == len (test .expectedFeatures ) {
113
- t .Errorf ("Feature: %s not found in the expected features " , feature )
144
+ if allPlanFeatures == len (test .planConfig . Features ) - 1 {
145
+ t .Errorf ("Feature: '%s' not found in: %v " , expectedFeature , test . planConfig . Features )
114
146
}
115
147
}
116
148
}
117
149
})
118
150
}
119
151
}
152
+
153
+ func Test_filterGitRepositoryManager (t * testing.T ) {
154
+ tests := []struct {
155
+ title string
156
+ planConfig types.Plan
157
+ expectedFeature []string
158
+ expectedError error
159
+ }{
160
+ {
161
+ title : "SCM field is populated for gitlab" ,
162
+ planConfig : types.Plan {
163
+ SCM : types .GitLabManager ,
164
+ },
165
+ expectedFeature : []string {types .GitLabSCM },
166
+ expectedError : nil ,
167
+ },
168
+ {
169
+ title : "SCM field is populated for github" ,
170
+ planConfig : types.Plan {
171
+ SCM : types .GitHubManager ,
172
+ },
173
+ expectedFeature : []string {types .GitHubSCM },
174
+ expectedError : nil ,
175
+ },
176
+ {
177
+ title : "SCM field is populated for with unsupported Git repository manager" ,
178
+ planConfig : types.Plan {
179
+ SCM : "bitbucket" ,
180
+ },
181
+ expectedFeature : []string {},
182
+ expectedError : errors .New ("Error unsupported Git repository manager: bitbucket" ),
183
+ },
184
+ }
185
+ for _ , test := range tests {
186
+ t .Run (test .title , func (t * testing.T ) {
187
+ var configErr error
188
+ test .planConfig , configErr = filterGitRepositoryManager (test .planConfig )
189
+ if configErr != nil && test .expectedError != nil {
190
+ if ! strings .EqualFold (configErr .Error (), test .expectedError .Error ()) {
191
+ t .Errorf ("Expected error: '%s' got: '%s'" , test .expectedError , configErr )
192
+ }
193
+ }
194
+ })
195
+ }
196
+ }
0 commit comments