jauhararifin / gomocker Goto Github PK
View Code? Open in Web Editor NEWGomocker generates Golang code to easily mock a function or interface
License: MIT License
Gomocker generates Golang code to easily mock a function or interface
License: MIT License
I have created this issue as a feature request because I'm not sure if generation of mocks in a different package/directory is supposed to be supported in the first place by gomocker
.
I have tried the following command in an attempt to generate mocks in a different package/directory from the source file:
//go:generate gomocker gen MyInterface --force --package mock --output ../../gen/mock/my_interface_mock.go
This works when ../../gen/mock
already exists but fails with the following error if it doesn't:
Error: open ../../gen/mock/my_interface_mock.go: no such file or directory
So right now it seems we just need to create directories when they don't exist in order to support the function stated in this feature request.
The reason of this request is that it's easier to have all mocks in a separate directory if we do some bulk operations on generated files (say, excluding them from format checks).
Hi @jauhararifin
It seems gomocker
may not generate the correct package name if the package path contains special characters like underscore _
. An example is to run gomocker --package abc/my_package MyInterfaceToMock
. In this case, the generated file will have package mypackage
. If the source package is actually package my_package
, the generated file will cause build errors due to different package names used within the same directory.
I think this comes from the fact that gomocker
uses jen.NewFilePath
to create the generated file:
// NewFilePath creates a new file while specifying the package path - the
// package name is inferred from the path.
func NewFilePath(packagePath string) *File {
return &File{
Group: &Group{
multi: true,
},
name: guessAlias(packagePath),
path: packagePath,
imports: map[string]importdef{},
hints: map[string]importdef{},
}
}
...
func guessAlias(path string) string {
alias := path
if strings.HasSuffix(alias, "/") {
// training slashes are usually tolerated, so we can get rid of one if
// it exists
alias = alias[:len(alias)-1]
}
if strings.Contains(alias, "/") {
// if the path contains a "/", use the last part
alias = alias[strings.LastIndex(alias, "/")+1:]
}
// alias should be lower case
alias = strings.ToLower(alias)
// alias should now only contain alphanumerics
importsRegex := regexp.MustCompile(`[^a-z0-9]`)
alias = importsRegex.ReplaceAllString(alias, "")
return alias
}
I think this makes sense for jen
lib. But maybe for gomocker
, the better way is to use jen.NewFilePathName
to create the new file if the package is already provided by the user?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.