github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/packages/packagestest/gopath.go (about) 1 // Copyright 2018 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 packagestest 6 7 import ( 8 "path" 9 "path/filepath" 10 ) 11 12 // GOPATH is the exporter that produces GOPATH layouts. 13 // Each "module" is put in it's own GOPATH entry to help test complex cases. 14 // Given the two files 15 // golang.org/repoa#a/a.go 16 // golang.org/repob#b/b.go 17 // You would get the directory layout 18 // /sometemporarydirectory 19 // ├── repoa 20 // │ └── src 21 // │ └── golang.org 22 // │ └── repoa 23 // │ └── a 24 // │ └── a.go 25 // └── repob 26 // └── src 27 // └── golang.org 28 // └── repob 29 // └── b 30 // └── b.go 31 // GOPATH would be set to 32 // /sometemporarydirectory/repoa;/sometemporarydirectory/repob 33 // and the working directory would be 34 // /sometemporarydirectory/repoa/src 35 var GOPATH = gopath{} 36 37 func init() { 38 All = append(All, GOPATH) 39 } 40 41 type gopath struct{} 42 43 func (gopath) Name() string { 44 return "GOPATH" 45 } 46 47 func (gopath) Filename(exported *Exported, module, fragment string) string { 48 return filepath.Join(gopathDir(exported, module), "src", module, fragment) 49 } 50 51 func (gopath) Finalize(exported *Exported) error { 52 exported.Config.Env = append(exported.Config.Env, "GO111MODULE=off") 53 gopath := "" 54 for module := range exported.written { 55 if gopath != "" { 56 gopath += string(filepath.ListSeparator) 57 } 58 dir := gopathDir(exported, module) 59 gopath += dir 60 if module == exported.primary { 61 exported.Config.Dir = filepath.Join(dir, "src") 62 } 63 } 64 exported.Config.Env = append(exported.Config.Env, "GOPATH="+gopath) 65 return nil 66 } 67 68 func gopathDir(exported *Exported, module string) string { 69 dir := path.Base(module) 70 if versionSuffixRE.MatchString(dir) { 71 dir = path.Base(path.Dir(module)) + "_" + dir 72 } 73 return filepath.Join(exported.temp, dir) 74 }