x/pkgsite: add tests cases for fetch.FetchModule

Fixes golang/go#40109

Change-Id: Id065d436b4605473e47cfc98b8cf95a096bad126
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/247577
Run-TryBot: Julie Qiu <julie@golang.org>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
This commit is contained in:
Krzysztof Dąbrowski 2020-08-09 13:48:47 +02:00 коммит произвёл Julie Qiu
Родитель 23ec2ddea2
Коммит fa59886cd8
3 изменённых файлов: 99 добавлений и 5 удалений

Просмотреть файл

@ -49,8 +49,9 @@ func TestFetchModule(t *testing.T) {
MaxDocumentationHTML = 1 * megabyte
for _, test := range []struct {
name string
mod *testModule
name string
mod *testModule
fetchVersion string
}{
{name: "basic", mod: moduleNoGoMod},
{name: "wasm", mod: moduleWasm},
@ -67,6 +68,8 @@ func TestFetchModule(t *testing.T) {
{name: "module with method example", mod: moduleMethodExample},
{name: "module with nonredistributable packages", mod: moduleNonRedist},
{name: "stdlib module", mod: moduleStd},
{name: "master version of module", mod: moduleMaster, fetchVersion: "master"},
{name: "latest version of module", mod: moduleLatest, fetchVersion: "latest"},
} {
t.Run(test.name, func(t *testing.T) {
ctx := experiment.NewContext(context.Background(), internal.ExperimentExecutableExamples)
@ -75,9 +78,13 @@ func TestFetchModule(t *testing.T) {
modulePath := test.mod.mod.ModulePath
version := test.mod.mod.Version
fetchVersion := test.fetchVersion
if version == "" {
version = "v1.0.0"
}
if fetchVersion == "" {
fetchVersion = version
}
sourceClient := source.NewClient(sourceTimeout)
proxyClient, teardownProxy := proxy.SetupTestClient(t, []*proxy.Module{{
ModulePath: modulePath,
@ -85,11 +92,11 @@ func TestFetchModule(t *testing.T) {
Files: test.mod.mod.Files,
}})
defer teardownProxy()
got := FetchModule(ctx, modulePath, version, proxyClient, sourceClient)
got := FetchModule(ctx, modulePath, fetchVersion, proxyClient, sourceClient)
if got.Error != nil {
t.Fatal(got.Error)
}
d := licenseDetector(ctx, t, modulePath, version, proxyClient)
d := licenseDetector(ctx, t, modulePath, fetchVersion, proxyClient)
fr := cleanFetchResult(test.mod.fr, d)
sortFetchResult(fr)
sortFetchResult(got)

Просмотреть файл

@ -865,6 +865,91 @@ var moduleStd = &testModule{
},
}
var moduleMaster = &testModule{
mod: &proxy.Module{
ModulePath: "github.com/my/module",
Files: map[string]string{
"foo/foo.go": "// package foo exports a helpful constant.\npackage foo\nconst Bar = 1",
},
Version: "v0.0.0-20200706064627-355bc3f705ed",
},
fr: &FetchResult{
RequestedVersion: "master",
Module: &internal.Module{
LegacyModuleInfo: internal.LegacyModuleInfo{
ModuleInfo: internal.ModuleInfo{
ModulePath: "github.com/my/module",
Version: "v0.0.0-20200706064627-355bc3f705ed",
VersionType: "pseudo",
SourceInfo: source.NewGitHubInfo("https://github.com/my/module", "", "355bc3f705ed"),
},
},
Directories: []*internal.Directory{
{
DirectoryMeta: internal.DirectoryMeta{
Path: "github.com/my/module",
V1Path: "github.com/my/module",
},
},
{
DirectoryMeta: internal.DirectoryMeta{
Path: "github.com/my/module/foo",
V1Path: "github.com/my/module/foo",
},
Package: &internal.Package{
Name: "foo",
Documentation: &internal.Documentation{
Synopsis: "package foo exports a helpful constant.",
},
},
},
},
},
},
}
var moduleLatest = &testModule{
mod: &proxy.Module{
ModulePath: "github.com/my/module",
Files: map[string]string{
"foo/foo.go": "// package foo exports a helpful constant.\npackage foo\nconst Bar = 1",
},
Version: "v1.2.4",
},
fr: &FetchResult{
RequestedVersion: "latest",
Module: &internal.Module{
LegacyModuleInfo: internal.LegacyModuleInfo{
ModuleInfo: internal.ModuleInfo{
ModulePath: "github.com/my/module",
Version: "v1.2.4",
SourceInfo: source.NewGitHubInfo("https://github.com/my/module", "", "v1.2.4"),
},
},
Directories: []*internal.Directory{
{
DirectoryMeta: internal.DirectoryMeta{
Path: "github.com/my/module",
V1Path: "github.com/my/module",
},
},
{
DirectoryMeta: internal.DirectoryMeta{
Path: "github.com/my/module/foo",
V1Path: "github.com/my/module/foo",
},
Package: &internal.Package{
Name: "foo",
Documentation: &internal.Documentation{
Synopsis: "package foo exports a helpful constant.",
},
},
},
},
},
},
}
// moduleWithExamples returns a testModule that contains an example.
// It provides the common bits for the tests for package, function,
// type, and method examples below.

Просмотреть файл

@ -36,7 +36,9 @@ func cleanFetchResult(fr *FetchResult, detector *licenses.Detector) *FetchResult
if fr.Module.Version == "" {
fr.Module.Version = "v1.0.0"
}
fr.RequestedVersion = fr.Module.Version
if fr.RequestedVersion == "" {
fr.RequestedVersion = fr.Module.Version
}
fr.ResolvedVersion = fr.Module.Version
if fr.Module.VersionType == "" {
fr.Module.VersionType = version.TypeRelease