Permalink
Browse files

add NewParser method to create parser from option

  • Loading branch information...
1 parent 9d803d4 commit 1f02bcbdc083e2d0e0c748ed5d50814d67aa35c4 @haya14busa committed Dec 11, 2016
Showing with 94 additions and 26 deletions.
  1. +2 −26 cmd/reviewdog/main.go
  2. +34 −0 parser.go
  3. +58 −0 parser_test.go
View
@@ -106,9 +106,9 @@ func run(r io.Reader, w io.Writer, opt *option) error {
return runList(w)
}
- p, err := parser(opt)
+ p, err := reviewdog.NewParser(&reviewdog.ParserOpt{FormatName: opt.f, Errorformat: opt.efms})
if err != nil {
- return err
+ return fmt.Errorf("fail to create parser. use either -f or -efm: %v", err)
}
var cs reviewdog.CommentService
@@ -184,30 +184,6 @@ func sortedFmts(fs fmts.Fmts) []*fmts.Fmt {
return r
}
-func parser(opt *option) (reviewdog.Parser, error) {
- if opt.f == "checkstyle" {
- return reviewdog.NewCheckStyleParser(), nil
- }
-
- // use defined errorformat
- if opt.f != "" {
- if len(opt.efms) > 0 {
- return nil, errors.New("you cannot specify both -f and -efm at the same time")
- }
- efm, ok := fmts.DefinedFmts()[opt.f]
- if !ok {
- return nil, fmt.Errorf("%q is not supported. Use -efm or consider to add new errrorformat to https://github.com/haya14busa/errorformat", opt.f)
- }
- opt.efms = efm.Errorformat
- }
-
- if len(opt.efms) == 0 {
- return nil, errors.New("cannot parse input. Please specify -f or -efm")
- }
-
- return reviewdog.NewErrorformatParserString(opt.efms)
-}
-
// FlashCommentService is CommentService which uses Flash method to post comment.
type FlashCommentService interface {
reviewdog.CommentService
View
@@ -2,12 +2,46 @@ package reviewdog
import (
"encoding/xml"
+ "errors"
"fmt"
"io"
"github.com/haya14busa/errorformat"
+ "github.com/haya14busa/errorformat/fmts"
)
+// ParserOpt represents option to create Parser. Either FormatName or
+// Errorformat should be specified.
+type ParserOpt struct {
+ FormatName string
+ Errorformat []string
+}
+
+// NewParser returns Parser based on ParserOpt.
+func NewParser(opt *ParserOpt) (Parser, error) {
+ name := opt.FormatName
+
+ if name != "" && len(opt.Errorformat) > 0 {
+ return nil, errors.New("you cannot specify both format name and errorformat at the same time")
+ }
+
+ if name == "checkstyle" {
+ return NewCheckStyleParser(), nil
+ }
+ // use defined errorformat
+ if name != "" {
+ efm, ok := fmts.DefinedFmts()[name]
+ if !ok {
+ return nil, fmt.Errorf("%q is not supported. consider to add new errrorformat to https://github.com/haya14busa/errorformat", name)
+ }
+ opt.Errorformat = efm.Errorformat
+ }
+ if len(opt.Errorformat) == 0 {
+ return nil, errors.New("errorformat is empty")
+ }
+ return NewErrorformatParserString(opt.Errorformat)
+}
+
var _ Parser = &ErrorformatParser{}
// ErrorformatParser is errorformat parser.
View
@@ -1,10 +1,68 @@
package reviewdog
import (
+ "reflect"
"strings"
"testing"
)
+func TestNewParser(t *testing.T) {
+ tests := []struct {
+ in *ParserOpt
+ typ Parser
+ wantErr bool
+ }{
+ {
+ in: &ParserOpt{
+ FormatName: "checkstyle",
+ },
+ typ: &CheckStyleParser{},
+ },
+ {
+ in: &ParserOpt{
+ FormatName: "golint",
+ },
+ typ: &ErrorformatParser{},
+ },
+ {
+ in: &ParserOpt{
+ Errorformat: []string{`%f:%l:%c:%m`},
+ },
+ typ: &ErrorformatParser{},
+ },
+ { // empty
+ in: &ParserOpt{},
+ wantErr: true,
+ },
+ { // both
+ in: &ParserOpt{
+ FormatName: "checkstyle",
+ Errorformat: []string{`%f:%l:%c:%m`},
+ },
+ wantErr: true,
+ },
+ { // unsupported
+ in: &ParserOpt{
+ FormatName: "unsupported format",
+ },
+ wantErr: true,
+ },
+ }
+ for _, tt := range tests {
+ p, err := NewParser(tt.in)
+ if tt.wantErr && err != nil {
+ continue
+ }
+ if err != nil {
+ t.Error(err)
+ continue
+ }
+ if got, want := reflect.TypeOf(p), reflect.TypeOf(tt.typ); got != want {
+ t.Errorf("typ: got %v, want %v", got, want)
+ }
+ }
+}
+
func TestNewErrorformatParserString(t *testing.T) {
in := []string{`%f:%l:%c:%m`, `%-G%.%#`}

0 comments on commit 1f02bcb

Please sign in to comment.