-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
348 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2019 George Aristy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package filter | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/llorllale/go-gitlint/internal/commits" | ||
"github.com/llorllale/go-gitlint/internal/commits/issues" | ||
) | ||
|
||
/* | ||
Filters identify issues with a commit. | ||
A filter returning a zero-valued Issue signals that it found no issue | ||
with the commit. | ||
*/ | ||
|
||
// OfSubjectRegex tests a commit's subject with the regex. | ||
func OfSubjectRegex(regex string) func(*commits.Commit) issues.Issue { | ||
return func(c *commits.Commit) issues.Issue { | ||
var issue issues.Issue | ||
matched, err := regexp.MatchString(regex, c.Subject()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if !matched { | ||
issue = issues.Issue{ | ||
Desc: fmt.Sprintf("subject does not match regex [%s]", regex), | ||
Commit: *c, | ||
} | ||
} | ||
return issue | ||
} | ||
} | ||
|
||
// OfBodyRegex tests a commit's body with the regex. | ||
func OfBodyRegex(regex string) func(*commits.Commit) issues.Issue { | ||
return func(c *commits.Commit) issues.Issue { | ||
var issue issues.Issue | ||
matched, err := regexp.MatchString(regex, c.Body()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if !matched { | ||
issue = issues.Issue{ | ||
Desc: fmt.Sprintf("body does not conform to regex [%s]", regex), | ||
Commit: *c, | ||
} | ||
} | ||
return issue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2019 George Aristy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package filter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/llorllale/go-gitlint/internal/commits" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOfSubjectRegexMatch(t *testing.T) { | ||
assert.Zero(t, | ||
OfSubjectRegex(`\(#\d+\) [\w ]{10,50}`)( | ||
&commits.Commit{ | ||
Message: "(#123) This is a good subject", | ||
}, | ||
), | ||
"filter.OfSubjectRegex() must match if the commit's subject matches the regex", | ||
) | ||
} | ||
|
||
func TestOfSubjectRegexNonMatch(t *testing.T) { | ||
assert.NotZero(t, | ||
OfSubjectRegex(`\(#\d+\) [\w ]{,50}`)( | ||
&commits.Commit{ | ||
Message: "I break all the rules!", | ||
}, | ||
), | ||
"filter.OfSubjectRegex() must not match if the commit's subject does not match the regex", | ||
) | ||
} | ||
|
||
func TestOfBodyRegexMatch(t *testing.T) { | ||
assert.Zero(t, | ||
OfBodyRegex(`^.{10,20}$`)( | ||
&commits.Commit{ | ||
Message: "subject\n\nBetween 10 and 20", | ||
}, | ||
), | ||
"filter.OfBodyRegex() must match if the commit's subject matches the regex", | ||
) | ||
} | ||
|
||
func TestOfBodyRegexNonMatch(t *testing.T) { | ||
assert.NotZero(t, | ||
OfBodyRegex(`^.{10,20}$`)( | ||
&commits.Commit{ | ||
Message: "subject\n\nMore than twenty characters!", | ||
}, | ||
), | ||
"filter.OfBodyRegex() must not match if the commit's subject does not match the regex", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2019 George Aristy | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package issues | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
|
||
"github.com/llorllale/go-gitlint/internal/commits" | ||
) | ||
|
||
// Issue is a problem found with a commit. | ||
type Issue struct { | ||
Desc string | ||
Commit commits.Commit | ||
} | ||
|
||
func (i *Issue) String() string { | ||
return fmt.Sprintf("Issue{Desc=%s Commit=%+v}", i.Desc, i.Commit) | ||
} | ||
|
||
// Issues is a collection of Issues. | ||
type Issues func() []Issue | ||
|
||
// Collected returns a collection of issues identified. | ||
func Collected(filters []func(c *commits.Commit) Issue, cmts commits.Commits) Issues { | ||
return func() []Issue { | ||
issues := make([]Issue, 0) | ||
for _, c := range cmts() { | ||
for _, f := range filters { | ||
if issue := f(c); issue != (Issue{}) { | ||
issues = append(issues, issue) | ||
break | ||
} | ||
} | ||
} | ||
return issues | ||
} | ||
} | ||
|
||
// Printed prints the issues to the writer. | ||
func Printed(w io.Writer, sep string, issues Issues) Issues { | ||
return func() []Issue { | ||
iss := issues() | ||
for i := range iss { | ||
_, err := w.Write( | ||
[]byte(fmt.Sprintf("%s%s", iss[i].String(), sep)), | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
return iss | ||
} | ||
} |
Oops, something went wrong.
b52f151
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
16-e0d2cf8f
discovered incheck_coverage.sh
and submitted as #18. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.