mirror of
https://github.com/TwiN/gatus.git
synced 2024-11-21 15:33:17 +01:00
Fix issue with backslashes when using pattern function
This commit is contained in:
parent
f1c0bbe73c
commit
c27cb7af08
@ -1,12 +1,19 @@
|
|||||||
package pattern
|
package pattern
|
||||||
|
|
||||||
import "path/filepath"
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Match checks whether a string matches a pattern
|
// Match checks whether a string matches a pattern
|
||||||
func Match(pattern, s string) bool {
|
func Match(pattern, s string) bool {
|
||||||
if pattern == "*" {
|
if pattern == "*" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
// Backslashes break filepath.Match, so we'll remove all of them.
|
||||||
|
// This has a pretty significant impact on performance when there
|
||||||
|
// are backslashes, but at least it doesn't break filepath.Match.
|
||||||
|
s = strings.ReplaceAll(s, "\\", "")
|
||||||
matched, _ := filepath.Match(pattern, s)
|
matched, _ := filepath.Match(pattern, s)
|
||||||
return matched
|
return matched
|
||||||
}
|
}
|
||||||
|
21
pattern/pattern_bench_test.go
Normal file
21
pattern/pattern_bench_test.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package pattern
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func BenchmarkMatch(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
if !Match("*ing*", "livingroom") {
|
||||||
|
b.Error("should've matched")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkMatchWithBackslash(b *testing.B) {
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
if !Match("*ing*", "living\\room") {
|
||||||
|
b.Error("should've matched")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
package pattern
|
package pattern
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestMatch(t *testing.T) {
|
func TestMatch(t *testing.T) {
|
||||||
testMatch(t, "*", "livingroom_123", true)
|
testMatch(t, "*", "livingroom_123", true)
|
||||||
@ -15,6 +18,7 @@ func TestMatch(t *testing.T) {
|
|||||||
testMatch(t, "*vin*om*2*", "livingroom_123", true)
|
testMatch(t, "*vin*om*2*", "livingroom_123", true)
|
||||||
testMatch(t, "livingroom_123", "livingroom_123", true)
|
testMatch(t, "livingroom_123", "livingroom_123", true)
|
||||||
testMatch(t, "*livingroom_123*", "livingroom_123", true)
|
testMatch(t, "*livingroom_123*", "livingroom_123", true)
|
||||||
|
testMatch(t, "*test*", "\\test", true)
|
||||||
testMatch(t, "livingroom", "livingroom_123", false)
|
testMatch(t, "livingroom", "livingroom_123", false)
|
||||||
testMatch(t, "livingroom123", "livingroom_123", false)
|
testMatch(t, "livingroom123", "livingroom_123", false)
|
||||||
testMatch(t, "what", "livingroom_123", false)
|
testMatch(t, "what", "livingroom_123", false)
|
||||||
@ -24,14 +28,16 @@ func TestMatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testMatch(t *testing.T, pattern, key string, expectedToMatch bool) {
|
func testMatch(t *testing.T, pattern, key string, expectedToMatch bool) {
|
||||||
matched := Match(pattern, key)
|
t.Run(fmt.Sprintf("pattern '%s' from '%s'", pattern, key), func(t *testing.T) {
|
||||||
if expectedToMatch {
|
matched := Match(pattern, key)
|
||||||
if !matched {
|
if expectedToMatch {
|
||||||
t.Errorf("%s should've matched pattern '%s'", key, pattern)
|
if !matched {
|
||||||
|
t.Errorf("%s should've matched pattern '%s'", key, pattern)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if matched {
|
||||||
|
t.Errorf("%s shouldn't have matched pattern '%s'", key, pattern)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
})
|
||||||
if matched {
|
|
||||||
t.Errorf("%s shouldn't have matched pattern '%s'", key, pattern)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user