Skip to content

Commit

Permalink
Handle umlauts in marking exceeding characters
Browse files Browse the repository at this point in the history
  • Loading branch information
rockstaedt committed Aug 4, 2023
1 parent a360b1e commit d658d42
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions internal/model/commit_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"strings"
)

type Subject []rune

type CommitMessage struct {
Subject string
Subject Subject
Body []string
InvalidBody bool
}
Expand All @@ -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
}

Expand All @@ -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])
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/model/commit_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
})

Expand All @@ -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)
})

Expand Down

0 comments on commit d658d42

Please sign in to comment.