-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathregexputils_test.go
92 lines (85 loc) · 5.85 KB
/
regexputils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package utils
import (
"strings"
"testing"
gofrogio "github.com/jfrog/gofrog/io"
"github.com/jfrog/jfrog-client-go/utils/log"
)
func init() {
log.SetLogger(log.NewLogger(log.DEBUG, nil))
}
func TestRemoveCredentialsFromLine(t *testing.T) {
regExpProtocol, err := GetRegExp(CredentialsInUrlRegexp)
if err != nil {
t.Error(err)
}
tests := []struct {
name string
regex gofrogio.CmdOutputPattern
expectedLine string
matched bool
}{
{"http", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line http://127.0.0.1:8081/artifactory/path/to/repo", true},
{"https", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true},
{"git", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line git://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line git://127.0.0.1:8081/artifactory/path/to/repo", true},
{"Special characters 1", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://u-s!<e>_r:!p-a&%%s%sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true},
{"Special characters 2", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://!user:[p]a(s)sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true},
{"http with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line http://123456@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line http://127.0.0.1:8081/artifactory/path/to/repo", true},
{"https with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://123456@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true},
{"git with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line git://123456@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line git://127.0.0.1:8081/artifactory/path/to/repo", true},
{"Special characters 1 with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://u-s!<e>_r!p-a&%%s%sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true},
{"Special characters 2 with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://!user[p]a(s)sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true},
{"No credentials", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", false},
{"No http", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line"}, "This is an example line", false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.regex.MatchedResults = test.regex.RegExp.FindStringSubmatch(test.regex.Line)
if test.matched && len(test.regex.MatchedResults) > 3 {
t.Errorf("Expected to find 3 results, however, found %d.", len(test.regex.MatchedResults))
}
if test.matched && test.regex.MatchedResults[0] == "" {
t.Error("Expected to find a match.")
}
if test.matched {
actual := RemoveCredentials(test.regex.Line, test.regex.MatchedResults[0])
if !strings.EqualFold(actual, test.expectedLine) {
t.Errorf("Expected: %s, The Regex found %s and the masked line: %s", test.expectedLine, test.regex.MatchedResults[0], actual)
}
}
if !test.matched && len(test.regex.MatchedResults) != 0 {
t.Error("Expected to find zero match, found:", test.regex.MatchedResults[0])
}
})
}
}
func TestReturnErrorOnNotFound(t *testing.T) {
regExpProtocol, err := GetRegExp(`^go: ([^\/\r\n]+\/[^\r\n\s:]*).*(404( Not Found)?[\s]?)$`)
if err != nil {
t.Error(err)
}
tests := []struct {
name string
regex gofrogio.CmdOutputPattern
error bool
}{
{"Without Error", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/jfrog/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, false},
{"With Error No Response Message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/jfrog/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo: 404"}, true},
{"With Error With response message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/jfrog/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo: 404 Not Found"}, true},
{"On Different Message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: finding github.com/elazarl/go-bindata-assetfs v0.0.0-20151224045452-57eb5e1fc594"}, false},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
test.regex.MatchedResults = test.regex.RegExp.FindStringSubmatch(test.regex.Line)
if test.error && len(test.regex.MatchedResults) < 3 {
t.Errorf("Expected to find at least 3 results, however, found %d.", len(test.regex.MatchedResults))
}
if test.error && test.regex.MatchedResults[0] == "" {
t.Error("Expected to find 404 not found, found nothing.")
}
if !test.error && len(test.regex.MatchedResults) != 0 {
t.Error("Expected regex to return empty result. Got:", test.regex.MatchedResults[0])
}
})
}
}