Fix: bt2: _trim_docstring(): docstring can have 0 or 1 line
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 10 Sep 2020 20:47:52 +0000 (16:47 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 10 Sep 2020 22:07:55 +0000 (18:07 -0400)
It is possible that a user component class's docstring has no lines or a
single one, for example:

    # no line
    class MySink(bt2._UserSinkComponent):
        """"""

        def _user_consume(self):
            pass

    # single line
    class MySink(bt2._UserSinkComponent):
        """The single line"""

        def _user_consume(self):
            pass

The previous version of _trim_docstring() expects this format:

    class MySink(bt2._UserSinkComponent):
        """
        My sink's description

        Dolore officia ex et aliquip eiusmod enim pariatur reprehenderit
        ad adipisicing non occaecat ullamco aliquip laborum duis
        proident ex duis.

        Irure commodo proident esse non pariatur in aute cillum id aute.
        """

        def _user_consume(self):
            pass

In _trim_docstring(), accept no lines or a single one.

Adding new tests to `test_component_class.py` to validate this
behaviour. The

    # fmt: off
    """
    """
    # fmt: on

docstring has off/on formatting markers as Black 20.8b1 transforms this
into the non-equivalent

    """"""

(which is another test now).

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ia12b0e9bfd4d1e1aaa86f0c8c207c3c1535f5c3e
Reviewed-on: https://review.lttng.org/c/babeltrace/+/4072

src/bindings/python/bt2/bt2/component.py
tests/bindings/python/bt2/test_component_class.py

index b8ca19f24bfcb840bff421adaaaaeb942d2d5535..5395e0b6b59ccf11699d5a1c76461e928c5c0671 100644 (file)
@@ -327,17 +327,22 @@ def _create_component_class_from_const_ptr_and_get_ref(ptr, comp_cls_type):
 
 def _trim_docstring(docstring):
     lines = docstring.expandtabs().splitlines()
+
+    if len(lines) == 0:
+        return ''
+
     indent = sys.maxsize
 
-    for line in lines[1:]:
-        stripped = line.lstrip()
+    if len(lines) > 1:
+        for line in lines[1:]:
+            stripped = line.lstrip()
 
-        if stripped:
-            indent = min(indent, len(line) - len(stripped))
+            if stripped:
+                indent = min(indent, len(line) - len(stripped))
 
     trimmed = [lines[0].strip()]
 
-    if indent < sys.maxsize:
+    if indent < sys.maxsize and len(lines) > 1:
         for line in lines[1:]:
             trimmed.append(line[indent:].rstrip())
 
index 685a0b996487f7ba75b607a263270f0561972ecc..bee368a13055f0553b0307358419f483ea65aa91 100644 (file)
@@ -114,16 +114,38 @@ class UserComponentClassTestCase(unittest.TestCase):
 
         self.assertEqual(MySink.description, 'The description.')
 
-    def test_empty_description(self):
+    def test_empty_description_no_lines(self):
         class MySink(bt2._UserSinkComponent):
+            # fmt: off
+            """"""
+            # fmt: on
+
+            def _user_consume(self):
+                pass
+
+        self.assertIsNone(MySink.description)
+
+    def test_empty_description_no_contents(self):
+        class MySink(bt2._UserSinkComponent):
+            # fmt: off
             """
             """
+            # fmt: on
 
             def _user_consume(self):
                 pass
 
         self.assertIsNone(MySink.description)
 
+    def test_empty_description_single_line(self):
+        class MySink(bt2._UserSinkComponent):
+            """my description"""
+
+            def _user_consume(self):
+                pass
+
+        self.assertEqual(MySink.description, "my description")
+
     def test_help(self):
         class MySink(bt2._UserSinkComponent):
             """
This page took 0.033736 seconds and 4 git commands to generate.