-
Notifications
You must be signed in to change notification settings - Fork 2
/
generator.go
48 lines (42 loc) · 1.19 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package fake
import (
"go/ast"
"go/token"
"os"
"path"
"github.com/sonalys/fake/internal/files"
"github.com/sonalys/fake/internal/imports"
"golang.org/x/mod/modfile"
)
// Generator is the controller for the whole module, caching files and holding metadata.
type Generator struct {
FileSet *token.FileSet
MockPackageName string
cachedPackageInfo func(f *ast.File) (nameMap, pathMap map[string]*imports.ImportEntry)
goModFilename string
goMod *modfile.File
}
// NewGenerator will create a new mock generator for the specified module.
func NewGenerator(pkgName, baseDir string) (*Generator, error) {
goModPath, err := files.FindFile(baseDir, "go.mod")
if err != nil {
return nil, err
}
// Read the contents of the go.mod file
modFileContent, err := os.ReadFile(goModPath)
if err != nil {
return nil, err
}
// Parse the go.mod file
modFile, err := modfile.Parse(goModPath, modFileContent, nil)
if err != nil {
return nil, err
}
return &Generator{
FileSet: token.NewFileSet(),
goModFilename: goModPath,
goMod: modFile,
MockPackageName: pkgName,
cachedPackageInfo: imports.CachedImportInformation(path.Dir(goModPath)),
}, nil
}