| 1 |
package assertions |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"strconv" |
| 9 |
"text/template" |
| 10 |
|
| 11 |
"github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common" |
| 12 |
) |
| 13 |
|
| 14 |
type GenInfo struct { |
| 15 |
ConstMap map[string]bool |
| 16 |
// the package the catalog is generated into |
| 17 |
PackageName string |
| 18 |
AssertPackageName string |
| 19 |
VersionText string |
| 20 |
CreateDate string |
| 21 |
ExpectedVals []*AntExpect |
| 22 |
NumericGuidanceVals []*AntGuidance |
| 23 |
BooleanGuidanceVals []*AntGuidance |
| 24 |
HasAssertions bool |
| 25 |
HasNumericGuidance bool |
| 26 |
HasBooleanGuidance bool |
| 27 |
} |
| 28 |
|
| 29 |
func IsGeneratedFile(file_name string) bool { |
| 30 |
base_name := filepath.Base(file_name) |
| 31 |
return base_name == common.GENERATED_CATALOG_FILE |
| 32 |
} |
| 33 |
|
| 34 |
// expectOutputFile creates or opens the catalog output file in the given |
| 35 |
// directory. The file is named antithesis_catalog.go. |
| 36 |
func expectOutputFile(outputDir string) (*os.File, error) { |
| 37 |
output_file_name := filepath.Join(outputDir, common.GENERATED_CATALOG_FILE) |
| 38 |
|
| 39 |
var file *os.File |
| 40 |
var err error |
| 41 |
if file, err = os.OpenFile(output_file_name, os.O_RDWR|os.O_CREATE, 0644); err != nil { |
| 42 |
file = nil |
| 43 |
} |
| 44 |
if file != nil { |
| 45 |
if err = file.Truncate(0); err != nil { |
| 46 |
file = nil |
| 47 |
} |
| 48 |
} |
| 49 |
if err == nil { |
| 50 |
common.Logger.Printf(common.Normal, "Assertion Catalog: %q\n", output_file_name) |
| 51 |
} else { |
| 52 |
common.Logger.Printf(common.Normal, "Unable to generate Assertion Catalog: %q\n", output_file_name) |
| 53 |
} |
| 54 |
return file, err |
| 55 |
} |
| 56 |
|
| 57 |
func assertionNameRepr(s string) string { |
| 58 |
if s == "Reachable" || s == "Unreachable" { |
| 59 |
return fmt.Sprintf("%s(message, details)", s) |
| 60 |
} |
| 61 |
return fmt.Sprintf("%s(cond, message, details)", s) |
| 62 |
} |
| 63 |
|
| 64 |
func numericGuidanceNameRepr(s string) string { |
| 65 |
return fmt.Sprintf("%s(left, right, message, details)", s) |
| 66 |
} |
| 67 |
|
| 68 |
func booleanGuidanceNameRepr(s string) string { |
| 69 |
return fmt.Sprintf("%s(pairs, message, details)", s) |
| 70 |
} |
| 71 |
|
| 72 |
func hitRepr(b bool) string { |
| 73 |
if !b { |
| 74 |
return "notHit" |
| 75 |
} |
| 76 |
return "wasHit" |
| 77 |
} |
| 78 |
|
| 79 |
func condRepr(b bool) string { |
| 80 |
if b { |
| 81 |
return "condTrue" |
| 82 |
} |
| 83 |
return "condFalse" |
| 84 |
} |
| 85 |
|
| 86 |
func mustHitRepr(b bool) string { |
| 87 |
if b { |
| 88 |
return "mustBeHit" |
| 89 |
} |
| 90 |
return "optionallyHit" |
| 91 |
} |
| 92 |
|
| 93 |
func textRepr(s string) string { |
| 94 |
return strconv.Quote(s) |
| 95 |
} |
| 96 |
|
| 97 |
func guidanceFnRepr(n GuidanceFnType) string { |
| 98 |
gp := "" |
| 99 |
switch n { |
| 100 |
case GuidanceFnMaximize: |
| 101 |
gp = "maximize" |
| 102 |
case GuidanceFnMinimize: |
| 103 |
gp = "minimize" |
| 104 |
case GuidanceFnExplore: |
| 105 |
gp = "explore" |
| 106 |
case GuidanceFnWantAll: |
| 107 |
gp = "all" |
| 108 |
case GuidanceFnWantNone: |
| 109 |
gp = "none" |
| 110 |
} |
| 111 |
return textRepr(gp) |
| 112 |
} |
| 113 |
|
| 114 |
func assertTypeRepr(s string) string { |
| 115 |
reprText := "reachabilityTest" |
| 116 |
|
| 117 |
switch s { |
| 118 |
case "always": |
| 119 |
reprText = "universalTest" |
| 120 |
case "sometimes": |
| 121 |
reprText = "existentialTest" |
| 122 |
case "reachability": |
| 123 |
reprText = "reachabilityTest" |
| 124 |
} |
| 125 |
return reprText |
| 126 |
} |
| 127 |
|
| 128 |
func usesConst(cm map[string]bool, c string) bool { |
| 129 |
return cm[c] |
| 130 |
} |
| 131 |
|
| 132 |
// GenerateAssertionsCatalog renders the assertion catalog into the given |
| 133 |
// output directory as antithesis_catalog.go. |
| 134 |
func GenerateAssertionsCatalog(outputDir string, genInfo *GenInfo) error { |
| 135 |
if genInfo.PackageName == "" { |
| 136 |
return fmt.Errorf("cannot generate an assertion catalog with no package name") |
| 137 |
} |
| 138 |
|
| 139 |
var tmpl *template.Template |
| 140 |
var err error |
| 141 |
|
| 142 |
tmpl = template.New("expector") |
| 143 |
|
| 144 |
tmpl = tmpl.Funcs(template.FuncMap{ |
| 145 |
"hitRepr": hitRepr, |
| 146 |
"condRepr": condRepr, |
| 147 |
"mustHitRepr": mustHitRepr, |
| 148 |
"assertTypeRepr": assertTypeRepr, |
| 149 |
"assertionNameRepr": assertionNameRepr, |
| 150 |
"usesConst": usesConst, |
| 151 |
"textRepr": textRepr, |
| 152 |
"numericGuidanceNameRepr": numericGuidanceNameRepr, |
| 153 |
"booleanGuidanceNameRepr": booleanGuidanceNameRepr, |
| 154 |
"guidanceFnRepr": guidanceFnRepr, |
| 155 |
}) |
| 156 |
|
| 157 |
all_template_text := getExpectorText() + getNumericGuidanceText() + getBooleanGuidanceText() |
| 158 |
if tmpl, err = tmpl.Parse(all_template_text); err != nil { |
| 159 |
return err |
| 160 |
} |
| 161 |
|
| 162 |
var outFile io.Writer |
| 163 |
if outFile, err = expectOutputFile(outputDir); err != nil { |
| 164 |
return err |
| 165 |
} |
| 166 |
|
| 167 |
return tmpl.Execute(outFile, genInfo) |
| 168 |
} |
| 169 |
|
| 170 |
func getExpectorText() string { |
| 171 |
const text = `// Code generated by antithesis-go-instrumentor; DO NOT EDIT. |
| 172 |
|
| 173 |
package {{.PackageName}} |
| 174 |
|
| 175 |
// ---------------------------------------------------- |
| 176 |
// {{.VersionText}} |
| 177 |
// |
| 178 |
// Assertion Catalog |
| 179 |
// |
| 180 |
// Generated on {{.CreateDate}} |
| 181 |
// ---------------------------------------------------- |
| 182 |
|
| 183 |
{{if .HasAssertions -}}import "{{.AssertPackageName}}"{{- end}} |
| 184 |
|
| 185 |
{{if .HasAssertions -}} |
| 186 |
func init() { |
| 187 |
|
| 188 |
{{if usesConst .ConstMap "condFalse"}} const condFalse = false{{- end}} |
| 189 |
{{if usesConst .ConstMap "condTrue"}} const condTrue = true {{- end}} |
| 190 |
const wasHit = true |
| 191 |
{{if usesConst .ConstMap "notHit"}} const notHit = !wasHit {{- end}} |
| 192 |
{{if usesConst .ConstMap "mustBeHit"}} const mustBeHit = true {{- end}} |
| 193 |
{{if usesConst .ConstMap "optionallyHit"}} const optionallyHit = false {{- end}} |
| 194 |
{{if usesConst .ConstMap "universalTest"}} const universalTest = "always" {{- end}} |
| 195 |
{{if usesConst .ConstMap "existentialTest"}} const existentialTest = "sometimes" {{- end}} |
| 196 |
{{if usesConst .ConstMap "reachabilityTest"}} const reachabilityTest = "reachability" {{- end}} |
| 197 |
|
| 198 |
var noDetails map[string]any = nil |
| 199 |
|
| 200 |
{{- range .ExpectedVals }} |
| 201 |
{{- $cond := condRepr .AssertionFuncInfo.Condition -}} |
| 202 |
{{- $didHit := hitRepr false -}} |
| 203 |
{{- $mustHit := mustHitRepr .AssertionFuncInfo.MustHit -}} |
| 204 |
{{- $assertionName := assertionNameRepr .Assertion -}} |
| 205 |
{{- $assertType := assertTypeRepr .AssertionFuncInfo.AssertType -}} |
| 206 |
{{- $message := textRepr .Message -}} |
| 207 |
{{- $classname := textRepr .Classname -}} |
| 208 |
{{- $funcname := textRepr .Funcname -}} |
| 209 |
{{- $filename := textRepr .Filename -}} |
| 210 |
{{- $displayname := textRepr .Assertion}} |
| 211 |
|
| 212 |
// {{$assertionName}} |
| 213 |
assert.AssertRaw({{$cond}}, {{$message}}, noDetails, {{$classname}}, {{$funcname}}, {{$filename}}, {{.Line}}, 0, {{$didHit}}, {{$mustHit}}, {{$assertType}}, {{$displayname}}, {{$message}}) |
| 214 |
{{- end}} |
| 215 |
} |
| 216 |
{{- end}} |
| 217 |
` |
| 218 |
|
| 219 |
return text |
| 220 |
} |
| 221 |
|
| 222 |
func getNumericGuidanceText() string { |
| 223 |
const text = ` |
| 224 |
|
| 225 |
{{if .HasNumericGuidance -}} |
| 226 |
func init() { |
| 227 |
|
| 228 |
const notHit = false |
| 229 |
const left = 0 |
| 230 |
const right = 0 |
| 231 |
|
| 232 |
{{- range .NumericGuidanceVals }} |
| 233 |
{{- $guidanceName := numericGuidanceNameRepr .Assertion -}} |
| 234 |
{{- $message := textRepr .Message -}} |
| 235 |
{{- $classname := textRepr .Classname -}} |
| 236 |
{{- $funcname := textRepr .Funcname -}} |
| 237 |
{{- $filename := textRepr .Filename -}} |
| 238 |
{{- $guidanceFn := guidanceFnRepr .GuidanceFuncInfo.GuidanceFn}} |
| 239 |
|
| 240 |
// {{$guidanceName}} |
| 241 |
assert.NumericGuidanceRaw(left, right, {{$message}}, {{$message}}, {{$classname}}, {{$funcname}}, {{$filename}}, {{.Line}}, {{$guidanceFn}}, notHit) |
| 242 |
{{- end}} |
| 243 |
} |
| 244 |
{{- end}} |
| 245 |
` |
| 246 |
|
| 247 |
return text |
| 248 |
} |
| 249 |
|
| 250 |
func getBooleanGuidanceText() string { |
| 251 |
const text = ` |
| 252 |
|
| 253 |
{{if .HasBooleanGuidance -}} |
| 254 |
func init() { |
| 255 |
|
| 256 |
const notHit = false |
| 257 |
var named_bools = []assert.NamedBool{} |
| 258 |
|
| 259 |
{{- range .BooleanGuidanceVals }} |
| 260 |
{{- $guidanceName := booleanGuidanceNameRepr .Assertion -}} |
| 261 |
{{- $message := textRepr .Message -}} |
| 262 |
{{- $classname := textRepr .Classname -}} |
| 263 |
{{- $funcname := textRepr .Funcname -}} |
| 264 |
{{- $filename := textRepr .Filename -}} |
| 265 |
{{- $guidanceFn := guidanceFnRepr .GuidanceFuncInfo.GuidanceFn}} |
| 266 |
|
| 267 |
// {{$guidanceName}} |
| 268 |
assert.BooleanGuidanceRaw(named_bools, {{$message}}, {{$message}}, {{$classname}}, {{$funcname}}, {{$filename}}, {{.Line}}, {{$guidanceFn}}, notHit) |
| 269 |
{{- end}} |
| 270 |
} |
| 271 |
{{- end}} |
| 272 |
` |
| 273 |
|
| 274 |
return text |
| 275 |
} |