|
1 | 1 | package codeowners
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "strings" |
4 | 5 | "testing"
|
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert"
|
7 | 8 | )
|
8 | 9 |
|
| 10 | +func TestParseFile(t *testing.T) { |
| 11 | + examples := []struct { |
| 12 | + name string |
| 13 | + contents string |
| 14 | + expected Ruleset |
| 15 | + err string |
| 16 | + }{ |
| 17 | + // Success cases |
| 18 | + { |
| 19 | + name: "empty file", |
| 20 | + contents: "", |
| 21 | + expected: Ruleset{}, |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "single rule", |
| 25 | + contents: "file.txt @user", |
| 26 | + expected: Ruleset{ |
| 27 | + { |
| 28 | + pattern: mustBuildPattern(t, "file.txt"), |
| 29 | + Owners: []Owner{{Value: "user", Type: "username"}}, |
| 30 | + LineNumber: 1, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "multiple rules", |
| 36 | + contents: "file.txt @user\nfile2.txt @org/team", |
| 37 | + expected: Ruleset{ |
| 38 | + { |
| 39 | + pattern: mustBuildPattern(t, "file.txt"), |
| 40 | + Owners: []Owner{{Value: "user", Type: "username"}}, |
| 41 | + LineNumber: 1, |
| 42 | + }, |
| 43 | + { |
| 44 | + pattern: mustBuildPattern(t, "file2.txt"), |
| 45 | + Owners: []Owner{{Value: "org/team", Type: "team"}}, |
| 46 | + LineNumber: 2, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + |
| 51 | + // Error cases |
| 52 | + { |
| 53 | + name: "malformed rule", |
| 54 | + contents: "malformed rule\n", |
| 55 | + err: "line 1: invalid owner format 'rule' at position 11", |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + for _, e := range examples { |
| 60 | + t.Run("parses "+e.name, func(t *testing.T) { |
| 61 | + reader := strings.NewReader(e.contents) |
| 62 | + actual, err := ParseFile(reader) |
| 63 | + if e.err != "" { |
| 64 | + assert.EqualError(t, err, e.err) |
| 65 | + } else { |
| 66 | + assert.NoError(t, err) |
| 67 | + assert.Equal(t, e.expected, actual) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
9 | 73 | func TestParseRule(t *testing.T) {
|
10 | 74 | examples := []struct {
|
11 | 75 | name string
|
|
0 commit comments