test(download): add tests for repo downloading

This commit is contained in:
jguer 2021-08-02 20:42:36 +02:00 committed by J Guerreiro
parent 413ad23abc
commit 98378642bc
6 changed files with 238 additions and 10 deletions

View file

@ -34,16 +34,16 @@ type Executor interface {
LocalSatisfierExists(string) bool
PackageConflicts(IPackage) []Depend
PackageDepends(IPackage) []Depend
SatisfierFromDB(string, string) IPackage
PackageGroups(IPackage) []string
PackageOptionalDepends(IPackage) []Depend
PackageProvides(IPackage) []Depend
PackagesFromGroup(string) []IPackage
RefreshHandle() error
RepoUpgrades(bool) ([]Upgrade, error)
Repos() []string
SatisfierFromDB(string, string) IPackage
SyncPackage(string) IPackage
SyncPackages(...string) []IPackage
SyncSatisfier(string) IPackage
SyncSatisfierExists(string) bool
Repos() []string
}

View file

@ -49,6 +49,9 @@ func getPackageURL(db, pkgName string) (string, error) {
// https://github.com/archlinux/svntogit-community.git
func getPackageRepoURL(db string) (string, error) {
repoURL, err := getRepoURL(db)
if err != nil {
return "", err
}
return repoURL + ".git", err
}

View file

@ -1,11 +1,17 @@
package download
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/Jguer/yay/v10/pkg/settings/exe"
)
const gitExtrasPKGBUILD = `pkgname=git-extras
@ -137,3 +143,110 @@ func TestGetABSPkgbuild(t *testing.T) {
})
}
}
func Test_getPackageRepoURL(t *testing.T) {
ABSPackageURL = "https://github.com/archlinux/svntogit-packages"
ABSCommunityURL = "https://github.com/archlinux/svntogit-community"
type args struct {
db string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "community package",
args: args{db: "community"},
want: "https://github.com/archlinux/svntogit-community.git",
wantErr: false,
},
{
name: "core package",
args: args{db: "core"},
want: "https://github.com/archlinux/svntogit-packages.git",
wantErr: false,
},
{
name: "personal repo package",
args: args{db: "sweswe"},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getPackageRepoURL(tt.args.db)
if tt.wantErr {
assert.ErrorIs(t, err, ErrInvalidRepository)
}
assert.Equal(t, tt.want, got)
})
}
}
// GIVEN no previous existing folder
// WHEN ABSPKGBUILDRepo is called
// THEN a clone command should be formed
func TestABSPKGBUILDRepo(t *testing.T) {
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
want: "/usr/local/bin/git --no-replace-objects -C /tmp/doesnt-exist clone --no-progress --single-branch -b packages/linux https://github.com/archlinux/svntogit-packages.git linux",
parentBuilder: &exe.CmdBuilder{
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
err := ABSPKGBUILDRepo(cmdRunner, cmdBuilder, "core", "linux", "/tmp/doesnt-exist", false)
assert.NoError(t, err)
}
// GIVEN a previous existing folder without permissions
// WHEN ABSPKGBUILDRepo is called
// THEN a clone command should be formed
func TestABSPKGBUILDRepoExistsNoPerms(t *testing.T) {
dir, _ := ioutil.TempDir("/tmp/", "yay-test")
defer os.RemoveAll(dir)
os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o600)
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
parentBuilder: &exe.CmdBuilder{
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
err := ABSPKGBUILDRepo(cmdRunner, cmdBuilder, "core", "linux", dir, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error fetching linux: error reading")
}
// GIVEN a previous existing folder with permissions
// WHEN ABSPKGBUILDRepo is called
// THEN a pull command should be formed
func TestABSPKGBUILDRepoExistsPerms(t *testing.T) {
dir, _ := ioutil.TempDir("/tmp/", "yay-test")
defer os.RemoveAll(dir)
os.MkdirAll(filepath.Join(dir, "linux", ".git"), 0o777)
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
want: fmt.Sprintf("/usr/local/bin/git --no-replace-objects -C %s/linux pull --ff-only", dir),
parentBuilder: &exe.CmdBuilder{
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
err := ABSPKGBUILDRepo(cmdRunner, cmdBuilder, "core", "linux", dir, false)
assert.NoError(t, err)
}

View file

@ -1,11 +1,17 @@
package download
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/Jguer/yay/v10/pkg/settings/exe"
)
func TestGetAURPkgbuild(t *testing.T) {
@ -63,3 +69,67 @@ func TestGetAURPkgbuild(t *testing.T) {
})
}
}
// GIVEN no previous existing folder
// WHEN AURPKGBUILDRepo is called
// THEN a clone command should be formed
func TestAURPKGBUILDRepo(t *testing.T) {
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{
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
err := AURPKGBUILDRepo(cmdRunner, cmdBuilder, "https://aur.archlinux.org", "yay-bin", "/tmp/doesnt-exist", false)
assert.NoError(t, err)
}
// GIVEN a previous existing folder without permissions
// WHEN AURPKGBUILDRepo is called
// THEN a clone command should be formed
func TestAURPKGBUILDRepoExistsNoPerms(t *testing.T) {
dir, _ := ioutil.TempDir("/tmp/", "yay-test")
defer os.RemoveAll(dir)
os.MkdirAll(filepath.Join(dir, "yay-bin", ".git"), 0o600)
cmdRunner := &testRunner{}
cmdBuilder := &testGitBuilder{
index: 0,
test: t,
parentBuilder: &exe.CmdBuilder{
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
err := AURPKGBUILDRepo(cmdRunner, cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
assert.Error(t, err)
assert.Contains(t, err.Error(), "error fetching yay-bin: error reading")
}
// GIVEN a previous existing folder with permissions
// WHEN AURPKGBUILDRepo is called
// THEN a pull command should be formed
func TestAURPKGBUILDRepoExistsPerms(t *testing.T) {
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{
GitBin: "/usr/local/bin/git",
GitFlags: []string{"--no-replace-objects"},
},
}
err := AURPKGBUILDRepo(cmdRunner, cmdBuilder, "https://aur.archlinux.org", "yay-bin", dir, false)
assert.NoError(t, err)
}

View file

@ -17,22 +17,27 @@ import (
"github.com/Jguer/yay/v10/pkg/text"
)
type DBSearcher interface {
SyncPackage(string) db.IPackage
SatisfierFromDB(string, string) db.IPackage
}
func downloadGitRepo(cmdRunner exe.Runner,
cmdBuilder exe.GitCmdBuilder, pkgURL, pkgName, dest string, force bool, gitArgs ...string) error {
gitArgs = append(gitArgs, pkgURL, pkgName)
cloneArgs := make([]string, 0, len(gitArgs)+4)
cloneArgs = append(cloneArgs, "clone", "--no-progress")
cloneArgs = append(cloneArgs, gitArgs...)
finalDir := filepath.Join(dest, pkgName)
if _, err := os.Stat(filepath.Join(finalDir, ".git")); os.IsNotExist(err) {
if _, err := os.Stat(filepath.Join(finalDir, ".git")); os.IsNotExist(err) || (err == nil && force) {
if _, errD := os.Stat(finalDir); force && errD == nil {
if errR := os.RemoveAll(finalDir); errR != nil {
return ErrGetPKGBUILDRepo{inner: errR, pkgName: pkgName, errOut: ""}
}
}
gitArgs = append(gitArgs, pkgURL, pkgName)
cloneArgs := make([]string, 0, len(gitArgs)+4)
cloneArgs = append(cloneArgs, "clone", "--no-progress")
cloneArgs = append(cloneArgs, gitArgs...)
cmd := cmdBuilder.BuildGitCmd(dest, cloneArgs...)
_, stderr, errCapture := cmdRunner.Capture(cmd, 0)
@ -66,7 +71,7 @@ func getURLName(pkg db.IPackage) string {
return name
}
func GetPkgbuilds(dbExecutor db.Executor, httpClient *http.Client, targets []string, mode settings.TargetMode) (map[string][]byte, error) {
func GetPkgbuilds(dbExecutor DBSearcher, httpClient *http.Client, targets []string, mode settings.TargetMode) (map[string][]byte, error) {
pkgbuilds := make(map[string][]byte, len(targets))
var (
@ -128,7 +133,7 @@ func GetPkgbuilds(dbExecutor db.Executor, httpClient *http.Client, targets []str
return pkgbuilds, errs.Return()
}
func PKGBUILDRepos(dbExecutor db.Executor,
func PKGBUILDRepos(dbExecutor DBSearcher,
cmdRunner exe.Runner,
cmdBuilder exe.GitCmdBuilder,
targets []string, mode settings.TargetMode, aurURL, dest string, force bool) (map[string]bool, error) {

View file

@ -0,0 +1,37 @@
package download
import (
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/Jguer/yay/v10/pkg/settings/exe"
)
type testRunner struct {
}
func (t *testRunner) Capture(cmd *exec.Cmd, timeout int64) (stdout string, stderr string, err error) {
return "", "", nil
}
func (t *testRunner) Show(cmd *exec.Cmd) error {
return nil
}
type testGitBuilder struct {
index int
test *testing.T
want string
parentBuilder *exe.CmdBuilder
}
func (t *testGitBuilder) BuildGitCmd(dir string, extraArgs ...string) *exec.Cmd {
cmd := t.parentBuilder.BuildGitCmd(dir, extraArgs...)
assert.Equal(t.test, t.want, cmd.String())
t.index += 1
return cmd
}