diff --git a/collector/autoindex/autoindex_collector.go b/collector/autoindex/autoindex_collector.go index cacd89a..fd6f05c 100644 --- a/collector/autoindex/autoindex_collector.go +++ b/collector/autoindex/autoindex_collector.go @@ -46,6 +46,11 @@ func NewCollector(downloadPageURL string) (*Collector, error) { return &c, nil } +// Name Collector name +func (c *Collector) Name() string { + return Name +} + func (c *Collector) loadDocument() (err error) { resp, err := http.Get(c.url) if err != nil { diff --git a/collector/collector.go b/collector/collector.go index ff3ab8b..2f13025 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -12,9 +12,11 @@ import ( // official collector const ( - // OfficialDownloadPageURL Golang official site URL + // OriginalOfficialDownloadPageURL Golang official site download page URL + OriginalOfficialDownloadPageURL = "https://golang.org/dl/" + // OfficialDownloadPageURL Golang official site download page URL OfficialDownloadPageURL = "https://go.dev/dl/" - // CNDownloadPageURL China mirror site URL + // CNDownloadPageURL China mirror site download page URL CNDownloadPageURL = "https://golang.google.cn/dl/" ) @@ -36,6 +38,8 @@ const ( // Collector Version information collector type Collector interface { + // Name Collector name + Name() string // StableVersions Return all stable versions StableVersions() (items []*version.Version, err error) // UnstableVersions Return all stable versions @@ -81,7 +85,7 @@ func NewCollector(urls ...string) (c Collector, err error) { } switch urls[i] { - case OfficialDownloadPageURL, CNDownloadPageURL: + case OfficialDownloadPageURL, OriginalOfficialDownloadPageURL, CNDownloadPageURL: return official.NewCollector(urls[i]) case AliYunDownloadPageURL, HUSTDownloadPageURL, NJUDownloadPageURL: diff --git a/collector/collector_test.go b/collector/collector_test.go new file mode 100644 index 0000000..ff6ac87 --- /dev/null +++ b/collector/collector_test.go @@ -0,0 +1,110 @@ +package collector + +import ( + "reflect" + "testing" + + "github.com/voidint/g/collector/autoindex" + "github.com/voidint/g/collector/fancyindex" + "github.com/voidint/g/collector/official" + "github.com/voidint/g/pkg/errs" +) + +func TestNewCollector(t *testing.T) { + type args struct { + urls []string + } + tests := []struct { + name string + args args + wantCollectorName string + wantErr error + }{ + { + name: "nil parameter", + args: args{urls: nil}, + wantCollectorName: official.Name, + }, + { + name: "A slice containing an empty string", + args: args{urls: []string{""}}, + wantCollectorName: official.Name, + }, + { + name: "The parameter is a URL slice without a trailing backslash", + args: args{urls: []string{"https://mirrors.aliyun.com/golang"}}, + wantCollectorName: fancyindex.Name, + }, + { + name: "A slice containing the name of the official collector", + args: args{urls: []string{"official|https://golang.google.cn/dl/"}}, + wantCollectorName: official.Name, + }, + { + name: "A slice containing the name of the fancyindex collector", + args: args{urls: []string{"fancyindex|https://mirrors.hust.edu.cn/golang/"}}, + wantCollectorName: fancyindex.Name, + }, + { + name: "A slice containing the name of the autoindex collector", + args: args{urls: []string{"autoindex|https://mirrors.ustc.edu.cn/golang/"}}, + wantCollectorName: autoindex.Name, + }, + { + name: "A slice containing only official collector URLs", + args: args{urls: []string{OfficialDownloadPageURL}}, + wantCollectorName: official.Name, + }, + { + name: "A slice containing only original official collector URLs", + args: args{urls: []string{OriginalOfficialDownloadPageURL}}, + wantCollectorName: official.Name, + }, + { + name: "A slice containing only china official mirror site collector URLs", + args: args{urls: []string{CNDownloadPageURL}}, + wantCollectorName: official.Name, + }, + { + name: "A slice containing only Alibaba cloud mirror site collector URLs", + args: args{urls: []string{AliYunDownloadPageURL}}, + wantCollectorName: fancyindex.Name, + }, + { + name: "A slice containing only HUST mirror site collector URLs", + args: args{urls: []string{HUSTDownloadPageURL}}, + wantCollectorName: fancyindex.Name, + }, + { + name: "A slice containing only NJU mirror site collector URLs", + args: args{urls: []string{NJUDownloadPageURL}}, + wantCollectorName: fancyindex.Name, + }, + { + name: "A slice containing only USTC mirror site collector URLs", + args: args{urls: []string{USTCDownloadPageURL}}, + wantCollectorName: autoindex.Name, + }, + { + name: "Collector not found", + args: args{urls: []string{"hello world"}}, + wantCollectorName: "", + wantErr: errs.ErrCollectorNotFound, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotC, err := NewCollector(tt.args.urls...) + if err != tt.wantErr { + t.Errorf("NewCollector() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if gotC != nil { + if !reflect.DeepEqual(gotC.Name(), tt.wantCollectorName) { + t.Errorf("NewCollector() = %v, want %v", gotC.Name(), tt.wantCollectorName) + } + } + }) + } +} diff --git a/collector/fancyindex/fancyindex_collector.go b/collector/fancyindex/fancyindex_collector.go index ea90c41..3647db5 100644 --- a/collector/fancyindex/fancyindex_collector.go +++ b/collector/fancyindex/fancyindex_collector.go @@ -46,6 +46,11 @@ func NewCollector(downloadPageURL string) (*Collector, error) { return &c, nil } +// Name Collector name +func (c *Collector) Name() string { + return Name +} + func (c *Collector) loadDocument() (err error) { resp, err := http.Get(c.url) if err != nil { diff --git a/collector/official/official_collector.go b/collector/official/official_collector.go index 3d05167..02d22c7 100644 --- a/collector/official/official_collector.go +++ b/collector/official/official_collector.go @@ -46,6 +46,11 @@ func NewCollector(downloadPageURL string) (*Collector, error) { return &c, nil } +// Name Collector name +func (c *Collector) Name() string { + return Name +} + func (c *Collector) loadDocument() (err error) { resp, err := http.Get(c.url) if err != nil {