test: create tests for the util functions

This commit is contained in:
Theis
2024-10-16 20:47:35 +02:00
parent 5cfa40876b
commit 87a6de6b6f
7 changed files with 130 additions and 10 deletions
@@ -47,9 +47,7 @@ func CheckAuthorFile() string {
}
func DeleteOneAuthor(author string) {
//author_file := Find_authorfile()
author_file := "author_file"
author_file := Find_authorfile()
// open author_file
file, err := os.OpenFile(author_file, os.O_RDWR, 0644)
+1 -1
View File
@@ -98,7 +98,7 @@ func add_x_users(excludeMode []string) {
}
}
// helper function to select a group of users to exclude in the commit message
// helper function to select groups of users to exclude in the commit message
func group_selection(group []User, excludeMode []string) []string {
for _, user := range Users {
if !(slices.Contains(group, user)) {
+9
View File
@@ -40,6 +40,15 @@ func Define_users(author_file string) {
group_info = append(group_info, strings.Split(input[1], "|")...)
}
info := strings.Split(input_str, "|")
if len(info) < 4 {
if len(info) > 0 {
println("Error: User ", info[0], " is missing information")
} else {
println("Error: Some user is missing information")
}
println("Please check the author file for proper syntax")
os.Exit(1)
}
usr := User{Username: info[2], Email: info[3], Names: info[0] + "/" + info[1]}
Users[info[0]] = usr
Users[info[1]] = usr
+98
View File
@@ -0,0 +1,98 @@
package utils_test
import (
"main/src_code/go_src/cmd/utils"
"os"
"testing"
)
const author_data = `syntax for the test file
te|testing|TestUser|test@test.test|ex
ti|testtest|UserName2|testing@user.io;;gr1`
var envVar = os.Getenv("author_file")
func setup() {
// setup test data
os.WriteFile("author_file_test", []byte(author_data), 0644)
os.Setenv("author_file", "author_file_test")
}
func teardown() {
// remove test data
os.Remove("author_file_test")
os.Setenv("author_file", envVar)
}
// Author tests BEGIN
func Test_FindAuthorFile(t* testing.T) {
setup()
defer teardown()
// Test Find_authorfile
authorfile := utils.Find_authorfile()
if authorfile != "author_file_test" {
t.Errorf("Find_authorfile() = %v; want authors_file_test", authorfile)
}
}
func Test_DeleteAuthor(t *testing.T) {
setup()
defer teardown()
// Test DeleteOneAuthor
og_bytes, err := os.ReadFile("author_file_test")
if err != nil {
t.Errorf("Error reading file: %v", err)
}
utils.DeleteOneAuthor("te")
deleted_bytes, err := os.ReadFile("author_file_test")
if err != nil {
t.Errorf("Error reading file: %v", err)
}
if string(og_bytes) == string(deleted_bytes) {
t.Errorf("DeleteOneAuthor() did not delete author")
}
}
// Author tests END
// User tests BEGIN
func Test_DefineUsers(t *testing.T) {
setup()
defer teardown()
// Test Define_users
utils.Define_users("author_file_test")
if len(utils.Users) != 4 {
t.Errorf("Define_users() = %v; want 4", len(utils.Users))
}
}
func Test_RemoveUser(t *testing.T) {
setup()
defer teardown()
// Test RemoveUser
utils.Define_users("author_file_test")
utils.RemoveUser("te")
if len(utils.Users) != 3 {
t.Errorf("RemoveUser() = %v; want 3", len(utils.Users))
}
}
// User tests END
// Commit tests BEGIN
func Test_Commit(t* testing.T) {
setup()
defer teardown()
utils.Define_users("author_file_test")
// Test Commit
authors := []string{"te"}
message := "Test commit message"
commit := utils.Commit(message, authors)
if commit != "Test commit message\n\nCo-authored-by: TestUser <test@test.test>" {
t.Errorf("Commit() = %v; want Test commit message\n", commit)
}
}
// Commit tests END