Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Response to code review comments: refactors getting author logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tamara Kaufler committed Sep 13, 2017
1 parent 259a03c commit b71d165
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
15 changes: 6 additions & 9 deletions cmd/fluxctl/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down
27 changes: 23 additions & 4 deletions cmd/fluxctl/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,35 @@ 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
pull.ff=only
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
Expand All @@ -49,25 +62,27 @@ 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 := `
`
expected := make(map[string]string)

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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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": "",
Expand Down

0 comments on commit b71d165

Please sign in to comment.