| // Copyright 2023 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package fetchserver |
| |
| import ( |
| "context" |
| "testing" |
| |
| "golang.org/x/pkgsite/internal" |
| "golang.org/x/pkgsite/internal/postgres" |
| "golang.org/x/pkgsite/internal/source" |
| "golang.org/x/pkgsite/internal/testing/sample" |
| ) |
| |
| var testDB *postgres.DB |
| |
| func TestMain(m *testing.M) { |
| postgres.RunDBTests("discovery_fetchserver_test", m, &testDB) |
| } |
| |
| type testModule struct { |
| path string |
| redistributable bool |
| versions []string |
| packages []testPackage |
| } |
| |
| type testPackage struct { |
| name string |
| suffix string |
| readmeContents string |
| readmeFilePath string |
| docs []*internal.Documentation |
| } |
| |
| func insertTestModules(ctx context.Context, t *testing.T, mods []testModule) { |
| for _, mod := range mods { |
| var ( |
| suffixes []string |
| pkgs = make(map[string]testPackage) |
| ) |
| for _, pkg := range mod.packages { |
| suffixes = append(suffixes, pkg.suffix) |
| pkgs[pkg.suffix] = pkg |
| } |
| for _, ver := range mod.versions { |
| m := sample.Module(mod.path, ver, suffixes...) |
| m.SourceInfo = source.NewGitHubInfo(sample.RepositoryURL, "", ver) |
| m.IsRedistributable = mod.redistributable |
| if !m.IsRedistributable { |
| m.Licenses = nil |
| } |
| for _, u := range m.Units { |
| if pkg, ok := pkgs[internal.Suffix(u.Path, m.ModulePath)]; ok { |
| if pkg.name != "" { |
| u.Name = pkg.name |
| } |
| if pkg.readmeContents != "" { |
| u.Readme = &internal.Readme{ |
| Contents: pkg.readmeContents, |
| Filepath: pkg.readmeFilePath, |
| } |
| } |
| if pkg.docs != nil { |
| u.Documentation = pkg.docs |
| } |
| } |
| if !mod.redistributable { |
| u.IsRedistributable = false |
| u.Licenses = nil |
| u.Documentation = nil |
| u.Readme = nil |
| } |
| } |
| postgres.MustInsertModule(ctx, t, testDB, m) |
| } |
| } |
| } |