-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from afmahmuda/exclude
add flag for file exclusion
- Loading branch information
Showing
6 changed files
with
162 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package astParser | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
type parserOptions struct { | ||
recursive bool | ||
excludedFilesRegex *regexp.Regexp | ||
} | ||
|
||
type ParserOptionFunc func(*parserOptions) | ||
|
||
func WithRecursive() func(*parserOptions) { | ||
return func(opt *parserOptions) { | ||
opt.recursive = true | ||
} | ||
} | ||
|
||
func WithFileExclusion(exclusionRegex string) func(*parserOptions) { | ||
return func(opt *parserOptions) { | ||
var re = regexp.MustCompile(exclusionRegex) | ||
opt.excludedFilesRegex = re | ||
} | ||
} |
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,51 @@ | ||
package astParser | ||
|
||
import ( | ||
"reflect" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestWithRecursive(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want *parserOptions | ||
}{ | ||
{"positive", &parserOptions{recursive: true}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
op := &parserOptions{} | ||
opt := WithRecursive() | ||
opt(op) | ||
if !reflect.DeepEqual(op, tt.want) { | ||
t.Errorf("WithRecursive() = %v, want %v", op, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestWithFileExclusion(t *testing.T) { | ||
type args struct { | ||
exclusionRegex string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *parserOptions | ||
}{ | ||
{"positive", args{"^somefile.go$"}, &parserOptions{excludedFilesRegex: regexp.MustCompile("^somefile.go$")}}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
op := &parserOptions{} | ||
opt := WithFileExclusion(tt.args.exclusionRegex) | ||
opt(op) | ||
if !reflect.DeepEqual(op, tt.want) { | ||
t.Errorf("WithRecursive() = %v, want %v", op, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,32 @@ | ||
package astParser | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func Test_isExcluded(t *testing.T) { | ||
type args struct { | ||
fileName string | ||
regex *regexp.Regexp | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{"positive_non_go_file_returns_true", args{"parser.js", nil}, true}, | ||
{"positive_go_test_file_returns_true", args{"parser_test.go", nil}, true}, | ||
{"positive_match_regex_returns_true", args{"parser_mock.go", regexp.MustCompile(`^.+_mock.go$`)}, true}, | ||
{"positive_match_regex_due_to_dot_returns_true", args{"parser_mock|tmp.go", regexp.MustCompile(`^.+_mock.tmp.go$`)}, true}, | ||
{"negative_go_file_no_regex_returns_false", args{"parser.go", nil}, false}, | ||
{"negative_go_file_did_not_match_regex_returns_false", args{"parser_mick.go", regexp.MustCompile(`^.+_mock.go$`)}, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := isExcluded(tt.args.fileName, tt.args.regex); got != tt.want { | ||
t.Errorf("isExcluded() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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