Skip to content

Commit

Permalink
test(versiongetter): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 16, 2023
1 parent b649063 commit 2d541c4
Show file tree
Hide file tree
Showing 13 changed files with 562 additions and 73 deletions.
10 changes: 8 additions & 2 deletions pkg/controller/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ func InitializeGenerateCommandController(ctx context.Context, param *config.Para
versiongetter.NewFuzzy,
wire.Bind(new(generate.FuzzyGetter), new(*versiongetter.FuzzyGetter)),
),
versiongetter.NewGenerator,
wire.NewSet(
versiongetter.NewGeneralVersionGetter,
wire.Bind(new(versiongetter.VersionGetter), new(*versiongetter.GeneralVersionGetter)),
),
versiongetter.NewCargo,
versiongetter.NewGitHubRelease,
versiongetter.NewGitHubTag,
Expand Down Expand Up @@ -788,7 +791,10 @@ func InitializeUpdateCommandController(ctx context.Context, param *config.Param,
wire.Bind(new(update.FuzzyFinder), new(*fuzzyfinder.Finder)),
wire.Bind(new(versiongetter.FuzzyFinder), new(*fuzzyfinder.Finder)),
),
versiongetter.NewGenerator,
wire.NewSet(
versiongetter.NewGeneralVersionGetter,
wire.Bind(new(versiongetter.VersionGetter), new(*versiongetter.GeneralVersionGetter)),
),
versiongetter.NewCargo,
versiongetter.NewGitHubRelease,
versiongetter.NewGitHubTag,
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 127 additions & 0 deletions pkg/versiongetter/cargo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package versiongetter_test

import (
"context"
"testing"

"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/fuzzyfinder"
"github.com/aquaproj/aqua/v2/pkg/versiongetter"
"github.com/google/go-cmp/cmp"
)

func TestCargoVersionGetter_Get(t *testing.T) {
t.Parallel()
data := []struct {
name string
versions map[string][]string
pkg *registry.PackageInfo
filters []*versiongetter.Filter
isErr bool
version string
}{
{
name: "normal",
filters: []*versiongetter.Filter{
{},
},
versions: map[string][]string{
"crates.io/skim": {
"3.0.0",
"2.0.0",
"1.0.0",
},
},
pkg: &registry.PackageInfo{
Crate: "crates.io/skim",
},
version: "3.0.0",
},
}

ctx := context.Background()
for _, d := range data {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()
cargoClient := versiongetter.NewMockCargoClient(d.versions)
cargoGetter := versiongetter.NewCargo(cargoClient)
version, err := cargoGetter.Get(ctx, d.pkg, d.filters)
if err != nil {
if d.isErr {
return
}
t.Fatal(err)
}
if d.isErr {
t.Fatal("error must be returned")
}
if version != d.version {
t.Fatalf("wanted %s, got %s", d.version, version)
}
})
}
}

