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 23, 2023
1 parent e2d1599 commit baa8909
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 0 deletions.
28 changes: 28 additions & 0 deletions collector/aliyun/aliyun_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/stretchr/testify/assert"
"github.com/voidint/g/version"
)

func getCollector() (*Collector, error) {
Expand Down Expand Up @@ -52,3 +53,30 @@ func Test_findGoFileItems(t *testing.T) {
}
})
}

func TestCollector_StableVersions(t *testing.T) {
t.Run("稳定版本列表", func(t *testing.T) {
c := &Collector{}
vs, err := c.StableVersions()
assert.Nil(t, err)
assert.Equal(t, []*version.Version{}, vs)
})
}

func TestCollector_UnstableVersions(t *testing.T) {
t.Run("非稳定版本列表", func(t *testing.T) {
c := &Collector{}
vs, err := c.UnstableVersions()
assert.Nil(t, err)
assert.Equal(t, []*version.Version{}, vs)
})
}

func TestCollector_ArchivedVersions(t *testing.T) {
t.Run("已归档版本列表", func(t *testing.T) {
c := &Collector{}
vs, err := c.ArchivedVersions()
assert.Nil(t, err)
assert.Equal(t, []*version.Version{}, vs)
})
}
64 changes: 64 additions & 0 deletions version/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/voidint/g/pkg/errs"
)

func genVersions() ([]*Version, error) {
Expand Down Expand Up @@ -232,3 +233,66 @@ func TestFinder_MustFind(t *testing.T) {
})
})
}

func TestFinder_findLatest(t *testing.T) {
vs := []*Version{
MustNew("1.16.1", WithPackages([]*Package{
{
FileName: "go1.16.1.darwin-amd64.tar.gz",
URL: "https://golang.google.cn/dl/go1.16.1.darwin-amd64.tar.gz",
Kind: ArchiveKind,
OS: "macOS",
Arch: "x86-64",
Size: "124MB",
Checksum: "a760929667253cdaa5b10117f536a912be2b0be1006215ff86e957f98f76fd58",
Algorithm: "SHA256",
},
{
FileName: "go1.16.1.darwin-arm64.tar.gz",
URL: "https://golang.google.cn/dl/go1.16.1.darwin-arm64.tar.gz",
Kind: ArchiveKind,
OS: "macOS",
Arch: "ARM64",
Size: "120MB",
Checksum: "de2847f49faac2d0608b4afc324cbb3029a496c946db616c294d26082e45f32d",
Algorithm: "SHA256",
},
})),
}

tests := []struct {
name string
fdr *Finder
wantV *Version
wantErr error
}{
{
name: "查找器中版本列表为空",
fdr: NewFinder(nil, WithFinderPackageKind(ArchiveKind), WithFinderGoos("darwin"), WithFinderGoarch("arm64")),
wantV: nil,
wantErr: errs.NewVersionNotFoundError(Latest, "darwin", "arm64"),
},
{
name: "查找器中版本列表非空且软件包亦匹配",
fdr: NewFinder(vs, WithFinderPackageKind(ArchiveKind), WithFinderGoos("darwin"), WithFinderGoarch("arm64")),
wantV: vs[0],
wantErr: nil,
},
{
name: "查找器中版本列表非空但未找到匹配的软件包",
fdr: NewFinder(vs, WithFinderPackageKind(InstallerKind), WithFinderGoos("windows"), WithFinderGoarch("arm64")),
wantV: nil,
wantErr: errs.NewPackageNotFoundError(string(InstallerKind), "windows", "arm64"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v, err := tt.fdr.findLatest()
if err != nil {
assert.Equal(t, tt.wantErr.Error(), err.Error())
} else {
assert.Equal(t, tt.wantV.Name(), v.Name())
}
})
}
}
124 changes: 124 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"
"time"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/voidint/g/pkg/errs"
)
Expand Down Expand Up @@ -82,6 +83,39 @@ func TestSemantify(t *testing.T) {
}
}

func TestNew(t *testing.T) {
t.Run("合法的版本号", func(t *testing.T) {
vname := "1.21.4"
opts := []func(v *Version){nil, nil, nil}
v, err := New(vname, opts...)
assert.Nil(t, err)
assert.True(t, reflect.DeepEqual(v, &Version{name: "1.21.4", sv: semver.MustParse("1.21.4")}))
})

t.Run("非法的版本号", func(t *testing.T) {
vname := "1.2.3.4"
v, err := New(vname)
assert.NotNil(t, err)
assert.True(t, errs.IsMalformedVersion(err))
assert.Nil(t, v)
})
}

func TestMustNew(t *testing.T) {
t.Run("合法的版本号", func(t *testing.T) {
vname := "1.21.4"
v := MustNew(vname)
assert.True(t, reflect.DeepEqual(v, &Version{name: "1.21.4", sv: semver.MustParse("1.21.4")}))
})

t.Run("非法的版本号", func(t *testing.T) {
vname := "1.2.3.4"
assert.Panics(t, func() {
MustNew(vname)
})
})
}

func TestVersion_FindPackages(t *testing.T) {
vs, err := genVersions()
if err != nil {
Expand Down Expand Up @@ -121,6 +155,96 @@ func TestVersion_FindPackages(t *testing.T) {
})
}

func TestVersion_Packages(t *testing.T) {
filename := "go1.21.4.darwin-arm64.tar.gz"
url := "https://golang.google.cn/dl/go1.21.4.darwin-arm64.tar.gz"
kind := ArchiveKind
os := "macOS"
arch := "ARM64"
size := "62MB"
checksum := "8b7caf2ac60bdff457dba7d4ff2a01def889592b834453431ae3caecf884f6a5"
algorithm := "SHA256"

tests := []struct {
name string
v *Version
want []Package
}{
{
name: "软件包列表为空",
v: MustNew("1"),
want: make([]Package, 0),
},
{
name: "软件包列表非空",
v: MustNew("1.21.4", WithPackages([]*Package{
{
FileName: filename,
URL: url,
Kind: kind,
OS: os,
Arch: arch,
Size: size,
Checksum: checksum,
Algorithm: algorithm,
},
})),
want: []Package{
{
FileName: filename,
URL: url,
Kind: kind,
OS: os,
Arch: arch,
Size: size,
Checksum: checksum,
Algorithm: algorithm,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.v.Packages(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Version.Packages() = %v, want %v", got, tt.want)
}
})
}
}

func TestVersion_MatchConstraint(t *testing.T) {
cs, err := semver.NewConstraint("~1.18")
assert.Nil(t, err)
assert.NotNil(t, cs)

tests := []struct {
name string
v *Version
cs *semver.Constraints
want bool
}{
{
name: "不匹配约束",
v: MustNew("1.21.4"),
cs: cs,
want: false,
},
{
name: "匹配约束",
v: MustNew("1.18.4"),
cs: cs,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.v.MatchConstraint(tt.cs); got != tt.want {
t.Errorf("Version.MatchConstraint() = %v, want %v", got, tt.want)
}
})
}
}

func TestVerifyChecksum(t *testing.T) {
t.Run("检查安装包校验和", func(t *testing.T) {
filename := fmt.Sprintf("%d.txt", time.Now().Unix())
Expand Down

0 comments on commit baa8909

Please sign in to comment.