Split populating author as a method and add unit test

This commit is contained in:
Wenkai Yin 2017-12-15 10:40:24 +08:00
parent 745d83e393
commit d9b0f54c5e
2 changed files with 38 additions and 3 deletions

View File

@ -466,15 +466,26 @@ func getTagDetail(client *registry.Repository, tag string) (*tagDetail, error) {
return detail, err
}
if len(detail.Author) == 0 && detail.Config != nil {
populateAuthor(detail)
return detail, nil
}
func populateAuthor(detail *tagDetail) {
// has author info already
if len(detail.Author) > 0 {
return
}
// try to set author with the value of label "maintainer"
if detail.Config != nil {
for k, v := range detail.Config.Labels {
if strings.ToLower(k) == "maintainer" {
detail.Author = v
return
}
}
}
return detail, nil
}
// GetManifests returns the manifest of a tag

View File

@ -199,3 +199,27 @@ func TestGetReposTop(t *testing.T) {
fmt.Printf("\n")
}
func TestPopulateAuthor(t *testing.T) {
author := "author"
detail := &tagDetail{
Author: author,
}
populateAuthor(detail)
assert.Equal(t, author, detail.Author)
detail = &tagDetail{}
populateAuthor(detail)
assert.Equal(t, "", detail.Author)
maintainer := "maintainer"
detail = &tagDetail{
Config: &cfg{
Labels: map[string]string{
"Maintainer": maintainer,
},
},
}
populateAuthor(detail)
assert.Equal(t, maintainer, detail.Author)
}