go/packages/packagestest: make versioned modules

If a Module's Name ends with a version suffix (v2), create its module
with an appropriate version number (v2.0.0).

Change-Id: I1a16f054fac5717e79871ba1bf9a6343c0ce2f5b
Reviewed-on: https://go-review.googlesource.com/c/146097
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Heschi Kreinick 2018-10-30 16:50:41 -04:00
parent 240a8bfde1
commit 6c7e314b65
5 changed files with 31 additions and 11 deletions

View File

@ -24,6 +24,11 @@ var testdata = []packagestest.Module{{
Files: map[string]interface{}{
"other/a.go": "package fake2",
},
}, {
Name: "golang.org/fake2/v2",
Files: map[string]interface{}{
"other/a.go": "package fake2",
},
}}
type fileTest struct {

View File

@ -66,5 +66,9 @@ func (gopath) Finalize(exported *Exported) error {
}
func gopathDir(exported *Exported, module string) string {
return filepath.Join(exported.temp, path.Base(module))
dir := path.Base(module)
if versionSuffixRE.MatchString(dir) {
dir = path.Base(path.Dir(module)) + "_" + dir
}
return filepath.Join(exported.temp, dir)
}

View File

@ -23,5 +23,6 @@ func TestGOPATHExport(t *testing.T) {
{"golang.org/fake1", "a.go", "fake1/src/golang.org/fake1/a.go", checkLink("testdata/a.go")},
{"golang.org/fake1", "b.go", "fake1/src/golang.org/fake1/b.go", checkContent("package fake1")},
{"golang.org/fake2", "other/a.go", "fake2/src/golang.org/fake2/other/a.go", checkContent("package fake2")},
{"golang.org/fake2/v2", "other/a.go", "fake2_v2/src/golang.org/fake2/v2/other/a.go", checkContent("package fake2")},
})
}

View File

@ -13,6 +13,7 @@ import (
"os/exec"
"path"
"path/filepath"
"regexp"
"golang.org/x/tools/go/packages"
)
@ -37,8 +38,6 @@ import (
// /sometemporarydirectory/repoa
var Modules = modules{}
const theVersion = "v1.0.0"
type modules struct{}
func (modules) Name() string {
@ -64,7 +63,7 @@ func (modules) Finalize(exported *Exported) error {
if other == exported.primary {
continue
}
primaryGomod += fmt.Sprintf("\t%v %v\n", other, theVersion)
primaryGomod += fmt.Sprintf("\t%v %v\n", other, moduleVersion(other))
}
primaryGomod += ")\n"
if err := ioutil.WriteFile(filepath.Join(primaryDir, "go.mod"), []byte(primaryGomod), 0644); err != nil {
@ -124,12 +123,13 @@ func (modules) Finalize(exported *Exported) error {
// writeModuleProxy creates a directory in the proxy dir for a module.
func writeModuleProxy(dir, module string, files map[string]string) error {
ver := moduleVersion(module)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
// list file. Just the single version.
if err := ioutil.WriteFile(filepath.Join(dir, "list"), []byte(theVersion+"\n"), 0644); err != nil {
if err := ioutil.WriteFile(filepath.Join(dir, "list"), []byte(ver+"\n"), 0644); err != nil {
return err
}
@ -138,24 +138,24 @@ func writeModuleProxy(dir, module string, files map[string]string) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(dir, theVersion+".mod"), modContents, 0644); err != nil {
if err := ioutil.WriteFile(filepath.Join(dir, ver+".mod"), modContents, 0644); err != nil {
return err
}
// info file, just the bare bones.
infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, theVersion))
if err := ioutil.WriteFile(filepath.Join(dir, theVersion+".info"), infoContents, 0644); err != nil {
infoContents := []byte(fmt.Sprintf(`{"Version": "%v", "Time":"2017-12-14T13:08:43Z"}`, ver))
if err := ioutil.WriteFile(filepath.Join(dir, ver+".info"), infoContents, 0644); err != nil {
return err
}
// zip of all the source files.
f, err := os.OpenFile(filepath.Join(dir, theVersion+".zip"), os.O_CREATE|os.O_WRONLY, 0644)
f, err := os.OpenFile(filepath.Join(dir, ver+".zip"), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
z := zip.NewWriter(f)
for name, path := range files {
zf, err := z.Create(module + "@" + theVersion + "/" + name)
zf, err := z.Create(module + "@" + ver + "/" + name)
if err != nil {
return err
}
@ -197,5 +197,14 @@ func primaryDir(exported *Exported) string {
}
func moduleDir(exported *Exported, module string) string {
return filepath.Join(modCache(exported), path.Dir(module), path.Base(module)+"@"+theVersion)
return filepath.Join(modCache(exported), path.Dir(module), path.Base(module)+"@"+moduleVersion(module))
}
var versionSuffixRE = regexp.MustCompile(`v\d+`)
func moduleVersion(module string) string {
if versionSuffixRE.MatchString(path.Base(module)) {
return path.Base(module) + ".0.0"
}
return "v1.0.0"
}

View File

@ -27,5 +27,6 @@ func TestModulesExport(t *testing.T) {
{"golang.org/fake1", "b.go", "primarymod/fake1/b.go", checkContent("package fake1")},
{"golang.org/fake2", "go.mod", "modcache/pkg/mod/golang.org/fake2@v1.0.0/go.mod", nil},
{"golang.org/fake2", "other/a.go", "modcache/pkg/mod/golang.org/fake2@v1.0.0/other/a.go", checkContent("package fake2")},
{"golang.org/fake2/v2", "other/a.go", "modcache/pkg/mod/golang.org/fake2/v2@v2.0.0/other/a.go", checkContent("package fake2")},
})
}