blob: bbce2ab9b43581fae10601077c42beaaa515edeb [file] [log] [blame]
// 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 postgres
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"golang.org/x/pkgsite/internal/licenses"
"golang.org/x/pkgsite/internal/testing/sample"
)
func TestLegacyGetModuleLicenses(t *testing.T) {
modulePath := "test.module"
testModule := sample.Module(modulePath, "v1.2.3", "", "foo", "bar")
testModule.LegacyPackages[0].Licenses = []*licenses.Metadata{{Types: []string{"ISC"}, FilePath: "LICENSE"}}
testModule.LegacyPackages[1].Licenses = []*licenses.Metadata{{Types: []string{"MIT"}, FilePath: "foo/LICENSE"}}
testModule.LegacyPackages[2].Licenses = []*licenses.Metadata{{Types: []string{"GPL2"}, FilePath: "bar/LICENSE.txt"}}
defer ResetTestDB(testDB, t)
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
testModule.Licenses = nil
for _, p := range testModule.LegacyPackages {
testModule.Licenses = append(testModule.Licenses, &licenses.License{
Metadata: p.Licenses[0],
Contents: []byte(`Lorem Ipsum`),
})
}
if err := testDB.InsertModule(ctx, testModule); err != nil {
t.Fatal(err)
}
got, err := testDB.LegacyGetModuleLicenses(ctx, modulePath, testModule.Version)
if err != nil {
t.Fatal(err)
}
// We only want the top-level license.
wantLicenses := []*licenses.License{testModule.Licenses[0]}
if diff := cmp.Diff(wantLicenses, got); diff != "" {
t.Errorf("testDB.LegacyGetModuleLicenses(ctx, %q, %q) mismatch (-want +got):\n%s", modulePath, testModule.Version, diff)
}
}
func TestLegacyGetPackageLicenses(t *testing.T) {
modulePath := "test.module"
testModule := sample.Module(modulePath, "v1.2.3", "", "foo")
testModule.LegacyPackages[0].Licenses = nil
testModule.LegacyPackages[1].Licenses = sample.LicenseMetadata
tests := []struct {
label, pkgPath string
wantLicenses []*licenses.License
}{
{
label: "package with licenses",
pkgPath: "test.module/foo",
wantLicenses: sample.Licenses,
}, {
label: "package with no licenses",
pkgPath: "test.module",
wantLicenses: nil,
},
}
defer ResetTestDB(testDB, t)
ctx, cancel := context.WithTimeout(context.Background(), testTimeout)
defer cancel()
if err := testDB.InsertModule(ctx, testModule); err != nil {
t.Fatal(err)
}
for _, test := range tests {
t.Run(test.label, func(t *testing.T) {
got, err := testDB.LegacyGetPackageLicenses(ctx, test.pkgPath, modulePath, testModule.Version)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(test.wantLicenses, got); diff != "" {
t.Errorf("testDB.GetLicenses(ctx, %q, %q) mismatch (-want +got):\n%s", test.pkgPath, testModule.Version, diff)
}
})
}
}