Skip to content

Commit 0c55fca

Browse files
authored
Moving from codeowners.New to codeowners.NewDecoder (#4)
1 parent deb9489 commit 0c55fca

3 files changed

Lines changed: 50 additions & 13 deletions

File tree

codeowners.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,33 @@ var DefaultLocations = [...]string{"CODEOWNERS", "docs/CODEOWNERS", ".github/COD
1313
// Decoder providers functionality to read CODEOWNERS data
1414
type Decoder struct {
1515
scanner *bufio.Scanner
16-
line *string
16+
line string
1717
done bool
1818
}
1919

20-
// New generates a new CodeOwnersScanner instance. The reader should be a reader containing the contents of the CODEOWNERS file
21-
func New(r io.Reader) *Decoder {
20+
// NewDecoder generates a new CodeOwnersScanner instance. The reader should be a reader containing the contents of the CODEOWNERS file
21+
func NewDecoder(r io.Reader) *Decoder {
2222
return &Decoder{
2323
scanner: bufio.NewScanner(r),
24-
line: nil,
24+
line: "",
2525
done: false,
2626
}
2727
}
2828

29+
// peek will scan the next line
2930
func (s *Decoder) peek() {
3031
if !s.scanner.Scan() {
3132
s.done = true
33+
return
3234
}
3335
line := sanitiseLine(s.scanner.Text())
34-
s.line = &line
35-
if len(*s.line) == 0 && !s.done {
36+
s.line = line
37+
if len(s.line) == 0 && !s.done {
3638
s.peek()
3739
}
3840
}
3941

42+
// sanitiseLine removes all empty space and comments from a given line
4043
func sanitiseLine(line string) string {
4144
i := strings.Index(line, "#")
4245
if i >= 0 {
@@ -45,15 +48,18 @@ func sanitiseLine(line string) string {
4548
return strings.Trim(line, " ")
4649
}
4750

48-
// More returns if there are available CODEOWNERS lines to be scanned
51+
// More returns true if there are available CODEOWNERS lines to be scanned.
52+
// And also advances to the next line.
4953
func (s *Decoder) More() bool {
5054
s.peek()
5155
return !s.done
5256
}
5357

54-
// Token parses the next available line in the CODEOWNERS file
58+
// Token parses the next available line in the CODEOWNERS file.
59+
// If More was never called it will return an empty token.
60+
// After end of file Token will always return the last line.
5561
func (s *Decoder) Token() Token {
56-
line := strings.ReplaceAll(*s.line, "\\ ", "\\s")
62+
line := strings.ReplaceAll(s.line, "\\ ", "\\s")
5763

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

codeowners_test.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import (
99
)
1010

1111
func exec(input string) ([][]string, int) {
12-
scanner := codeowners.New(strings.NewReader(input))
12+
decoder := codeowners.NewDecoder(strings.NewReader(input))
1313
got := [][]string{}
1414
c := 0
15-
for scanner.More() {
15+
for decoder.More() {
1616
c++
17-
token := scanner.Token()
17+
token := decoder.Token()
1818
got = append(got, append([]string{token.Path()}, token.Owners()...))
1919
}
2020
return got, c
@@ -90,3 +90,34 @@ func TestNoOwners(t *testing.T) {
9090
{"*"},
9191
})
9292
}
93+
94+
func TestLastToken(t *testing.T) {
95+
decoder := codeowners.NewDecoder(strings.NewReader(`filepattern @owner`))
96+
if !decoder.More() {
97+
t.Error("More should be true")
98+
}
99+
for i := 0; i < 3; i++ { //calling 3 times to prove it always returns the last line
100+
token := decoder.Token()
101+
if token.Path() != "filepattern" {
102+
t.Error("Path should be 'filepattern'")
103+
}
104+
if len(token.Owners()) != 1 || token.Owners()[0] != "@owner" {
105+
t.Error("Owners should match ['@owner']")
106+
}
107+
108+
if decoder.More() {
109+
t.Error("More should be false")
110+
}
111+
}
112+
}
113+
114+
func TestMoreNotCalled(t *testing.T) {
115+
decoder := codeowners.NewDecoder(strings.NewReader(`filepattern @owner`))
116+
token := decoder.Token()
117+
if token.Path() != "" {
118+
t.Error("Path should be empty")
119+
}
120+
if len(token.Owners()) != 0 {
121+
t.Error("Owners should be empty")
122+
}
123+
}

example_codeowners_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
)
99

1010
func Example() {
11-
codeownerDecoder := codeowners.New(strings.NewReader(`* test@example.org`))
11+
codeownerDecoder := codeowners.NewDecoder(strings.NewReader(`* test@example.org`))
1212
for codeownerDecoder.More() {
1313
token := codeownerDecoder.Token()
1414
fmt.Printf("File Pattern: %s\n", token.Path())

0 commit comments

Comments
 (0)