Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search vendor directories when using import path #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions locator/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func GetInterfaceFromFilePath(interfaceName, filePath string) (*model.InterfaceT
return nil, err
}

vendorPaths, err := vendorPathsForDirPath(dirPath)
vendorPaths, err := VendorPathsForDirPath(dirPath)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func importPathForDirPath(sourcePath string) (string, error) {
return "", fmt.Errorf("Path '%s' is not on GOPATH", sourcePath)
}

func vendorPathsForDirPath(dirPath string) ([]string, error) {
func VendorPathsForDirPath(dirPath string) ([]string, error) {
dirPath, err := filepath.Abs(dirPath)
if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func generateFake(interfaceName, sourcePackageDir, importPath, outputPath, desti
var err error
var iface *model.InterfaceToFake
if sourcePackageDir == "" {
iface, err = locator.GetInterfaceFromImportPath(interfaceName, importPath)
var vendorPaths []string
vendorPaths, err = locator.VendorPathsForDirPath(".")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like this could call to check for vendor paths could easily done inside the call to GetInterfaceFromImportPath. I think that would simplify this code here in main.go, which we should absolutely do, since it's not tested by unit tests.

In general, if you're calling multiple methods to an object (or in this case, a package) and then passing the results from one directly into the other, it's a case of the abstraction you've created leaking, and not having a well defined interface for how to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I wasn't sure how to do that.

I would rename GetInterfaceFromImportPath to an unexported function, change all calls to that, then create another GetInterfaceFromImportPath without the last parameter, and call locator.VendorPathsForDirPath(".") before calling the unexported function.

There's also another PR that changes these functions, so that complicates things a bit.

if err == nil {
iface, err = locator.GetInterfaceFromImportPath(interfaceName, importPath, vendorPaths...)
}
} else {
iface, err = locator.GetInterfaceFromFilePath(interfaceName, sourcePackageDir)
}
Expand Down