commit
c156bf3a75
2
main.go
2
main.go
@ -9,7 +9,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/matryer/moq/package/moq"
|
"github.com/matryer/moq/pkg/moq"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -18,6 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type customImporter struct {
|
type customImporter struct {
|
||||||
|
source string
|
||||||
imported map[string]*types.Package
|
imported map[string]*types.Package
|
||||||
base types.Importer
|
base types.Importer
|
||||||
skipTestFiles bool
|
skipTestFiles bool
|
||||||
@ -43,8 +44,13 @@ func (i *customImporter) Import(path string) (*types.Package, error) {
|
|||||||
return pkg, nil
|
return pkg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gopathDir(pkg string) (string, error) {
|
func gopathDir(source, pkg string) (string, error) {
|
||||||
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
|
// check vendor directory
|
||||||
|
vendorPath, found := vendorPath(source, pkg)
|
||||||
|
if found {
|
||||||
|
return vendorPath, nil
|
||||||
|
}
|
||||||
|
for _, gopath := range gopaths() {
|
||||||
absPath, err := filepath.Abs(path.Join(gopath, "src", pkg))
|
absPath, err := filepath.Abs(path.Join(gopath, "src", pkg))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -53,18 +59,52 @@ func gopathDir(pkg string) (string, error) {
|
|||||||
return absPath, nil
|
return absPath, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "", fmt.Errorf("%s not in $GOPATH", pkg)
|
return "", fmt.Errorf("%s not in $GOPATH or %s", pkg, path.Join(source, "vendor"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func vendorPath(source, pkg string) (string, bool) {
|
||||||
|
for {
|
||||||
|
if isGopath(source) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
source, err = filepath.Abs(source)
|
||||||
|
if err != nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
vendorPath, err := filepath.Abs(path.Join(source, "vendor", pkg))
|
||||||
|
if err != nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
if dir, err := os.Stat(vendorPath); err == nil && dir.IsDir() {
|
||||||
|
return vendorPath, true
|
||||||
|
}
|
||||||
|
source = filepath.Dir(source)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeGopath(p string) string {
|
func removeGopath(p string) string {
|
||||||
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
|
for _, gopath := range gopaths() {
|
||||||
p = strings.Replace(p, path.Join(gopath, "src")+"/", "", 1)
|
p = strings.Replace(p, path.Join(gopath, "src")+"/", "", 1)
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func gopaths() []string {
|
||||||
|
return strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator))
|
||||||
|
}
|
||||||
|
|
||||||
|
func isGopath(path string) bool {
|
||||||
|
for _, p := range gopaths() {
|
||||||
|
if p == path {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (i *customImporter) fsPkg(pkg string) (*types.Package, error) {
|
func (i *customImporter) fsPkg(pkg string) (*types.Package, error) {
|
||||||
dir, err := gopathDir(pkg)
|
dir, err := gopathDir(i.source, pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return importOrErr(i.base, pkg, err)
|
return importOrErr(i.base, pkg, err)
|
||||||
}
|
}
|
||||||
@ -118,8 +158,9 @@ func importOrErr(base types.Importer, pkg string, err error) (*types.Package, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newImporter returns an importer that will try to import code from gopath before using go/importer.Default and skipping test files
|
// newImporter returns an importer that will try to import code from gopath before using go/importer.Default and skipping test files
|
||||||
func newImporter() types.Importer {
|
func newImporter(source string) types.Importer {
|
||||||
return &customImporter{
|
return &customImporter{
|
||||||
|
source: source,
|
||||||
imported: make(map[string]*types.Package),
|
imported: make(map[string]*types.Package),
|
||||||
base: importer.Default(),
|
base: importer.Default(),
|
||||||
skipTestFiles: true,
|
skipTestFiles: true,
|
||||||
@ -138,8 +179,8 @@ func newImporter() types.Importer {
|
|||||||
// stripGopath teks the directory to a package and remove the gopath to get the
|
// stripGopath teks the directory to a package and remove the gopath to get the
|
||||||
// cannonical package name
|
// cannonical package name
|
||||||
func stripGopath(p string) string {
|
func stripGopath(p string) string {
|
||||||
for _, gopath := range strings.Split(os.Getenv("GOPATH"), ":") {
|
for _, gopath := range gopaths() {
|
||||||
p = strings.Replace(p, path.Join(gopath, "src")+"/", "", 1)
|
p = strings.TrimPrefix(p, path.Join(gopath, "src")+"/")
|
||||||
}
|
}
|
||||||
return p
|
return p
|
||||||
}
|
}
|
@ -76,7 +76,7 @@ func (m *Mocker) Mock(w io.Writer, name ...string) error {
|
|||||||
files[i] = file
|
files[i] = file
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
conf := types.Config{Importer: newImporter()}
|
conf := types.Config{Importer: newImporter(m.src)}
|
||||||
tpkg, err := conf.Check(m.src, m.fset, files, nil)
|
tpkg, err := conf.Check(m.src, m.fset, files, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestMoq(t *testing.T) {
|
func TestMoq(t *testing.T) {
|
||||||
m, err := New("testdata/example", "")
|
m, err := New("testpackages/example", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ func TestMoq(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMoqExplicitPackage(t *testing.T) {
|
func TestMoqExplicitPackage(t *testing.T) {
|
||||||
m, err := New("testdata/example", "different")
|
m, err := New("testpackages/example", "different")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
@ -70,7 +70,7 @@ func TestMoqExplicitPackage(t *testing.T) {
|
|||||||
// expected.
|
// expected.
|
||||||
// see https://github.com/matryer/moq/issues/5
|
// see https://github.com/matryer/moq/issues/5
|
||||||
func TestVariadicArguments(t *testing.T) {
|
func TestVariadicArguments(t *testing.T) {
|
||||||
m, err := New("testdata/variadic", "")
|
m, err := New("testpackages/variadic", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
@ -95,7 +95,7 @@ func TestVariadicArguments(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestNothingToReturn(t *testing.T) {
|
func TestNothingToReturn(t *testing.T) {
|
||||||
m, err := New("testdata/example", "")
|
m, err := New("testpackages/example", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
@ -120,7 +120,7 @@ func TestNothingToReturn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestChannelNames(t *testing.T) {
|
func TestChannelNames(t *testing.T) {
|
||||||
m, err := New("testdata/channels", "")
|
m, err := New("testpackages/channels", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
@ -141,7 +141,7 @@ func TestChannelNames(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestImports(t *testing.T) {
|
func TestImports(t *testing.T) {
|
||||||
m, err := New("testdata/imports/two", "")
|
m, err := New("testpackages/imports/two", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("moq.New: %s", err)
|
t.Fatalf("moq.New: %s", err)
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ func TestImports(t *testing.T) {
|
|||||||
s := buf.String()
|
s := buf.String()
|
||||||
var strs = []string{
|
var strs = []string{
|
||||||
` "sync"`,
|
` "sync"`,
|
||||||
` "github.com/matryer/moq/package/moq/testdata/imports/one"`,
|
` "github.com/matryer/moq/pkg/moq/testpackages/imports/one"`,
|
||||||
}
|
}
|
||||||
for _, str := range strs {
|
for _, str := range strs {
|
||||||
if !strings.Contains(s, str) {
|
if !strings.Contains(s, str) {
|
||||||
@ -171,3 +171,25 @@ func TestTemplateFuncs(t *testing.T) {
|
|||||||
t.Errorf("exported didn't work: %s", fn("var"))
|
t.Errorf("exported didn't work: %s", fn("var"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestVendoredPackages(t *testing.T) {
|
||||||
|
m, err := New("testpackages/vendoring/user", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("moq.New: %s", err)
|
||||||
|
}
|
||||||
|
var buf bytes.Buffer
|
||||||
|
err = m.Mock(&buf, "Service")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("mock error: %s", err)
|
||||||
|
}
|
||||||
|
s := buf.String()
|
||||||
|
// assertions of things that should be mentioned
|
||||||
|
var strs = []string{
|
||||||
|
`"github.com/matryer/somerepo"`,
|
||||||
|
}
|
||||||
|
for _, str := range strs {
|
||||||
|
if !strings.Contains(s, str) {
|
||||||
|
t.Errorf("expected but missing: \"%s\"", str)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package two
|
package two
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/matryer/moq/package/moq/testdata/imports/one"
|
"github.com/matryer/moq/pkg/moq/testpackages/imports/one"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DoSomething does something.
|
// DoSomething does something.
|
8
pkg/moq/testpackages/vendoring/user/user.go
Normal file
8
pkg/moq/testpackages/vendoring/user/user.go
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package user
|
||||||
|
|
||||||
|
import "github.com/matryer/somerepo"
|
||||||
|
|
||||||
|
// Service does something good with computers.
|
||||||
|
type Service interface {
|
||||||
|
DoSomething(somerepo.SomeType) error
|
||||||
|
}
|
9
pkg/moq/testpackages/vendoring/vendor/github.com/matryer/somerepo/code.go
generated
vendored
Normal file
9
pkg/moq/testpackages/vendoring/vendor/github.com/matryer/somerepo/code.go
generated
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// Package somerepo is a vendored package to test how moq deals with
|
||||||
|
// packages in the vendor package.
|
||||||
|
package somerepo
|
||||||
|
|
||||||
|
// SomeType is just some old type.
|
||||||
|
type SomeType struct {
|
||||||
|
// Truth indicates whether true is true or not. Computers.
|
||||||
|
Truth bool
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user