Skip to content

Commit

Permalink
(#24) Filter of subject length
Browse files Browse the repository at this point in the history
  • Loading branch information
llorllale committed Mar 2, 2019
1 parent 4be918f commit e67fc01
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
5 changes: 1 addition & 4 deletions cmd/go-gitlint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ func main() {
}

func configure() {
var args []string
if len(os.Args) > 1 {
args = append(args, os.Args[1:]...)
}
args := os.Args[1:]
const file = ".gitlint"
if _, err := os.Stat(file); err == nil {
config, err := kingpin.ExpandArgsFromFile(file)
Expand Down
22 changes: 20 additions & 2 deletions internal/issues/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ import (
)

var (
subjectRegex = kingpin.Flag("subject-regex", "Filters commit subjects based on a regular expression.").String() //nolint[gochecknoglobals]
bodyRegex = kingpin.Flag("body-regex", "Filters commit message bodies based on a regular expression.").String() //nolint[gochecknoglobals]
subjectRegex = kingpin.Flag("subject-regex", "Filters commit subjects based on a regular expression.").String() //nolint[gochecknoglobals]
subjectLength = kingpin.Flag("subject-len", "Filters commit subjects based on length.").Int() //nolint[gochecknoglobals]
bodyRegex = kingpin.Flag("body-regex", "Filters commit message bodies based on a regular expression.").String() //nolint[gochecknoglobals]
)

// Filter identifies an issue with a commit.
Expand All @@ -41,6 +42,9 @@ func Filters() []Filter {
if bodyRegex != nil {
filters = append(filters, OfBodyRegex(*bodyRegex))
}
if subjectLength != nil && *subjectLength > 0 {
filters = append(filters, OfSubjectLength(*subjectLength))
}
return filters
}

Expand Down Expand Up @@ -79,3 +83,17 @@ func OfBodyRegex(regex string) Filter {
return issue
}
}

// OfSubjectLength tests a commit subject's length.
func OfSubjectLength(length int) Filter {
return func(c *commits.Commit) Issue {
var issue Issue
if len(c.Subject()) > length {
issue = Issue{
Desc: fmt.Sprintf("subject is longer than %d", length),
Commit: *c,
}
}
return issue
}
}
22 changes: 22 additions & 0 deletions internal/issues/filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,25 @@ func TestOfBodyRegexNonMatch(t *testing.T) {
"filter.OfBodyRegex() must not match if the commit's subject does not match the regex",
)
}

func TestOfSubjectLengthMatch(t *testing.T) {
assert.NotZero(t,
OfSubjectLength(5)(
&commits.Commit{
Message: "very very very VERY long subject\n\nand body",
},
),
"filter.OfSubjectLength() must match if the commit's subject is too long",
)
}

func TestOfSubjectLengthNonMatch(t *testing.T) {
assert.Zero(t,
OfSubjectLength(10)(
&commits.Commit{
Message: "short\n\nmessage",
},
),
"filter.OfSubjectLength() must not match if the commit's subject is not too long",
)
}

0 comments on commit e67fc01

Please sign in to comment.