|
| 1 | +package codeowners_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/fmenezes/codeowners" |
| 9 | +) |
| 10 | + |
| 11 | +func exec(input string) ([][]string, int) { |
| 12 | + scanner := codeowners.New(strings.NewReader(input)) |
| 13 | + got := [][]string{} |
| 14 | + c := 0 |
| 15 | + for scanner.More() { |
| 16 | + c++ |
| 17 | + token := scanner.Token() |
| 18 | + got = append(got, append([]string{token.Path()}, token.Owners()...)) |
| 19 | + } |
| 20 | + return got, c |
| 21 | +} |
| 22 | + |
| 23 | +func assert(t *testing.T, input string, want [][]string) { |
| 24 | + got, gotCount := exec(input) |
| 25 | + if !reflect.DeepEqual(got, want) { |
| 26 | + t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got) |
| 27 | + } |
| 28 | + if gotCount != len(want) { |
| 29 | + t.Errorf("Input: %v, Want: %v scans, Got: %v scans", input, len(want), gotCount) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestSimple(t *testing.T) { |
| 34 | + assert(t, `* test@example.org`, [][]string{ |
| 35 | + {"*", "test@example.org"}, |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +func TestMultipleOwners(t *testing.T) { |
| 40 | + assert(t, `* test@example.org @owner @company/team`, [][]string{ |
| 41 | + {"*", "test@example.org", "@owner", "@company/team"}, |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func TestFilesWithSpaces(t *testing.T) { |
| 46 | + assert(t, `file\ with\ spaces @owner`, [][]string{ |
| 47 | + {"file with spaces", "@owner"}, |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func TestMultipleLines(t *testing.T) { |
| 52 | + assert(t, `* test@example.org |
| 53 | +file @owner`, [][]string{ |
| 54 | + {"*", "test@example.org"}, |
| 55 | + {"file", "@owner"}, |
| 56 | + }) |
| 57 | +} |
| 58 | + |
| 59 | +func TestEmptyFile(t *testing.T) { |
| 60 | + assert(t, ``, [][]string{}) |
| 61 | +} |
| 62 | + |
| 63 | +func TestEmptyLines(t *testing.T) { |
| 64 | + assert(t, `* test@example.org |
| 65 | +
|
| 66 | +
|
| 67 | +
|
| 68 | +file @owner |
| 69 | +
|
| 70 | +
|
| 71 | +
|
| 72 | +
|
| 73 | +`, [][]string{ |
| 74 | + {"*", "test@example.org"}, |
| 75 | + {"file", "@owner"}, |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func TestIgnoreComments(t *testing.T) { |
| 80 | + assert(t, `* test@example.org # comment |
| 81 | +# comment |
| 82 | +file @owner`, [][]string{ |
| 83 | + {"*", "test@example.org"}, |
| 84 | + {"file", "@owner"}, |
| 85 | + }) |
| 86 | +} |
| 87 | + |
| 88 | +func TestNoOwners(t *testing.T) { |
| 89 | + assert(t, `*`, [][]string{ |
| 90 | + {"*"}, |
| 91 | + }) |
| 92 | +} |
0 commit comments