From 62e39ed058ce340ead5d878a386caa9e65676f63 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Fri, 24 Aug 2018 13:27:25 +0200 Subject: tests: py: Fix coloring of differences This was surprisingly hard to get right, but this should do the trick. Signed-off-by: Phil Sutter Signed-off-by: Pablo Neira Ayuso --- tests/py/nft-test.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'tests/py/nft-test.py') diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py index e4367adc..1b7e04c9 100755 --- a/tests/py/nft-test.py +++ b/tests/py/nft-test.py @@ -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): -- cgit v1.2.3