Skip to content

Commit d6b6251

Browse files
authored
Refactor Parameters (#39)
1 parent be05457 commit d6b6251

7 files changed

Lines changed: 161 additions & 39 deletions

File tree

checker.go

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ func RegisterChecker(name string, checker Checker) error {
3535
return nil
3636
}
3737

38+
// ValidatorOptions provide input arguments for checkers to use
39+
type ValidatorOptions struct {
40+
Directory string
41+
CodeownersFileLocation string
42+
}
43+
3844
// Checker provides tools for validating CODEOWNER file contents
3945
type Checker interface {
40-
CheckLine(file string, lineNo int, line string) []CheckResult
46+
NewValidator(options ValidatorOptions) Validator
47+
}
48+
49+
// Validator provides tools for validating CODEOWNER file contents
50+
type Validator interface {
51+
ValidateLine(lineNo int, line string) []CheckResult
4152
}
4253

4354
// SeverityLevel exposes all possible levels of severity check results
@@ -86,15 +97,23 @@ type CheckResult struct {
8697
CheckName string
8798
}
8899

100+
// CheckOptions provides parameters for running a list of checks
101+
type CheckOptions struct {
102+
Directory string
103+
Checkers []string
104+
GithubTokenType string
105+
GithubToken string
106+
}
107+
89108
// Check evaluates the file contents against the checkers and return the results back.
90-
func Check(directory string, checkers ...string) ([]CheckResult, error) {
109+
func Check(options CheckOptions) ([]CheckResult, error) {
91110

92-
fileLocation, result := findCodeownersFile(directory)
111+
fileLocation, result := findCodeownersFile(options.Directory)
93112
if result != nil {
94113
return []CheckResult{*result}, nil
95114
}
96115

97-
file, err := os.Open(fileLocation)
116+
file, err := os.Open(filepath.Join(options.Directory, fileLocation))
98117
if err != nil {
99118
return nil, err
100119
}
@@ -103,15 +122,24 @@ func Check(directory string, checkers ...string) ([]CheckResult, error) {
103122
results := []CheckResult{}
104123
scanner := bufio.NewScanner(file)
105124
lineNo := 0
125+
126+
validators := make(map[string]Validator)
127+
for _, checker := range options.Checkers {
128+
c, ok := availableCheckers[checker]
129+
if !ok {
130+
return nil, fmt.Errorf("'%s' not found", checker)
131+
}
132+
validators[checker] = c.NewValidator(ValidatorOptions{
133+
Directory: options.Directory,
134+
CodeownersFileLocation: fileLocation,
135+
})
136+
}
137+
106138
for scanner.Scan() {
107139
line := scanner.Text()
108140
lineNo++
109-
for _, checker := range checkers {
110-
c, ok := availableCheckers[checker]
111-
if !ok {
112-
return nil, fmt.Errorf("'%s' not found", checker)
113-
}
114-
lineResults := c.CheckLine(fileLocation, lineNo, line)
141+
for _, c := range validators {
142+
lineResults := c.ValidateLine(lineNo, line)
115143
if lineResults != nil {
116144
results = append(results, lineResults...)
117145
}
@@ -139,7 +167,7 @@ func findCodeownersFile(dir string) (string, *CheckResult) {
139167
if fileExists(currentFile) {
140168
filesFound = append(filesFound, fileLocation)
141169
if len(codeownersLocation) == 0 {
142-
codeownersLocation = currentFile
170+
codeownersLocation = fileLocation
143171
}
144172
}
145173
}

checker_test.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@ import (
66
"testing"
77

88
"github.com/fmenezes/codeowners"
9-
_ "github.com/fmenezes/codeowners/checkers"
109
)
1110

1211
const dummyCheckerName string = "dummy"
1312

1413
type dummyChecker struct {
1514
}
1615

17-
func (c dummyChecker) CheckLine(file string, lineNo int, line string) []codeowners.CheckResult {
16+
type dummyCheckerValidator struct {
17+
codeownersFileLocation string
18+
directory string
19+
}
20+
21+
func (c dummyChecker) NewValidator(options codeowners.ValidatorOptions) codeowners.Validator {
22+
return dummyCheckerValidator{
23+
codeownersFileLocation: options.CodeownersFileLocation,
24+
directory: options.Directory,
25+
}
26+
}
27+
28+
func (c dummyCheckerValidator) ValidateLine(lineNo int, line string) []codeowners.CheckResult {
1829
return []codeowners.CheckResult{
1930
{
2031
Position: codeowners.Position{
21-
FilePath: file,
32+
FilePath: c.codeownersFileLocation,
2233
StartLine: lineNo,
2334
EndLine: lineNo,
2435
},
@@ -136,7 +147,7 @@ func TestSimpleCheck(t *testing.T) {
136147
want := []codeowners.CheckResult{
137148
{
138149
Position: codeowners.Position{
139-
FilePath: "test/data/pass/CODEOWNERS",
150+
FilePath: "CODEOWNERS",
140151
StartLine: 1,
141152
StartColumn: 0,
142153
EndLine: 1,
@@ -149,7 +160,10 @@ func TestSimpleCheck(t *testing.T) {
149160
}
150161

151162
codeowners.RegisterChecker(dummyCheckerName, dummyChecker{})
152-
got, err := codeowners.Check(input, dummyCheckerName)
163+
got, err := codeowners.Check(codeowners.CheckOptions{
164+
Directory: input,
165+
Checkers: []string{dummyCheckerName},
166+
})
153167
if err != nil {
154168
t.Errorf("Input %s, Error %v", input, err)
155169
}
@@ -160,7 +174,9 @@ func TestSimpleCheck(t *testing.T) {
160174

161175
func TestNoProblemsFound(t *testing.T) {
162176
input := "./test/data/pass"
163-
got, err := codeowners.Check(input)
177+
got, err := codeowners.Check(codeowners.CheckOptions{
178+
Directory: input,
179+
})
164180
if err != nil {
165181
t.Errorf("Input %s, Error %v", input, err)
166182
}
@@ -171,7 +187,10 @@ func TestNoProblemsFound(t *testing.T) {
171187

172188
func TestCheckerNotFound(t *testing.T) {
173189
input := "./test/data/pass"
174-
_, err := codeowners.Check(input, "NonExistentChecker")
190+
_, err := codeowners.Check(codeowners.CheckOptions{
191+
Directory: input,
192+
Checkers: []string{"NonExistentChecker"},
193+
})
175194
if err == nil {
176195
t.Error("Should have errored")
177196
}
@@ -187,7 +206,10 @@ func TestNoCodeownersCheck(t *testing.T) {
187206
},
188207
}
189208

190-
got, err := codeowners.Check(input, dummyCheckerName)
209+
got, err := codeowners.Check(codeowners.CheckOptions{
210+
Directory: input,
211+
Checkers: []string{dummyCheckerName},
212+
})
191213
if err != nil {
192214
t.Errorf("Input %s, Error %v", input, err)
193215
}
@@ -206,7 +228,10 @@ func TestMultipleCodeownersCheck(t *testing.T) {
206228
},
207229
}
208230

209-
got, err := codeowners.Check(input, dummyCheckerName)
231+
got, err := codeowners.Check(codeowners.CheckOptions{
232+
Directory: input,
233+
Checkers: []string{dummyCheckerName},
234+
})
210235
if err != nil {
211236
t.Errorf("Input %s, Error %v", input, err)
212237
}
@@ -216,7 +241,10 @@ func TestMultipleCodeownersCheck(t *testing.T) {
216241
}
217242

218243
func ExampleCheck() {
219-
checks, err := codeowners.Check(".", codeowners.AvailableCheckers()...)
244+
checks, err := codeowners.Check(codeowners.CheckOptions{
245+
Directory: ".",
246+
Checkers: codeowners.AvailableCheckers(),
247+
})
220248
if err != nil {
221249
panic(err)
222250
}

checkers/invalidowner.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@ func init() {
1515
}
1616

1717
// InvalidOwner represents checker to decide validate owners in each of CODEOWNERS lines
18-
type InvalidOwner struct{}
18+
type InvalidOwner struct {
19+
}
20+
21+
// NewValidator returns validating capabilities for this checker
22+
func (c InvalidOwner) NewValidator(options codeowners.ValidatorOptions) codeowners.Validator {
23+
return invalidOwnerValidator{
24+
options: options,
25+
}
26+
}
1927

2028
func ownerValid(owner string) bool {
2129
var emailRegex = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
@@ -42,8 +50,12 @@ func ownerValid(owner string) bool {
4250
return true
4351
}
4452

45-
// CheckLine runs this InvalidOwner's check against each line
46-
func (c InvalidOwner) CheckLine(file string, lineNo int, line string) []codeowners.CheckResult {
53+
type invalidOwnerValidator struct {
54+
options codeowners.ValidatorOptions
55+
}
56+
57+
// ValidateLine runs this InvalidOwner's check against each line
58+
func (v invalidOwnerValidator) ValidateLine(lineNo int, line string) []codeowners.CheckResult {
4759
var results []codeowners.CheckResult
4860

4961
_, owners := codeowners.ParseLine(line)
@@ -54,7 +66,7 @@ func (c InvalidOwner) CheckLine(file string, lineNo int, line string) []codeowne
5466
}
5567
result := codeowners.CheckResult{
5668
Position: codeowners.Position{
57-
FilePath: file,
69+
FilePath: v.options.CodeownersFileLocation,
5870
StartLine: lineNo,
5971
EndLine: lineNo,
6072
StartColumn: strings.Index(line, owner) + 1,

checkers/invalidowner_test.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ func TestInvalidOwnerCheckInvalidLongUsername(t *testing.T) {
3232
}
3333

3434
checker := checkers.InvalidOwner{}
35-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
35+
validator := checker.NewValidator(codeowners.ValidatorOptions{
36+
Directory: ".",
37+
CodeownersFileLocation: "CODEOWNERS",
38+
})
39+
got := validator.ValidateLine(input.lineNo, input.line)
3640
if !reflect.DeepEqual(got, want) {
3741
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
3842
}
@@ -62,7 +66,11 @@ func TestInvalidOwnerCheckInvalidNoAt(t *testing.T) {
6266
}
6367

6468
checker := checkers.InvalidOwner{}
65-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
69+
validator := checker.NewValidator(codeowners.ValidatorOptions{
70+
Directory: ".",
71+
CodeownersFileLocation: "CODEOWNERS",
72+
})
73+
got := validator.ValidateLine(input.lineNo, input.line)
6674
if !reflect.DeepEqual(got, want) {
6775
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
6876
}
@@ -92,7 +100,11 @@ func TestInvalidOwnerCheckInvalidHyphens(t *testing.T) {
92100
}
93101

94102
checker := checkers.InvalidOwner{}
95-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
103+
validator := checker.NewValidator(codeowners.ValidatorOptions{
104+
Directory: ".",
105+
CodeownersFileLocation: "CODEOWNERS",
106+
})
107+
got := validator.ValidateLine(input.lineNo, input.line)
96108
if !reflect.DeepEqual(got, want) {
97109
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
98110
}
@@ -122,7 +134,11 @@ func TestInvalidOwnerCheckInvalidFormat(t *testing.T) {
122134
}
123135

124136
checker := checkers.InvalidOwner{}
125-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
137+
validator := checker.NewValidator(codeowners.ValidatorOptions{
138+
Directory: ".",
139+
CodeownersFileLocation: "CODEOWNERS",
140+
})
141+
got := validator.ValidateLine(input.lineNo, input.line)
126142
if !reflect.DeepEqual(got, want) {
127143
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
128144
}
@@ -151,7 +167,11 @@ func TestInvalidOwnerCheckInvalidTrailingHyphen(t *testing.T) {
151167
}
152168

153169
checker := checkers.InvalidOwner{}
154-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
170+
validator := checker.NewValidator(codeowners.ValidatorOptions{
171+
Directory: ".",
172+
CodeownersFileLocation: "CODEOWNERS",
173+
})
174+
got := validator.ValidateLine(input.lineNo, input.line)
155175
if !reflect.DeepEqual(got, want) {
156176
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
157177
}
@@ -193,7 +213,11 @@ func TestInvalidOwnerCheckMultipleInvalid(t *testing.T) {
193213
}
194214

195215
checker := checkers.InvalidOwner{}
196-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
216+
validator := checker.NewValidator(codeowners.ValidatorOptions{
217+
Directory: ".",
218+
CodeownersFileLocation: "CODEOWNERS",
219+
})
220+
got := validator.ValidateLine(input.lineNo, input.line)
197221
if !reflect.DeepEqual(got, want) {
198222
t.Errorf("Input: %v, Want: %v, Got: %v", input, want, got)
199223
}
@@ -209,7 +233,11 @@ func TestInvalidOwnerCheckPassUser(t *testing.T) {
209233
}
210234

211235
checker := checkers.InvalidOwner{}
212-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
236+
validator := checker.NewValidator(codeowners.ValidatorOptions{
237+
Directory: ".",
238+
CodeownersFileLocation: "CODEOWNERS",
239+
})
240+
got := validator.ValidateLine(input.lineNo, input.line)
213241
if got != nil {
214242
t.Errorf("Input: %v, Want: %v, Got: %v", input, nil, got)
215243
}
@@ -225,7 +253,11 @@ func TestInvalidOwnerCheckPassEmail(t *testing.T) {
225253
}
226254

227255
checker := checkers.InvalidOwner{}
228-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
256+
validator := checker.NewValidator(codeowners.ValidatorOptions{
257+
Directory: ".",
258+
CodeownersFileLocation: "CODEOWNERS",
259+
})
260+
got := validator.ValidateLine(input.lineNo, input.line)
229261
if got != nil {
230262
t.Errorf("Input: %v, Want: %v, Got: %v", input, nil, got)
231263
}
@@ -241,7 +273,11 @@ func TestInvalidOwnerCheckPassUserOrg(t *testing.T) {
241273
}
242274

243275
checker := checkers.InvalidOwner{}
244-
got := checker.CheckLine("CODEOWNERS", input.lineNo, input.line)
276+
validator := checker.NewValidator(codeowners.ValidatorOptions{
277+
Directory: ".",
278+
CodeownersFileLocation: "CODEOWNERS",
279+
})
280+
got := validator.ValidateLine(input.lineNo, input.line)
245281
if got != nil {
246282
t.Errorf("Input: %v, Want: %v, Got: %v", input, nil, got)
247283
}

checkers/noowner.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ func init() {
1111
// NoOwner represents checker to decide validate presence of owners in each of CODEOWNERS lines
1212
type NoOwner struct{}
1313

14-
// CheckLine runs this NoOwner's check against each line
15-
func (c NoOwner) CheckLine(file string, lineNo int, line string) []codeowners.CheckResult {
14+
// NewValidator returns validating capabilities for this checker
15+
func (c NoOwner) NewValidator(options codeowners.ValidatorOptions) codeowners.Validator {
16+
return noOwnerValidator{
17+
options: options,
18+
}
19+
}
20+
21+
type noOwnerValidator struct {
22+
options codeowners.ValidatorOptions
23+
}
24+
25+
// ValidateLine runs this NoOwner's check against each line
26+
func (v noOwnerValidator) ValidateLine(lineNo int, line string) []codeowners.CheckResult {
1627
var results []codeowners.CheckResult
1728

1829
_, owners := codeowners.ParseLine(line)
@@ -21,7 +32,7 @@ func (c NoOwner) CheckLine(file string, lineNo int, line string) []codeowners.Ch
2132
results = []codeowners.CheckResult{
2233
{
2334
Position: codeowners.Position{
24-
FilePath: file,
35+
FilePath: v.options.CodeownersFileLocation,
2536
StartLine: lineNo,
2637
EndLine: lineNo,
2738
StartColumn: 0,

0 commit comments

Comments
 (0)