| 1 |
// Copyright 2021 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package pkgload |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
|
| 20 |
"golang.org/x/tools/go/packages" |
| 21 |
) |
| 22 |
|
| 23 |
func TestPkgStatus(t *testing.T) { |
| 24 |
tests := []struct { |
| 25 |
importPath string |
| 26 |
doc string |
| 27 |
version string |
| 28 |
want string |
| 29 |
}{ |
| 30 |
{ |
| 31 |
importPath: "cloud.google.com/go", |
| 32 |
want: "", |
| 33 |
}, |
| 34 |
{ |
| 35 |
importPath: "cloud.google.com/go/storage/v1alpha1", |
| 36 |
want: "alpha", |
| 37 |
}, |
| 38 |
{ |
| 39 |
importPath: "cloud.google.com/go/storage/v2beta2", |
| 40 |
want: "beta", |
| 41 |
}, |
| 42 |
{ |
| 43 |
doc: "NOTE: This package is in beta. It is not stable, and may be subject to changes.", |
| 44 |
want: "beta", |
| 45 |
}, |
| 46 |
{ |
| 47 |
doc: "NOTE: This package is in alpha. It is not stable, and is likely to change.", |
| 48 |
want: "alpha", |
| 49 |
}, |
| 50 |
{ |
| 51 |
doc: "Package foo is great\nDeprecated: not anymore", |
| 52 |
want: "deprecated", |
| 53 |
}, |
| 54 |
{ |
| 55 |
importPath: "cloud.google.com/go/storage/v1alpha1", |
| 56 |
doc: "Package foo is great\nDeprecated: not anymore", |
| 57 |
want: "deprecated", // Deprecated comes before alpha and beta. |
| 58 |
}, |
| 59 |
{ |
| 60 |
importPath: "cloud.google.com/go/storage/v1beta1", |
| 61 |
doc: "Package foo is great\nDeprecated: not anymore", |
| 62 |
want: "deprecated", // Deprecated comes before alpha and beta. |
| 63 |
}, |
| 64 |
{ |
| 65 |
version: "v0.1.0", |
| 66 |
want: "", |
| 67 |
}, |
| 68 |
{ |
| 69 |
version: "v2.1.0-alpha", |
| 70 |
want: "preview", // Preview comes before alpha and beta. |
| 71 |
}, |
| 72 |
} |
| 73 |
for _, test := range tests { |
| 74 |
if got := pkgStatus(test.importPath, test.doc, test.version); got != test.want { |
| 75 |
t.Errorf("pkgStatus(%q, %q, %q) got %q, want %q", test.importPath, test.doc, test.version, got, test.want) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
func TestGetModulePath(t *testing.T) { |
| 81 |
tests := []struct { |
| 82 |
name string |
| 83 |
pkgs []*packages.Package |
| 84 |
want string |
| 85 |
}{ |
| 86 |
{ |
| 87 |
name: "empty", |
| 88 |
pkgs: []*packages.Package{}, |
| 89 |
want: "", |
| 90 |
}, |
| 91 |
{ |
| 92 |
name: "no module", |
| 93 |
pkgs: []*packages.Package{ |
| 94 |
{Module: nil}, |
| 95 |
}, |
| 96 |
want: "", |
| 97 |
}, |
| 98 |
{ |
| 99 |
name: "module no path", |
| 100 |
pkgs: []*packages.Package{ |
| 101 |
{Module: &packages.Module{}}, |
| 102 |
}, |
| 103 |
want: "", |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "one with path", |
| 107 |
pkgs: []*packages.Package{ |
| 108 |
{Module: &packages.Module{Path: "example.com/foo"}}, |
| 109 |
}, |
| 110 |
want: "example.com/foo", |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "many", |
| 114 |
pkgs: []*packages.Package{ |
| 115 |
{Module: nil}, |
| 116 |
{Module: &packages.Module{}}, |
| 117 |
{Module: &packages.Module{Path: "example.com/bar"}}, |
| 118 |
}, |
| 119 |
want: "example.com/bar", |
| 120 |
}, |
| 121 |
} |
| 122 |
for _, tt := range tests { |
| 123 |
t.Run(tt.name, func(t *testing.T) { |
| 124 |
if got := getModulePath(tt.pkgs); got != tt.want { |
| 125 |
t.Errorf("getModulePath() = %v, want %v", got, tt.want) |
| 126 |
} |
| 127 |
}) |
| 128 |
} |
| 129 |
} |