2021-08-11 18:40:59 +02:00
|
|
|
package download
|
|
|
|
|
|
|
|
import (
|
2021-08-12 18:56:23 +02:00
|
|
|
"context"
|
2021-08-11 18:40:59 +02:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/h2non/gock.v1"
|
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
"github.com/Jguer/aur"
|
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
mockaur "github.com/Jguer/yay/v12/pkg/dep/mock"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/exe"
|
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/parser"
|
2021-08-11 18:40:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo
|
|
|
|
// GIVEN package in repo is already present
|
|
|
|
// WHEN defining package db as a target
|
|
|
|
// THEN all should be found and cloned, except the repo one
|
|
|
|
func TestPKGBUILDReposDefinedDBPull(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2022-02-11 09:30:54 +01:00
|
|
|
dir := t.TempDir()
|
2021-08-11 18:40:59 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{{}}, nil // fakes a package found for all
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:40:59 +02:00
|
|
|
os.MkdirAll(filepath.Join(dir, "yay", ".git"), 0o777)
|
|
|
|
|
|
|
|
targets := []string{"core/yay", "yay-bin", "yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
2023-02-21 00:58:13 +01:00
|
|
|
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
|
2021-08-11 18:40:59 +02:00
|
|
|
cmdBuilder,
|
|
|
|
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"core/yay": false, "yay-bin": true, "yay-git": true}, cloned)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo
|
|
|
|
// WHEN defining package db as a target
|
|
|
|
// THEN all should be found and cloned
|
|
|
|
func TestPKGBUILDReposDefinedDBClone(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2022-02-11 09:30:54 +01:00
|
|
|
dir := t.TempDir()
|
2021-08-11 18:40:59 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{{}}, nil // fakes a package found for all
|
|
|
|
},
|
|
|
|
}
|
2021-08-11 18:40:59 +02:00
|
|
|
targets := []string{"core/yay", "yay-bin", "yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
2023-02-21 00:58:13 +01:00
|
|
|
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
|
2021-08-11 18:40:59 +02:00
|
|
|
cmdBuilder,
|
|
|
|
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"core/yay": true, "yay-bin": true, "yay-git": true}, cloned)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo
|
|
|
|
// WHEN defining as non specified targets
|
|
|
|
// THEN all should be found and cloned
|
|
|
|
func TestPKGBUILDReposClone(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2022-02-11 09:30:54 +01:00
|
|
|
dir := t.TempDir()
|
2021-08-11 18:40:59 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{{}}, nil // fakes a package found for all
|
|
|
|
},
|
|
|
|
}
|
2021-08-11 18:40:59 +02:00
|
|
|
targets := []string{"yay", "yay-bin", "yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
2023-02-21 00:58:13 +01:00
|
|
|
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
|
2021-08-11 18:40:59 +02:00
|
|
|
cmdBuilder,
|
|
|
|
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"yay": true, "yay-bin": true, "yay-git": true}, cloned)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo but wrong db
|
|
|
|
// WHEN defining as non specified targets
|
|
|
|
// THEN all aur be found and cloned
|
|
|
|
func TestPKGBUILDReposNotFound(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2022-02-11 09:30:54 +01:00
|
|
|
dir := t.TempDir()
|
2021-08-11 18:40:59 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{{}}, nil // fakes a package found for all
|
|
|
|
},
|
|
|
|
}
|
2021-08-11 18:40:59 +02:00
|
|
|
targets := []string{"extra/yay", "yay-bin", "yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
2023-02-21 00:58:13 +01:00
|
|
|
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
|
2021-08-11 18:40:59 +02:00
|
|
|
cmdBuilder,
|
|
|
|
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"yay-bin": true, "yay-git": true}, cloned)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo
|
|
|
|
// WHEN defining as non specified targets in repo mode
|
|
|
|
// THEN only repo should be cloned
|
|
|
|
func TestPKGBUILDReposRepoMode(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2022-02-11 09:30:54 +01:00
|
|
|
dir := t.TempDir()
|
2021-08-11 18:40:59 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{}, nil // fakes a package found for all
|
|
|
|
},
|
|
|
|
}
|
2021-08-11 18:40:59 +02:00
|
|
|
targets := []string{"yay", "yay-bin", "yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
2023-02-21 00:58:13 +01:00
|
|
|
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
|
2021-08-11 18:40:59 +02:00
|
|
|
cmdBuilder,
|
|
|
|
targets, parser.ModeRepo, "https://aur.archlinux.org", dir, false)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"yay": true}, cloned)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo
|
|
|
|
// WHEN defining as specified targets
|
|
|
|
// THEN all aur be found and cloned
|
|
|
|
func TestPKGBUILDFull(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2021-08-11 18:40:59 +02:00
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{{}}, nil
|
|
|
|
},
|
|
|
|
}
|
2021-08-11 18:40:59 +02:00
|
|
|
gock.New("https://aur.archlinux.org").
|
|
|
|
Get("/cgit/aur.git/plain/PKGBUILD").MatchParam("h", "yay-git").
|
|
|
|
Reply(200).
|
|
|
|
BodyString("example_yay-git")
|
|
|
|
gock.New("https://aur.archlinux.org").
|
|
|
|
Get("/cgit/aur.git/plain/PKGBUILD").MatchParam("h", "yay-bin").
|
|
|
|
Reply(200).
|
|
|
|
BodyString("example_yay-bin")
|
|
|
|
|
|
|
|
gock.New("https://github.com/").
|
|
|
|
Get("/archlinux/svntogit-packages/raw/packages/yay/trunk/PKGBUILD").
|
|
|
|
Reply(200).
|
|
|
|
BodyString("example_yay")
|
|
|
|
|
2021-08-11 20:13:28 +02:00
|
|
|
defer gock.Off()
|
2021-08-11 18:40:59 +02:00
|
|
|
targets := []string{"core/yay", "aur/yay-bin", "yay-git"}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
|
|
|
|
2023-02-21 00:58:13 +01:00
|
|
|
fetched, err := PKGBUILDs(searcher, mockClient, &http.Client{},
|
2021-08-11 18:40:59 +02:00
|
|
|
targets, "https://aur.archlinux.org", parser.ModeAny)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string][]byte{
|
|
|
|
"core/yay": []byte("example_yay"),
|
|
|
|
"aur/yay-bin": []byte("example_yay-bin"),
|
|
|
|
"yay-git": []byte("example_yay-git"),
|
|
|
|
}, fetched)
|
|
|
|
}
|
2023-02-21 00:58:13 +01:00
|
|
|
|
|
|
|
// GIVEN 2 aur packages and 1 in repo
|
|
|
|
// WHEN aur packages are not found
|
|
|
|
// only repo should be cloned
|
|
|
|
func TestPKGBUILDReposMissingAUR(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
|
|
|
mockClient := &mockaur.MockAUR{
|
|
|
|
GetFn: func(ctx context.Context, query *aur.Query) ([]aur.Pkg, error) {
|
|
|
|
return []aur.Pkg{}, nil // fakes a package found for all
|
|
|
|
},
|
|
|
|
}
|
|
|
|
targets := []string{"core/yay", "aur/yay-bin", "aur/yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
searcher := &testDBSearcher{
|
|
|
|
absPackagesDB: map[string]string{"yay": "core"},
|
|
|
|
}
|
|
|
|
cloned, err := PKGBUILDRepos(context.Background(), searcher, mockClient,
|
|
|
|
cmdBuilder,
|
|
|
|
targets, parser.ModeAny, "https://aur.archlinux.org", dir, false)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"core/yay": true}, cloned)
|
|
|
|
}
|