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

Commit

Permalink
making it Windows compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhp committed Mar 30, 2020
1 parent 85964b7 commit ce5ef7c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions mockgen/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ func parsePackageImport(srcDir string) (string, error) {
for {
dat, err := ioutil.ReadFile(filepath.Join(currentDir, "go.mod"))
if os.IsNotExist(err) {
if currentDir == "/" {
if currentDir == filepath.Dir(currentDir) {
// at the root
break
}
currentDir = filepath.Dir(currentDir)
Expand All @@ -571,17 +572,17 @@ func parsePackageImport(srcDir string) (string, error) {
return "", err
}
modulePath := modfile.ModulePath(dat)
return filepath.Join(modulePath, strings.TrimPrefix(srcDir, currentDir)), nil
return filepath.ToSlash(filepath.Join(modulePath, strings.TrimPrefix(srcDir, currentDir))), nil
}
}
// fall back to GOPATH mode
goPath := os.Getenv("GOPATH")
if goPath == "" {
return "", fmt.Errorf("GOPATH is not set")
}
sourceRoot := filepath.Join(goPath, "src") + "/"
sourceRoot := filepath.Join(goPath, "src") + string(os.PathSeparator)
if !strings.HasPrefix(srcDir, sourceRoot) {
return "", errOutsideGoPath
}
return strings.TrimPrefix(srcDir, sourceRoot), nil
return filepath.ToSlash(strings.TrimPrefix(srcDir, sourceRoot)), nil
}
2 changes: 1 addition & 1 deletion mockgen/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestParsePackageImport(t *testing.T) {
for key, value := range testCase.envs {
os.Setenv(key, value)
}
pkgPath, err := parsePackageImport(testCase.dir)
pkgPath, err := parsePackageImport(filepath.Clean(testCase.dir))
if err != testCase.err {
t.Errorf("expect %v, got %v", testCase.err, err)
}
Expand Down

0 comments on commit ce5ef7c

Please sign in to comment.