diff --git a/cmd/validate.go b/cmd/validate.go index 3690b2d..7934e38 100644 --- a/cmd/validate.go +++ b/cmd/validate.go @@ -33,7 +33,7 @@ func (h *Handler) validate() int { message := fmt.Sprintf("Your subject exceeds the soft limit of 50 chars by %d chars.", numOfExceedingChars) h.notify(message, "yellow") - h.notify(cm.Subject[:softLimit] + color.InYellow(cm.Subject[softLimit:])) + h.notify(cm.Subject[:softLimit].String() + color.InYellow(cm.Subject[softLimit:].String())) return 0 } diff --git a/cmd/validate_test.go b/cmd/validate_test.go index 3b54a86..16458b9 100644 --- a/cmd/validate_test.go +++ b/cmd/validate_test.go @@ -28,7 +28,7 @@ func TestValidate(t *testing.T) { t.Run("returns 0 when soft limit exceeds and logs a warning", func(t *testing.T) { buffer.Reset() testFile := t.TempDir() + "/text.txt" - err := os.WriteFile(testFile, []byte("i am two characters more thaaaaaaaaaaaaaaaaaaaaan 50"), 0666) + err := os.WriteFile(testFile, []byte("i am two characters more thäaaaaaaaaaaaaaaaaaaaan 50"), 0666) assert.Nil(t, err) handler := NewHandler(model.Config{CommitMsgFile: testFile}) handler.Writer = buffer @@ -37,7 +37,7 @@ func TestValidate(t *testing.T) { assert.Equal(t, status, 0) assert.Contains(t, buffer.String(), color.Yellow+"Your subject exceeds the soft limit of 50 chars by 2 chars.") - assert.Contains(t, buffer.String(), "i am two characters more thaaaaaaaaaaaaaaaaaaaaan "+color.Yellow+"50") + assert.Contains(t, buffer.String(), "i am two characters more thäaaaaaaaaaaaaaaaaaaaan "+color.Yellow+"50") }) t.Run("returns 1 when commit message too long", func(t *testing.T) { diff --git a/internal/model/commit_message.go b/internal/model/commit_message.go index 2d28a43..77748e3 100644 --- a/internal/model/commit_message.go +++ b/internal/model/commit_message.go @@ -4,8 +4,10 @@ import ( "strings" ) +type Subject []rune + type CommitMessage struct { - Subject string + Subject Subject Body []string InvalidBody bool } @@ -19,9 +21,9 @@ func CreateCommitMessageFrom(messageLines []string) *CommitMessage { } func (cm *CommitMessage) ValidateSubject() int { - currentSubjectLength := len([]rune(cm.Subject)) + currentSubjectLength := len(cm.Subject) - if strings.HasPrefix(cm.Subject, "Merge ") { + if strings.HasPrefix(cm.Subject.String(), "Merge ") { return 0 } @@ -32,9 +34,13 @@ func (cm *CommitMessage) ValidateSubject() int { return 0 } +func (s Subject) String() string { + return string(s) +} + func (cm *CommitMessage) addSubject(messageLines []string) { if len(messageLines) >= 1 { - cm.Subject = messageLines[0] + cm.Subject = []rune(messageLines[0]) } } diff --git a/internal/model/commit_message_test.go b/internal/model/commit_message_test.go index da532d7..9c2ef93 100644 --- a/internal/model/commit_message_test.go +++ b/internal/model/commit_message_test.go @@ -19,7 +19,7 @@ func TestNewCommitMessage(t *testing.T) { t.Run("create new commit message object from File", func(t *testing.T) { cm := CreateCommitMessageFrom(validCommitMsgLines) - assert.Equal(t, "I am a valid Subject with less than 50 characters", cm.Subject) + assert.Equal(t, "I am a valid Subject with less than 50 characters", cm.Subject.String()) assert.Len(t, cm.Body, 3) assert.False(t, cm.InvalidBody) }) @@ -28,7 +28,7 @@ func TestNewCommitMessage(t *testing.T) { var emptyCommitMsgLines []string cm := CreateCommitMessageFrom(emptyCommitMsgLines) - assert.Equal(t, "", cm.Subject) + assert.Equal(t, "", cm.Subject.String()) assert.Len(t, cm.Body, 0) }) @@ -41,7 +41,7 @@ func TestNewCommitMessage(t *testing.T) { cm := CreateCommitMessageFrom(invalidCommitMsgLines) t.Run("sets body correct", func(t *testing.T) { - assert.Equal(t, "subject line", cm.Subject) + assert.Equal(t, "subject line", cm.Subject.String()) assert.Len(t, cm.Body, 2) })