From patchwork Tue May 8 11:08:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Sutter X-Patchwork-Id: 910127 X-Patchwork-Delegate: pablo@netfilter.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netfilter-devel-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=nwl.cc Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40gGvt1mZdz9s0W for ; Tue, 8 May 2018 21:09:14 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752399AbeEHLJN (ORCPT ); Tue, 8 May 2018 07:09:13 -0400 Received: from orbyte.nwl.cc ([151.80.46.58]:36686 "EHLO orbyte.nwl.cc" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752239AbeEHLJN (ORCPT ); Tue, 8 May 2018 07:09:13 -0400 Received: from localhost ([::1]:51962 helo=tatos) by orbyte.nwl.cc with esmtp (Exim 4.90_1) (envelope-from ) id 1fG0Ua-0003rK-58; Tue, 08 May 2018 13:09:12 +0200 From: Phil Sutter To: Pablo Neira Ayuso Cc: netfilter-devel@vger.kernel.org Subject: [nft PATCH v3 13/14] tests/py: Highlight offending parts in differences warnings Date: Tue, 8 May 2018 13:08:44 +0200 Message-Id: <20180508110845.26364-14-phil@nwl.cc> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180508110845.26364-1-phil@nwl.cc> References: <20180508110845.26364-1-phil@nwl.cc> Sender: netfilter-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netfilter-devel@vger.kernel.org Print the non-equal parts of the two rules in yellow when printing the differences warning. Signed-off-by: Phil Sutter --- tests/py/nft-test.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py index 5f00b7d658f55..9ec38c24bdd37 100755 --- a/tests/py/nft-test.py +++ b/tests/py/nft-test.py @@ -121,8 +121,41 @@ def print_warning(reason, filename=None, lineno=None): print_msg(reason, filename, lineno, Colors.YELLOW, "WARNING:") +def color_differences(rule, other, color): + rlen = len(rule) + olen = len(other) + + # find equal part at start + for i in range(rlen): + if i >= olen or rule[i] != other[i]: + break + start_idx = i + + # 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 + 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:] + + return out + def print_differences_warning(filename, lineno, rule1, rule2, cmd): - reason = "'" + rule1 + "' mismatches '" + rule2 + "'" + colored_rule1 = color_differences(rule1, rule2, Colors.YELLOW) + colored_rule2 = color_differences(rule2, rule1, Colors.YELLOW) + reason = "'" + colored_rule1 + "' mismatches '" + colored_rule2 + "'" print filename + ": " + Colors.YELLOW + "WARNING: " + Colors.ENDC + \ "line: " + str(lineno + 1) + ": '" + cmd + "': " + reason