Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewriter,restorer: add the DO NOT EDIT comment to the generated Go code #42

Closed
wants to merge 2 commits into from
Closed
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/restorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (r Restorer) Restore() error {
if err != nil {
return err
}
if bytes.HasSuffix(rewritedContent, generatedSourceIndicator) {
rewritedContent = rewritedContent[:len(rewritedContent)-len(generatedSourceIndicator)]
}
originContent, err := ioutil.ReadFile(filePath)
if err != nil {
return err
Expand Down Expand Up @@ -153,6 +156,7 @@ func init() {
func %s(name string) string {
return __failpointBindingCache.pkgpath + "/" + name
}
`, pak, extendPkgName)
%s
`, pak, extendPkgName, generatedSourceIndicator)
return ioutil.WriteFile(bindingFile, []byte(bindingContent), os.ModePerm)
}
14 changes: 13 additions & 1 deletion code/rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
labelSuffix = "-tmp-marker"
)

// generatedSourceIndicator is inserted at the end of the rewritten files
// to show that the source file should not be edited.
var generatedSourceIndicator = []byte("\n// Code generated by failpoint DO NOT EDIT.\n")

// Rewriter represents a rewriting tool for converting the failpoint marker functions to
// corresponding statements in Golang. It will traverse the specified path and filter
// out files which do not have failpoint injection sites, and rewrite the remain files.
Expand Down Expand Up @@ -643,8 +647,16 @@ func (r *Rewriter) RewriteFile(path string) (err error) {
if err != nil {
return err
}
if err != nil {
return err
}
defer newFile.Close()
return format.Node(newFile, fset, file)
err = format.Node(newFile, fset, file)
if err != nil {
return err
}
_, err = newFile.Write(generatedSourceIndicator)
return err
}

// Rewrite does the rewrite action for specified path. It contains the main steps:
Expand Down
Loading