-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b649063
commit 2d541c4
Showing
13 changed files
with
562 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ®istry.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: ®istry.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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ®istry.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: ®istry.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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.