Skip to content

Commit

Permalink
Merge pull request #37 from rockstaedt/36-amount-of-characters-after-…
Browse files Browse the repository at this point in the history
…soft-limit-is-wrong

Resolve "Amount of characters after soft limit is wrong"
  • Loading branch information
rockstaedt authored Aug 4, 2023
2 parents 146f6f1 + d658d42 commit 8f65978
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 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
22 changes: 10 additions & 12 deletions internal/model/commit_message.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package model

import (
"regexp"
"strings"
)

type Subject []rune

type CommitMessage struct {
Subject string
Subject Subject
Body []string
InvalidBody bool
}
Expand All @@ -22,27 +23,24 @@ func CreateCommitMessageFrom(messageLines []string) *CommitMessage {
func (cm *CommitMessage) ValidateSubject() int {
currentSubjectLength := len(cm.Subject)

if strings.HasPrefix(cm.Subject, "Merge ") {
if strings.HasPrefix(cm.Subject.String(), "Merge ") {
return 0
}

if currentSubjectLength > 72 {
return currentSubjectLength - 50
}

if currentSubjectLength > 50 {
re := regexp.MustCompile(`^#\d+ -\s*(.*)$`)
trimmedSubject := re.ReplaceAllString(cm.Subject, `$1`)

return len(trimmedSubject) - 50
return currentSubjectLength - 50
}

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
9 changes: 5 additions & 4 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 All @@ -62,10 +62,11 @@ func TestNewCommitMessage(t *testing.T) {
}{
{"more than................72....................................characters", 23},
{"more than................50................less than 72 characters", 16},
{"#1301 - more than........50..............through ID prefix", 0},
{"#1301 - more than........50..............through ID prefix", 8},
{"short subject line", 0},
{"Merge pull request commits are ignored because they can easily exceed 52 characters", 0},
{"Merge branch commits are also ignored..............................................", 0},
{"I am a commit containing an umlaut ü.................", 3},
}

for _, tc := range testcases {
Expand Down

0 comments on commit 8f65978

Please sign in to comment.