| // Copyright 2020 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 frontend |
| |
| import ( |
| "context" |
| "testing" |
| |
| "github.com/google/go-cmp/cmp" |
| "golang.org/x/pkgsite/internal" |
| "golang.org/x/pkgsite/internal/postgres" |
| "golang.org/x/pkgsite/internal/stdlib" |
| "golang.org/x/pkgsite/internal/testing/sample" |
| ) |
| |
| type testUnitPage struct { |
| unit *internal.UnitMeta |
| name string |
| wantTitle string |
| wantType string |
| wantLabels []string |
| } |
| |
| func TestPageTitlePageTypePageLabels(t *testing.T) { |
| |
| var tests []*testUnitPage |
| m := sample.LegacyModule("golang.org/x/tools", "v1.0.0", "go/packages", "cmd/godoc") |
| for _, u := range m.Units { |
| um := &u.UnitMeta |
| switch um.Path { |
| case "golang.org/x/tools": |
| tests = append(tests, &testUnitPage{um, "module golang.org/x/tools", "tools", pageTypeModule, []string{pageTypeModule}}) |
| case "golang.org/x/tools/go/packages": |
| tests = append(tests, &testUnitPage{um, "package golang.org/x/tools/go/packages", "packages", pageTypePackage, []string{pageTypePackage}}) |
| case "golang.org/x/tools/go": |
| tests = append(tests, &testUnitPage{um, "directory golang.org/x/tools/go", "go/", pageTypeDirectory, []string{pageTypeDirectory}}) |
| case "golang.org/x/tools/cmd/godoc": |
| um.Name = "main" |
| tests = append(tests, &testUnitPage{um, "package golang.org/x/tools/cmd/godoc", "godoc", pageTypeCommand, []string{pageTypeCommand}}) |
| case "golang.org/x/tools/cmd": |
| tests = append(tests, &testUnitPage{um, "directory golang.org/x/tools/cmd", "cmd/", pageTypeDirectory, []string{pageTypeDirectory}}) |
| default: |
| t.Fatalf("Unexpected path: %q", um.Path) |
| } |
| } |
| |
| m2 := sample.LegacyModule("golang.org/x/tools/gopls", "v1.0.0", "") |
| m2.Units[0].Name = "main" |
| tests = append(tests, &testUnitPage{&m2.Units[0].UnitMeta, "module golang.org/x/tools/gopls", "gopls", pageTypeCommand, []string{pageTypeCommand, pageTypeModule}}) |
| |
| std := sample.LegacyModule(stdlib.ModulePath, "v1.0.0", "cmd/go") |
| for _, u := range std.Units { |
| um := &u.UnitMeta |
| switch um.Path { |
| case stdlib.ModulePath: |
| tests = append(tests, &testUnitPage{um, "module std", "Standard library", pageTypeModuleStd, nil}) |
| case "cmd": |
| tests = append(tests, &testUnitPage{um, "directory cmd", "cmd/", pageTypeDirectory, []string{pageTypeDirectory, pageTypeStdlib}}) |
| case "cmd/go": |
| um.Name = "main" |
| tests = append(tests, &testUnitPage{um, "command go", "go", pageTypeCommand, []string{pageTypeCommand, pageTypeStdlib}}) |
| default: |
| t.Fatalf("Unexpected path: %q", um.Path) |
| } |
| } |
| |
| for _, test := range tests { |
| t.Run(test.name, func(t *testing.T) { |
| gotTitle := pageTitle(test.unit) |
| if gotTitle != test.wantTitle { |
| t.Errorf("pageTitle(%q): %q; want = %q", test.unit.Path, gotTitle, test.wantTitle) |
| } |
| gotType := pageType(test.unit) |
| if gotType != test.wantType { |
| t.Errorf("pageType(%q): %q; want = %q", test.unit.Path, gotType, test.wantType) |
| } |
| gotLabels := pageLabels(test.unit) |
| if diff := cmp.Diff(test.wantLabels, gotLabels); diff != "" { |
| t.Errorf("mismatch on pageLabels (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |
| |
| func TestGetNestedModules(t *testing.T) { |
| ctx, cancel := context.WithTimeout(context.Background(), testTimeout) |
| defer cancel() |
| defer postgres.ResetTestDB(testDB, t) |
| |
| for _, m := range []*internal.Module{ |
| sample.LegacyModule("cloud.google.com/go", "v0.46.2", "storage", "spanner", "pubsub"), |
| sample.LegacyModule("cloud.google.com/go/pubsub", "v1.6.1", sample.Suffix), |
| sample.LegacyModule("cloud.google.com/go/spanner", "v1.9.0", sample.Suffix), |
| sample.LegacyModule("cloud.google.com/go/storage", "v1.10.0", sample.Suffix), |
| sample.LegacyModule("cloud.google.com/go/storage/v11", "v11.0.0", sample.Suffix), |
| sample.LegacyModule("cloud.google.com/go/storage/v9", "v9.0.0", sample.Suffix), |
| sample.LegacyModule("cloud.google.com/go/storage/module", "v1.10.0", sample.Suffix), |
| sample.LegacyModule("cloud.google.com/go/v2", "v2.0.0", "storage", "spanner", "pubsub"), |
| } { |
| if err := testDB.InsertModule(ctx, m); err != nil { |
| t.Fatal(err) |
| } |
| } |
| |
| for _, tc := range []struct { |
| modulePath string |
| want []*NestedModule |
| }{ |
| { |
| modulePath: "cloud.google.com/go", |
| want: []*NestedModule{ |
| { |
| Suffix: "pubsub", |
| URL: "/cloud.google.com/go/pubsub", |
| }, |
| { |
| Suffix: "spanner", |
| URL: "/cloud.google.com/go/spanner", |
| }, |
| { |
| Suffix: "storage", |
| URL: "/cloud.google.com/go/storage/v11", |
| }, |
| { |
| Suffix: "storage/module", |
| URL: "/cloud.google.com/go/storage/module", |
| }, |
| }, |
| }, |
| { |
| modulePath: "cloud.google.com/go/spanner", |
| }, |
| { |
| modulePath: "cloud.google.com/go/storage", |
| want: []*NestedModule{ |
| { |
| Suffix: "module", |
| URL: "/cloud.google.com/go/storage/module", |
| }, |
| }, |
| }, |
| } { |
| t.Run(tc.modulePath, func(t *testing.T) { |
| got, err := getNestedModules(ctx, testDB, &internal.UnitMeta{ |
| Path: tc.modulePath, |
| ModulePath: tc.modulePath, |
| }) |
| if err != nil { |
| t.Fatal(err) |
| } |
| if diff := cmp.Diff(tc.want, got); diff != "" { |
| t.Errorf("mismatch (-want +got):\n%s", diff) |
| } |
| }) |
| } |
| } |