-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add more code to plumb through all the values needed by moq registry …
…methods In this commit, we gather all the template data needed by the moq logic to generate its template. This is untested as of yet. TODO: need to start testing this works by calling upon `moq` in `.mockery.yaml`.
- Loading branch information
1 parent
860c2c8
commit d66be2c
Showing
9 changed files
with
108 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1 @@ | ||
package registry | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func BenchmarkNew(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
New("../../pkg/moq/testpackages/example", "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,110 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"go/types" | ||
|
||
"github.com/chigopher/pathlib" | ||
"github.com/rs/zerolog" | ||
"github.com/vektra/mockery/v2/pkg/config" | ||
"github.com/vektra/mockery/v2/pkg/registry" | ||
"github.com/vektra/mockery/v2/pkg/template" | ||
"golang.org/x/tools/go/packages" | ||
) | ||
|
||
type TemplateGeneratorConfig struct { | ||
Style string | ||
} | ||
type TemplateGenerator struct { | ||
config TemplateGeneratorConfig | ||
config TemplateGeneratorConfig | ||
registry *registry.Registry | ||
} | ||
|
||
func NewTemplateGenerator(config TemplateGeneratorConfig) *TemplateGenerator { | ||
return &TemplateGenerator{ | ||
config: config, | ||
func NewTemplateGenerator(srcPkg *packages.Package, config TemplateGeneratorConfig) (*TemplateGenerator, error) { | ||
reg, err := registry.New(srcPkg) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating new registry: %w", err) | ||
} | ||
|
||
return &TemplateGenerator{ | ||
config: config, | ||
registry: reg, | ||
}, nil | ||
} | ||
|
||
func (g *TemplateGenerator) Generate(iface *Interface, ifaceConfig *config.Config) error { | ||
templ, err := template.New(g.config.Style) | ||
if err != nil { | ||
return err | ||
} | ||
func (g *TemplateGenerator) Generate(ctx context.Context, iface *Interface, ifaceConfig *config.Config) error { | ||
log := zerolog.Ctx(ctx) | ||
log.Info().Msg("generating mock for interface") | ||
|
||
imports := Imports{} | ||
for _, method := range iface.Methods() { | ||
method.populateImports(imports) | ||
} | ||
// TODO: Work on getting these imports into the template | ||
methods := make([]template.MethodData, iface.ActualInterface.NumMethods()) | ||
|
||
for i := 0; i < iface.ActualInterface.NumMethods(); i++ { | ||
method := iface.ActualInterface.Method(i) | ||
methodScope := g.registry.MethodScope() | ||
|
||
signature := method.Type().(*types.Signature) | ||
params := make([]template.ParamData, signature.Params().Len()) | ||
for j := 0; j < signature.Params().Len(); j++ { | ||
param := signature.Params().At(j) | ||
params[j] = template.ParamData{ | ||
Var: methodScope.AddVar(param, ""), | ||
Variadic: signature.Variadic() && j == signature.Params().Len()-1, | ||
} | ||
} | ||
|
||
returns := make([]template.ParamData, signature.Results().Len()) | ||
for j := 0; j < signature.Results().Len(); j++ { | ||
param := signature.Results().At(j) | ||
returns[j] = template.ParamData{ | ||
Var: methodScope.AddVar(param, "Out"), | ||
Variadic: false, | ||
} | ||
} | ||
|
||
methods[i] = template.MethodData{ | ||
Name: method.Name(), | ||
Params: params, | ||
Returns: returns, | ||
} | ||
|
||
} | ||
|
||
// For now, mockery only supports one mock per file, which is why we're creating | ||
// a single-element list. moq seems to have supported multiple mocks per file. | ||
mockData := []template.MockData{ | ||
{ | ||
InterfaceName: iface.Name, | ||
MockName: ifaceConfig.MockName, | ||
Methods: methods, | ||
}, | ||
} | ||
data := template.Data{ | ||
PkgName: ifaceConfig.Outpkg, | ||
SrcPkgQualifier: iface.Pkg.Name() + ".", | ||
Imports: | ||
Imports: g.registry.Imports(), | ||
Mocks: mockData, | ||
} | ||
|
||
templ, err := template.New(g.config.Style) | ||
if err != nil { | ||
return fmt.Errorf("creating new template: %w", err) | ||
} | ||
|
||
var buf bytes.Buffer | ||
if err := templ.Execute(&buf, data); err != nil { | ||
return fmt.Errorf("executing template: %w", err) | ||
} | ||
|
||
outPath := pathlib.NewPath(ifaceConfig.Dir).Join(ifaceConfig.FileName) | ||
if err := outPath.WriteFile(buf.Bytes()); err != nil { | ||
log.Error().Err(err).Msg("couldn't write to output file") | ||
return fmt.Errorf("writing to output file: %w", err) | ||
} | ||
return nil | ||
} |