827 lines
29 KiB
Go
827 lines
29 KiB
Go
//go:build !integration
|
|
|
|
package query
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Jguer/aur"
|
|
|
|
"github.com/Jguer/yay/v13/pkg/db/mock"
|
|
mockaur "github.com/Jguer/yay/v13/pkg/dep/mock"
|
|
"github.com/Jguer/yay/v13/pkg/settings/parser"
|
|
"github.com/Jguer/yay/v13/pkg/text"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// linuxCkLastModified matches the mock linux-ck package fixture.
|
|
const linuxCkLastModified = 1646250901
|
|
|
|
var linuxCkFixedNow = time.Unix(linuxCkLastModified+365*24*3600, 0)
|
|
|
|
func TestSourceQueryBuilder(t *testing.T) {
|
|
// Cannot run in parallel: mutates text.NowFunc, a package-level var.
|
|
text.NowFunc = func() time.Time { return linuxCkFixedNow }
|
|
t.Cleanup(func() { text.NowFunc = time.Now })
|
|
|
|
type testCase struct {
|
|
desc string
|
|
search []string
|
|
bottomUp bool
|
|
separateSources bool
|
|
sortBy string
|
|
verbosity SearchVerbosity
|
|
targetMode parser.TargetMode
|
|
singleLineResults bool
|
|
searchBy string
|
|
wantResults []string
|
|
wantOutput []string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
desc: "sort-by-metric bottomup separatesources",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "",
|
|
verbosity: Detailed,
|
|
wantResults: []string{"linux-ck", "linux-zen", "linux"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-metric topdown separatesources",
|
|
search: []string{"linux"},
|
|
bottomUp: false,
|
|
separateSources: true,
|
|
sortBy: "",
|
|
verbosity: Detailed,
|
|
wantResults: []string{"linux", "linux-zen", "linux-ck"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-metric bottomup noseparatesources",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: false,
|
|
sortBy: "",
|
|
verbosity: Detailed,
|
|
wantResults: []string{"linux-ck", "linux-zen", "linux"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-metric topdown noseparatesources",
|
|
search: []string{"linux"},
|
|
bottomUp: false,
|
|
separateSources: false,
|
|
sortBy: "",
|
|
verbosity: Detailed,
|
|
wantResults: []string{"linux", "linux-zen", "linux-ck"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-metric bottomup separatesources number-menu",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "",
|
|
verbosity: NumberMenu,
|
|
wantResults: []string{"linux-ck", "linux-zen", "linux"},
|
|
wantOutput: []string{
|
|
"\x1b[35m3\x1b[0m \x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
"\x1b[35m2\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b[35m1\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-metric topdown separatesources number-menu",
|
|
search: []string{"linux"},
|
|
bottomUp: false,
|
|
separateSources: true,
|
|
sortBy: "",
|
|
verbosity: NumberMenu,
|
|
wantResults: []string{"linux", "linux-zen", "linux-ck"},
|
|
wantOutput: []string{
|
|
"\x1b[35m1\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
"\x1b[35m2\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b[35m3\x1b[0m \x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-name bottomup separatesources number-menu",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "name",
|
|
verbosity: NumberMenu,
|
|
wantResults: []string{"linux-ck", "linux", "linux-zen"},
|
|
wantOutput: []string{
|
|
"\x1b[35m3\x1b[0m \x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
"\x1b[35m2\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
"\x1b[35m1\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-name topdown separatesources number-menu",
|
|
search: []string{"linux"},
|
|
bottomUp: false,
|
|
separateSources: true,
|
|
sortBy: "name",
|
|
verbosity: NumberMenu,
|
|
wantResults: []string{"linux-zen", "linux", "linux-ck"},
|
|
wantOutput: []string{
|
|
"\x1b[35m1\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux ZEN kernel and modules\n",
|
|
"\x1b[35m2\x1b[0m \x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\n The Linux kernel and modules\n",
|
|
"\x1b[35m3\x1b[0m \x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-name bottomup noseparatesources minimal",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: false,
|
|
sortBy: "name",
|
|
verbosity: Minimal,
|
|
wantResults: []string{"linux", "linux-ck", "linux-zen"},
|
|
wantOutput: []string{
|
|
"linux\n",
|
|
"linux-ck\n",
|
|
"linux-zen\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "only-aur minimal",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "name",
|
|
verbosity: Minimal,
|
|
targetMode: parser.ModeAUR,
|
|
wantResults: []string{"linux-ck"},
|
|
wantOutput: []string{
|
|
"linux-ck\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "only-repo minimal",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "name",
|
|
verbosity: Minimal,
|
|
targetMode: parser.ModeRepo,
|
|
wantResults: []string{"linux", "linux-zen"},
|
|
wantOutput: []string{
|
|
"linux\n",
|
|
"linux-zen\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-name singleline",
|
|
search: []string{"linux"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "name",
|
|
verbosity: Detailed,
|
|
singleLineResults: true,
|
|
wantResults: []string{"linux-ck", "linux", "linux-zen"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \tThe Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux kernel and modules\n",
|
|
"\x1b]8;;https://archlinux.org/packages/core/x86_64/linux-zen\x1b\\\x1b[1m\x1b[33mcore\x1b[0m\x1b[0m/\x1b[1mlinux-zen\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.0\x1b[0m\x1b[1m (1.0 B 1.0 B) \x1b[0m\tThe Linux ZEN kernel and modules\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "sort-by-name search-by-name",
|
|
search: []string{"linux-ck"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
sortBy: "name",
|
|
verbosity: Detailed,
|
|
searchBy: "name",
|
|
targetMode: parser.ModeAUR,
|
|
wantResults: []string{"linux-ck"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
},
|
|
},
|
|
{
|
|
desc: "only-aur search-by-several-terms",
|
|
search: []string{"linux-ck", "hrtimer"},
|
|
bottomUp: true,
|
|
separateSources: true,
|
|
verbosity: Detailed,
|
|
targetMode: parser.ModeAUR,
|
|
wantResults: []string{"linux-ck"},
|
|
wantOutput: []string{
|
|
"\x1b]8;;https://aur.archlinux.org/packages/linux-ck\x1b\\\x1b[1m\x1b[34maur\x1b[0m\x1b[0m/\x1b[1mlinux-ck\x1b[0m\x1b]8;;\x1b\\ \x1b[36m5.16.12-1\x1b[0m\x1b[1m (+450\x1b[0m \x1b[1m1.51) \x1b[0m\x1b[36m[365d]\x1b[0m \n The Linux-ck kernel and modules with ck's hrtimer patches\n",
|
|
},
|
|
},
|
|
}
|
|
|
|
mockDB := &mock.DBExecutor{
|
|
ReposFn: func() []string {
|
|
// Match pacman.conf parsing order for source separation.
|
|
return []string{"core"}
|
|
},
|
|
SyncPackagesFn: func(pkgs ...string) []mock.IPackage {
|
|
mockDB := mock.NewDB("core")
|
|
return []mock.IPackage{
|
|
&mock.Package{
|
|
PBase: "linux",
|
|
PName: "linux",
|
|
PVersion: "5.16.0",
|
|
PDescription: "The Linux kernel and modules",
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: mockDB,
|
|
PArchitecture: "x86_64",
|
|
},
|
|
&mock.Package{
|
|
PBase: "linux-zen",
|
|
PName: "linux-zen",
|
|
PVersion: "5.16.0",
|
|
PDescription: "The Linux ZEN kernel and modules",
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: mockDB,
|
|
PArchitecture: "x86_64",
|
|
},
|
|
}
|
|
},
|
|
LocalPackageFn: func(string) mock.IPackage {
|
|
return nil
|
|
},
|
|
}
|
|
|
|
mockAUR := &mockaur.MockAUR{
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
return []aur.Pkg{
|
|
{
|
|
Description: "The Linux-ck kernel and modules with ck's hrtimer patches",
|
|
FirstSubmitted: 1311346274,
|
|
ID: 1045311,
|
|
LastModified: 1646250901,
|
|
Maintainer: "graysky",
|
|
Name: "linux-ck",
|
|
NumVotes: 450,
|
|
OutOfDate: 0,
|
|
PackageBase: "linux-ck",
|
|
PackageBaseID: 50911,
|
|
Popularity: 1.511141,
|
|
URL: "https://wiki.archlinux.org/index.php/Linux-ck",
|
|
URLPath: "/cgit/aur.git/snapshot/linux-ck.tar.gz",
|
|
Version: "5.16.12-1",
|
|
},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
w := &strings.Builder{}
|
|
queryBuilder := NewSourceQueryBuilder(mockAUR,
|
|
text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
|
|
tc.sortBy, tc.targetMode, tc.searchBy, tc.bottomUp,
|
|
tc.singleLineResults, tc.separateSources)
|
|
|
|
queryBuilder.Execute(t.Context(), mockDB, tc.search)
|
|
|
|
assert.Len(t, queryBuilder.results, len(tc.wantResults))
|
|
assert.Equal(t, len(tc.wantResults), queryBuilder.Len())
|
|
for i, name := range tc.wantResults {
|
|
assert.Equal(t, name, queryBuilder.results[i].name)
|
|
}
|
|
|
|
queryBuilder.Results(mockDB, tc.verbosity)
|
|
|
|
assert.Equal(t, strings.Join(tc.wantOutput, ""), w.String())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceQueryBuilderTieSortsByRepoOrder(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
type testCase struct {
|
|
desc string
|
|
bottomUp bool
|
|
repoOrder []string
|
|
wantNames []string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
desc: "sort-by-metric topdown repo-order-core-extra",
|
|
bottomUp: false,
|
|
repoOrder: []string{"core", "extra"},
|
|
wantNames: []string{"systemd", "systemd-libs", "python-systemd", "systemd-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-metric topdown repo-order-extra-core",
|
|
bottomUp: false,
|
|
repoOrder: []string{"extra", "core"},
|
|
wantNames: []string{"systemd", "python-systemd", "systemd-libs", "systemd-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-metric bottomup repo-order-core-extra",
|
|
bottomUp: true,
|
|
repoOrder: []string{"core", "extra"},
|
|
wantNames: []string{"systemd-git", "python-systemd", "systemd-libs", "systemd"},
|
|
},
|
|
{
|
|
desc: "sort-by-metric bottomup repo-order-extra-core",
|
|
bottomUp: true,
|
|
repoOrder: []string{"extra", "core"},
|
|
wantNames: []string{"systemd-git", "systemd-libs", "python-systemd", "systemd"},
|
|
},
|
|
}
|
|
|
|
mockDB := &mock.DBExecutor{
|
|
SyncPackagesFn: func(pkgs ...string) []mock.IPackage {
|
|
return []mock.IPackage{
|
|
&mock.Package{
|
|
PBase: "systemd",
|
|
PName: "systemd",
|
|
PVersion: "259-1",
|
|
PDescription: "system and service manager",
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: mock.NewDB("core"),
|
|
PArchitecture: "x86_64",
|
|
},
|
|
&mock.Package{
|
|
PBase: "systemd",
|
|
PName: "systemd-libs",
|
|
PVersion: "259-1",
|
|
PDescription: "systemd client libraries",
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: mock.NewDB("core"),
|
|
PArchitecture: "x86_64",
|
|
},
|
|
&mock.Package{
|
|
PBase: "python-systemd",
|
|
PName: "python-systemd",
|
|
PVersion: "235-4",
|
|
PDescription: "Python bindings for systemd",
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: mock.NewDB("extra"),
|
|
PArchitecture: "x86_64",
|
|
},
|
|
}
|
|
},
|
|
LocalPackageFn: func(string) mock.IPackage {
|
|
return nil
|
|
},
|
|
}
|
|
|
|
mockAUR := &mockaur.MockAUR{
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
return []aur.Pkg{
|
|
{
|
|
Description: "system and service manager (git version)",
|
|
FirstSubmitted: 1445633397,
|
|
ID: 1909597,
|
|
LastModified: 1765571424,
|
|
Maintainer: "Atsutane",
|
|
Name: "systemd-git",
|
|
NumVotes: 11,
|
|
OutOfDate: 0,
|
|
PackageBase: "systemd-git",
|
|
PackageBaseID: 102323,
|
|
Popularity: 0.005618,
|
|
URL: "https://www.github.com/systemd/systemd",
|
|
URLPath: "/cgit/aur.git/snapshot/systemd-git.tar.gz",
|
|
Version: "259.rc3.r85286.7524671f74c-1",
|
|
},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
w := &strings.Builder{}
|
|
mockDB.ReposFn = func() []string {
|
|
return tc.repoOrder
|
|
}
|
|
queryBuilder := NewSourceQueryBuilder(mockAUR,
|
|
text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
|
|
"", parser.ModeAny, "", tc.bottomUp,
|
|
false, true)
|
|
|
|
queryBuilder.Execute(t.Context(), mockDB, []string{"systemd"})
|
|
|
|
gotNames := make([]string, len(queryBuilder.results))
|
|
for i, result := range queryBuilder.results {
|
|
gotNames[i] = result.name
|
|
}
|
|
|
|
assert.Equal(t, tc.wantNames, gotNames)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceQueryBuilderTieDoesNotSeparateSources(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
type testCase struct {
|
|
desc string
|
|
bottomUp bool
|
|
wantNames []string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
desc: "sort-by-metric topdown",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay", "ruby-yard", "yay-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-metric bottomup",
|
|
bottomUp: true,
|
|
wantNames: []string{"yay-git", "ruby-yard", "yay"},
|
|
},
|
|
}
|
|
|
|
mockDB, mockAUR := newYayQueryBuilderMocks()
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
t.Parallel()
|
|
w := &strings.Builder{}
|
|
queryBuilder := NewSourceQueryBuilder(mockAUR,
|
|
text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
|
|
"", parser.ModeAny, "", tc.bottomUp,
|
|
false, true)
|
|
|
|
queryBuilder.Execute(t.Context(), mockDB, []string{"yay"})
|
|
|
|
gotNames := make([]string, len(queryBuilder.results))
|
|
for i, result := range queryBuilder.results {
|
|
gotNames[i] = result.name
|
|
}
|
|
|
|
assert.Equal(t, tc.wantNames, gotNames)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceQueryBuilderSortByFields(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
type testCase struct {
|
|
desc string
|
|
sortBy string
|
|
bottomUp bool
|
|
wantNames []string
|
|
}
|
|
|
|
testCases := []testCase{
|
|
{
|
|
desc: "sort-by-base topdown",
|
|
sortBy: "base",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay-git", "yay", "ruby-yard"},
|
|
},
|
|
{
|
|
desc: "sort-by-base bottomup",
|
|
sortBy: "base",
|
|
bottomUp: true,
|
|
wantNames: []string{"ruby-yard", "yay", "yay-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-modified topdown",
|
|
sortBy: "modified",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay-git", "yay", "ruby-yard"},
|
|
},
|
|
{
|
|
desc: "sort-by-modified bottomup",
|
|
sortBy: "modified",
|
|
bottomUp: true,
|
|
wantNames: []string{"ruby-yard", "yay", "yay-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-name topdown",
|
|
sortBy: "name",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay-git", "yay", "ruby-yard"},
|
|
},
|
|
{
|
|
desc: "sort-by-name bottomup",
|
|
sortBy: "name",
|
|
bottomUp: true,
|
|
wantNames: []string{"ruby-yard", "yay", "yay-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-popularity topdown",
|
|
sortBy: "popularity",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay", "yay-git", "ruby-yard"},
|
|
},
|
|
{
|
|
desc: "sort-by-popularity bottomup",
|
|
sortBy: "popularity",
|
|
bottomUp: true,
|
|
wantNames: []string{"ruby-yard", "yay-git", "yay"},
|
|
},
|
|
{
|
|
desc: "sort-by-votes topdown",
|
|
sortBy: "votes",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay", "yay-git", "ruby-yard"},
|
|
},
|
|
{
|
|
desc: "sort-by-votes bottomup",
|
|
sortBy: "votes",
|
|
bottomUp: true,
|
|
wantNames: []string{"ruby-yard", "yay-git", "yay"},
|
|
},
|
|
{
|
|
desc: "sort-by-submitted topdown",
|
|
sortBy: "submitted",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay-git", "yay", "ruby-yard"},
|
|
},
|
|
{
|
|
desc: "sort-by-submitted bottomup",
|
|
sortBy: "submitted",
|
|
bottomUp: true,
|
|
wantNames: []string{"ruby-yard", "yay", "yay-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-metric topdown",
|
|
sortBy: "",
|
|
bottomUp: false,
|
|
wantNames: []string{"yay", "ruby-yard", "yay-git"},
|
|
},
|
|
{
|
|
desc: "sort-by-metric bottomup",
|
|
sortBy: "",
|
|
bottomUp: true,
|
|
wantNames: []string{"yay-git", "ruby-yard", "yay"},
|
|
},
|
|
}
|
|
|
|
mockDB, mockAUR := newYayQueryBuilderMocks()
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.desc, func(t *testing.T) {
|
|
t.Parallel()
|
|
w := &strings.Builder{}
|
|
queryBuilder := NewSourceQueryBuilder(mockAUR,
|
|
text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
|
|
tc.sortBy, parser.ModeAny, "", tc.bottomUp,
|
|
false, false)
|
|
|
|
queryBuilder.Execute(t.Context(), mockDB, []string{"yay"})
|
|
|
|
gotNames := make([]string, len(queryBuilder.results))
|
|
for i, result := range queryBuilder.results {
|
|
gotNames[i] = result.name
|
|
}
|
|
|
|
assert.Equal(t, tc.wantNames, gotNames)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceQueryBuilderChromeRanking(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
mockDB := &mock.DBExecutor{
|
|
ReposFn: func() []string {
|
|
return []string{"extra"}
|
|
},
|
|
SyncPackagesFn: func(pkgs ...string) []mock.IPackage {
|
|
return nil
|
|
},
|
|
LocalPackageFn: func(string) mock.IPackage {
|
|
return nil
|
|
},
|
|
}
|
|
|
|
mockAUR := &mockaur.MockAUR{
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
return []aur.Pkg{
|
|
{
|
|
Description: "The popular web browser by Google (Stable Channel)",
|
|
Name: "google-chrome",
|
|
NumVotes: 2351,
|
|
Popularity: 13.24,
|
|
PackageBase: "google-chrome",
|
|
Version: "149.0.7827.53-1",
|
|
},
|
|
{
|
|
Description: "Extract Android APKs for running in Chrome OS OR Chrome in OS X, Linux and Windows",
|
|
Name: "chromeos-apk-git",
|
|
NumVotes: 37,
|
|
Popularity: 0.00,
|
|
PackageBase: "chromeos-apk-git",
|
|
Version: "20201218.r129.f13a94d-1",
|
|
},
|
|
{
|
|
Description: "External extension updater for Chromium based browsers",
|
|
Name: "chromexup",
|
|
NumVotes: 5,
|
|
Popularity: 0.00,
|
|
PackageBase: "chromexup",
|
|
Version: "0.5.4-5",
|
|
},
|
|
{
|
|
Description: "Standalone server that implements the W3C WebDriver standard (for goog",
|
|
Name: "chromedriver",
|
|
NumVotes: 52,
|
|
Popularity: 0.50,
|
|
PackageBase: "chromedriver",
|
|
Version: "149.0.7827.54-1",
|
|
},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
w := &strings.Builder{}
|
|
queryBuilder := NewSourceQueryBuilder(mockAUR,
|
|
text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
|
|
"", parser.ModeAny, "", false,
|
|
false, false)
|
|
|
|
queryBuilder.Execute(t.Context(), mockDB, []string{"chrome"})
|
|
|
|
gotNames := make([]string, len(queryBuilder.results))
|
|
for i, result := range queryBuilder.results {
|
|
gotNames[i] = result.name
|
|
}
|
|
|
|
assert.Equal(t, []string{"google-chrome", "chromedriver", "chromeos-apk-git", "chromexup"}, gotNames)
|
|
}
|
|
|
|
func newYayQueryBuilderMocks() (*mock.DBExecutor, *mockaur.MockAUR) {
|
|
mockDB := &mock.DBExecutor{
|
|
ReposFn: func() []string {
|
|
// Match pacman.conf parsing order for source separation.
|
|
return []string{"core", "extra"}
|
|
},
|
|
SyncPackagesFn: func(pkgs ...string) []mock.IPackage {
|
|
mockDB := mock.NewDB("extra")
|
|
return []mock.IPackage{
|
|
&mock.Package{
|
|
PBase: "ruby-yard",
|
|
PName: "ruby-yard",
|
|
PVersion: "0.9.34-5",
|
|
PDescription: "YARD is a Ruby Documentation tool. The Y stands for \"Yay!\"",
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: mockDB,
|
|
PArchitecture: "x86_64",
|
|
},
|
|
}
|
|
},
|
|
LocalPackageFn: func(string) mock.IPackage {
|
|
return nil
|
|
},
|
|
}
|
|
|
|
mockAUR := &mockaur.MockAUR{
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
return []aur.Pkg{
|
|
{
|
|
Description: "Yet another yogurt. Pacman wrapper and AUR helper written in go.",
|
|
FirstSubmitted: 1475688004,
|
|
ID: 1911141,
|
|
LastModified: 1765742501,
|
|
Maintainer: "jguer",
|
|
Name: "yay",
|
|
NumVotes: 2461,
|
|
OutOfDate: 0,
|
|
PackageBase: "yay",
|
|
PackageBaseID: 115973,
|
|
Popularity: 34.903162,
|
|
URL: "https://github.com/Jguer/yay",
|
|
URLPath: "/cgit/aur.git/snapshot/yay.tar.gz",
|
|
Version: "12.5.7-1",
|
|
},
|
|
{
|
|
Description: "Yet another yogurt. Pacman wrapper and AUR helper written in go. (development version)",
|
|
FirstSubmitted: 1517205142,
|
|
ID: 1911143,
|
|
LastModified: 1765742519,
|
|
Maintainer: "jguer",
|
|
Name: "yay-git",
|
|
NumVotes: 55,
|
|
OutOfDate: 0,
|
|
PackageBase: "yay-git",
|
|
PackageBaseID: 129573,
|
|
Popularity: 1.850171,
|
|
URL: "https://github.com/Jguer/yay",
|
|
URLPath: "/cgit/aur.git/snapshot/yay-git.tar.gz",
|
|
Version: "12.5.7.r0.g44dfda05-1",
|
|
},
|
|
}, nil
|
|
},
|
|
}
|
|
|
|
return mockDB, mockAUR
|
|
}
|
|
|
|
// TestSourceQueryBuilderSamePkgNameAcrossRepos verifies that when the same
|
|
// package name appears in two different repos (e.g. extra and extra-testing),
|
|
// Results() renders each entry from its own repo's metadata. Without the
|
|
// composite db/name key in repoQueryMap, both results would share the last
|
|
// stored entry and one description would appear twice while the other was lost.
|
|
func TestSourceQueryBuilderSamePkgNameAcrossRepos(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
const (
|
|
descExtra = "The Linux kernel and modules (extra)"
|
|
descExtraTesting = "The Linux kernel and modules (extra-testing)"
|
|
)
|
|
|
|
extraDB := mock.NewDB("extra")
|
|
extraTestingDB := mock.NewDB("extra-testing")
|
|
|
|
mockDB := &mock.DBExecutor{
|
|
ReposFn: func() []string {
|
|
return []string{"extra", "extra-testing"}
|
|
},
|
|
SyncPackagesFn: func(pkgs ...string) []mock.IPackage {
|
|
return []mock.IPackage{
|
|
&mock.Package{
|
|
PBase: "linux",
|
|
PName: "linux",
|
|
PVersion: "6.1.0",
|
|
PDescription: descExtra,
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: extraDB,
|
|
PArchitecture: "x86_64",
|
|
},
|
|
&mock.Package{
|
|
PBase: "linux",
|
|
PName: "linux",
|
|
PVersion: "6.2.0-rc1",
|
|
PDescription: descExtraTesting,
|
|
PSize: 1,
|
|
PISize: 1,
|
|
PDB: extraTestingDB,
|
|
PArchitecture: "x86_64",
|
|
},
|
|
}
|
|
},
|
|
LocalPackageFn: func(string) mock.IPackage {
|
|
return nil
|
|
},
|
|
}
|
|
|
|
mockAUR := &mockaur.MockAUR{
|
|
GetFn: func(_ context.Context, _ *aur.Query) ([]aur.Pkg, error) {
|
|
return nil, nil
|
|
},
|
|
}
|
|
|
|
w := &strings.Builder{}
|
|
queryBuilder := NewSourceQueryBuilder(
|
|
mockAUR,
|
|
text.NewLogger(w, io.Discard, strings.NewReader(""), false, "test"),
|
|
"", parser.ModeAny, "", false, false, false,
|
|
)
|
|
|
|
queryBuilder.Execute(t.Context(), mockDB, []string{"linux"})
|
|
|
|
require.Len(t, queryBuilder.results, 2, "both repo entries must be in results")
|
|
|
|
err := queryBuilder.Results(mockDB, Detailed)
|
|
require.NoError(t, err)
|
|
|
|
out := w.String()
|
|
|
|
// Each description must appear exactly once. A failure here means both
|
|
// results were rendered using the same repoQueryMap entry.
|
|
assert.Equal(t, 1, strings.Count(out, descExtra), "extra description must appear exactly once")
|
|
assert.Equal(t, 1, strings.Count(out, descExtraTesting), "extra-testing description must appear exactly once")
|
|
}
|