refactor: break out into functions and draft tests

Signed-off-by: Jesse Simpson <jesse.simpson36@gmail.com>
pull/31198/head
Jesse Simpson 2 weeks ago
parent 284bd980b6
commit b060911075
No known key found for this signature in database
GPG Key ID: 237495C89AB0AAFC

@ -360,12 +360,56 @@ func parseTemplateExecErrorString(s string) (TraceableError, bool) {
// Special case: "template: no template %q associated with template %q" // Special case: "template: no template %q associated with template %q"
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=191 // Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=191
if strings.HasPrefix(remainder, "no template ") { traceableError, done := parseTemplateNoTemplateError(s, remainder)
return TraceableError{message: s}, true if done {
return traceableError, done
} }
// Executing form: "<templateName>: executing \"<funcName>\" at <<location>>: <errMsg>[ template:...]" // Executing form: "<templateName>: executing \"<funcName>\" at <<location>>: <errMsg>[ template:...]"
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=141 // Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=141
traceableError, done = parseTemplateExecutingAtErrorType(remainder)
if done {
return traceableError, done
}
// Simple form: "<templateName>: <errMsg>"
// Use LastIndex to avoid splitting colons within line:col info.
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=138
traceableError, done = parseTemplateSimpleErrorString(remainder)
if done {
return traceableError, done
}
return TraceableError{}, false
}
// Special case: "template: no template %q associated with template %q"
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=191
func parseTemplateNoTemplateError(s string, remainder string) (TraceableError, bool) {
if strings.HasPrefix(remainder, "no template ") {
return TraceableError{message: s}, true
}
return TraceableError{}, false
}
// Simple form: "<templateName>: <errMsg>"
// Use LastIndex to avoid splitting colons within line:col info.
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=138
func parseTemplateSimpleErrorString(remainder string) (TraceableError, bool) {
if sep := strings.LastIndex(remainder, ": "); sep != -1 {
templateName := remainder[:sep]
errMsg := remainder[sep+2:]
if cut := strings.Index(errMsg, " template:"); cut != -1 {
errMsg = errMsg[:cut]
}
return TraceableError{location: templateName, message: errMsg}, true
}
return TraceableError{}, false
}
// Executing form: "<templateName>: executing \"<funcName>\" at <<location>>: <errMsg>[ template:...]"
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=141
func parseTemplateExecutingAtErrorType(remainder string) (TraceableError, bool) {
if idx := strings.Index(remainder, ": executing "); idx != -1 { if idx := strings.Index(remainder, ": executing "); idx != -1 {
templateName := remainder[:idx] templateName := remainder[:idx]
after := remainder[idx+len(": executing "):] after := remainder[idx+len(": executing "):]
@ -404,19 +448,6 @@ func parseTemplateExecErrorString(s string) (TraceableError, bool) {
executedFunction: "executing \"" + functionName + "\" at <" + locationName + ">:", executedFunction: "executing \"" + functionName + "\" at <" + locationName + ">:",
}, true }, true
} }
// Simple form: "<templateName>: <errMsg>"
// Use LastIndex to avoid splitting colons within line:col info.
// Matches https://cs.opensource.google/go/go/+/refs/tags/go1.23.6:src/text/template/exec.go;l=138
if sep := strings.LastIndex(remainder, ": "); sep != -1 {
templateName := remainder[:sep]
errMsg := remainder[sep+2:]
if cut := strings.Index(errMsg, " template:"); cut != -1 {
errMsg = errMsg[:cut]
}
return TraceableError{location: templateName, message: errMsg}, true
}
return TraceableError{}, false return TraceableError{}, false
} }

@ -1428,3 +1428,50 @@ func TestRenderCustomTemplateFuncs(t *testing.T) {
t.Errorf("Expected %q, got %q", expected, rendered) t.Errorf("Expected %q, got %q", expected, rendered)
} }
} }
func TestTraceableError_SimpleForm(t *testing.T) {
testStrings := []string{
"function_not_found/templates/secret.yaml: error calling include",
}
for _, errString := range testStrings {
trace, done := parseTemplateSimpleErrorString(errString)
if !done {
t.Errorf("Expected parse to pass but did not")
}
if trace.message != "error calling include" {
t.Errorf("Expected %q, got %q", errString, trace.message)
}
}
}
func TestTraceableError_ExecutingForm(t *testing.T) {
testStrings := [][]string{
{"template: executing \"function_not_found/templates/secret.yaml\" at <include \"name\" .>: ", "function_not_found/templates/secret.yaml"},
{"template: executing \"name\" at <include \"common.names.get_name\" .>: ", "name"},
}
for _, errTuple := range testStrings {
errString := errTuple[0]
expectedLocation := errTuple[1]
trace, done := parseTemplateExecutingAtErrorType(errString)
if !done {
t.Errorf("Expected parse to pass but did not")
}
if trace.location != expectedLocation {
t.Errorf("Expected %q, got %q", expectedLocation, trace.location)
}
}
}
func TestTraceableError_NoTemplateForm(t *testing.T) {
testStrings := []string{
"no template \"common.names.get_name\" associated with template \"gotpl\"",
}
for _, errString := range testStrings {
trace, done := parseTemplateNoTemplateError(errString, "")
if !done {
t.Errorf("Expected parse to pass but did not")
}
if trace.message != errString {
t.Errorf("Expected %q, got %q", errString, trace.message)
}
}
}

Loading…
Cancel
Save