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

Commit

Permalink
Reflect in Current Directory First
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Nguyen committed Jan 22, 2020
1 parent 3dcdcb6 commit b9bb3cc
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions mockgen/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ func runInDir(program []byte, dir string) (*model.Package, error) {
if err := cmd.Run(); err != nil {
return nil, err
}
return run(filepath.Join(tmpDir, progBinary))
p, err := run(filepath.Join(tmpDir, progBinary))
if err == nil {
log.Printf("Successfully reflected in directory: %v\n", dir)
}
return p, err
}

// reflectMode generates mocks via reflection on an interface.
Expand All @@ -152,6 +156,11 @@ func reflectMode(importPath string, symbols []string) (*model.Package, error) {

wd, _ := os.Getwd()

// Try to run the reflection program in the current working directory.
if p, err := runInDir(program, wd); err == nil {
return p, nil
}

// Try to run the program in the same directory as the input package.
if p, err := build.Import(importPath, wd, build.FindOnly); err == nil {
dir := p.Dir
Expand All @@ -160,11 +169,7 @@ func reflectMode(importPath string, symbols []string) (*model.Package, error) {
}
}

// Since that didn't work, try to run it in the current working directory.
if p, err := runInDir(program, wd); err == nil {
return p, nil
}
// Since that didn't work, try to run it in a standard temp directory.
// Try to run it in a standard temp directory.
return runInDir(program, "")
}

Expand Down

0 comments on commit b9bb3cc

Please sign in to comment.