Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(vcs): remove mux and use interface for other packages #1859

Merged
merged 4 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aur_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ func (installer *Installer) installAURPackages(ctx context.Context,
deps, exps := make([]string, 0, aurDepNames.Cardinality()), make([]string, 0, aurExpNames.Cardinality())
pkgArchives := make([]string, 0, len(exps)+len(deps))

var (
mux sync.Mutex
wg sync.WaitGroup
)
var wg sync.WaitGroup

for _, name := range all {
base := nameToBase[name]
Expand Down Expand Up @@ -207,7 +204,10 @@ func (installer *Installer) installAURPackages(ctx context.Context,

srcinfo := srcinfos[base]
wg.Add(1)
go installer.vcsStore.Update(ctx, name, srcinfo.Source, &mux, &wg)
go func(name string) {
installer.vcsStore.Update(ctx, name, srcinfo.Source)
wg.Done()
}(name)
}

wg.Wait()
Expand Down
10 changes: 5 additions & 5 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,10 +794,7 @@ func buildInstallPkgbuilds(
}
text.Debugln("deps:", deps, "exp:", exp, "pkgArchives:", pkgArchives)

var (
mux sync.Mutex
wg sync.WaitGroup
)
var wg sync.WaitGroup

for _, pkg := range base {
if srcinfo == nil {
Expand All @@ -808,7 +805,10 @@ func buildInstallPkgbuilds(
wg.Add(1)

text.Debugln("checking vcs store for:", pkg.Name)
go config.Runtime.VCSStore.Update(ctx, pkg.Name, srcinfo.Source, &mux, &wg)
go func(name string) {
config.Runtime.VCSStore.Update(ctx, name, srcinfo.Source)
wg.Done()
}(pkg.Name)
}

wg.Wait()
Expand Down
42 changes: 9 additions & 33 deletions pkg/upgrade/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package upgrade

import (
"context"
"sync"

"github.com/leonelquinteros/gotext"

Expand All @@ -14,49 +13,26 @@ import (

func UpDevel(
ctx context.Context,
remote []db.IPackage,
remote []db.IPackage, // should be a map
aurdata map[string]*query.Pkg,
localCache *vcs.InfoStore,
localCache vcs.Store,
) UpSlice {
toUpdate := make([]db.IPackage, 0, len(aurdata))
toRemove := make([]string, 0)

var (
mux1, mux2 sync.Mutex
wg sync.WaitGroup
)

checkUpdate := func(pkgName string, e vcs.OriginInfoByURL) {
defer wg.Done()

if localCache.NeedsUpdate(ctx, e) {
if _, ok := aurdata[pkgName]; ok {
for _, pkg := range remote {
if pkg.Name() == pkgName {
mux1.Lock()
toUpdate = append(toUpdate, pkg)
mux1.Unlock()

return
}
for _, pkgName := range localCache.ToUpgrade(ctx) {
if _, ok := aurdata[pkgName]; ok {
for _, pkg := range remote {
if pkg.Name() == pkgName {
toUpdate = append(toUpdate, pkg)
}
}

mux2.Lock()
} else {
toRemove = append(toRemove, pkgName)
mux2.Unlock()
}
}

for pkgName, e := range localCache.OriginsByPackage {
wg.Add(1)

go checkUpdate(pkgName, e)
}

wg.Wait()

toUpgrade := UpSlice{Up: make([]Upgrade, 0), Repos: []string{"devel"}}
toUpgrade := UpSlice{Up: make([]Upgrade, 0, len(toUpdate)), Repos: []string{"devel"}}

for _, pkg := range toUpdate {
if pkg.ShouldIgnore() {
Expand Down
124 changes: 8 additions & 116 deletions pkg/upgrade/sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package upgrade

import (
"context"
"fmt"
"os/exec"
"strconv"
"testing"
"time"

Expand All @@ -14,7 +11,6 @@ import (
alpm "github.com/Jguer/go-alpm/v2"

"github.com/Jguer/yay/v11/pkg/db/mock"
"github.com/Jguer/yay/v11/pkg/settings/exe"
"github.com/Jguer/yay/v11/pkg/vcs"
)

Expand Down Expand Up @@ -76,42 +72,13 @@ func Test_upAUR(t *testing.T) {
}
}

type MockRunner struct {
Returned []string
Index int
t *testing.T
}

func (r *MockRunner) Show(cmd *exec.Cmd) error {
return nil
}

func (r *MockRunner) Capture(cmd *exec.Cmd) (stdout, stderr string, err error) {
i, _ := strconv.Atoi(cmd.Args[len(cmd.Args)-1])
if i >= len(r.Returned) {
fmt.Println(r.Returned)
fmt.Println(cmd.Args)
fmt.Println(i)
}
stdout = r.Returned[i]
assert.Contains(r.t, cmd.Args, "ls-remote")
return stdout, stderr, err
}

func Test_upDevel(t *testing.T) {
t.Parallel()
returnValue := []string{
"7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
"7f4c277ce7149665d1c79b76ca8fbb832a65a03b HEAD",
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa HEAD",
"cccccccccccccccccccccccccccccccccccccccc HEAD",
"991c5b4146fd27f4aacf4e3111258a848934aaa1 HEAD",
}

type args struct {
remote []alpm.IPackage
aurdata map[string]*aur.Pkg
cached vcs.InfoStore
cached vcs.Store
}
tests := []struct {
name string
Expand All @@ -122,13 +89,7 @@ func Test_upDevel(t *testing.T) {
{
name: "No Updates",
args: args{
cached: vcs.InfoStore{
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
},
cached: &vcs.Mock{},
remote: []alpm.IPackage{
&mock.Package{PName: "hello", PVersion: "2.0.0"},
&mock.Package{PName: "local_pkg", PVersion: "1.1.0"},
Expand All @@ -145,47 +106,8 @@ func Test_upDevel(t *testing.T) {
name: "Simple Update",
finalLen: 3,
args: args{
cached: vcs.InfoStore{
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
OriginsByPackage: map[string]vcs.OriginInfoByURL{
"hello": {
"github.com/Jguer/z.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "0",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
"hello-non-existent": {
"github.com/Jguer/y.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "0",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
"hello2": {
"github.com/Jguer/a.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "1",
SHA: "7f4c277ce7149665d1c79b76ca8fbb832a65a03b",
},
},
"hello4": {
"github.com/Jguer/b.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "2",
SHA: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
"github.com/Jguer/c.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "3",
SHA: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
},
},
},
cached: &vcs.Mock{
ToUpgradeReturn: []string{"hello", "hello4"},
},
remote: []alpm.IPackage{
&mock.Package{PName: "hello", PVersion: "2.0.0"},
Expand Down Expand Up @@ -219,22 +141,7 @@ func Test_upDevel(t *testing.T) {
name: "No update returned",
finalLen: 1,
args: args{
cached: vcs.InfoStore{
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
OriginsByPackage: map[string]vcs.OriginInfoByURL{
"hello": {
"github.com/Jguer/d.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "4",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
},
},
cached: &vcs.Mock{ToUpgradeReturn: []string{}},
remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0"}},
aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
},
Expand All @@ -244,21 +151,8 @@ func Test_upDevel(t *testing.T) {
name: "No update returned - ignored",
finalLen: 1,
args: args{
cached: vcs.InfoStore{
CmdBuilder: &exe.CmdBuilder{
Runner: &MockRunner{
Returned: returnValue,
},
},
OriginsByPackage: map[string]vcs.OriginInfoByURL{
"hello": {
"github.com/Jguer/e.git": vcs.OriginInfo{
Protocols: []string{"https"},
Branch: "3",
SHA: "991c5b4146fd27f4aacf4e3111258a848934aaa1",
},
},
},
cached: &vcs.Mock{
ToUpgradeReturn: []string{"hello"},
},
remote: []alpm.IPackage{&mock.Package{PName: "hello", PVersion: "2.0.0", PShouldIgnore: true}},
aurdata: map[string]*aur.Pkg{"hello": {Version: "2.0.0", Name: "hello"}},
Expand All @@ -270,10 +164,8 @@ func Test_upDevel(t *testing.T) {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
tt.args.cached.CmdBuilder.(*exe.CmdBuilder).Runner.(*MockRunner).t = t
got := UpDevel(context.TODO(), tt.args.remote, tt.args.aurdata, &tt.args.cached)
got := UpDevel(context.TODO(), tt.args.remote, tt.args.aurdata, tt.args.cached)
assert.ElementsMatch(t, tt.want.Up, got.Up)
assert.Equal(t, tt.finalLen, len(tt.args.cached.OriginsByPackage))
})
}
}
13 changes: 9 additions & 4 deletions pkg/vcs/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package vcs

import (
"context"
"sync"

gosrc "github.com/Morganamilo/go-srcinfo"
)

type Mock struct{}
type Mock struct {
OriginsByPackage map[string]OriginInfoByURL
ToUpgradeReturn []string
}

func (m *Mock) ToUpgrade(ctx context.Context) []string {
return m.ToUpgradeReturn
}

func (m *Mock) Update(ctx context.Context, pkgName string, sources []gosrc.ArchString, mux sync.Locker, wg *sync.WaitGroup) {
wg.Done()
func (m *Mock) Update(ctx context.Context, pkgName string, sources []gosrc.ArchString) {
}

func (m *Mock) Save() error {
Expand Down
Loading