[Lldb-commits] [lldb] 2f84b59 - [lldb] Also Catch invalid calls to TestPExpectTest's expect()

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Thu Nov 5 05:09:06 PST 2020


Author: Raphael Isemann
Date: 2020-11-05T14:08:46+01:00
New Revision: 2f84b59a4cf92a0ce1b985e7f44e17483efa33c0

URL: https://github.com/llvm/llvm-project/commit/2f84b59a4cf92a0ce1b985e7f44e17483efa33c0
DIFF: https://github.com/llvm/llvm-project/commit/2f84b59a4cf92a0ce1b985e7f44e17483efa33c0.diff

LOG: [lldb] Also Catch invalid calls to TestPExpectTest's expect()

This is a follow up to D88792 which found an issue in a call to PExpectTest's
expect function that allows passing a string to the `substrs` parameter. However
this issue was found by just grepping and TestPExpect's expect function is still
accepting a single string as a value to `substrs`.

This patch adds the same sanity check that D88792 added to the PExpectTest's
implementation of `expect` and also adds a small test for it.

Reviewed By: kastiglione, JDevlieghere

Differential Revision: https://reviews.llvm.org/D89302

Added: 
    lldb/test/API/test_utils/TestPExpectTest.py

Modified: 
    lldb/packages/Python/lldbsuite/test/lldbpexpect.py

Removed: 
    


################################################################################
diff  --git a/lldb/packages/Python/lldbsuite/test/lldbpexpect.py b/lldb/packages/Python/lldbsuite/test/lldbpexpect.py
index 12ac4d381e2c..1a6c32094ba8 100644
--- a/lldb/packages/Python/lldbsuite/test/lldbpexpect.py
+++ b/lldb/packages/Python/lldbsuite/test/lldbpexpect.py
@@ -54,6 +54,10 @@ def launch(self, executable=None, extra_args=None, timeout=30, dimensions=None):
     def expect(self, cmd, substrs=None):
         self.assertNotIn('\n', cmd)
         self.child.sendline(cmd)
+        # If 'substrs' is a string then this code would just check that every
+        # character of the string is in the output.
+        assert not isinstance(substrs, six.string_types), \
+            "substrs must be a collection of strings"
         if substrs is not None:
             for s in substrs:
                 self.child.expect_exact(s)

diff  --git a/lldb/test/API/test_utils/TestPExpectTest.py b/lldb/test/API/test_utils/TestPExpectTest.py
new file mode 100644
index 000000000000..c754f1faa7ec
--- /dev/null
+++ b/lldb/test/API/test_utils/TestPExpectTest.py
@@ -0,0 +1,29 @@
+"""
+Test the PExpectTest test functions.
+"""
+
+
+import lldb
+import lldbsuite.test.lldbutil as lldbutil
+from lldbsuite.test.lldbtest import *
+from textwrap import dedent
+
+
+class TestPExpectTestCase(TestBase):
+
+    mydir = TestBase.compute_mydir(__file__)
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def assert_expect_fails_with(self, cmd, expect_args, expected_msg):
+        try:
+            self.expect(cmd, **expect_args)
+        except AssertionError as e:
+            self.assertIn(expected_msg, str(e))
+        else:
+            self.fail("expect should have raised AssertionError!")
+
+    def test_expect(self):
+        # Test that passing a string to the 'substrs' argument is rejected.
+        self.assert_expect_fails_with("settings list prompt",
+            dict(substrs="some substring"),
+            "substrs must be a collection of strings")


        


More information about the lldb-commits mailing list