-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
318 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"path/filepath" | ||
"text/template" | ||
|
||
"github.com/fmenezes/codeowners" | ||
_ "github.com/fmenezes/codeowners/checkers" | ||
) | ||
|
||
type options struct { | ||
directory string | ||
format string | ||
} | ||
|
||
func run(wr io.Writer, opt options) error { | ||
dir, err := filepath.Abs(opt.directory) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format := "{{range .}}{{ .LineNo }} ::{{ .Severity }}:: {{ .Message }} [{{ .CheckName }}]\n{{end}}" | ||
if len(opt.format) > 0 { | ||
format = opt.format | ||
} | ||
tpl, err := template.New("main").Parse(format) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
checkers := codeowners.AvailableCheckers() | ||
|
||
checks, _ := codeowners.Check(dir, checkers...) | ||
|
||
if len(checks) > 0 { | ||
err = tpl.Execute(wr, checks) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
wr.Write([]byte("Everything ok ;)\n")) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func testRun(opt options) (string, error) { | ||
var output bytes.Buffer | ||
err := run(&output, opt) | ||
if err != nil { | ||
return "", err | ||
} | ||
return output.String(), nil | ||
} | ||
|
||
func TestPass(t *testing.T) { | ||
opt := options{ | ||
directory: "../test/data/pass", | ||
format: "", | ||
} | ||
|
||
got, err := testRun(opt) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
want := `Everything ok ;) | ||
` | ||
if got != want { | ||
t.Errorf("Input: %v Want: '%s' Got: '%s'", opt, want, got) | ||
} | ||
} | ||
|
||
func TestNoOwners(t *testing.T) { | ||
opt := options{ | ||
directory: "../test/data/no_owners", | ||
format: "", | ||
} | ||
|
||
got, err := testRun(opt) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
want := `1 ::Error:: No owners specified [NoOwner] | ||
` | ||
if got != want { | ||
t.Errorf("Input: %v Want: '%s' Got: '%s'", opt, want, got) | ||
} | ||
} | ||
|
||
func TestCustomFormat(t *testing.T) { | ||
opt := options{ | ||
directory: "../test/data/noowners", | ||
format: "test", | ||
} | ||
|
||
got, err := testRun(opt) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
want := `test` | ||
if got != want { | ||
t.Errorf("Input: %v Want: '%s' Got: '%s'", opt, want, got) | ||
} | ||
} | ||
|
||
func TestInvalidFormat(t *testing.T) { | ||
opt := options{ | ||
directory: "../test/data/noowners", | ||
format: " {{template \"one\"}} ", | ||
} | ||
|
||
_, err := testRun(opt) | ||
if err == nil { | ||
t.Errorf("Should have errored") | ||
} | ||
|
||
opt = options{ | ||
directory: "../test/data/noowners", | ||
format: " {{ . ", | ||
} | ||
|
||
_, err = testRun(opt) | ||
if err == nil { | ||
t.Errorf("Should have errored") | ||
} | ||
} | ||
|
||
func TestInvalidDirectory(t *testing.T) { | ||
opt := options{ | ||
directory: "'", | ||
format: "", | ||
} | ||
|
||
got, err := testRun(opt) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
want := `0 ::Error:: No CODEOWNERS file found [NoCodeowners] | ||
` | ||
if got != want { | ||
t.Errorf("Input: %v Want: '%s' Got: '%s'", opt, want, got) | ||
} | ||
} |
Oops, something went wrong.