diff --git a/code/restorer.go b/code/restorer.go index 088bfc5..29866c6 100644 --- a/code/restorer.go +++ b/code/restorer.go @@ -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 @@ -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) } diff --git a/code/rewriter.go b/code/rewriter.go index 3ed2a76..be0df7d 100644 --- a/code/rewriter.go +++ b/code/rewriter.go @@ -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. @@ -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: diff --git a/code/rewriter_test.go b/code/rewriter_test.go index b41f9eb..cd36621 100644 --- a/code/rewriter_test.go +++ b/code/rewriter_test.go @@ -72,7 +72,8 @@ func unittest() { fmt.Println("unit-test", val) } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -106,7 +107,8 @@ func unittest() { fmt.Println("unit-test") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -140,7 +142,8 @@ func unittest() { fmt.Println("unit-test") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -228,7 +231,8 @@ func (s *S) unittest() { fmt.Println("unit-test", val) } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -264,7 +268,8 @@ func unittest() { fmt.Println("unit-test", val) } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -300,7 +305,8 @@ func unittest() { fmt.Println("unit-test") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -356,7 +362,8 @@ func unittest() { fmt.Println("unit-test", val, cs) } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -400,7 +407,8 @@ func unittest() { } fmt.Printf("nano() runs successfully: %d\n", runtimeNano()) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -498,7 +506,8 @@ func unittest() { f1() f2() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -548,7 +557,8 @@ func unittest() { f1() f2() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -586,7 +596,8 @@ func unittest() { } }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -632,7 +643,8 @@ func unittest() { } }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -670,7 +682,8 @@ func unittest() { } }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -716,7 +729,8 @@ func unittest() { } }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -764,7 +778,8 @@ func unittest() { return 1000 }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -822,7 +837,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -872,7 +888,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -942,7 +959,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -998,7 +1016,8 @@ func unittest() { fmt.Printf("i = %d success\n", i) } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1090,7 +1109,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1192,7 +1212,8 @@ func unittest() { fn() } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1320,7 +1341,8 @@ func unittest() { typeSwitch2(num) typeSwitch3(num) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1422,7 +1444,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1492,7 +1515,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1596,7 +1620,8 @@ func unittest() { fmt.Printf("get value: %d\n", i) } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1658,7 +1683,8 @@ func unittest() { fn() } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1754,7 +1780,8 @@ outer: } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1794,7 +1821,8 @@ func unittest() { } } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1846,7 +1874,8 @@ func unittest() { return struct{}{} }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -1968,7 +1997,8 @@ labelCall: fmt.Println("output in failpoint") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2022,7 +2052,8 @@ func unittest() { }()] fmt.Println(x) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2086,7 +2117,8 @@ func unittest() { }()] fmt.Println(x) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2136,7 +2168,8 @@ func unittest() { }() fmt.Println(x.A) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2190,7 +2223,8 @@ func unittest() { } fmt.Println(x.A) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2224,7 +2258,8 @@ func unittest() { failpoint.Eval(_curpkg_("failpoint-name")) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2258,7 +2293,8 @@ func unittest() { failpoint.EvalContext(nil, _curpkg_("failpoint-name")) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2292,7 +2328,8 @@ func unittest() (int, int, error) { return 123, 456, errors.New("something") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2326,7 +2363,8 @@ func unittest() (int, int, error) { return val.(int), 456, errors.New("something") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2372,7 +2410,8 @@ func unittest() { return &X{Y: 100} }().Y++ } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2400,7 +2439,8 @@ func unittest() { failpoint.Eval(_curpkg_("failpoint-name")) failpoint.Eval(_curpkg_("failpoint-name")) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, } @@ -2467,7 +2507,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2487,7 +2528,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2507,7 +2549,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2527,7 +2570,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2547,7 +2591,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2567,7 +2612,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2587,7 +2633,8 @@ func unittest() { fmt.Println("unit-test", val) }, "failpoint-name") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2605,7 +2652,8 @@ import ( func unittest() { failpoint.Inject("failpoint-name", "failpoint-name") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2625,7 +2673,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2645,7 +2694,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2665,7 +2715,8 @@ func unittest() { fmt.Println("unit-test", val) }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2683,7 +2734,8 @@ import ( func unittest() { failpoint.InjectContext(nil, "failpoint-name", "string literal") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2701,7 +2753,8 @@ import ( func unittest() { failpoint.InjectContext(nil, "failpoint-name", func(val int, val int) {}) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2719,7 +2772,8 @@ import ( func unittest() { failpoint.InjectContext(nil, "failpoint-name", func(val, val int) {}) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2737,7 +2791,8 @@ import ( func unittest() { failpoint.Break("11", "22") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2755,7 +2810,8 @@ import ( func unittest() { failpoint.Continue("11", "22") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2773,7 +2829,8 @@ import ( func unittest() { failpoint.Label("11", "22") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2791,7 +2848,8 @@ import ( func unittest() { failpoint.Goto("11", "22") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2812,7 +2870,8 @@ func unittest() { } x() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2835,7 +2894,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2858,7 +2918,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2878,7 +2939,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2899,7 +2961,8 @@ func unittest() { return []int{1} }()[0] } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2920,7 +2983,8 @@ func unittest() { return 0 }():1] } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2941,7 +3005,8 @@ func unittest() { return 1 }()] } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2963,7 +3028,8 @@ func unittest() { return 2 }()] } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -2984,7 +3050,8 @@ func unittest() { return 1 }()} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3004,7 +3071,8 @@ func unittest() { failpoint.Goto("11", "22") }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3024,7 +3092,8 @@ func unittest() { failpoint.Goto("11", "22") }) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3045,7 +3114,8 @@ func unittest() { return true }() && true } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3065,7 +3135,8 @@ func unittest() { failpoint.Goto("11", "22") }()) } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3086,7 +3157,8 @@ func unittest() int { return 1 }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3107,7 +3179,8 @@ func unittest() { } _, _ = a, b } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3127,7 +3200,8 @@ func unittest() { failpoint.Goto("11", "22") }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3147,7 +3221,8 @@ func unittest() { failpoint.Goto("11", "22") }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3167,7 +3242,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3191,7 +3267,8 @@ func unittest() { return } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3212,7 +3289,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3234,7 +3312,8 @@ func unittest() { case 1: } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3256,7 +3335,8 @@ func unittest() { case 1: } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3279,7 +3359,8 @@ func unittest() { }(): } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3302,7 +3383,8 @@ func unittest() { }(): } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3323,7 +3405,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3342,7 +3425,8 @@ func unittest() { select {} failpoint.Goto("11", "22") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3362,7 +3446,8 @@ func unittest() { failpoint.Goto("11", "22") }());; {} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3382,7 +3467,8 @@ func unittest() { failpoint.Goto("11", "22") }()) {} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3402,7 +3488,8 @@ func unittest() { failpoint.Goto("11", "22") }()) {} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3422,7 +3509,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3442,7 +3530,8 @@ func unittest() { failpoint.Goto("11", "22") }()) {} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3462,7 +3551,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3482,7 +3572,8 @@ func unittest() { failpoint.Goto("11", "22") }()).(type) {} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3502,7 +3593,8 @@ func unittest() { failpoint.Goto("11", "22") }()).(type) {} } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3523,7 +3615,8 @@ func unittest() { failpoint.Goto("11", "22") } } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3543,7 +3636,8 @@ func unittest() { failpoint.Goto("11", "22") }() } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, { @@ -3562,7 +3656,8 @@ func unittest() { label: failpoint.Goto("11", "22") } -`, + +// Code generated by failpoint DO NOT EDIT.`, }, }