diff --git a/code/rewrite.go b/code/rewrite.go index fcf53cf..12365a7 100644 --- a/code/rewrite.go +++ b/code/rewrite.go @@ -112,7 +112,11 @@ func ToComments(wdst io.Writer, rsrc io.Reader) ([]*Failpoint, error) { ws = strings.Split(l, "i")[0] n := strings.Split(strings.Split(l, "__fp_")[1], ".")[0] - t := strings.Split(strings.Split(l, ".(")[1], ")")[0] + parts := strings.Split(l, ".(") + if len(parts) < 2 { + return fps, fmt.Errorf("failpoint %q header is missing its type assertion, the generated code may have been reformatted: %q", n, lTrim) + } + t := strings.Split(parts[1], ")")[0] dst.WriteString(ws + pfx + " var " + n + " " + t + "\n") if !strings.Contains(l, "; goto __nomock") { // not single liner diff --git a/code/rewrite_test.go b/code/rewrite_test.go index 07dc685..9925e90 100644 --- a/code/rewrite_test.go +++ b/code/rewrite_test.go @@ -124,3 +124,16 @@ func TestToComment(t *testing.T) { require.Equalf(t, len(fps), ex.wfps, "%d: got %d failpoints but expected %d", i, len(fps), ex.wfps) } } + +func TestToCommentsReformattedHeader(t *testing.T) { + // gofmt splits the header across lines: + // if vTest, __fpErr := __fp_Test.Acquire(); __fpErr == nil { + // Test, __fpTypeOK := vTest.(int); ... + // The second line contains ".(" but not the first, so strings.Split(l, ".(")[1] panics. + reformatted := "if vTest, __fpErr := __fp_Test.Acquire(); __fpErr == nil {\n\tTest, __fpTypeOK := vTest.(int)\n\tfmt.Println(Test)\n}\n" + dst := bytes.NewBuffer(nil) + src := strings.NewReader(reformatted) + _, err := ToComments(dst, src) + require.Error(t, err) + require.Contains(t, err.Error(), "type assertion") +}