Skip to content

Commit

Permalink
tests for ParseFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hmarr committed Jun 26, 2024
1 parent 8e52b4a commit 477e3fd
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,75 @@
package codeowners

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestParseFile(t *testing.T) {
examples := []struct {
name string
contents string
expected Ruleset
err string
}{
// Success cases
{
name: "empty file",
contents: "",
expected: Ruleset{},
},
{
name: "single rule",
contents: "file.txt @user",
expected: Ruleset{
{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{{Value: "user", Type: "username"}},
LineNumber: 1,
},
},
},
{
name: "multiple rules",
contents: "file.txt @user\nfile2.txt @org/team",
expected: Ruleset{
{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{{Value: "user", Type: "username"}},
LineNumber: 1,
},
{
pattern: mustBuildPattern(t, "file2.txt"),
Owners: []Owner{{Value: "org/team", Type: "team"}},
LineNumber: 2,
},
},
},

// Error cases
{
name: "malformed rule",
contents: "malformed rule\n",
err: "line 1: invalid owner format 'rule' at position 11",
},
}

for _, e := range examples {
t.Run("parses "+e.name, func(t *testing.T) {
reader := strings.NewReader(e.contents)
actual, err := ParseFile(reader)
if e.err != "" {
assert.EqualError(t, err, e.err)
} else {
assert.NoError(t, err)
assert.Equal(t, e.expected, actual)
}
})
}
}

func TestParseRule(t *testing.T) {
examples := []struct {
name string
Expand Down

0 comments on commit 477e3fd

Please sign in to comment.