diff --git a/cmd/edit_test.go b/cmd/edit_test.go index 9360c5c..abf6bfe 100644 --- a/cmd/edit_test.go +++ b/cmd/edit_test.go @@ -140,11 +140,21 @@ func TestConstructCommitMessageNoEmoji(t *testing.T) { NoEmoji: true, } - result := constructCommitMessage(cfg, "feat", "", "add feature") - expected := "feat: add feature" - if result != expected { - t.Errorf("Expected %q, got %q", expected, result) - } + t.Run("without scope", func(t *testing.T) { + result := constructCommitMessage(cfg, "feat", "", "add feature") + expected := "feat: add feature" + if result != expected { + t.Errorf("Expected %q, got %q", expected, result) + } + }) + + t.Run("with scope", func(t *testing.T) { + result := constructCommitMessage(cfg, "feat", "lang", "add Polish language") + expected := "feat(lang): add Polish language" + if result != expected { + t.Errorf("Expected %q, got %q", expected, result) + } + }) } // TestContainsHelper tests the contains helper function @@ -349,7 +359,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) { // Test parsing logic with malformed input malformedOutput := "sha1\nsubject\n---COMMIT-END---\nsha2\n---COMMIT-END---" rawCommits := strings.Split(strings.TrimSpace(malformedOutput), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { @@ -379,7 +389,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) { t.Run("empty git log output", func(t *testing.T) { emptyOutput := "" rawCommits := strings.Split(strings.TrimSpace(emptyOutput), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { @@ -406,7 +416,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) { t.Run("commit with body", func(t *testing.T) { outputWithBody := "sha123\nsubject line\nbody content\n---COMMIT-END---" rawCommits := strings.Split(strings.TrimSpace(outputWithBody), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { @@ -436,7 +446,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) { t.Run("commit without body", func(t *testing.T) { outputWithoutBody := "sha123\nsubject line\n---COMMIT-END---" rawCommits := strings.Split(strings.TrimSpace(outputWithoutBody), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { @@ -657,7 +667,7 @@ func TestGetRecentCommits_ErrorPaths(t *testing.T) { // Test that commits with body are parsed correctly output := "sha123\nsubject line\nbody content\n---COMMIT-END---" rawCommits := strings.Split(strings.TrimSpace(output), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { @@ -688,7 +698,7 @@ func TestGetRecentCommits_ErrorPaths(t *testing.T) { // Test that commits without body are parsed correctly output := "sha123\nsubject line\n---COMMIT-END---" rawCommits := strings.Split(strings.TrimSpace(output), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { @@ -719,7 +729,7 @@ func TestGetRecentCommits_ErrorPaths(t *testing.T) { // Test that malformed commits are skipped malformedOutput := "sha1\n---COMMIT-END---\nsha2\nsubject\n---COMMIT-END---" rawCommits := strings.Split(strings.TrimSpace(malformedOutput), "---COMMIT-END---") - + var commits []GitCommit for _, rawCommit := range rawCommits { if strings.TrimSpace(rawCommit) == "" { diff --git a/cmd/root.go b/cmd/root.go index 6d9613c..955e87c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -104,10 +104,12 @@ func init() { func constructCommitMessage(cfg *config.Config, typeFlag, scopeFlag, messageFlag string) string { typeMatch := typeFlag + emoji := "" for _, t := range cfg.Types { if typeFlag == t.Name { if !cfg.NoEmoji { - typeMatch = fmt.Sprintf("%s %s", t.Name, t.Emoji) + typeMatch = t.Name + emoji = t.Emoji break } else { typeMatch = t.Name @@ -117,8 +119,11 @@ func constructCommitMessage(cfg *config.Config, typeFlag, scopeFlag, messageFlag } commitHeader := typeMatch + if emoji != "" { + commitHeader += " " + emoji + } if scopeFlag != "" { - if !cfg.NoEmoji { + if emoji != "" { commitHeader += fmt.Sprintf(" (%s)", scopeFlag) } else { commitHeader += fmt.Sprintf("(%s)", scopeFlag) diff --git a/pkg/utils/askQuestions.go b/pkg/utils/askQuestions.go index ac2b0c4..87f0c0b 100644 --- a/pkg/utils/askQuestions.go +++ b/pkg/utils/askQuestions.go @@ -15,7 +15,9 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st // Validate that Types is not empty if len(config.Types) == 0 { - return nil, fmt.Errorf("no commit types configured. Please run 'goji init --global' or 'goji init --repo' to initialize your configuration") + return nil, fmt.Errorf( + "no commit types configured. Please run 'goji init --global' or 'goji init --repo' to initialize your configuration", + ) } nameStyle := lipgloss.NewStyle().Width(15).Align(lipgloss.Left) @@ -28,12 +30,13 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st nameStyle.Render(ct.Name), emojiStyle.Render(ct.Emoji), descStyle.Render(ct.Description)) - commitTypeOptions[i] = huh.NewOption[string](row, fmt.Sprintf("%s %s", ct.Name, func() string { - if !config.NoEmoji { - return ct.Emoji - } - return "" - }())) + + // Build value: "type emoji" or just "type" if NoEmoji + value := ct.Name + if !config.NoEmoji && ct.Emoji != "" { + value = fmt.Sprintf("%s %s", ct.Name, ct.Emoji) + } + commitTypeOptions[i] = huh.NewOption[string](row, value) } if presetType != "" { @@ -99,7 +102,12 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st commitMessage := commitType if commitScope != "" { - commitMessage += fmt.Sprintf(" (%s)", strings.TrimSpace(commitScope)) + // Add space before scope if emoji is present (commitType would be "type emoji") + if !config.NoEmoji && strings.Contains(commitType, " ") { + commitMessage += fmt.Sprintf(" (%s)", strings.TrimSpace(commitScope)) + } else { + commitMessage += fmt.Sprintf("(%s)", strings.TrimSpace(commitScope)) + } } commitMessage += fmt.Sprintf(": %s", strings.TrimSpace(commitSubject))