Julie Qiu | 0563d71 | 2020-10-13 17:40:39 -0400 | [diff] [blame] | 1 | // Copyright 2020 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 frontend |
| 6 | |
| 7 | import ( |
| 8 | "context" |
Julie Qiu | 2e50f52 | 2020-10-22 11:59:30 -0400 | [diff] [blame] | 9 | "path" |
Julie Qiu | 0563d71 | 2020-10-13 17:40:39 -0400 | [diff] [blame] | 10 | "testing" |
| 11 | |
| 12 | "github.com/google/go-cmp/cmp" |
| 13 | "golang.org/x/pkgsite/internal" |
| 14 | "golang.org/x/pkgsite/internal/postgres" |
| 15 | "golang.org/x/pkgsite/internal/testing/sample" |
| 16 | ) |
| 17 | |
| 18 | func TestGetNestedModules(t *testing.T) { |
| 19 | ctx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 20 | defer cancel() |
| 21 | defer postgres.ResetTestDB(testDB, t) |
| 22 | |
| 23 | for _, m := range []*internal.Module{ |
| 24 | sample.LegacyModule("cloud.google.com/go", "v0.46.2", "storage", "spanner", "pubsub"), |
| 25 | sample.LegacyModule("cloud.google.com/go/pubsub", "v1.6.1", sample.Suffix), |
| 26 | sample.LegacyModule("cloud.google.com/go/spanner", "v1.9.0", sample.Suffix), |
| 27 | sample.LegacyModule("cloud.google.com/go/storage", "v1.10.0", sample.Suffix), |
| 28 | sample.LegacyModule("cloud.google.com/go/storage/v11", "v11.0.0", sample.Suffix), |
| 29 | sample.LegacyModule("cloud.google.com/go/storage/v9", "v9.0.0", sample.Suffix), |
| 30 | sample.LegacyModule("cloud.google.com/go/storage/module", "v1.10.0", sample.Suffix), |
| 31 | sample.LegacyModule("cloud.google.com/go/v2", "v2.0.0", "storage", "spanner", "pubsub"), |
| 32 | } { |
| 33 | if err := testDB.InsertModule(ctx, m); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | for _, tc := range []struct { |
| 39 | modulePath string |
| 40 | want []*NestedModule |
| 41 | }{ |
| 42 | { |
| 43 | modulePath: "cloud.google.com/go", |
| 44 | want: []*NestedModule{ |
| 45 | { |
| 46 | Suffix: "pubsub", |
| 47 | URL: "/cloud.google.com/go/pubsub", |
| 48 | }, |
| 49 | { |
| 50 | Suffix: "spanner", |
| 51 | URL: "/cloud.google.com/go/spanner", |
| 52 | }, |
| 53 | { |
| 54 | Suffix: "storage", |
| 55 | URL: "/cloud.google.com/go/storage/v11", |
| 56 | }, |
| 57 | { |
| 58 | Suffix: "storage/module", |
| 59 | URL: "/cloud.google.com/go/storage/module", |
| 60 | }, |
| 61 | }, |
| 62 | }, |
| 63 | { |
| 64 | modulePath: "cloud.google.com/go/spanner", |
| 65 | }, |
| 66 | { |
| 67 | modulePath: "cloud.google.com/go/storage", |
| 68 | want: []*NestedModule{ |
| 69 | { |
| 70 | Suffix: "module", |
| 71 | URL: "/cloud.google.com/go/storage/module", |
| 72 | }, |
| 73 | }, |
| 74 | }, |
| 75 | } { |
| 76 | t.Run(tc.modulePath, func(t *testing.T) { |
| 77 | got, err := getNestedModules(ctx, testDB, &internal.UnitMeta{ |
| 78 | Path: tc.modulePath, |
| 79 | ModulePath: tc.modulePath, |
| 80 | }) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | if diff := cmp.Diff(tc.want, got); diff != "" { |
| 85 | t.Errorf("mismatch (-want +got):\n%s", diff) |
| 86 | } |
| 87 | }) |
| 88 | } |
| 89 | } |
Julie Qiu | 2e50f52 | 2020-10-22 11:59:30 -0400 | [diff] [blame] | 90 | |
| 91 | func TestGetImportedByCount(t *testing.T) { |
| 92 | defer postgres.ResetTestDB(testDB, t) |
| 93 | |
| 94 | ctx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| 95 | defer cancel() |
| 96 | |
| 97 | newModule := func(modPath string, pkgs ...*internal.Unit) *internal.Module { |
| 98 | m := sample.LegacyModule(modPath, sample.VersionString) |
| 99 | for _, p := range pkgs { |
| 100 | sample.AddUnit(m, p) |
| 101 | } |
| 102 | return m |
| 103 | } |
| 104 | |
Julie Qiu | c4e44bd | 2020-10-25 21:46:17 -0400 | [diff] [blame] | 105 | pkg1 := sample.UnitForPackage("path.to/foo/bar", "path.to/foo", sample.VersionString, "bar", true) |
| 106 | pkg2 := sample.UnitForPackage("path2.to/foo/bar2", "path.to/foo", sample.VersionString, "bar", true) |
Julie Qiu | 2e50f52 | 2020-10-22 11:59:30 -0400 | [diff] [blame] | 107 | pkg2.Imports = []string{pkg1.Path} |
| 108 | |
Julie Qiu | c4e44bd | 2020-10-25 21:46:17 -0400 | [diff] [blame] | 109 | pkg3 := sample.UnitForPackage("path3.to/foo/bar3", "path.to/foo", sample.VersionString, "bar3", true) |
Julie Qiu | 2e50f52 | 2020-10-22 11:59:30 -0400 | [diff] [blame] | 110 | pkg3.Imports = []string{pkg2.Path, pkg1.Path} |
| 111 | |
| 112 | testModules := []*internal.Module{ |
| 113 | newModule("path.to/foo", pkg1), |
| 114 | newModule("path2.to/foo", pkg2), |
| 115 | newModule("path3.to/foo", pkg3), |
| 116 | } |
| 117 | |
| 118 | for _, m := range testModules { |
| 119 | if err := testDB.InsertModule(ctx, m); err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | importedByLimit = 2 |
| 125 | for _, tc := range []struct { |
| 126 | pkg *internal.Unit |
| 127 | want string |
| 128 | }{ |
| 129 | { |
| 130 | pkg: pkg3, |
| 131 | want: "0", |
| 132 | }, |
| 133 | { |
| 134 | pkg: pkg2, |
| 135 | want: "1", |
| 136 | }, |
| 137 | { |
| 138 | pkg: pkg1, |
| 139 | want: "1+", |
| 140 | }, |
| 141 | } { |
| 142 | t.Run(tc.pkg.Path, func(t *testing.T) { |
| 143 | otherVersion := newModule(path.Dir(tc.pkg.Path), tc.pkg) |
| 144 | otherVersion.Version = "v1.0.5" |
| 145 | pkg := otherVersion.Units[1] |
| 146 | got, err := getImportedByCount(ctx, testDB, pkg) |
| 147 | if err != nil { |
| 148 | t.Fatalf("getImportedByCount(ctx, db, %q) = %v err = %v, want %v", |
| 149 | tc.pkg.Path, got, err, tc.want) |
| 150 | } |
| 151 | if diff := cmp.Diff(tc.want, got); diff != "" { |
| 152 | t.Errorf("getImportedByCount(ctx, db, %q) mismatch (-want +got):\n%s", tc.pkg.Path, diff) |
| 153 | } |
| 154 | }) |
| 155 | } |
| 156 | } |