Coder Social home page Coder Social logo

gomocker's People

Contributors

jauhararifin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

fatanugraha

gomocker's Issues

Feature request: allow mocks to be created in a different package/directory from source files

Hi @jauhararifin

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).

Unable to generate package name correctly

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.