-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
88 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,30 @@ | ||
// Package codeowners provides funcionality to evaluate CODEOWNERS file. | ||
package codeowners // import "github.com/fmenezes/codeowners" | ||
|
||
import ( | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// DefaultLocations provides default locations for the CODEOWNERS file | ||
var DefaultLocations = [...]string{"CODEOWNERS", "docs/CODEOWNERS", ".github/CODEOWNERS"} | ||
|
||
// ParseLine parses a CODEOWNERS line into file pattern and owners | ||
func ParseLine(line string) (string, []string) { | ||
line = sanitiseLine(line) | ||
|
||
var previousRune rune | ||
data := strings.FieldsFunc(line, func(r rune) bool { | ||
result := unicode.IsSpace(r) && previousRune != '\\' | ||
previousRune = r | ||
return result | ||
}) | ||
|
||
if len(data) > 1 { | ||
return data[0], data[1:] | ||
} else if len(data) == 1 { | ||
return data[0], nil | ||
} else { | ||
return "", nil | ||
} | ||
} |
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,59 @@ | ||
package codeowners_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/fmenezes/codeowners" | ||
) | ||
|
||
func TestParseLine(t *testing.T) { | ||
testCases := []struct { | ||
input string | ||
wantPattern string | ||
wantOwners []string | ||
}{ | ||
{ | ||
input: "* test@example.org", | ||
wantPattern: "*", | ||
wantOwners: []string{"test@example.org"}, | ||
}, | ||
{ | ||
input: "filepattern test@example.org @owner @company/team", | ||
wantPattern: "filepattern", | ||
wantOwners: []string{"test@example.org", "@owner", "@company/team"}, | ||
}, | ||
{ | ||
input: "file\\ with\\ spaces @owner", | ||
wantPattern: "file\\ with\\ spaces", | ||
wantOwners: []string{"@owner"}, | ||
}, | ||
{ | ||
input: " filepattern ", | ||
wantPattern: "filepattern", | ||
wantOwners: nil, | ||
}, | ||
{ | ||
input: "filepattern @owner # comments", | ||
wantPattern: "filepattern", | ||
wantOwners: []string{"@owner"}, | ||
}, | ||
{ | ||
input: "", | ||
wantPattern: "", | ||
wantOwners: nil, | ||
}, | ||
{ | ||
input: "# only comments on the line", | ||
wantPattern: "", | ||
wantOwners: nil, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
gotPattern, gotOwners := codeowners.ParseLine(testCase.input) | ||
if gotPattern != testCase.wantPattern || !reflect.DeepEqual(gotOwners, testCase.wantOwners) { | ||
t.Errorf("Input: %s, Want: %s, %v, Got: %s, %v", testCase.input, testCase.wantPattern, testCase.wantOwners, gotPattern, gotOwners) | ||
} | ||
} | ||
} |
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