From e9e82cd34d7fe0de13e8c896f4b5f322757d8afb Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Wed, 3 Aug 2022 08:13:17 -0600 Subject: Test framework improvements Replaces one custom function with one from Python stdlib (textwrap.dedent) Fixes one test case that did not check for None Changes "fill line with a character" implementations to use Python string formatting, which has this capability. Signed-off-by: Mats Wichmann --- testing/framework/TestCmd.py | 2 +- testing/framework/TestCmdTests.py | 3 ++- testing/framework/TestCommonTests.py | 22 ++++++++-------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/testing/framework/TestCmd.py b/testing/framework/TestCmd.py index fc5e328..039f9e0 100644 --- a/testing/framework/TestCmd.py +++ b/testing/framework/TestCmd.py @@ -1088,7 +1088,7 @@ class TestCmd: def banner(self, s, width=None): if width is None: width = self.banner_width - return s + self.banner_char * (width - len(s)) + return f"{s:{self.banner_char}<{width}}" escape = staticmethod(escape) diff --git a/testing/framework/TestCmdTests.py b/testing/framework/TestCmdTests.py index a760382..6f2dac5 100644 --- a/testing/framework/TestCmdTests.py +++ b/testing/framework/TestCmdTests.py @@ -2208,7 +2208,8 @@ sys.stderr.write("run2 STDERR second line\\n") except IndexError: pass else: - raise IndexError("got unexpected output:\n" + output) + if output is not None: + raise IndexError("got unexpected output:\n" + output) test.program_set('run1') test.run(arguments = 'foo bar') test.program_set('run2') diff --git a/testing/framework/TestCommonTests.py b/testing/framework/TestCommonTests.py index 1f8db42..525f263 100644 --- a/testing/framework/TestCommonTests.py +++ b/testing/framework/TestCommonTests.py @@ -27,6 +27,7 @@ import re import signal import sys import unittest +from textwrap import dedent # Strip the current directory so we get the right TestCommon.py module. sys.path = sys.path[1:] @@ -34,15 +35,9 @@ sys.path = sys.path[1:] import TestCmd import TestCommon +# this used to be a custom function, now use the stdlib equivalent def lstrip(s): - lines = [ _.expandtabs() for _ in s.split('\n') ] - if lines[0] == '': - lines = lines[1:] - spaces = len(re.match('^( *).*', lines[0]).group(1)) - if spaces: - lines = [ l[spaces:] for l in lines ] - return '\n'.join(lines) - + return dedent(s) expected_newline = '\\n' @@ -52,17 +47,16 @@ def assert_display(expect, result, error=None): expect = expect.pattern except AttributeError: pass - result = [ + display = [ '\n', - 'EXPECTED'+('*'*80) + '\n', + f'{"EXPECTED: " :*<80}' + '\n', expect, - 'GOT'+('*'*80) + '\n', + f'{"GOT: " :*<80}' + '\n', result, + error if error else '', ('*'*80) + '\n', ] - if error: - result.append(error) - return ''.join(result) + return ''.join(display) class TestCommonTestCase(unittest.TestCase): -- cgit v0.12