test(gh): add utils tests to gh

This commit is contained in:
Slug-Boi
2025-04-09 13:07:09 +02:00
parent c59d8ddc85
commit 823dc0ff3d
2 changed files with 78 additions and 2 deletions
+7
View File
@@ -41,6 +41,13 @@ func ContainsUser(users []User, user User) bool {
}) })
} }
func CheckUserFields(user User) bool {
if user.Shortname == "" || user.Longname == "" || user.Username == "" || user.Email == "" {
return false
}
return true
}
func Define_users(author_file string) { func Define_users(author_file string) {
// wipe the users map // wipe the users map
Users = map[string]User{} Users = map[string]User{}
+70 -1
View File
@@ -1,9 +1,11 @@
package utils_test package utils_test
import ( import (
"github.com/Slug-Boi/cocommit/src/cmd/utils" "encoding/json"
"os" "os"
"testing" "testing"
"github.com/Slug-Boi/cocommit/src/cmd/utils"
) )
const author_data = ` const author_data = `
@@ -77,6 +79,45 @@ func Test_DeleteAuthor(t *testing.T) {
} }
} }
func Test_CreateAuthor(t *testing.T) {
setup()
defer teardown()
// Test CreateAuthor
author := utils.User{
Shortname: "epic",
Longname: "Test",
Username: "TestUser",
Email: "bestemailever@github.io",
Ex: false,
Groups: []string{"test"},
}
utils.CreateAuthor(author)
// Check if author was added
_, ok := utils.Users["epic"]
if !ok {
t.Errorf("CreateAuthor() did not add author")
}
// Check if author was added to the file
author_file := utils.Find_authorfile()
author_data, err := os.ReadFile(author_file)
if err != nil {
t.Errorf("Error reading file: %v", err)
}
//unmarshal the data
var authors utils.Author
err = json.Unmarshal(author_data, &authors)
if err != nil {
t.Errorf("Error unmarshalling file: %v", err)
}
if authors.Authors["Test"].Shortname != "epic" {
t.Errorf("CreateAuthor() did not add author to file: %v", authors.Authors)
}
}
// Author tests END // Author tests END
// User tests BEGIN // User tests BEGIN
@@ -140,3 +181,31 @@ func Test_Commit(t* testing.T) {
} }
} }
// Commit tests END // Commit tests END
// Github tests BEGIN
func Test_FetchGHProfile(t *testing.T) {
setup()
defer teardown()
// Test FetchGithubProfile
profile := utils.FetchGithubProfile("Slug-Boi")
if profile.Username != "Slug-Boi" {
t.Errorf("FetchGithubProfile() = %v; want Slug-Boi", profile.Username)
}
if profile.Email != "" {
t.Errorf("FetchGithubProfile() = %v; want empty email", profile.Email)
}
if profile.Shortname != "th" {
t.Errorf("FetchGithubProfile() = %v; want th", profile.Shortname)
}
if profile.Longname != "Theis" {
t.Errorf("FetchGithubProfile() = %v; want Theis", profile.Longname)
}
if profile.Ex != false {
t.Errorf("FetchGithubProfile() = %v; want false", profile.Ex)
}
if len(profile.Groups) != 0 {
t.Errorf("FetchGithubProfile() = %v; want 0", len(profile.Groups))
}
}
// Github tests END