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

Commit e46c6d8

Browse files
committed
Add tests
1 parent 8c0e4e3 commit e46c6d8

File tree

4 files changed

+88
-20
lines changed

4 files changed

+88
-20
lines changed

mockgen/parse.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717
// This file contains the model construction by parsing source files.
1818

1919
import (
20+
"errors"
2021
"flag"
2122
"fmt"
2223
"go/ast"
@@ -49,7 +50,7 @@ func sourceMode(source string) (*model.Package, error) {
4950
return nil, fmt.Errorf("failed getting source directory: %v", err)
5051
}
5152

52-
packageImport, err := parsePackageImport(source, srcDir)
53+
packageImport, err := parsePackageImport(srcDir)
5354
if err != nil {
5455
return nil, err
5556
}
@@ -539,52 +540,48 @@ func packageNameOfDir(srcDir string) (string, error) {
539540
return "", fmt.Errorf("go source file not found %s", srcDir)
540541
}
541542

542-
packageImport, err := parsePackageImport(goFilePath, srcDir)
543+
packageImport, err := parsePackageImport(srcDir)
543544
if err != nil {
544545
return "", err
545546
}
546547
return packageImport, nil
547548
}
548549

550+
var ErrOutsideGoPath = errors.New("Source directory is outside GOPATH")
551+
549552
// parseImportPackage get package import path via source file
550553
// an alternative implementation is to use:
551554
// cfg := &packages.Config{Mode: packages.NeedName, Tests: true, Dir: srcDir}
552555
// pkgs, err := packages.Load(cfg, "file="+source)
553556
// However, it will call "go list" and slow down the performance
554-
func parsePackageImport(source, srcDir string) (string, error) {
557+
func parsePackageImport(srcDir string) (string, error) {
555558
moduleMode := os.Getenv("GO111MODULE")
556559
// trying to find the module
557560
if moduleMode != "off" {
558561
currentDir := srcDir
559-
var modulePath string
560562
for {
561563
dat, err := ioutil.ReadFile(filepath.Join(currentDir, "go.mod"))
562564
if os.IsNotExist(err) {
563-
if currentDir == "." || currentDir == "/" {
565+
if currentDir == "/" {
564566
break
565567
}
566568
currentDir = filepath.Dir(currentDir)
567569
continue
568570
} else if err != nil {
569571
return "", err
570572
}
571-
modulePath = modfile.ModulePath(dat)
572-
break
573-
}
574-
if modulePath != "" {
573+
modulePath := modfile.ModulePath(dat)
575574
return filepath.Join(modulePath, strings.TrimPrefix(srcDir, currentDir)), nil
576575
}
577576
}
578-
if moduleMode != "on" {
579-
goPath := os.Getenv("GOPATH")
580-
if goPath == "" {
581-
return "", fmt.Errorf("GOPATH is not set")
582-
}
583-
sourceRoot := filepath.Join(goPath, "src") + "/"
584-
if !strings.HasPrefix(srcDir, sourceRoot) {
585-
return "", fmt.Errorf("%s is outside GOPATH %s", srcDir, goPath)
586-
}
587-
return strings.TrimPrefix(srcDir, sourceRoot), nil
577+
// fall back to GOPATH mode
578+
goPath := os.Getenv("GOPATH")
579+
if goPath == "" {
580+
return "", fmt.Errorf("GOPATH is not set")
581+
}
582+
sourceRoot := filepath.Join(goPath, "src") + "/"
583+
if !strings.HasPrefix(srcDir, sourceRoot) {
584+
return "", ErrOutsideGoPath
588585
}
589-
return "", fmt.Errorf("cannot find package path for %s", srcDir)
586+
return strings.TrimPrefix(srcDir, sourceRoot), nil
590587
}

mockgen/parse_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"go/ast"
55
"go/parser"
66
"go/token"
7+
"io/ioutil"
8+
"os"
9+
"path/filepath"
710
"testing"
811
)
912

@@ -113,3 +116,69 @@ func Benchmark_parseFile(b *testing.B) {
113116
sourceMode(source)
114117
}
115118
}
119+
120+
func TestParsePackageImport(t *testing.T) {
121+
for _, testCase := range []struct {
122+
name string
123+
envs map[string]string
124+
dir string
125+
pkgPath string
126+
err error
127+
}{
128+
{
129+
name: "go mod default",
130+
envs: map[string]string{"GO111MODULE": ""},
131+
dir: "testdata/gomod/bar",
132+
pkgPath: "github.com/golang/foo/bar",
133+
},
134+
{
135+
name: "go mod off",
136+
envs: map[string]string{"GO111MODULE": "off", "GOPATH": "testdata/gopath"},
137+
dir: "testdata/gopath/src/example.com/foo",
138+
pkgPath: "example.com/foo",
139+
},
140+
{
141+
name: "outside GOPATH",
142+
envs: map[string]string{"GO111MODULE": "off", "GOPATH": "testdata/gopath"},
143+
dir: "testdata",
144+
err: ErrOutsideGoPath,
145+
},
146+
} {
147+
t.Run(testCase.name, func(t *testing.T) {
148+
for key, value := range testCase.envs {
149+
os.Setenv(key, value)
150+
}
151+
pkgPath, err := parsePackageImport(testCase.dir)
152+
if err != testCase.err {
153+
t.Errorf("expect %v, got %v", testCase.err, err)
154+
}
155+
if pkgPath != testCase.pkgPath {
156+
t.Errorf("expect %s, got %s", testCase.pkgPath, pkgPath)
157+
}
158+
})
159+
}
160+
}
161+
162+
func TestParsePackageImport_FallbackGoPath(t *testing.T) {
163+
goPath, err := ioutil.TempDir("", "gopath")
164+
if err != nil {
165+
t.Error(err)
166+
}
167+
defer func() {
168+
if err = os.RemoveAll(goPath); err != nil {
169+
t.Error(err)
170+
}
171+
}()
172+
srcDir := filepath.Join(goPath, "src/example.com/foo")
173+
err = os.MkdirAll(srcDir, 0755)
174+
if err != nil {
175+
t.Error(err)
176+
}
177+
os.Setenv("GOPATH", goPath)
178+
os.Setenv("GO111MODULE", "on")
179+
pkgPath, err := parsePackageImport(srcDir)
180+
expected := "example.com/foo"
181+
if pkgPath != expected {
182+
t.Errorf("expect %s, got %s", expected, pkgPath)
183+
}
184+
}

mockgen/testdata/gomod/bar/bar.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package bar

mockgen/testdata/gomod/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module github.com/golang/foo

0 commit comments

Comments
 (0)