Debian Codegraph @

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

raw
1 package config
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7
8 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/args"
9 "github.com/antithesishq/antithesis-sdk-go/tools/antithesis-go-instrumentor/common"
10 "golang.org/x/mod/modfile"
11 )
12
13 // CommonConfig holds fields shared across both coverage instrumentation
14 // and assertion scanning.
15 type CommonConfig struct {
16 // The set of exclusions that were obtained from
17 // reading the 'excludeFile'. A map is used where
18 // the value is always 'true', in lieu of a specific
19 // 'set' abstraction not available for the version
20 // of go used for this tool.
21 Exclusions map[string]bool
22
23 // SHA256 Hash (48-bits worth) of all the files
24 // in sourceFiles
25 FilesHash string
26
27 // The base directry of a go module to be instrumented/cataloged
28 // Contains a go.mod file
29 InputDirectory string
30
31 // Option file containing a list (one per line) of
32 // any files or directories to be excluded from both
33 // instumentation and assertion scanning. Empty lines
34 // and lines beginining with '#' are ignored.
35 ExcludeFile string
36
37 // created and written to during instrumentation.
38 // Will contain a copy of the inputDirectory, where All
39 // non-excluded '.go' files are instrumented
40 CustomerDirectory string
41
42 // All of the files (after exclusions) to be instrumented
43 // and scanned for assertions that should appear in the
44 // assertion catalog
45 SourceFiles []string
46
47 // Number of files skipped when creating the sourceFiles
48 // list.
49 FilesSkipped int
50
51 // Indicates that '*_test.go' files should be skipped (not instrumented)
52 // default is false
53 SkipTestFiles bool
54
55 // Indicates that '*.pb.go' files should be skipped (not instrumented)
56 // default is false
57 SkipProtoBufFiles bool
58 }
59
60 func NewCommonConfig(args *args.Args) (*CommonConfig, error) {
61 customerInputDirectory := common.GetAbsoluteDirectory(args.InputDir)
62
63 if _, err := getModuleName(customerInputDirectory); err != nil {
64 return nil, fmt.Errorf("unable to obtain go module name from %q", customerInputDirectory)
65 }
66
67 customerDirectory := ""
68 if args.WantsInstrumentor {
69 outputDirectory := common.GetAbsoluteDirectory(args.OutputDir)
70 customerDirectory = filepath.Join(outputDirectory, common.INSTRUMENTED_SOURCE_FOLDER)
71 }
72
73 return &CommonConfig{
74 InputDirectory: customerInputDirectory,
75 CustomerDirectory: customerDirectory,
76 ExcludeFile: args.ExcludeFile,
77 SkipTestFiles: args.SkipTestFiles,
78 SkipProtoBufFiles: args.SkipProtoBufFiles,
79 }, nil
80 }
81
82 // CommonConfig methods
83
84 func (cc *CommonConfig) GetSourceDir() string {
85 return cc.InputDirectory
86 }
87
88 func (cc *CommonConfig) ParseExclusionsFile() (err error) {
89 if cc.ExcludeFile == "" {
90 return
91 }
92 cc.Exclusions = map[string]bool{}
93 var parsedExclusions map[string]bool
94
95 parsedExclusions, err = ParseExclusionsFile(cc.ExcludeFile, cc.InputDirectory)
96 if err == nil {
97 cc.Exclusions = parsedExclusions
98 }
99 return
100 }
101
102 // Private helpers
103
104 func getModuleName(inputDir string) (moduleName string, err error) {
105 var moduleData []byte
106 moduleName = ""
107 moduleFilenamePath := filepath.Join(inputDir, "go.mod")
108 if moduleData, err = os.ReadFile(moduleFilenamePath); err != nil {
109 return
110 }
111
112 var f *modfile.File
113 if f, err = modfile.ParseLax("go.mod", moduleData, nil); err == nil {
114 moduleName = f.Module.Mod.Path
115 }
116 return
117 }