2021-01-30 12:52:50 +01:00
|
|
|
package download
|
|
|
|
|
|
|
|
import (
|
2021-08-12 18:56:23 +02:00
|
|
|
"context"
|
2021-08-02 20:42:36 +02:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-09-06 23:51:07 +02:00
|
|
|
"os/exec"
|
2021-08-02 20:42:36 +02:00
|
|
|
"path/filepath"
|
2021-01-30 12:52:50 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-02 20:42:36 +02:00
|
|
|
|
2023-03-07 22:04:06 +01:00
|
|
|
"github.com/Jguer/yay/v12/pkg/settings/exe"
|
2021-01-30 12:52:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetAURPkgbuild(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2021-01-30 12:52:50 +01:00
|
|
|
|
|
|
|
type args struct {
|
2021-08-11 22:05:47 +02:00
|
|
|
body string
|
|
|
|
status int
|
2021-01-30 12:52:50 +01:00
|
|
|
pkgName string
|
2021-08-11 22:05:47 +02:00
|
|
|
wantURL string
|
2021-01-30 12:52:50 +01:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "found package",
|
|
|
|
args: args{
|
2021-08-11 22:05:47 +02:00
|
|
|
body: gitExtrasPKGBUILD,
|
|
|
|
status: 200,
|
2021-01-30 12:52:50 +01:00
|
|
|
pkgName: "git-extras",
|
2021-08-11 22:05:47 +02:00
|
|
|
wantURL: "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=git-extras",
|
2021-01-30 12:52:50 +01:00
|
|
|
},
|
|
|
|
want: gitExtrasPKGBUILD,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found package",
|
|
|
|
args: args{
|
2021-08-11 22:05:47 +02:00
|
|
|
body: "",
|
|
|
|
status: 404,
|
|
|
|
pkgName: "git-git",
|
|
|
|
wantURL: "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=git-git",
|
2021-01-30 12:52:50 +01:00
|
|
|
},
|
|
|
|
want: "",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
2021-08-11 20:13:28 +02:00
|
|
|
tt := tt
|
2021-01-30 12:52:50 +01:00
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2021-08-11 22:05:47 +02:00
|
|
|
httpClient := &testClient{
|
|
|
|
t: t,
|
|
|
|
wantURL: tt.args.wantURL,
|
|
|
|
body: tt.args.body,
|
|
|
|
status: tt.args.status,
|
|
|
|
}
|
|
|
|
got, err := AURPKGBUILD(httpClient, tt.args.pkgName, "https://aur.archlinux.org")
|
2021-01-30 12:52:50 +01:00
|
|
|
if tt.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tt.want, string(got))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-02 20:42:36 +02:00
|
|
|
|
|
|
|
// GIVEN no previous existing folder
|
|
|
|
// WHEN AURPKGBUILDRepo is called
|
|
|
|
// THEN a clone command should be formed
|
|
|
|
func TestAURPKGBUILDRepo(t *testing.T) {
|
2021-08-11 20:13:28 +02:00
|
|
|
t.Parallel()
|
2021-09-06 23:51:07 +02:00
|
|
|
want := "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin"
|
|
|
|
if os.Getuid() == 0 {
|
|
|
|
ld := "systemd-run"
|
|
|
|
if path, _ := exec.LookPath(ld); path != "" {
|
|
|
|
ld = path
|
|
|
|
}
|
2021-12-20 17:49:25 +01:00
|
|
|
want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty --quiet -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin", ld)
|
2021-09-06 23:51:07 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 20:42:36 +02:00
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
2021-09-06 23:51:07 +02:00
|
|
|
want: want,
|
2021-08-02 20:42:36 +02:00
|
|
|
parentBuilder: &exe.CmdBuilder{
|
2021-08-08 20:22:39 +02:00
|
|
|
Runner: cmdRunner,
|
2021-08-02 20:42:36 +02:00
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{"--no-replace-objects"},
|
|
|
|
},
|
|
|
|
}
|
2023-01-03 22:43:56 +01:00
|
|
|
newCloned, err := AURPKGBUILDRepo(context.Background(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", "/tmp/doesnt-exist", false)
|
2021-08-02 20:42:36 +02:00
|
|
|
assert.NoError(t, err)
|
2021-08-07 19:11:52 +02:00
|
|
|
assert.Equal(t, true, newCloned)
|
2021-08-02 20:42:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GIVEN a previous existing folder with permissions
|
|
|
|
// WHEN AURPKGBUILDRepo is called
|
|
|
|
// THEN a pull command should be formed
|
|
|
|
func TestAURPKGBUILDRepoExistsPerms(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-02 20:42:36 +02:00
|
|
|
|
|
|
|
os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
|
|
|
|
|
2021-12-12 19:05:48 +01:00
|
|
|
want := fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --rebase --autostash", dir)
|
2021-09-06 23:51:07 +02:00
|
|
|
if os.Getuid() == 0 {
|
|
|
|
ld := "systemd-run"
|
|
|
|
if path, _ := exec.LookPath(ld); path != "" {
|
|
|
|
ld = path
|
|
|
|
}
|
2021-12-20 17:49:25 +01:00
|
|
|
want = fmt.Sprintf("%s --service-type=oneshot --pipe --wait --pty --quiet -p DynamicUser=yes -p CacheDirectory=yay -E HOME=/tmp --no-replace-objects -C %s/yay-bin pull --rebase --autostash", ld, dir)
|
2021-09-06 23:51:07 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 20:42:36 +02:00
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
2021-09-06 23:51:07 +02:00
|
|
|
want: want,
|
2021-08-02 20:42:36 +02:00
|
|
|
parentBuilder: &exe.CmdBuilder{
|
2021-08-08 20:22:39 +02:00
|
|
|
Runner: cmdRunner,
|
2021-08-02 20:42:36 +02:00
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{"--no-replace-objects"},
|
|
|
|
},
|
|
|
|
}
|
2023-01-03 22:43:56 +01:00
|
|
|
cloned, err := AURPKGBUILDRepo(context.Background(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
|
2021-08-02 20:42:36 +02:00
|
|
|
assert.NoError(t, err)
|
2021-08-07 19:11:52 +02:00
|
|
|
assert.Equal(t, false, cloned)
|
2021-08-02 20:42:36 +02:00
|
|
|
}
|
2021-08-11 18:40:59 +02:00
|
|
|
|
|
|
|
func TestAURPKGBUILDRepos(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
|
|
|
|
|
|
|
os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
|
|
|
|
|
|
|
|
targets := []string{"yay", "yay-bin", "yay-git"}
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
want: "",
|
|
|
|
parentBuilder: &exe.CmdBuilder{
|
|
|
|
Runner: cmdRunner,
|
|
|
|
GitBin: "/usr/local/bin/git",
|
|
|
|
GitFlags: []string{},
|
|
|
|
},
|
|
|
|
}
|
2023-01-03 22:43:56 +01:00
|
|
|
cloned, err := AURPKGBUILDRepos(context.Background(), cmdBuilder, targets, "https://aur.archlinux.org", dir, false)
|
2021-08-11 18:40:59 +02:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[string]bool{"yay": true, "yay-bin": false, "yay-git": true}, cloned)
|
|
|
|
}
|