Skip to content

Commit

Permalink
Add support to line numbers (#7)
Browse files Browse the repository at this point in the history
* Buffer the original line

* Add lines
  • Loading branch information
fmenezes authored Jul 21, 2020
1 parent f42c408 commit eec2566
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
17 changes: 11 additions & 6 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type Decoder struct {
scanner *bufio.Scanner
line string
lineNo int
done bool
}

Expand All @@ -18,6 +19,7 @@ func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
scanner: bufio.NewScanner(r),
line: "",
lineNo: 0,
done: false,
}
}
Expand All @@ -28,9 +30,11 @@ func (d *Decoder) peek() {
d.done = true
return
}
line := sanitiseLine(d.scanner.Text())
d.line = line
if len(d.line) == 0 && !d.done {

d.line = d.scanner.Text()
line := sanitiseLine(d.line)
d.lineNo++
if len(line) == 0 && !d.done {
d.peek()
}
}
Expand All @@ -54,8 +58,9 @@ func (d *Decoder) More() bool {
// Token parses the next available line in the CODEOWNERS file.
// 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 {
line := strings.ReplaceAll(d.line, "\\ ", "\\s")
func (d *Decoder) Token() (Token, int) {
line := sanitiseLine(d.line)
line = strings.ReplaceAll(line, "\\ ", "\\s")

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

Expand All @@ -66,7 +71,7 @@ func (d *Decoder) Token() Token {
return Token{
path: data[0],
owners: data[1:],
}
}, d.lineNo
}

// Token providers reading capabilities for every CODEOWNERS line
Expand Down
37 changes: 22 additions & 15 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codeowners_test
import (
"fmt"
"reflect"
"strconv"
"strings"
"testing"

Expand All @@ -15,8 +16,8 @@ func exec(input string) ([][]string, int) {
c := 0
for decoder.More() {
c++
token := decoder.Token()
got = append(got, append([]string{token.Path()}, token.Owners()...))
token, line := decoder.Token()
got = append(got, append([]string{strconv.Itoa(line), token.Path()}, token.Owners()...))
}
return got, c
}
Expand All @@ -33,27 +34,27 @@ func assert(t *testing.T, input string, want [][]string) {

func TestSimple(t *testing.T) {
assert(t, `* test@example.org`, [][]string{
{"*", "test@example.org"},
{"1", "*", "test@example.org"},
})
}

func TestMultipleOwners(t *testing.T) {
assert(t, `* test@example.org @owner @company/team`, [][]string{
{"*", "test@example.org", "@owner", "@company/team"},
{"1", "*", "test@example.org", "@owner", "@company/team"},
})
}

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

func TestMultipleLines(t *testing.T) {
assert(t, `* test@example.org
file @owner`, [][]string{
{"*", "test@example.org"},
{"file", "@owner"},
{"1", "*", "test@example.org"},
{"2", "file", "@owner"},
})
}

Expand All @@ -72,23 +73,23 @@ file @owner
`, [][]string{
{"*", "test@example.org"},
{"file", "@owner"},
{"1", "*", "test@example.org"},
{"5", "file", "@owner"},
})
}

func TestIgnoreComments(t *testing.T) {
assert(t, `* test@example.org # comment
# comment
file @owner`, [][]string{
{"*", "test@example.org"},
{"file", "@owner"},
{"1", "*", "test@example.org"},
{"3", "file", "@owner"},
})
}

func TestNoOwners(t *testing.T) {
assert(t, `*`, [][]string{
{"*"},
{"1", "*"},
})
}

Expand All @@ -98,7 +99,10 @@ func TestLastToken(t *testing.T) {
t.Error("More should be true")
}
for i := 0; i < 3; i++ { //calling 3 times to prove it always returns the last line
token := decoder.Token()
token, line := decoder.Token()
if line != 1 {
t.Error("Line should be '1'")
}
if token.Path() != "filepattern" {
t.Error("Path should be 'filepattern'")
}
Expand All @@ -114,7 +118,7 @@ func TestLastToken(t *testing.T) {

func TestMoreNotCalled(t *testing.T) {
decoder := codeowners.NewDecoder(strings.NewReader(`filepattern @owner`))
token := decoder.Token()
token, _ := decoder.Token()
if token.Path() != "" {
t.Error("Path should be empty")
}
Expand All @@ -127,13 +131,16 @@ func ExampleDecoder() {
decoder := codeowners.NewDecoder(strings.NewReader(`* test@example.org
filepattern @owner`))
for decoder.More() {
token := decoder.Token()
token, line := decoder.Token()
fmt.Printf("Line: %d\n", line)
fmt.Printf("File Pattern: %s\n", token.Path())
fmt.Printf("Owners: %v\n", token.Owners())
}
// Output:
// Line: 1
// File Pattern: *
// Owners: [test@example.org]
// Line: 2
// File Pattern: filepattern
// Owners: [@owner]
}

0 comments on commit eec2566

Please sign in to comment.