diff --git a/cmd/fluxctl/args.go b/cmd/fluxctl/args.go index 52678874f1..c574d9b9ba 100644 --- a/cmd/fluxctl/args.go +++ b/cmd/fluxctl/args.go @@ -30,16 +30,13 @@ func getCommitAuthor(authorInfo map[string]string) string { userName := authorInfo["user.name"] userEmail := authorInfo["user.email"] - switch userName { - case "": - if userEmail != "" { - return userEmail - } - default: - if userEmail == "" { - return userName - } + switch { + case userName != "" && userEmail != "": return fmt.Sprintf("%s <%s>", userName, userEmail) + case userEmail != "": + return userEmail + case userName != "": + return userName } return "" } diff --git a/cmd/fluxctl/args_test.go b/cmd/fluxctl/args_test.go index 93a6735365..f5af3c8b16 100644 --- a/cmd/fluxctl/args_test.go +++ b/cmd/fluxctl/args_test.go @@ -9,9 +9,10 @@ func TestUserGitconfigMap_EmptyString(t *testing.T) { d := "" userGitconfigInfo := userGitconfigMap(d) if len(userGitconfigInfo) != 0 { - t.Fatal("expected no content") + t.Fatal("expected map with no keys") } } + func TestUserGitconfigMap(t *testing.T) { d := `push.default=simple merge.conflictstyle=diff3 @@ -19,12 +20,24 @@ func TestUserGitconfigMap(t *testing.T) { core.repositoryformatversion=0 core.filemode=true core.bare=false` + expected := map[string]string{ + "push.default": "simple", + "merge.conflictstyle": "diff3", + "pull.ff": "only", + "core.repositoryformatversion": "0", + "core.filemode": "true", + "core.bare": "false", + } userGitconfigInfo := userGitconfigMap(d) if len(userGitconfigInfo) != 6 { - t.Fatal("expected no content") + t.Fatal("got map with unexpected number of keys") + } + if !reflect.DeepEqual(userGitconfigInfo, expected) { + t.Fatal("result does not match expected structure") } } + func TestUserGitconfigMap_WithEmptyLines(t *testing.T) { d := ` user.name=Jane Doe @@ -49,12 +62,13 @@ func TestUserGitconfigMap_WithEmptyLines(t *testing.T) { userGitconfigInfo := userGitconfigMap(d) if len(userGitconfigInfo) != 7 { - t.Fatal("expected map with 1 key") + t.Fatal("got map with unexpected number of keys") } if !reflect.DeepEqual(userGitconfigInfo, expected) { t.Fatal("result does not match expected structure") } } + func TestUserGitconfigMap_WithNoKeys(t *testing.T) { d := ` ` @@ -62,12 +76,13 @@ func TestUserGitconfigMap_WithNoKeys(t *testing.T) { userGitconfigInfo := userGitconfigMap(d) if len(userGitconfigInfo) != 0 { - t.Fatal("expected map with one key") + t.Fatal("expected map with no keys") } if !reflect.DeepEqual(userGitconfigInfo, expected) { t.Fatal("result does not match expected structure") } } + func TestGetCommitAuthor_BothNameAndEmail(t *testing.T) { input := map[string]string{ "user.name": "Jane Doe", @@ -85,6 +100,7 @@ func TestGetCommitAuthor_BothNameAndEmail(t *testing.T) { t.Fatal("author did not match expected value") } } + func TestGetCommitAuthor_OnlyName(t *testing.T) { input := map[string]string{ "user.name": "Jane Doe", @@ -118,6 +134,7 @@ func TestGetCommitAuthor_OnlyEmail(t *testing.T) { t.Fatal("author did not match expected value") } } + func TestGetCommitAuthor_NoNameNoEmail(t *testing.T) { input := map[string]string{ "push.default": "simple", @@ -133,6 +150,7 @@ func TestGetCommitAuthor_NoNameNoEmail(t *testing.T) { t.Fatal("author did not match expected value") } } + func TestGetCommitAuthor_NameAndEmptyEmail(t *testing.T) { input := map[string]string{ "user.name": "Jane Doe", @@ -150,6 +168,7 @@ func TestGetCommitAuthor_NameAndEmptyEmail(t *testing.T) { t.Fatal("author did not match expected value") } } + func TestGetCommitAuthor_EmailAndEmptyName(t *testing.T) { input := map[string]string{ "user.name": "",