]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: py: Fix coloring of differences
authorPhil Sutter <phil@nwl.cc>
Fri, 24 Aug 2018 11:27:25 +0000 (13:27 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Thu, 30 Aug 2018 10:11:45 +0000 (12:11 +0200)
This was surprisingly hard to get right, but this should do the trick.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
tests/py/nft-test.py

index e4367adc561776fbc6be6156351094166da816a1..1b7e04c9d0b606b6980d225e9fd037c7202d059f 100755 (executable)
@@ -134,32 +134,29 @@ def print_info(reason, filename=None, lineno=None):
 def color_differences(rule, other, color):
     rlen = len(rule)
     olen = len(other)
+    out = ""
 
     # find equal part at start
     for i in range(rlen):
         if i >= olen or rule[i] != other[i]:
             break
-    start_idx = i
+    if i > 0:
+        out += rule[:i]
+        rule = rule[i:]
+        other = other[i:]
+        rlen = len(rule)
+        olen = len(other)
 
     # find equal part at end
-    found = False
-    for i in range(-1, start_idx -rlen - 1, -1):
-        if i < start_idx -olen:
-            break
-        if rule[i] != other[i]:
-            found = True
+    for i in range(1, rlen + 1):
+        if i > olen or rule[rlen - i] != other[olen - i]:
+            i -= 1
             break
-    end_idx = i
-    if found:
-        end_idx += 1
-
-    out = ""
-    if start_idx > 0:
-        out += rule[:start_idx]
-    out += color + rule[start_idx:end_idx] + Colors.ENDC
-    if end_idx < 0:
-        out += rule[end_idx:]
+    if rlen > i:
+        out += color + rule[:rlen - i] + Colors.ENDC
+        rule = rule[rlen - i:]
 
+    out += rule
     return out
 
 def print_differences_warning(filename, lineno, rule1, rule2, cmd):