From 7615cbe602689f05bb905631028210b87f663898 Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Sat, 25 Jan 2020 16:51:10 +0530 Subject: [PATCH] Improve Windows compatibility (#118) - Use travis build matrix to test on Windows and macOS - Make utility func compatible with host OS. Use filepath package to manipulate path strings for stripping $GOPATH from working directory. - Delegate to go/build pkg for getting Go paths Co-authored-by: Mike Lee Co-authored-by: Lucas Bremgartner --- .travis.yml | 19 ++++++++++++++----- pkg/moq/moq.go | 20 +++++++++----------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1bcf6df..1d8e8e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,11 +6,20 @@ branches: only: - master -go: - - 1.11.x - - 1.12.x - - 1.13.x - - tip +jobs: + include: + - os: linux + go: 1.11.x + - os: linux + go: 1.12.x + - os: linux + go: 1.13.x + - os: linux + go: tip + - os: osx + go: 1.13.x + - os: windows + go: 1.13.x before_install: - go get golang.org/x/lint/golint diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index adb378b..c963c9a 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "go/build" "go/format" "go/types" "io" @@ -355,18 +356,15 @@ func stripVendorPath(p string) string { return strings.TrimLeft(path.Join(parts[1:]...), "/") } -// stripGopath takes the directory to a package and remove the gopath to get the -// canonical package name. -// -// taken from https://github.com/ernesto-jimenez/gogen -// Copyright (c) 2015 Ernesto Jiménez +// stripGopath takes the directory to a package and removes the +// $GOPATH/src path to get the canonical package name. func stripGopath(p string) string { - for _, gopath := range gopaths() { - p = strings.TrimPrefix(p, path.Join(gopath, "src")+"/") + for _, srcDir := range build.Default.SrcDirs() { + rel, err := filepath.Rel(srcDir, p) + if err != nil || strings.HasPrefix(rel, "..") { + continue + } + return filepath.ToSlash(rel) } return p } - -func gopaths() []string { - return strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator)) -}