2021-01-30 12:52:50 +01:00
|
|
|
package download
|
|
|
|
|
|
|
|
import (
|
2021-08-02 20:42:36 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2021-01-30 12:52:50 +01:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-08-02 20:42:36 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-01-30 12:52:50 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-08-02 20:42:36 +02:00
|
|
|
|
|
|
|
"github.com/Jguer/yay/v10/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
|
|
|
pkgBuildHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Write([]byte(gitExtrasPKGBUILD))
|
|
|
|
})
|
|
|
|
|
|
|
|
notFoundHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
})
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
handler http.Handler
|
|
|
|
pkgName string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "found package",
|
|
|
|
args: args{
|
|
|
|
handler: pkgBuildHandler,
|
|
|
|
pkgName: "git-extras",
|
|
|
|
},
|
|
|
|
want: gitExtrasPKGBUILD,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found package",
|
|
|
|
args: args{
|
|
|
|
handler: notFoundHandler,
|
|
|
|
pkgName: "git-extras",
|
|
|
|
},
|
|
|
|
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()
|
|
|
|
PKGBuild := httptest.NewServer(pkgBuildHandler)
|
2021-01-30 12:52:50 +01:00
|
|
|
PKGBuild.Config.Handler = tt.args.handler
|
2021-08-11 18:40:59 +02:00
|
|
|
got, err := AURPKGBUILD(PKGBuild.Client(), tt.args.pkgName, PKGBuild.URL)
|
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-08-02 20:42:36 +02:00
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
want: "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress https://aur.archlinux.org/yay-bin.git yay-bin",
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
}
|
2021-08-08 20:22:39 +02:00
|
|
|
newCloned, err := AURPKGBUILDRepo(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()
|
2021-08-02 20:42:36 +02:00
|
|
|
dir, _ := ioutil.TempDir("/tmp/", "yay-test")
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o777)
|
|
|
|
|
|
|
|
cmdRunner := &testRunner{}
|
|
|
|
cmdBuilder := &testGitBuilder{
|
|
|
|
index: 0,
|
|
|
|
test: t,
|
|
|
|
want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/yay-bin pull --ff-only", dir),
|
|
|
|
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"},
|
|
|
|
},
|
|
|
|
}
|
2021-08-08 20:22:39 +02:00
|
|
|
cloned, err := AURPKGBUILDRepo(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()
|
2021-08-11 18:40:59 +02:00
|
|
|
dir, _ := ioutil.TempDir("/tmp/", "yay-test")
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
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(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)
|
|
|
|
}
|