Debian Codegraph @

tools/antithesis-go-instrumentor/antithesis-go-instrumentor.go

raw
1 package main
2
3 import (
4 _ "embed"
5 "fmt"
6 "os"
7 "strings"
8
9 "github.com/antithesishq/antithesis-sdk-go/internal"
10 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/args"
11 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
12 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/config"
13 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/assertions"
14 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage"
15 covconfig "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/scanners/coverage/config"
16 )
17
18 //go:embed version.txt
19 var versionText string
20
21 func main() {
22 versionString := strings.TrimSpace(versionText)
23 if strings.Contains(versionText, "%s") {
24 versionString = fmt.Sprintf(versionString, internal.SDK_Version)
25 }
26
27 //--------------------------------------------------------------------------------
28 // Parse and validate command arguments
29 // Establish global logging
30 //--------------------------------------------------------------------------------
31 thisVersion := fmt.Sprintf("v%s", internal.SDK_Version)
32 parsedArgs := args.ParseArgs(versionString, thisVersion)
33 if parsedArgs.ShowVersion {
34 fmt.Println(strings.TrimSpace(versionString))
35 os.Exit(0)
36 }
37 if parsedArgs.InvalidArgs {
38 os.Exit(1)
39 }
40
41 common.NewLogWriter(parsedArgs.LogFile, parsedArgs.VerbosityLevel)
42 common.Logger.Printf(common.Normal, "%s", strings.TrimSpace(versionString))
43 parsedArgs.ShowArguments()
44
45 //--------------------------------------------------------------------------------
46 // Verify Directories and Files are all as expected
47 // Prepare instrumentation output directories
48 //--------------------------------------------------------------------------------
49 cc, err := config.NewCommonConfig(parsedArgs)
50 if err != nil {
51 common.Logger.Printf(common.Normal, "%s", err.Error())
52 os.Exit(1)
53 }
54
55 source_dir := cc.GetSourceDir()
56 target_dir := source_dir
57
58 var cI *coverage.CoverageInstrumentor
59 var numSourceFiles int
60
61 //--------------------------------------------------------------------------------
62 // Coverage instrumentation (only when not in assert-only mode)
63 //--------------------------------------------------------------------------------
64 if parsedArgs.WantsInstrumentor {
65 cov, err := covconfig.NewCoverageConfig(parsedArgs)
66 if err != nil {
67 common.Logger.Printf(common.Normal, "%s", err.Error())
68 os.Exit(1)
69 }
70
71 var source_files []string
72 if source_files, err = cov.GetSourceFiles(cc); err != nil {
73 common.Logger.Printf(common.Normal, "%s", err.Error())
74 os.Exit(1)
75 }
76 numSourceFiles = len(source_files)
77
78 cI = coverage.NewCoverageInstrumentor(cc, cov)
79 target_dir = cc.CustomerDirectory
80
81 // Pass 1: Coverage instrumentation (file-by-file)
82 cov.ShowDependentModules()
83 for _, file_name := range source_files {
84 if assertions.IsGeneratedFile(file_name) {
85 common.Logger.Printf(common.Normal, "Skipping %s", file_name)
86 continue
87 }
88
89 if instrumented_source := cI.InstrumentFile(file_name); instrumented_source != "" {
90 cI.WriteInstrumentedOutput(cc, file_name, instrumented_source)
91 cov.UpdateDependentModules(file_name)
92 }
93 }
94 cov.ShowDependentModules()
95
96 // Wrap-up coverage instrumentation and generate notifier module
97 edge_count := cI.WrapUp()
98 if edge_count > 0 {
99 notifierDir := cov.GetNotifierDirectory()
100 cI.WriteNotifierSource(notifierDir, edge_count)
101 cov.CreateNotifierModule(cc)
102 }
103
104 cov.WrapUp(cc)
105 }
106
107 //--------------------------------------------------------------------------------
108 // Assertion catalog generation (go/packages-based, per-binary)
109 //--------------------------------------------------------------------------------
110 aScanner := assertions.NewAssertionScanner(source_dir, target_dir)
111 if err := aScanner.ScanAll(); err != nil {
112 common.Logger.Printf(common.Normal, "Assertion scanning failed: %s", err.Error())
113 common.Logger.Printf(common.Normal, "Assertion catalogs will not be generated")
114 } else if aScanner.HasAssertionsDefined() {
115 aScanner.WriteAssertionCatalogs(parsedArgs.VersionText)
116 }
117
118 //--------------------------------------------------------------------------------
119 // Summarize results in logger
120 //--------------------------------------------------------------------------------
121 if cI != nil {
122 cI.SummarizeWork(numSourceFiles)
123 }
124 aScanner.SummarizeWork()
125 }