Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion code/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions code/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}