All of lore.kernel.org
 help / color / mirror / Atom feed
* [iptables PATCH] tests: iptables-test: Fix conditional colors on stderr
@ 2021-09-22 11:39 Phil Sutter
  0 siblings, 0 replies; only message in thread
From: Phil Sutter @ 2021-09-22 11:39 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: netfilter-devel

Štěpán's patch to make colored output depend on whether output is a TTY
clashed with my change to print errors to stderr instead of stdout.

Fix this by telling maybe_colored() if it should print colors or not as
only caller knows where output is sent to.

Signed-off-by: Phil Sutter <phil@nwl.cc>
---
 iptables-test.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/iptables-test.py b/iptables-test.py
index a876f616dae4c..0ba3d36864fd7 100755
--- a/iptables-test.py
+++ b/iptables-test.py
@@ -33,15 +33,16 @@ LOGFILE="/tmp/iptables-test.log"
 log_file = None
 
 STDOUT_IS_TTY = sys.stdout.isatty()
+STDERR_IS_TTY = sys.stderr.isatty()
 
-def maybe_colored(color, text):
+def maybe_colored(color, text, isatty):
     terminal_sequences = {
         'green': '\033[92m',
         'red': '\033[91m',
     }
 
     return (
-        terminal_sequences[color] + text + '\033[0m' if STDOUT_IS_TTY else text
+        terminal_sequences[color] + text + '\033[0m' if isatty else text
     )
 
 
@@ -49,7 +50,7 @@ def print_error(reason, filename=None, lineno=None):
     '''
     Prints an error with nice colors, indicating file and line number.
     '''
-    print(filename + ": " + maybe_colored('red', "ERROR") +
+    print(filename + ": " + maybe_colored('red', "ERROR", STDERR_IS_TTY) +
         ": line %d (%s)" % (lineno, reason), file=sys.stderr)
 
 
@@ -288,7 +289,7 @@ def run_test_file(filename, netns):
     if netns:
         execute_cmd("ip netns del ____iptables-container-test", filename, 0)
     if total_test_passed:
-        print(filename + ": " + maybe_colored('green', "OK"))
+        print(filename + ": " + maybe_colored('green', "OK", STDOUT_IS_TTY))
 
     f.close()
     return tests, passed
-- 
2.33.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2021-09-22 11:40 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-22 11:39 [iptables PATCH] tests: iptables-test: Fix conditional colors on stderr Phil Sutter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.