Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix issues related to source package imports (#507)
Browse files Browse the repository at this point in the history
Fixes: #505
Fixes: #515
  • Loading branch information
codyoss authored Feb 19, 2021
1 parent f67ce0c commit a23c5e7
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 11 deletions.
12 changes: 12 additions & 0 deletions mockgen/internal/tests/extra_import/import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package extra_import makes sure output does not import it. See #515.
package extra_import

//go:generate mockgen -destination mock.go -package extra_import . Foo

type Message struct {
Text string
}

type Foo interface {
Bar(channels []string, message chan<- Message)
}
46 changes: 46 additions & 0 deletions mockgen/internal/tests/extra_import/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions mockgen/internal/tests/missing_import/output/source_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions mockgen/internal/tests/missing_import/source/source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:generate mockgen -package source -destination=../output/source_mock.go -source=source.go

// Package source makes sure output imports its. See #505.
package source

type Foo struct{}

type Bar interface {
Baz(Foo)
}
20 changes: 9 additions & 11 deletions mockgen/mockgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"encoding/json"
"flag"
"fmt"
"go/build"
"go/token"
"io"
"io/ioutil"
Expand Down Expand Up @@ -133,15 +132,14 @@ func main() {
// "package.X" since "package" is this package). This can happen if the mock
// is output into an already existing package.
outputPackagePath := *selfPackage
if len(outputPackagePath) == 0 && len(*destination) > 0 {
dst, _ := filepath.Abs(filepath.Dir(*destination))
for _, prefix := range build.Default.SrcDirs() {
if strings.HasPrefix(dst, prefix) {
if rel, err := filepath.Rel(prefix, dst); err == nil {
outputPackagePath = rel
break
}
}
if outputPackagePath == "" && *destination != "" {
dstPath, err := filepath.Abs(filepath.Dir(*destination))
if err != nil {
log.Fatalf("Unable to determine destination file path: %v", err)
}
outputPackagePath, err = parsePackageImport(dstPath)
if err != nil {
log.Fatalf("Unable to determine destination file path: %v", err)
}
}

Expand Down Expand Up @@ -330,7 +328,7 @@ func (g *generator) Generate(pkg *model.Package, outputPkgName string, outputPac
}

// Avoid importing package if source pkg == output pkg
if pth == pkg.PkgPath && outputPkgName == pkg.Name {
if pth == pkg.PkgPath && outputPackagePath == pkg.PkgPath {
continue
}

Expand Down

0 comments on commit a23c5e7

Please sign in to comment.