Skip to content

Commit

Permalink
Merge pull request #13 from heyitsanthony/labels
Browse files Browse the repository at this point in the history
code: support rewriting labels
  • Loading branch information
gyuho committed Apr 24, 2018
2 parents c3de8a5 + b409115 commit 32c1115
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
28 changes: 22 additions & 6 deletions code/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ func ToFailpoints(wdst io.Writer, rsrc io.Reader) (fps []*Failpoint, err error)
fps = append(fps, curfp)
curfp = nil
}
} else {
} else if label := gofailLabel(l); label != "" {
// expose gofail label
l = label
} else if curfp, err = newFailpoint(l); err != nil {
return
} else if curfp != nil {
// found a new failpoint
if curfp, err = newFailpoint(l); err != nil {
return
} else if curfp != nil {
continue
}
continue
}
if _, err = dst.WriteString(l); err != nil {
return
Expand Down Expand Up @@ -107,6 +108,10 @@ func ToComments(wdst io.Writer, rsrc io.Reader) (fps []*Failpoint, err error) {
continue
}

if isLabel := strings.Contains(l, "\t/* gofail-label */"); isLabel {
l = strings.Replace(l, "/* gofail-label */", "// gofail:", 1)
}

if _, werr := dst.WriteString(l); werr != nil {
return fps, werr
}
Expand All @@ -118,6 +123,17 @@ func ToComments(wdst io.Writer, rsrc io.Reader) (fps []*Failpoint, err error) {
return
}

func gofailLabel(l string) string {
if !strings.HasPrefix(strings.TrimSpace(l), "// gofail:") {
return ""
}
label := strings.SplitAfter(l, "// gofail:")[1]
if len(label) == 0 || !strings.Contains(label, ":") {
return ""
}
return strings.Replace(l, "// gofail:", "/* gofail-label */", 1)
}

func numBraces(l string) (opening int, closing int) {
for i := 0; i < len(l); i++ {
switch l[i] {
Expand Down
12 changes: 12 additions & 0 deletions code/rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ var examples = []struct {
{"func f() {\n\t// gofail: var NoTypeTest struct{}\n\t// fmt.Println(`hi`)\n}\n", 1},
{"func f() {\n\t// gofail: var NoTypeTest struct{}\n}\n", 1},
{"func f() {\n\t// gofail: var NoTypeTest struct{}\n\t// fmt.Println(`hi`)\n\t// fmt.Println(`bye`)\n}\n", 1},
{`
func f() {
// gofail: labelTest:
for {
if g() {
// gofail: var testLabel struct{}
// continue labelTest
return
}
}
}
`, 1},
}

func TestToFailpoint(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ func ExampleOneLineFunc() string {
// gofail: var ExampleOneLine struct{}
return "abc"
}

func ExampleLabelsFunc() (s string) {
i := 0
// gofail: myLabel:
for i < 5 {
s = s + "i"
i++
for j := 0; j < 5; j++ {
s = s + "j"
// gofail: var ExampleLabels struct{}
// continue myLabel
}
}
return s
}

0 comments on commit 32c1115

Please sign in to comment.