| 1 |
package coverage |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"os" |
| 6 |
"path" |
| 7 |
"text/template" |
| 8 |
|
| 9 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 10 |
) |
| 11 |
|
| 12 |
func GenerateNotifierSource(notifierDir string, notifierInfo *NotifierInfo) { |
| 13 |
var tmpl *template.Template |
| 14 |
var err error |
| 15 |
|
| 16 |
tmpl = template.New("notifier") |
| 17 |
|
| 18 |
if tmpl, err = tmpl.Parse(getNotifierText()); err != nil { |
| 19 |
panic(err) |
| 20 |
} |
| 21 |
|
| 22 |
var outFile io.Writer |
| 23 |
if outFile, err = notifierOutputFile(notifierDir); err != nil { |
| 24 |
panic(err) |
| 25 |
} |
| 26 |
|
| 27 |
if err = tmpl.Execute(outFile, notifierInfo); err != nil { |
| 28 |
panic(err) |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func getNotifierText() string { |
| 33 |
const text = `package {{.NotifierPackage}} |
| 34 |
|
| 35 |
// ---------------------------------------------------- |
| 36 |
// Generated by Antithesis instrumentor - do not modify |
| 37 |
// ---------------------------------------------------- |
| 38 |
|
| 39 |
import "{{.InstrumentationPackageName}}" |
| 40 |
|
| 41 |
func init() { |
| 42 |
instrumentation.InitializeModule("{{.SymbolTableName}}", {{.EdgeCount}}) |
| 43 |
} |
| 44 |
|
| 45 |
func Notify(edge int) { |
| 46 |
instrumentation.Notify(edge) |
| 47 |
} |
| 48 |
|
| 49 |
` |
| 50 |
|
| 51 |
return text |
| 52 |
} |
| 53 |
|
| 54 |
func notifierOutputFile(dir_name string) (*os.File, error) { |
| 55 |
output_file_name := path.Join(dir_name, common.GENERATED_NOTIFIER_SOURCE) |
| 56 |
|
| 57 |
var file *os.File |
| 58 |
var err error |
| 59 |
if file, err = os.OpenFile(output_file_name, os.O_RDWR|os.O_CREATE, 0644); err != nil { |
| 60 |
file = nil |
| 61 |
} |
| 62 |
if file != nil { |
| 63 |
if err = file.Truncate(0); err != nil { |
| 64 |
file = nil |
| 65 |
} |
| 66 |
} |
| 67 |
if err == nil { |
| 68 |
common.Logger.Printf(common.Normal, "Notifier file: %q\n", output_file_name) |
| 69 |
} else { |
| 70 |
common.Logger.Printf(common.Normal, "Unable to generate Notifier file: %q\n", output_file_name) |
| 71 |
} |
| 72 |
return file, err |
| 73 |
} |