Skip to content

Commit

Permalink
Add parse line (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmenezes authored Jul 24, 2020
1 parent 54479a1 commit fc2fc0a
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 11 deletions.
25 changes: 25 additions & 0 deletions codeowners.go
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
}
}
59 changes: 59 additions & 0 deletions codeowners_test.go
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)
}
}
}
13 changes: 3 additions & 10 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,11 @@ func (d *Decoder) More() bool {
// If More was never called it will return an empty token.
// After end of file Token will always return the last line.
func (d *Decoder) Token() (Token, int) {
line := sanitiseLine(d.line)
line = strings.ReplaceAll(line, "\\ ", "\\s")

data := strings.Split(line, " ")

for i := range data {
data[i] = strings.ReplaceAll(data[i], "\\s", " ")
}
pattern, owners := ParseLine(d.line)

return Token{
path: data[0],
owners: data[1:],
path: pattern,
owners: owners,
}, d.lineNo
}

Expand Down
2 changes: 1 addition & 1 deletion decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestMultipleOwners(t *testing.T) {

func TestFilesWithSpaces(t *testing.T) {
assert(t, `file\ with\ spaces @owner`, [][]string{
{"1", "file with spaces", "@owner"},
{"1", "file\\ with\\ spaces", "@owner"},
})
}

Expand Down

0 comments on commit fc2fc0a

Please sign in to comment.