-
Notifications
You must be signed in to change notification settings - Fork 3
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 #14 from mx-psi/mx-psi/fix-err-msg
Fix error message on incorrect regexp
- Loading branch information
Showing
3 changed files
with
69 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package porto | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func GetRegexpList(regexps string) ([]*regexp.Regexp, error) { | ||
var regexes []*regexp.Regexp | ||
if len(regexps) > 0 { | ||
for _, sfrp := range strings.Split(regexps, ",") { | ||
sfr, err := regexp.Compile(sfrp) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to compile regex %q: %w", sfrp, err) | ||
} | ||
regexes = append(regexes, sfr) | ||
} | ||
} | ||
|
||
return regexes, 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,44 @@ | ||
package porto | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetRegexpList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
regexps string | ||
expected []*regexp.Regexp | ||
err string | ||
}{ | ||
{ | ||
name: "single regex", | ||
regexps: "^.*pb.go$", | ||
expected: []*regexp.Regexp{regexp.MustCompile("^.*pb.go$")}, | ||
}, | ||
{ | ||
name: "failing regex", | ||
regexps: "^.*pb.go$,*$$", | ||
err: "failed to compile regex \"*$$\": error parsing regexp: missing argument to repetition operator: `*`", | ||
}, | ||
{ | ||
name: "multiple regexes", | ||
regexps: "^.*pb.go$,^tools.go$", | ||
expected: []*regexp.Regexp{regexp.MustCompile("^.*pb.go$"), regexp.MustCompile("^tools.go$")}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
actual, err := GetRegexpList(tt.regexps) | ||
if err != nil || tt.err != "" { | ||
assert.EqualError(t, err, tt.err) | ||
} else { | ||
assert.Equal(t, tt.expected, actual) | ||
} | ||
}) | ||
} | ||
} |