Skip to content

Commit

Permalink
fix: replace fnmatch with wcmatch for glob match, keep same behavior …
Browse files Browse the repository at this point in the history
…with Go Casbin (#149)

* fix: keep same behavior with go-casbin

Signed-off-by: Zxilly <[email protected]>

* build: add wcmatch as dependency

Signed-off-by: Zxilly <[email protected]>

* ci: refresh check

Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly authored May 12, 2021
1 parent fce716f commit 906f40c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions casbin/util/builtin_operators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fnmatch
import re
import ipaddress
import re

from wcmatch import pathlib

KEY_MATCH2_PATTERN = re.compile(r'(.*?):[^\/]+(.*?)')
KEY_MATCH3_PATTERN = re.compile(r'(.*?){[^\/]+}(.*?)')
Expand Down Expand Up @@ -86,7 +87,7 @@ def regex_match_func(*args):

def glob_match(string, pattern):
"""determines whether string matches the pattern in glob expression."""
return fnmatch.fnmatch(string, pattern)
return pathlib.Path(string).globmatch(pattern)


def glob_match_func(*args):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
simpleeval>=0.9.10
wcmatch >= 8.1.2
22 changes: 11 additions & 11 deletions tests/util/test_builtin_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,30 +90,30 @@ def test_glob_match(self):
self.assertTrue(util.glob_match_func("/foo", "/foo*"))
self.assertFalse(util.glob_match_func("/foo", "/foo/*"))
self.assertFalse(util.glob_match_func("/foo/bar", "/foo"))
self.assertTrue(util.glob_match_func("/foo/bar", "/foo*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/foo/bar", "/foo*"))
self.assertTrue(util.glob_match_func("/foo/bar", "/foo/*"))
self.assertFalse(util.glob_match_func("/foobar", "/foo"))
self.assertTrue(util.glob_match_func("/foobar", "/foo*"))
self.assertFalse(util.glob_match_func("/foobar", "/foo/*"))

self.assertTrue(util.glob_match_func("/prefix/foo", "*/foo")) # differ from Casbin Go
self.assertTrue(util.glob_match_func("/prefix/foo", "*/foo*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/prefix/foo", "*/foo"))
self.assertFalse(util.glob_match_func("/prefix/foo", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/foo", "*/foo/*"))
self.assertFalse(util.glob_match_func("/prefix/foo/bar", "*/foo"))
self.assertTrue(util.glob_match_func("/prefix/foo/bar", "*/foo*")) # differ from Casbin Go
self.assertTrue(util.glob_match_func("/prefix/foo/bar", "*/foo/*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/prefix/foo/bar", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/foo/bar", "*/foo/*"))
self.assertFalse(util.glob_match_func("/prefix/foobar", "*/foo"))
self.assertTrue(util.glob_match_func("/prefix/foobar", "*/foo*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/prefix/foobar", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/foobar", "*/foo/*"))

self.assertTrue(util.glob_match_func("/prefix/subprefix/foo", "*/foo")) # differ from Casbin Go
self.assertTrue(util.glob_match_func("/prefix/subprefix/foo", "*/foo*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo", "*/foo"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo", "*/foo/*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo/bar", "*/foo"))
self.assertTrue(util.glob_match_func("/prefix/subprefix/foo/bar", "*/foo*")) # differ from Casbin Go
self.assertTrue(util.glob_match_func("/prefix/subprefix/foo/bar", "*/foo/*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo/bar", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foo/bar", "*/foo/*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foobar", "*/foo"))
self.assertTrue(util.glob_match_func("/prefix/subprefix/foobar", "*/foo*")) # differ from Casbin Go
self.assertFalse(util.glob_match_func("/prefix/subprefix/foobar", "*/foo*"))
self.assertFalse(util.glob_match_func("/prefix/subprefix/foobar", "*/foo/*"))

def test_ip_match(self):
Expand Down

0 comments on commit 906f40c

Please sign in to comment.