Skip to content

Commit

Permalink
misc: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Nov 22, 2023
1 parent 2f4da91 commit 04bc50f
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 23 deletions.
1 change: 1 addition & 0 deletions cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func install(ctx *cli.Context) (err error) {
}

v, err := version.NewFinder(items,
version.WithFinderPackageKind(version.ArchiveKind),
version.WithFinderGoos(runtime.GOOS),
version.WithFinderGoarch(runtime.GOARCH),
).Find(vname)
Expand Down
2 changes: 1 addition & 1 deletion collector/aliyun/go_file_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (item goFileItem) isPackageFile() bool {
strings.HasSuffix(item.FileName, ".msi")
}

func (item goFileItem) getKind() string {
func (item goFileItem) getKind() version.PackageKind {
if strings.HasSuffix(item.FileName, ".src.tar.gz") {
return version.SourceKind
}
Expand Down
2 changes: 1 addition & 1 deletion collector/aliyun/go_file_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func Test_isPackageFile(t *testing.T) {
func Test_getKind(t *testing.T) {
items := []*struct {
In *goFileItem
Expected string
Expected version.PackageKind
}{
{
In: &goFileItem{
Expand Down
2 changes: 1 addition & 1 deletion collector/official/official_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (c *Collector) findPackages(table *goquery.Selection) (pkgs []*version.Pack
pkgs = append(pkgs, &version.Package{
FileName: td.Eq(0).Find("a").Text(),
URL: href,
Kind: td.Eq(1).Text(),
Kind: version.PackageKind(td.Eq(1).Text()),
OS: td.Eq(2).Text(),
Arch: td.Eq(3).Text(),
Size: td.Eq(4).Text(),
Expand Down
43 changes: 42 additions & 1 deletion pkg/errs/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type PackageNotFoundError struct {
goarch string
}

// IsPackageNotFound 若是软件包不存在错误,则返回true;反之,返回false。
func IsPackageNotFound(err error) bool {
if err == nil {
return false
Expand All @@ -30,6 +31,7 @@ func IsPackageNotFound(err error) bool {
return ok
}

// NewPackageNotFoundError 返回软件包不存在错误实例
func NewPackageNotFoundError(kind, goos, goarch string) error {
return &PackageNotFoundError{
kind: kind,
Expand All @@ -38,6 +40,7 @@ func NewPackageNotFoundError(kind, goos, goarch string) error {
}
}

// Error 返回错误详情
func (e PackageNotFoundError) Error() string {
return fmt.Sprintf("package not found [%s,%s,%s]", e.goos, e.goarch, e.kind)
}
Expand All @@ -49,6 +52,7 @@ type VersionNotFoundError struct {
goarch string
}

// IsVersionNotFound 若是版本不存在错误,返回true;反之,返回false。
func IsVersionNotFound(err error) bool {
if err == nil {
return false
Expand All @@ -57,6 +61,7 @@ func IsVersionNotFound(err error) bool {
return ok
}

// NewVersionNotFoundError 返回版本不存在错误实例
func NewVersionNotFoundError(version, goos, goarch string) error {
return &VersionNotFoundError{
version: version,
Expand All @@ -65,10 +70,12 @@ func NewVersionNotFoundError(version, goos, goarch string) error {
}
}

// Error 返回错误详情
func (e VersionNotFoundError) Error() string {
return fmt.Sprintf("version not found %q [%s,%s]", e.version, e.goos, e.goarch)
}

// Version 返回版本号
func (e VersionNotFoundError) Version() string {
return e.version
}
Expand All @@ -79,21 +86,34 @@ type MalformedVersionError struct {
version string
}

// IsMalformedVersion 若是版本号格式错误,返回true;反之,返回false。
func IsMalformedVersion(err error) bool {
if err == nil {
return false
}
_, ok := err.(*MalformedVersionError)
return ok
}

// NewMalformedVersionError 返回版本号格式错误实例
func NewMalformedVersionError(version string, err error) error {
return &MalformedVersionError{
err: err,
version: version,
}
}

// Error 返回错误详情
func (e MalformedVersionError) Error() string {
return fmt.Sprintf("malformed version string %q", e.version)
}

// Err 返回源错误
func (e MalformedVersionError) Err() error {
return e.err
}

// Version 返回版本号
func (e MalformedVersionError) Version() string {
return e.version
}
Expand All @@ -104,6 +124,15 @@ type URLUnreachableError struct {
url string
}

// IsURLUnreachable 若是URL不可达错误,返回true;反之,返回false。
func IsURLUnreachable(err error) bool {
if err == nil {
return false
}
_, ok := err.(*URLUnreachableError)
return ok
}

// NewURLUnreachableError 返回URL不可达错误实例
func NewURLUnreachableError(url string, err error) error {
return &URLUnreachableError{
Expand All @@ -112,6 +141,7 @@ func NewURLUnreachableError(url string, err error) error {
}
}

// Error 返回错误详情
func (e URLUnreachableError) Error() string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("URL %q is unreachable", e.url))
Expand All @@ -121,10 +151,12 @@ func (e URLUnreachableError) Error() string {
return buf.String()
}

// Err 返回源错误
func (e URLUnreachableError) Err() error {
return e.err
}

// URL 返回URL地址
func (e URLUnreachableError) URL() string {
return e.url
}
Expand All @@ -135,6 +167,15 @@ type DownloadError struct {
err error
}

// IsDownload 若是下载失败错误,返回true;反之,返回false。
func IsDownload(err error) bool {
if err == nil {
return false
}
_, ok := err.(*DownloadError)
return ok
}

// NewDownloadError 返回下载失败错误实例
func NewDownloadError(url string, err error) error {
return &DownloadError{
Expand All @@ -143,7 +184,7 @@ func NewDownloadError(url string, err error) error {
}
}

// Error 返回错误字符串
// Error 返回错误详情
func (e DownloadError) Error() string {
var buf strings.Builder
buf.WriteString(fmt.Sprintf("resource(%s) download failed", e.url))
Expand Down
59 changes: 59 additions & 0 deletions pkg/errs/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,61 @@ import (
"github.com/stretchr/testify/assert"
)

func TestPackageNotFoundError(t *testing.T) {
t.Run("软件包不存在错误", func(t *testing.T) {
kind := "Archive"
goos := "linux"
goarch := "amd64"

err := NewPackageNotFoundError(kind, goos, goarch)
assert.NotNil(t, err)
assert.Equal(t, fmt.Sprintf("package not found [%s,%s,%s]", goos, goarch, kind), err.Error())

e, ok := err.(*PackageNotFoundError)
assert.True(t, IsPackageNotFound(err))
assert.False(t, IsPackageNotFound(nil))
assert.True(t, ok)
assert.NotNil(t, e)
})
}

func TestVersionNotFoundError(t *testing.T) {
t.Run("版本号不存在错误", func(t *testing.T) {
v := "abcdef"
goos := "linux"
goarch := "amd64"

err := NewVersionNotFoundError(v, goos, goarch)
assert.NotNil(t, err)
assert.Equal(t, fmt.Sprintf("version not found %q [%s,%s]", v, goos, goarch), err.Error())

e, ok := err.(*VersionNotFoundError)
assert.True(t, IsVersionNotFound(err))
assert.False(t, IsVersionNotFound(nil))
assert.True(t, ok)
assert.NotNil(t, e)
assert.Equal(t, v, e.Version())
})
}

func TestMalformedVersionError(t *testing.T) {
t.Run("版本号格式错误", func(t *testing.T) {
v := "abcdef"
core := errors.New("malformed version string")
err := NewMalformedVersionError(v, errors.New("malformed version string"))
assert.NotNil(t, err)
assert.Equal(t, fmt.Sprintf("malformed version string %q", v), err.Error())

e, ok := err.(*MalformedVersionError)
assert.True(t, IsMalformedVersion(err))
assert.False(t, IsMalformedVersion(nil))
assert.True(t, ok)
assert.NotNil(t, e)
assert.Equal(t, v, e.Version())
assert.Equal(t, core, e.Err())
})
}

func TestURLUnreachableError(t *testing.T) {
t.Run("URL不可达错误", func(t *testing.T) {
url := "https://github.com/voidint"
Expand All @@ -17,6 +72,8 @@ func TestURLUnreachableError(t *testing.T) {
assert.NotNil(t, err)

e, ok := err.(*URLUnreachableError)
assert.True(t, IsURLUnreachable(err))
assert.False(t, IsURLUnreachable(nil))
assert.True(t, ok)
assert.NotNil(t, e)
assert.Equal(t, url, e.URL())
Expand All @@ -33,6 +90,8 @@ func TestDownloadError(t *testing.T) {
err := NewDownloadError(url, core)
assert.NotNil(t, err)
e, ok := err.(*DownloadError)
assert.True(t, IsDownload(err))
assert.False(t, IsDownload(nil))
assert.True(t, ok)
assert.NotNil(t, e)
assert.Equal(t, url, e.URL())
Expand Down
27 changes: 24 additions & 3 deletions version/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ import (

// Finder 版本查找器
type Finder struct {
kind PackageKind
goos string
goarch string
items []*Version
}

// WithFinderPackageKind 设置查找器查找的文件种类。
func WithFinderPackageKind(kind PackageKind) func(fdr *Finder) {
return func(fdr *Finder) {
fdr.kind = kind
}
}

// WithFinderGoos 设置查找器所在的目标操作系统,如darwin, freebsd, linux等。
func WithFinderGoos(goos string) func(fdr *Finder) {
return func(fdr *Finder) {
Expand All @@ -34,6 +42,7 @@ func NewFinder(items []*Version, opts ...func(fdr *Finder)) *Finder {
sort.Sort(Collection(items)) // 升序

fdr := Finder{
kind: ArchiveKind,
goos: runtime.GOOS,
goarch: runtime.GOARCH,
items: items,
Expand Down Expand Up @@ -74,11 +83,19 @@ func (fdr *Finder) Find(vname string) (*Version, error) {
return nil, errs.NewVersionNotFoundError(vname, fdr.goos, fdr.goarch)
}

versionFound := false
for i := len(fdr.items) - 1; i > 0; i-- { // 优先匹配高版本
if fdr.items[i].match(fdr.goos, fdr.goarch) && cs.Check(fdr.items[i].sv) {
return fdr.items[i], nil
if cs.Check(fdr.items[i].sv) {
versionFound = true

if fdr.items[i].match(fdr.goos, fdr.goarch) {
return fdr.items[i], nil
}
}
}
if versionFound {
return nil, errs.NewPackageNotFoundError(string(fdr.kind), fdr.goos, fdr.goarch)
}
return nil, errs.NewVersionNotFoundError(vname, fdr.goos, fdr.goarch)
}

Expand All @@ -95,10 +112,14 @@ func (fdr *Finder) MustFind(vname string) *Version {
const Latest = "latest"

func (fdr *Finder) findLatest() (*Version, error) {
if len(fdr.items) == 0 {
return nil, errs.NewVersionNotFoundError(Latest, fdr.goos, fdr.goarch)
}

for i := len(fdr.items) - 1; i > 0; i-- {
if fdr.items[i].match(fdr.goos, fdr.goarch) {
return fdr.items[i], nil
}
}
return nil, errs.NewVersionNotFoundError(Latest, fdr.goos, fdr.goarch)
return nil, errs.NewPackageNotFoundError(string(fdr.kind), fdr.goos, fdr.goarch)
}
25 changes: 25 additions & 0 deletions version/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,28 @@ func TestFinder_Find(t *testing.T) {
})
}
}

func TestFinder_MustFind(t *testing.T) {
vs, err := genVersions()
if err != nil {
assert.Nil(t, err)
}

fdr := NewFinder(vs,
WithFinderPackageKind(ArchiveKind),
WithFinderGoos("darwin"),
WithFinderGoarch("arm64"),
)

t.Run("查找到版本", func(t *testing.T) {
v := fdr.MustFind("1.21.4")
assert.NotNil(t, v)
assert.Equal(t, v.Name(), "1.21.4")
})

t.Run("查找不到版本", func(t *testing.T) {
assert.Panics(t, func() {
fdr.MustFind("~1.15")
})
})
}
Loading

0 comments on commit 04bc50f

Please sign in to comment.