github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/base/path.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package base 6 7 import ( 8 "os" 9 "path/filepath" 10 "strings" 11 ) 12 13 func getwd() string { 14 wd, err := os.Getwd() 15 if err != nil { 16 Fatalf("cannot determine current directory: %v", err) 17 } 18 return wd 19 } 20 21 var Cwd = getwd() 22 23 // ShortPath returns an absolute or relative name for path, whatever is shorter. 24 func ShortPath(path string) string { 25 if rel, err := filepath.Rel(Cwd, path); err == nil && len(rel) < len(path) { 26 return rel 27 } 28 return path 29 } 30 31 // RelPaths returns a copy of paths with absolute paths 32 // made relative to the current directory if they would be shorter. 33 func RelPaths(paths []string) []string { 34 var out []string 35 // TODO(rsc): Can this use Cwd from above? 36 pwd, _ := os.Getwd() 37 for _, p := range paths { 38 rel, err := filepath.Rel(pwd, p) 39 if err == nil && len(rel) < len(p) { 40 p = rel 41 } 42 out = append(out, p) 43 } 44 return out 45 } 46 47 // IsTestFile reports whether the source file is a set of tests and should therefore 48 // be excluded from coverage analysis. 49 func IsTestFile(file string) bool { 50 // We don't cover tests, only the code they test. 51 return strings.HasSuffix(file, "_test.go") 52 }