Skip to content

Commit b666551

Browse files
authored
Add File Path to output (#36)
1 parent f70047b commit b666551

6 files changed

Lines changed: 30 additions & 17 deletions

File tree

checker.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func RegisterChecker(name string, checker Checker) error {
3737

3838
// Checker provides tools for validating CODEOWNER file contents
3939
type Checker interface {
40-
CheckLine(lineNo int, line string) []CheckResult
40+
CheckLine(file string, lineNo int, line string) []CheckResult
4141
}
4242

4343
// SeverityLevel exposes all possible levels of severity check results
@@ -56,6 +56,7 @@ func (l SeverityLevel) String() string {
5656

5757
// Position provides structured way to evaluate where a given validation result is located in the CODEOWNERs file
5858
type Position struct {
59+
FilePath string
5960
StartLine int
6061
StartColumn int
6162
EndLine int
@@ -110,7 +111,7 @@ func Check(directory string, checkers ...string) ([]CheckResult, error) {
110111
if !ok {
111112
return nil, fmt.Errorf("'%s' not found", checker)
112113
}
113-
lineResults := c.CheckLine(lineNo, line)
114+
lineResults := c.CheckLine(fileLocation, lineNo, line)
114115
if lineResults != nil {
115116
results = append(results, lineResults...)
116117
}

checker_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ const dummyCheckerName string = "dummy"
1414
type dummyChecker struct {
1515
}
1616

17-
func (c dummyChecker) CheckLine(lineNo int, line string) []codeowners.CheckResult {
17+
func (c dummyChecker) CheckLine(file string, lineNo int, line string) []codeowners.CheckResult {
1818
return []codeowners.CheckResult{
1919
{
2020
Position: codeowners.Position{
21-
StartLine: 1,
22-
EndLine: 1,
21+
FilePath: file,
22+
StartLine: lineNo,
23+
EndLine: lineNo,
2324
},
2425
Message: "Dummy Error",
2526
Severity: codeowners.Error,
@@ -135,6 +136,7 @@ func TestSimpleCheck(t *testing.T) {
135136
want := []codeowners.CheckResult{
136137
{
137138
Position: codeowners.Position{
139+
FilePath: "test/data/pass/CODEOWNERS",
138140
StartLine: 1,
139141
StartColumn: 0,
140142
EndLine: 1,

checkers/invalidowner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func ownerValid(owner string) bool {
4343
}
4444

4545
// CheckLine runs this InvalidOwner's check against each line
46-
func (c InvalidOwner) CheckLine(lineNo int, line string) []codeowners.CheckResult {
46+
func (c InvalidOwner) CheckLine(file string, lineNo int, line string) []codeowners.CheckResult {
4747
var results []codeowners.CheckResult
4848

4949
_, owners := codeowners.ParseLine(line)
@@ -54,6 +54,7 @@ func (c InvalidOwner) CheckLine(lineNo int, line string) []codeowners.CheckResul
5454
}
5555
result := codeowners.CheckResult{
5656
Position: codeowners.Position{
57+
FilePath: file,
5758
StartLine: lineNo,
5859
EndLine: lineNo,
5960
StartColumn: strings.Index(line, owner) + 1,

checkers/invalidowner_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestInvalidOwnerCheckInvalidLongUsername(t *testing.T) {
1919
want := []codeowners.CheckResult{
2020
{
2121
Position: codeowners.Position{
22+
FilePath: "CODEOWNERS",
2223
StartLine: 1,
2324
StartColumn: 13,
2425
EndLine: 1,
@@ -31,7 +32,7 @@ func TestInvalidOwnerCheckInvalidLongUsername(t *testing.T) {
3132
}
3233

3334
checker := checkers.InvalidOwner{}
34-
got := checker.CheckLine(input.lineNo, input.line)
35+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
3536
if !reflect.DeepEqual(got, want) {
3637
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
3738
}
@@ -48,6 +49,7 @@ func TestInvalidOwnerCheckInvalidNoAt(t *testing.T) {
4849
want := []codeowners.CheckResult{
4950
{
5051
Position: codeowners.Position{
52+
FilePath: "CODEOWNERS",
5153
StartLine: 1,
5254
StartColumn: 13,
5355
EndLine: 1,
@@ -60,7 +62,7 @@ func TestInvalidOwnerCheckInvalidNoAt(t *testing.T) {
6062
}
6163

6264
checker := checkers.InvalidOwner{}
63-
got := checker.CheckLine(input.lineNo, input.line)
65+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
6466
if !reflect.DeepEqual(got, want) {
6567
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
6668
}
@@ -77,6 +79,7 @@ func TestInvalidOwnerCheckInvalidHyphens(t *testing.T) {
7779
want := []codeowners.CheckResult{
7880
{
7981
Position: codeowners.Position{
82+
FilePath: "CODEOWNERS",
8083
StartLine: 1,
8184
StartColumn: 13,
8285
EndLine: 1,
@@ -89,7 +92,7 @@ func TestInvalidOwnerCheckInvalidHyphens(t *testing.T) {
8992
}
9093

9194
checker := checkers.InvalidOwner{}
92-
got := checker.CheckLine(input.lineNo, input.line)
95+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
9396
if !reflect.DeepEqual(got, want) {
9497
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
9598
}
@@ -106,6 +109,7 @@ func TestInvalidOwnerCheckInvalidFormat(t *testing.T) {
106109
want := []codeowners.CheckResult{
107110
{
108111
Position: codeowners.Position{
112+
FilePath: "CODEOWNERS",
109113
StartLine: 1,
110114
StartColumn: 13,
111115
EndLine: 1,
@@ -118,7 +122,7 @@ func TestInvalidOwnerCheckInvalidFormat(t *testing.T) {
118122
}
119123

120124
checker := checkers.InvalidOwner{}
121-
got := checker.CheckLine(input.lineNo, input.line)
125+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
122126
if !reflect.DeepEqual(got, want) {
123127
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
124128
}
@@ -134,6 +138,7 @@ func TestInvalidOwnerCheckInvalidTrailingHyphen(t *testing.T) {
134138
want := []codeowners.CheckResult{
135139
{
136140
Position: codeowners.Position{
141+
FilePath: "CODEOWNERS",
137142
StartLine: 1,
138143
StartColumn: 13,
139144
EndLine: 1,
@@ -146,7 +151,7 @@ func TestInvalidOwnerCheckInvalidTrailingHyphen(t *testing.T) {
146151
}
147152

148153
checker := checkers.InvalidOwner{}
149-
got := checker.CheckLine(input.lineNo, input.line)
154+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
150155
if !reflect.DeepEqual(got, want) {
151156
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
152157
}
@@ -163,6 +168,7 @@ func TestInvalidOwnerCheckMultipleInvalid(t *testing.T) {
163168
want := []codeowners.CheckResult{
164169
{
165170
Position: codeowners.Position{
171+
FilePath: "CODEOWNERS",
166172
StartLine: 1,
167173
StartColumn: 13,
168174
EndLine: 1,
@@ -174,6 +180,7 @@ func TestInvalidOwnerCheckMultipleInvalid(t *testing.T) {
174180
},
175181
{
176182
Position: codeowners.Position{
183+
FilePath: "CODEOWNERS",
177184
StartLine: 1,
178185
StartColumn: 27,
179186
EndLine: 1,
@@ -186,7 +193,7 @@ func TestInvalidOwnerCheckMultipleInvalid(t *testing.T) {
186193
}
187194

188195
checker := checkers.InvalidOwner{}
189-
got := checker.CheckLine(input.lineNo, input.line)
196+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
190197
if !reflect.DeepEqual(got, want) {
191198
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
192199
}
@@ -202,7 +209,7 @@ func TestInvalidOwnerCheckPassUser(t *testing.T) {
202209
}
203210

204211
checker := checkers.InvalidOwner{}
205-
got := checker.CheckLine(input.lineNo, input.line)
212+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
206213
if got != nil {
207214
t.Errorf("Input: %v, Want: %v, Got: %v", input, nil, got)
208215
}
@@ -218,7 +225,7 @@ func TestInvalidOwnerCheckPassEmail(t *testing.T) {
218225
}
219226

220227
checker := checkers.InvalidOwner{}
221-
got := checker.CheckLine(input.lineNo, input.line)
228+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
222229
if got != nil {
223230
t.Errorf("Input: %v, Want: %v, Got: %v", input, nil, got)
224231
}
@@ -234,7 +241,7 @@ func TestInvalidOwnerCheckPassUserOrg(t *testing.T) {
234241
}
235242

236243
checker := checkers.InvalidOwner{}
237-
got := checker.CheckLine(input.lineNo, input.line)
244+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
238245
if got != nil {
239246
t.Errorf("Input: %v, Want: %v, Got: %v", input, nil, got)
240247
}

checkers/noowner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func init() {
1212
type NoOwner struct{}
1313

1414
// CheckLine runs this NoOwner's check against each line
15-
func (c NoOwner) CheckLine(lineNo int, line string) []codeowners.CheckResult {
15+
func (c NoOwner) CheckLine(file string, lineNo int, line string) []codeowners.CheckResult {
1616
var results []codeowners.CheckResult
1717

1818
_, owners := codeowners.ParseLine(line)
@@ -21,6 +21,7 @@ func (c NoOwner) CheckLine(lineNo int, line string) []codeowners.CheckResult {
2121
results = []codeowners.CheckResult{
2222
{
2323
Position: codeowners.Position{
24+
FilePath: file,
2425
StartLine: lineNo,
2526
EndLine: lineNo,
2627
StartColumn: 0,

checkers/noowner_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func TestNoOwnerCheck(t *testing.T) {
1919
want := []codeowners.CheckResult{
2020
{
2121
Position: codeowners.Position{
22+
FilePath: "CODEOWNERS",
2223
StartLine: 1,
2324
StartColumn: 0,
2425
EndLine: 1,
@@ -31,7 +32,7 @@ func TestNoOwnerCheck(t *testing.T) {
3132
}
3233

3334
checker := checkers.NoOwner{}
34-
got := checker.CheckLine(input.lineNo, input.line)
35+
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
3536
if !reflect.DeepEqual(got, want) {
3637
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
3738
}

0 commit comments

Comments
 (0)