func TestCargoVersionGetter_List(t *testing.T) {
t.Parallel()
data := []struct {
name string
versions map[string][]string
pkg *registry.PackageInfo
filters []*versiongetter.Filter
isErr bool
items []*fuzzyfinder.Item
}{
{
name: "normal",
filters: []*versiongetter.Filter{
{},
},
versions: map[string][]string{
"crates.io/skim": {
"3.0.0",
"2.0.0",
"1.0.0",
},
},
pkg: &registry.PackageInfo{
Crate: "crates.io/skim",
},
items: []*fuzzyfinder.Item{
{
Item: "3.0.0",
},
{
Item: "2.0.0",
},
{
Item: "1.0.0",
},
},
},
}

ctx := context.Background()
for _, d := range data {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()
cargoClient := versiongetter.NewMockCargoClient(d.versions)
cargoGetter := versiongetter.NewCargo(cargoClient)
items, err := cargoGetter.List(ctx, d.pkg, d.filters)
if err != nil {
if d.isErr {
return
}
t.Fatal(err)
}
if d.isErr {
t.Fatal("error must be returned")
}
if diff := cmp.Diff(items, d.items); diff != "" {
t.Fatalf(diff)
}
})
}
}
32 changes: 8 additions & 24 deletions pkg/versiongetter/get.go → pkg/versiongetter/fuzzy_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,16 @@ import (

type FuzzyGetter struct {
fuzzyFinder FuzzyFinder
gen *Generator
getter VersionGetter
}

func NewFuzzy(finder FuzzyFinder, gen *Generator) *FuzzyGetter {
func NewFuzzy(finder FuzzyFinder, getter VersionGetter) *FuzzyGetter {
return &FuzzyGetter{
fuzzyFinder: finder,
gen: gen,
getter: getter,
}
}

type MockFuzzyGetter struct {
versions map[string]string
}

func NewMockFuzzyGetter(versions map[string]string) *MockFuzzyGetter {
return &MockFuzzyGetter{
versions: versions,
}
}

func (g *MockFuzzyGetter) Get(ctx context.Context, _ *logrus.Entry, pkg *registry.PackageInfo, currentVersion string, useFinder bool) string {
return g.versions[pkg.GetName()]
}

type FuzzyFinder interface {
Find(items []*fuzzyfinder.Item, hasPreview bool) (int, error)
FindMulti(items []*fuzzyfinder.Item, hasPreview bool) ([]int, error)
Expand All @@ -46,16 +32,14 @@ func (g *FuzzyGetter) Get(ctx context.Context, _ *logrus.Entry, pkg *registry.Pa
return ""
}

versionGetter := g.gen.Get(pkg)
if versionGetter == nil {
return ""
}

if useFinder { //nolint:nestif
versions, err := versionGetter.List(ctx, pkg, filters)
versions, err := g.getter.List(ctx, pkg, filters)
if err != nil {
return ""
}
if versions == nil {
return ""
}
currentVersionIndex := 0
if currentVersion != "" {
for i, version := range versions {
Expand All @@ -76,7 +60,7 @@ func (g *FuzzyGetter) Get(ctx context.Context, _ *logrus.Entry, pkg *registry.Pa
return versions[idx].Item
}

version, err := versionGetter.Get(ctx, pkg, filters)
version, err := g.getter.Get(ctx, pkg, filters)
if err != nil {
return ""
}
Expand Down
85 changes: 85 additions & 0 deletions pkg/versiongetter/fuzzy_getter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package versiongetter_test

import (
"context"
"testing"

"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/fuzzyfinder"
"github.com/aquaproj/aqua/v2/pkg/versiongetter"
"github.com/sirupsen/logrus"
)

func TestFuzzyGetter_Get(t *testing.T) {
t.Parallel()
data := []struct {
name string
pkg *registry.PackageInfo
currentVersion string
useFinder bool
version string
idxs []int
versions map[string][]*fuzzyfinder.Item
}{
{
name: "normal",
pkg: &registry.PackageInfo{
RepoOwner: "suzuki-shunsuke",
RepoName: "tfcmt",
},
currentVersion: "v2.0.0",
version: "v4.6.0",
versions: map[string][]*fuzzyfinder.Item{
"suzuki-shunsuke/tfcmt": {
{
Item: "v4.6.0",
},
{
Item: "v3.0.0",
},
{
Item: "v2.0.0",
},
},
},
},
{
name: "finder",
pkg: &registry.PackageInfo{
RepoOwner: "suzuki-shunsuke",
RepoName: "tfcmt",
},
useFinder: true,
idxs: []int{1},
currentVersion: "v2.0.0",
version: "v3.0.0",
versions: map[string][]*fuzzyfinder.Item{
"suzuki-shunsuke/tfcmt": {
{
Item: "v4.6.0",
},
{
Item: "v3.0.0",
},
{
Item: "v2.0.0",
},
},
},
},
}
logE := logrus.NewEntry(logrus.New())
for _, d := range data {
d := d
t.Run(d.name, func(t *testing.T) {
t.Parallel()
finder := fuzzyfinder.NewMock(d.idxs, nil)
vg := versiongetter.NewMockVersionGetter(d.versions)
fg := versiongetter.NewFuzzy(finder, vg)
version := fg.Get(context.Background(), logE, d.pkg, d.currentVersion, d.useFinder)
if version != d.version {
t.Fatalf("wanted %s, got %s", d.version, version)
}
})
}
}
54 changes: 54 additions & 0 deletions pkg/versiongetter/general.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package versiongetter

import (
"context"

"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/fuzzyfinder"
)

type GeneralVersionGetter struct {
cargo *CargoVersionGetter
ghTag *GitHubTagVersionGetter
ghRelease *GitHubReleaseVersionGetter
}

func NewGeneralVersionGetter(cargo *CargoVersionGetter, ghTag *GitHubTagVersionGetter, ghRelease *GitHubReleaseVersionGetter) *GeneralVersionGetter {
return &GeneralVersionGetter{
cargo: cargo,
ghTag: ghTag,
ghRelease: ghRelease,
}
}

func (g *GeneralVersionGetter) Get(ctx context.Context, pkg *registry.PackageInfo, filters []*Filter) (string, error) {
getter := g.get(pkg)
if getter == nil {
return "", nil
}
return getter.Get(ctx, pkg, filters) //nolint:wrapcheck
}

func (g *GeneralVersionGetter) List(ctx context.Context, pkg *registry.PackageInfo, filters []*Filter) ([]*fuzzyfinder.Item, error) {
getter := g.get(pkg)
if getter == nil {
return nil, nil
}
return getter.List(ctx, pkg, filters) //nolint:wrapcheck
}

func (g *GeneralVersionGetter) get(pkg *registry.PackageInfo) VersionGetter {
if pkg.Type == "cargo" {
return g.cargo
}
if g.ghTag == nil {
return nil
}
if !pkg.HasRepo() {
return nil
}
if pkg.VersionSource == "github_tag" {
return g.ghTag
}
return g.ghRelease
}
Loading

0 comments on commit 2d541c4

Please sign in to comment.