File tree Expand file tree Collapse file tree 2 files changed +17
-15
lines changed Expand file tree Collapse file tree 2 files changed +17
-15
lines changed Original file line number Diff line number Diff line change @@ -283,9 +283,8 @@ func trimSuffix(suffix, s string) string {
283283 return strings .TrimSuffix (s , suffix )
284284}
285285
286- func generateFile (config Config , containers Context ) bool {
287- templatePath := config .Template
288- tmpl , err := template .New (filepath .Base (templatePath )).Funcs (template.FuncMap {
286+ func newTemplate (name string ) * template.Template {
287+ tmpl := template .New (name ).Funcs (template.FuncMap {
289288 "closest" : arrayClosest ,
290289 "coalesce" : coalesce ,
291290 "contains" : contains ,
@@ -312,7 +311,13 @@ func generateFile(config Config, containers Context) bool {
312311 "whereNotExist" : whereNotExist ,
313312 "whereAny" : whereAny ,
314313 "whereAll" : whereAll ,
315- }).ParseFiles (templatePath )
314+ })
315+ return tmpl
316+ }
317+
318+ func generateFile (config Config , containers Context ) bool {
319+ templatePath := config .Template
320+ tmpl , err := newTemplate (filepath .Base (templatePath )).ParseFiles (templatePath )
316321 if err != nil {
317322 log .Fatalf ("unable to parse template: %s" , err )
318323 }
Original file line number Diff line number Diff line change 55 "encoding/json"
66 "reflect"
77 "testing"
8+ "text/template"
89)
910
1011func TestContains (t * testing.T ) {
@@ -27,20 +28,16 @@ func TestKeys(t *testing.T) {
2728 expected : "demo.local" ,
2829 }
2930
30- k , err := keys (env )
31- if err != nil {
32- t .Fatalf ("Error fetching keys: %v" , err )
33- }
34- vk := reflect .ValueOf (k )
35- if vk .Kind () == reflect .Invalid {
36- t .Fatalf ("Got invalid kind for keys: %v" , vk )
37- }
31+ const text = "{{range (keys $)}}{{.}}{{end}}"
32+ tmpl := template .Must (newTemplate ("keys-test" ).Parse (text ))
3833
39- if len (env ) != vk .Len () {
40- t .Fatalf ("Incorrect key count; expected %s, got %s" , len (env ), vk .Len ())
34+ var b bytes.Buffer
35+ err := tmpl .ExecuteTemplate (& b , "keys-test" , env )
36+ if err != nil {
37+ t .Fatalf ("Error executing template: %v" , err )
4138 }
4239
43- got := vk . Index ( 0 ). Interface ()
40+ got := b . String ()
4441 if expected != got {
4542 t .Fatalf ("Incorrect key found; expected %s, got %s" , expected , got )
4643 }
You can’t perform that action at this time.
0 commit comments