2019-04-02 23:06:15 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2019-08-02 20:05:46 +00:00
|
|
|
"net/http"
|
2019-04-02 23:06:15 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
gh "github.com/google/go-github/github"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-08-02 20:05:46 +00:00
|
|
|
func TestNewGithubClient(t *testing.T) {
|
|
|
|
httpClient := &http.Client{}
|
|
|
|
g := &GitHub{}
|
|
|
|
client, err := g.newGithubClient(httpClient)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Contains(t, client.BaseURL.String(), "api.github.com")
|
|
|
|
g.EnterpriseBaseURL = "api.example.com/"
|
|
|
|
enterpriseClient, err := g.newGithubClient(httpClient)
|
|
|
|
require.Nil(t, err)
|
|
|
|
require.Contains(t, enterpriseClient.BaseURL.String(), "api.example.com")
|
|
|
|
}
|
|
|
|
|
2019-04-02 23:06:15 +00:00
|
|
|
func TestSplitRepositoryNameWithWorkingExample(t *testing.T) {
|
|
|
|
var validRepositoryNames = []struct {
|
|
|
|
fullName string
|
|
|
|
owner string
|
|
|
|
repository string
|
|
|
|
}{
|
|
|
|
{"influxdata/telegraf", "influxdata", "telegraf"},
|
|
|
|
{"influxdata/influxdb", "influxdata", "influxdb"},
|
|
|
|
{"rawkode/saltstack-dotfiles", "rawkode", "saltstack-dotfiles"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range validRepositoryNames {
|
|
|
|
t.Run(tt.fullName, func(t *testing.T) {
|
|
|
|
owner, repository, _ := splitRepositoryName(tt.fullName)
|
|
|
|
|
|
|
|
require.Equal(t, tt.owner, owner)
|
|
|
|
require.Equal(t, tt.repository, repository)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSplitRepositoryNameWithNoSlash(t *testing.T) {
|
|
|
|
var invalidRepositoryNames = []string{
|
|
|
|
"influxdata-influxdb",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range invalidRepositoryNames {
|
|
|
|
t.Run(tt, func(t *testing.T) {
|
|
|
|
_, _, err := splitRepositoryName(tt)
|
|
|
|
|
|
|
|
require.NotNil(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLicenseWhenExists(t *testing.T) {
|
|
|
|
licenseName := "MIT"
|
|
|
|
license := gh.License{Name: &licenseName}
|
|
|
|
repository := gh.Repository{License: &license}
|
|
|
|
|
|
|
|
getLicenseReturn := getLicense(&repository)
|
|
|
|
|
|
|
|
require.Equal(t, "MIT", getLicenseReturn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLicenseWhenMissing(t *testing.T) {
|
|
|
|
repository := gh.Repository{}
|
|
|
|
|
|
|
|
getLicenseReturn := getLicense(&repository)
|
|
|
|
|
|
|
|
require.Equal(t, "None", getLicenseReturn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetTags(t *testing.T) {
|
|
|
|
licenseName := "MIT"
|
|
|
|
license := gh.License{Name: &licenseName}
|
|
|
|
|
|
|
|
ownerName := "influxdata"
|
|
|
|
owner := gh.User{Login: &ownerName}
|
|
|
|
|
|
|
|
fullName := "influxdata/influxdb"
|
|
|
|
repositoryName := "influxdb"
|
|
|
|
|
|
|
|
language := "Go"
|
|
|
|
|
|
|
|
repository := gh.Repository{
|
|
|
|
FullName: &fullName,
|
|
|
|
Name: &repositoryName,
|
|
|
|
License: &license,
|
|
|
|
Owner: &owner,
|
|
|
|
Language: &language,
|
|
|
|
}
|
|
|
|
|
|
|
|
getTagsReturn := getTags(&repository)
|
|
|
|
|
|
|
|
correctTagsReturn := map[string]string{
|
|
|
|
"owner": ownerName,
|
|
|
|
"name": repositoryName,
|
|
|
|
"language": language,
|
|
|
|
"license": licenseName,
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, true, reflect.DeepEqual(getTagsReturn, correctTagsReturn))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFields(t *testing.T) {
|
|
|
|
stars := 1
|
|
|
|
forks := 2
|
|
|
|
openIssues := 3
|
|
|
|
size := 4
|
2019-07-23 23:04:51 +00:00
|
|
|
subscribers := 5
|
|
|
|
watchers := 6
|
2019-04-02 23:06:15 +00:00
|
|
|
|
|
|
|
repository := gh.Repository{
|
2019-07-23 23:04:51 +00:00
|
|
|
StargazersCount: &stars,
|
|
|
|
ForksCount: &forks,
|
|
|
|
OpenIssuesCount: &openIssues,
|
|
|
|
Size: &size,
|
|
|
|
NetworkCount: &forks,
|
|
|
|
SubscribersCount: &subscribers,
|
|
|
|
WatchersCount: &watchers,
|
2019-04-02 23:06:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getFieldsReturn := getFields(&repository)
|
|
|
|
|
|
|
|
correctFieldReturn := make(map[string]interface{})
|
|
|
|
|
|
|
|
correctFieldReturn["stars"] = 1
|
|
|
|
correctFieldReturn["forks"] = 2
|
2019-07-23 23:04:51 +00:00
|
|
|
correctFieldReturn["networks"] = 2
|
2019-04-02 23:06:15 +00:00
|
|
|
correctFieldReturn["open_issues"] = 3
|
|
|
|
correctFieldReturn["size"] = 4
|
2019-07-23 23:04:51 +00:00
|
|
|
correctFieldReturn["subscribers"] = 5
|
|
|
|
correctFieldReturn["watchers"] = 6
|
2019-04-02 23:06:15 +00:00
|
|
|
|
|
|
|
require.Equal(t, true, reflect.DeepEqual(getFieldsReturn, correctFieldReturn))
|
|
|
|
}
|