|
| 1 | +package templatelib_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "text/template" |
| 6 | + "unsafe" |
| 7 | + |
| 8 | + "github.com/docker-library/go-dockerlibrary/pkg/templatelib" |
| 9 | +) |
| 10 | + |
| 11 | +func TestTernaryPanic(t *testing.T) { |
| 12 | + // one of the only places template.IsTrue will return "false" for the "ok" value is an UnsafePointer (hence this test) |
| 13 | + |
| 14 | + defer func() { |
| 15 | + if r := recover(); r == nil { |
| 16 | + t.Errorf("Expected panic, executed successfully instead") |
| 17 | + } else if errText, ok := r.(string); !ok || errText != `template.IsTrue(<nil>) says things are NOT OK` { |
| 18 | + t.Errorf("Unexpected panic: %v", errText) |
| 19 | + } |
| 20 | + }() |
| 21 | + |
| 22 | + tmpl, err := template.New("unsafe-pointer").Funcs(templatelib.FuncMap).Parse(`{{ ternary "true" "false" . }}`) |
| 23 | + |
| 24 | + err = tmpl.Execute(nil, unsafe.Pointer(uintptr(0))) |
| 25 | + if err != nil { |
| 26 | + t.Errorf("Expected panic, got error instead: %v", err) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestJoinPanic(t *testing.T) { |
| 31 | + defer func() { |
| 32 | + if r := recover(); r == nil { |
| 33 | + t.Errorf("Expected panic, executed successfully instead") |
| 34 | + } else if errText, ok := r.(string); !ok || errText != `"join" requires at least one argument` { |
| 35 | + t.Errorf("Unexpected panic: %v", r) |
| 36 | + } |
| 37 | + }() |
| 38 | + |
| 39 | + tmpl, err := template.New("join-no-arg").Funcs(templatelib.FuncMap).Parse(`{{ join }}`) |
| 40 | + |
| 41 | + err = tmpl.Execute(nil, nil) |
| 42 | + if err != nil { |
| 43 | + t.Errorf("Expected panic, got error instead: %v", err) |
| 44 | + } |
| 45 | +} |
0 commit comments