yay/pkg/download/aur_test.go

163 lines
4.2 KiB
Go
Raw Normal View History

2021-01-30 12:52:50 +01:00
package download
import (
2021-08-12 18:56:23 +02:00
"context"
"fmt"
"os"
2021-09-06 23:51:07 +02:00
"os/exec"
"path/filepath"
2021-01-30 12:52:50 +01:00
"testing"
"github.com/stretchr/testify/assert"
"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))
})
}
}
// 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
}
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
}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
2021-09-06 23:51:07 +02:00
want: want,
parentBuilder: &exe.CmdBuilder{
Runner: cmdRunner,
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
newCloned, err := AURPKGBUILDRepo(context.Background(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", "/tmp/doesnt-exist", false)
assert.NoError(t, err)
assert.Equal(t, true, newCloned)
}
// 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()
dir := t.TempDir()
os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
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
}
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
}
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
2021-09-06 23:51:07 +02:00
want: want,
parentBuilder: &exe.CmdBuilder{
Runner: cmdRunner,
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
cloned, err := AURPKGBUILDRepo(context.Background(), cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
assert.NoError(t, err)
assert.Equal(t, false, cloned)
}
func TestAURPKGBUILDRepos(t *testing.T) {
2021-08-11 20:13:28 +02:00
t.Parallel()
dir := t.TempDir()
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{},
},
}
cloned, err := AURPKGBUILDRepos(context.Background(), cmdBuilder, targets, "https://aur.archlinux.org", dir, false)
assert.NoError(t, err)
assert.EqualValues(t, map[string]bool{"yay": true, "yay-bin": false, "yay-git": true}, cloned)
}