From 872cd7b5c0e617f7dac29d571a5b5306a7b174d7 Mon Sep 17 00:00:00 2001 From: Theis Date: Mon, 4 Mar 2024 22:18:11 +0100 Subject: [PATCH] added a few tests to the new functionality --- src_code/go_src/cocommit.go | 21 +++++++++++++-------- src_code/go_src/cocommit_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src_code/go_src/cocommit.go b/src_code/go_src/cocommit.go index e98ecbb..ece86ec 100644 --- a/src_code/go_src/cocommit.go +++ b/src_code/go_src/cocommit.go @@ -17,10 +17,10 @@ type user struct { var users = make(map[string]user) var sb strings.Builder +var all_flag = false func main() { - all_flag := false // Reads a shell env variable :: author_file authors := os.Getenv("author_file") @@ -88,19 +88,15 @@ func main() { skip_loop: if len(excludeMode) > 0 || all_flag { - for key, user := range users { - if !slices.Contains(excludeMode, user.username) { - sb_author(key) - excludeMode = append(excludeMode, user.username) - } - } + add_x_users(excludeMode) } // commit msg built commit := sb_build() - print(commit) + //NOTE: Uncomment for testing + //print(commit) // commit shell command cmd := exec.Command("git", "commit", "-m", commit) @@ -117,6 +113,15 @@ func main() { } +func add_x_users(excludeMode []string) { + for key, user := range users { + if !slices.Contains(excludeMode, user.username) { + sb_author(key) + excludeMode = append(excludeMode, user.username) + } + } +} + func sb_build() string { return sb.String() } diff --git a/src_code/go_src/cocommit_test.go b/src_code/go_src/cocommit_test.go index 2cc4bb7..f6bb5a9 100644 --- a/src_code/go_src/cocommit_test.go +++ b/src_code/go_src/cocommit_test.go @@ -37,6 +37,36 @@ func Test_usersInput(t *testing.T) { } t.Fatalf("process ran with err %v, want exit status 1", err) } +//TODO: Turn this into a fuzz test + +func Test_commit_message(t *testing.T) { + //authors := make(map[string]user) + users["test"] = user{username: "test", email: "test"} + sb_author("test") + commit := sb_build() + if commit != "\nCo-authored-by: test " { + t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test ",commit) + } +} +//TODO: Turn this into a fuzz test +func Test_add_all(t *testing.T) { + for k := range users { + delete(users, k) + } + users["test1"] = user{username: "test1", email: "test1"} + users["test2"] = user{username: "test2", email: "test2"} + users["test3"] = user{username: "test3", email: "test3"} + all_flag = true + add_x_users([]string{}) + + commit := sb_build() + + if commit != "\nCo-authored-by: test \nCo-authored-by: test1 \nCo-authored-by: test2 \nCo-authored-by: test3 " { + t.Fatalf("String built incorrectly. Strings did not match: Created -> %s Expected -> Co-authored-by: test ",commit) + } +} + +