|
| 1 | +package codeowners_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/fmenezes/codeowners" |
| 10 | + _ "github.com/fmenezes/codeowners/checkers" |
| 11 | +) |
| 12 | + |
| 13 | +const dummyCheckerName string = "dummy" |
| 14 | + |
| 15 | +type dummyChecker struct { |
| 16 | +} |
| 17 | + |
| 18 | +func (c dummyChecker) CheckLine(lineNo int, filePath string, owners ...string) []codeowners.CheckResult { |
| 19 | + return []codeowners.CheckResult{ |
| 20 | + { |
| 21 | + LineNo: 1, |
| 22 | + Message: "Dummy Error", |
| 23 | + Severity: codeowners.Error, |
| 24 | + CheckName: dummyCheckerName, |
| 25 | + }, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func TestRegisterChecker(t *testing.T) { |
| 30 | + err := codeowners.RegisterChecker(dummyCheckerName, dummyChecker{}) |
| 31 | + if err != nil { |
| 32 | + t.Error(err) |
| 33 | + } |
| 34 | + found := false |
| 35 | + for _, checker := range codeowners.AvailableCheckers() { |
| 36 | + if checker == dummyCheckerName { |
| 37 | + found = true |
| 38 | + } |
| 39 | + } |
| 40 | + if !found { |
| 41 | + t.Errorf("%s not properly registered", dummyCheckerName) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestRegisterCheckerAgain(t *testing.T) { |
| 46 | + codeowners.RegisterChecker(dummyCheckerName, dummyChecker{}) |
| 47 | + err := codeowners.RegisterChecker(dummyCheckerName, dummyChecker{}) |
| 48 | + if err == nil { |
| 49 | + t.Errorf("%s should be already registered, expecting an error", dummyCheckerName) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestSeverityLevelLabels(t *testing.T) { |
| 54 | + if codeowners.Error.String() != "Error" { |
| 55 | + t.Errorf("codeowners.Error.String() should evaluate to 'Error'") |
| 56 | + } |
| 57 | + if codeowners.Warning.String() != "Warning" { |
| 58 | + t.Errorf("codeowners.Warning.String() should evaluate to 'Warning'") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestSimpleCheck(t *testing.T) { |
| 63 | + input := `filepattern @owner` |
| 64 | + want := []codeowners.CheckResult{ |
| 65 | + { |
| 66 | + LineNo: 1, |
| 67 | + Message: "Dummy Error", |
| 68 | + Severity: codeowners.Error, |
| 69 | + CheckName: dummyCheckerName, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + codeowners.RegisterChecker(dummyCheckerName, dummyChecker{}) |
| 74 | + got, err := codeowners.Check(strings.NewReader(input), dummyCheckerName) |
| 75 | + if err != nil { |
| 76 | + t.Errorf("Input %s, Error %v", input, err) |
| 77 | + } |
| 78 | + if !reflect.DeepEqual(want, got) { |
| 79 | + t.Errorf("Input %s, Want %v, Got %v", input, want, got) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func ExampleCheck() { |
| 84 | + contents := strings.NewReader(`filepattern`) |
| 85 | + checks, err := codeowners.Check(contents, "NoOwner") |
| 86 | + if err != nil { |
| 87 | + |
| 88 | + } |
| 89 | + for _, check := range checks { |
| 90 | + fmt.Printf("%d ::%s:: %s [%s]\n", check.LineNo, check.Severity, check.Message, check.CheckName) |
| 91 | + } |
| 92 | + //Output: |
| 93 | + //1 ::Error:: No owners specified [NoOwner] |
| 94 | +} |
0 commit comments