File: rewrite.go

package info (click to toggle)
golang-github-etcd-io-gofail 0.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: makefile: 24; sh: 3
file content (177 lines) | stat: -rw-r--r-- 4,240 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package code

import (
	"bufio"
	"fmt"
	"io"
	"strings"
	"unicode"
)

const (
	pfxGofail    = `// gofail:`
	labelGofail  = `/* gofail-label */`
	errVarGoFail = `__fpErr`
)

// ToFailpoints turns all gofail comments into failpoint code. Returns a list of
// all failpoints it activated.
func ToFailpoints(wdst io.Writer, rsrc io.Reader) (fps []*Failpoint, err error) {
	var curfp *Failpoint

	dst := bufio.NewWriter(wdst)
	defer func() {
		if err == nil && curfp != nil {
			err = curfp.flush(dst)
		}
		if err == nil {
			err = dst.Flush()
		}
	}()

	src := bufio.NewReader(rsrc)
	for err == nil {
		l, rerr := src.ReadString('\n')
		if curfp != nil {
			if strings.HasPrefix(strings.TrimSpace(l), "//") {
				if len(l) > 0 && l[len(l)-1] == '\n' {
					l = l[:len(l)-1]
				}
				curfp.code = append(curfp.code, strings.Replace(l, "//", "\t", 1))
				continue
			} else {
				curfp.flush(dst)
				fps = append(fps, curfp)
				curfp = nil
			}
		} else if label := gofailLabel(l, pfxGofail, labelGofail); label != "" {
			// expose gofail label
			l = label
		} else if curfp, err = newFailpoint(l); err != nil {
			return
		} else if curfp != nil {
			// found a new failpoint
			continue
		}
		if _, err = dst.WriteString(l); err != nil {
			return
		}
		if rerr == io.EOF {
			break
		}
	}
	return
}

// ToComments turns all failpoint code into GOFAIL comments. It returns
// a list of all failpoints  it deactivated.
func ToComments(wdst io.Writer, rsrc io.Reader) (fps []*Failpoint, err error) {
	src := bufio.NewReader(rsrc)
	dst := bufio.NewWriter(wdst)
	ws := ""
	unmatchedBraces := 0
	for err == nil {
		l, rerr := src.ReadString('\n')
		err = rerr
		lTrim := strings.TrimSpace(l)

		if unmatchedBraces > 0 {
			opening, closing := numBraces(l)
			unmatchedBraces += opening - closing
			if unmatchedBraces == 0 {
				// strip off badType footer
				lTrim = strings.Split(lTrim, "; __badType")[0]
			}
			s := ws + "//" + wsPrefix(l, ws)[1:] + lTrim + "\n"
			dst.WriteString(s)
			continue
		}

		isErrVarGoFail := strings.Contains(l, fmt.Sprintf(", %s := __fp_", errVarGoFail))
		isHdr := isErrVarGoFail && strings.HasPrefix(lTrim, "if")
		if isHdr {
			pfx := pfxGofail

			ws = strings.Split(l, "i")[0]
			n := strings.Split(strings.Split(l, "__fp_")[1], ".")[0]
			t := strings.Split(strings.Split(l, ".(")[1], ")")[0]
			dst.WriteString(ws + pfx + " var " + n + " " + t + "\n")
			if !strings.Contains(l, "; __badType") {
				// not single liner
				unmatchedBraces = 1
			}
			fps = append(fps, &Failpoint{name: n, varType: t})
			continue
		}

		if isLabel := strings.Contains(l, "\t"+labelGofail); isLabel {
			l = strings.Replace(l, labelGofail, pfxGofail, 1)
		}

		if _, werr := dst.WriteString(l); werr != nil {
			return fps, werr
		}
	}
	if err == io.EOF {
		err = nil
	}
	dst.Flush()
	return
}

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

func numBraces(l string) (opening int, closing int) {
	for i := 0; i < len(l); i++ {
		switch l[i] {
		case '{':
			opening++
		case '}':
			closing++
		}
	}
	return
}

// wsPrefix computes the left padding of a line given a whitespace prefix.
func wsPrefix(l, wsPfx string) string {
	lws := ""
	if len(wsPfx) == 0 {
		lws = l
	} else {
		wsSplit := strings.SplitAfter(l, wsPfx)
		if len(wsSplit) < 2 {
			return ""
		}
		lws = strings.Join(wsSplit[1:], "")
	}
	for i, c := range lws {
		if !unicode.IsSpace(c) {
			return lws[:i]
		}
	}
	return lws
}