Browse Source

Merge "Use PCRE instead of Python's re for RedirectMatch tests"

tags/0.4.0^0
Zuul 1 year ago
parent
commit
98a91c0a3c
6 changed files with 35 additions and 2 deletions
  1. 3
    0
      bindep.txt
  2. 4
    0
      releasenotes/notes/pcre-9c6e76b391620545.yaml
  3. 1
    0
      requirements.txt
  4. 4
    0
      tox.ini
  5. 4
    2
      whereto/rules.py
  6. 19
    0
      whereto/tests/test_rules.py

+ 3
- 0
bindep.txt View File

@@ -0,0 +1,3 @@
1
+libpcre3-dev [platform:dpkg]
2
+pcre-devel [platform:rpm]
3
+dev-libs/libpcre [platform:gentoo]

+ 4
- 0
releasenotes/notes/pcre-9c6e76b391620545.yaml View File

@@ -0,0 +1,4 @@
1
+---
2
+features:
3
+  - whereto now uses the PCRE library - the same regex library used by
4
+    mod_alias - for compiling regular expressions.

+ 1
- 0
requirements.txt View File

@@ -3,3 +3,4 @@
3 3
 # process, which may cause wedges in the gate later.
4 4
 
5 5
 pbr>=2.0 # Apache-2.0
6
+python-pcre

+ 4
- 0
tox.ini View File

@@ -25,6 +25,10 @@ commands = flake8 {posargs}
25 25
 basepython = python3
26 26
 commands = {posargs}
27 27
 
28
+[testenv:bindep]
29
+deps = bindep
30
+commands = bindep test
31
+
28 32
 [testenv:cover]
29 33
 commands = python setup.py test --coverage --testr-args='{posargs}'
30 34
 

+ 4
- 2
whereto/rules.py View File

@@ -16,6 +16,7 @@
16 16
 # under the License.
17 17
 
18 18
 import logging
19
+import pcre
19 20
 import re
20 21
 
21 22
 
@@ -74,14 +75,15 @@ class RedirectMatch(Rule):
74 75
 
75 76
     def __init__(self, linenum, *params):
76 77
         super(RedirectMatch, self).__init__(linenum, *params)
77
-        self.regex = re.compile(self.pattern)
78
+        self.regex = pcre.compile(self.pattern)
78 79
         if self.target:
79 80
             self.target_repl = self._get_target_repl()
80 81
         else:
81 82
             self.target_repl = None
82 83
 
83 84
     def _get_target_repl(self):
84
-        return self._group_subst.sub(r'\\1', self.target).replace(r'\$', '$')
85
+        escaped = pcre.escape_template(self.target)
86
+        return self._group_subst.sub(r'{\1}', escaped).replace(r'\$', '$')
85 87
 
86 88
     def match(self, path):
87 89
         m = self.regex.search(path)

+ 19
- 0
whereto/tests/test_rules.py View File

@@ -96,6 +96,15 @@ class TestRedirectMatch(base.TestCase):
96 96
             rule.match('/user/'),
97 97
         )
98 98
 
99
+    def test_match_pcre_syntax(self):
100
+        rule = rules.RedirectMatch(
101
+            1,
102
+            'redirectmatch', '301', '^/((?i)pike)/user/.*$', '/pike/user/',
103
+        )
104
+        self.assertIsNone(
105
+            rule.match('/Pike/USER/')
106
+        )
107
+
99 108
     def test_match_with_group(self):
100 109
         rule = rules.RedirectMatch(
101 110
             1,
@@ -106,6 +115,16 @@ class TestRedirectMatch(base.TestCase):
106 115
             rule.match('/user/foo'),
107 116
         )
108 117
 
118
+    def test_match_with_group_braces(self):
119
+        rule = rules.RedirectMatch(
120
+            1,
121
+            'redirectmatch', '301', '^/user/(.*)$', '/pike/user/{1}/$1',
122
+        )
123
+        self.assertEqual(
124
+            ('301', '/pike/user/{1}/foo'),
125
+            rule.match('/user/foo'),
126
+        )
127
+
109 128
     def test_match_with_no_group_dollar(self):
110 129
         rule = rules.RedirectMatch(
111 130
             1,

Loading…
Cancel
Save