Skip to content

Commit

Permalink
Merge pull request #270 from vincent178/srv-pkg-support
Browse files Browse the repository at this point in the history
add source package support
  • Loading branch information
LandonTClipp authored May 28, 2020
2 parents 5e9c701 + aabaa30 commit 5b00589
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mocks
mockery.prof
dist
.idea
11 changes: 1 addition & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,4 @@ install:
go install ./...

integration:
rm -rf mocks
${GOPATH}/bin/mockery -all -recursive -cpuprofile="mockery.prof" -dir="mockery/fixtures"
if [ ! -d "mocks" ]; then \
echo "No Mock Dir Created"; \
exit 1; \
fi
if [ ! -f "mocks/AsyncProducer.go" ]; then \
echo "AsyncProducer.go not created"; \
echo 1; \
fi
./hack/run-e2e.sh
32 changes: 31 additions & 1 deletion cmd/mockery/mockery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"runtime/pprof"
"strings"
Expand All @@ -15,6 +16,7 @@ import (
"github.com/rs/zerolog"
"github.com/vektra/mockery/mockery"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/tools/go/packages"
)

const regexMetadataChars = "\\.+*?()|[]{}^$"
Expand All @@ -33,6 +35,7 @@ type Config struct {
fNote string
fProfile string
fVersion bool
fSrcPkg string
quiet bool
fkeepTree bool
buildTags string
Expand Down Expand Up @@ -70,6 +73,8 @@ func main() {
log.Fatal().Msgf("Specify -name or -all, but not both")
} else if (config.fFileName != "" || config.fStructName != "") && config.fAll {
log.Fatal().Msgf("Cannot specify -filename or -structname with -all")
} else if config.fDir != "" && config.fDir != "." && config.fSrcPkg != "" {
log.Fatal().Msgf("Specify -dir or -srcpkg, but not both")
} else if config.fName != "" {
recursive = config.fRecursive
if strings.ContainsAny(config.fName, regexMetadataChars) {
Expand Down Expand Up @@ -118,6 +123,30 @@ func main() {
}
}

baseDir := config.fDir

if config.fSrcPkg != "" {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedFiles,
}, config.fSrcPkg)
if err != nil || len(pkgs) == 0 {
log.Fatal().Err(err).Msgf("Failed to load package %s", config.fSrcPkg)
}

// NOTE: we only pass one package name (config.fSrcPkg) to packages.Load
// it should return one package at most
pkg := pkgs[0]

if pkg.Errors != nil {
log.Fatal().Err(pkg.Errors[0]).Msgf("Failed to load package %s", config.fSrcPkg)
}

if len(pkg.GoFiles) == 0 {
log.Fatal().Msgf("No go files in package %s", config.fSrcPkg)
}
baseDir = filepath.Dir(pkg.GoFiles[0])
}

visitor := &mockery.GeneratorVisitor{
InPackage: config.fIP,
Note: config.fNote,
Expand All @@ -127,7 +156,7 @@ func main() {
}

walker := mockery.Walker{
BaseDir: config.fDir,
BaseDir: baseDir,
Recursive: recursive,
Filter: filter,
LimitOne: limitOne,
Expand Down Expand Up @@ -165,6 +194,7 @@ func parseConfigFromArgs(args []string) Config {
flagSet.StringVar(&config.fFileName, "filename", "", "name of generated file (only works with -name and no regex)")
flagSet.StringVar(&config.fStructName, "structname", "", "name of generated struct (only works with -name and no regex)")
flagSet.StringVar(&config.fLogLevel, "log-level", "info", "Level of logging")
flagSet.StringVar(&config.fSrcPkg, "srcpkg", "", "source pkg to search for interfaces")

flagSet.Parse(args[1:])

Expand Down
4 changes: 3 additions & 1 deletion cmd/mockery/mockery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ func TestParseConfigDefaults(t *testing.T) {
assert.Equal(t, "", config.fNote)
assert.Equal(t, "", config.fFileName)
assert.Equal(t, "", config.fStructName)
assert.Equal(t, "", config.fSrcPkg)
}

func TestParseConfigFlippingValues(t *testing.T) {
config := configFromCommandLine("mockery -name hi -print -output output -dir dir -recursive -all -inpkg -testonly -case case -note note -structname structname -filename filename")
config := configFromCommandLine("mockery -name hi -print -output output -dir dir -recursive -all -inpkg -testonly -case case -note note -structname structname -filename filename -srcpkg github.com/vektra/mockery")
assert.Equal(t, "hi", config.fName)
assert.Equal(t, true, config.fPrint)
assert.Equal(t, "output", config.fOutput)
Expand All @@ -41,4 +42,5 @@ func TestParseConfigFlippingValues(t *testing.T) {
assert.Equal(t, "note", config.fNote)
assert.Equal(t, "filename", config.fFileName)
assert.Equal(t, "structname", config.fStructName)
assert.Equal(t, "github.com/vektra/mockery", config.fSrcPkg)
}
31 changes: 31 additions & 0 deletions hack/run-e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

set -euo pipefail

ROOT=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
cd $ROOT

reset() {
rm -rf mocks
}

verify() {
if [ ! -d "mocks" ]; then \
echo "No Mock Dir Created"; \
exit 1; \
fi
if [ ! -f "mocks/AsyncProducer.go" ]; then \
echo "AsyncProducer.go not created"; \
echo 1; \
fi
}

trap reset exit

reset
${GOPATH}/bin/mockery -all -recursive -cpuprofile="mockery.prof" -dir="mockery/fixtures"
verify

reset
${GOPATH}/bin/mockery -all -recursive -cpuprofile="mockery.prof" -srcpkg github.com/vektra/mockery/mockery/fixtures
verify

0 comments on commit 5b00589

Please sign in to comment.