]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
python: tests: function to generate a unique name from caller
authorRob van der Linde <rob@catalyst.net.nz>
Thu, 12 Oct 2023 02:21:08 +0000 (15:21 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 26 Oct 2023 23:32:34 +0000 (23:32 +0000)
Uses the caller function to generate a unique name from the test function name.

Unique name is converted to camel case

Signed-off-by: Rob van der Linde <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/__init__.py

index 27788a9fa45d77adbea7b563fc11d0356d6fe801..5ba18c9fdcbe3698f22aeaa9bcd15ae4e698f7ee 100644 (file)
@@ -61,6 +61,8 @@ HEXDUMP_FILTER = bytearray([x if ((len(repr(chr(x))) == 3) and (x < 127)) else o
 
 LDB_ERR_LUT = {v: k for k, v in vars(ldb).items() if k.startswith('ERR_')}
 
+RE_CAMELCASE = re.compile(r"([_\-])+")
+
 
 def ldb_err(v):
     if isinstance(v, ldb.LdbError):
@@ -165,6 +167,23 @@ class TestCase(unittest.TestCase):
         msg = "%s needs setUpDynamicTestCases() if @DynamicTestCase is used!" % (cls)
         raise Exception(msg)
 
+    def unique_name(self):
+        """Generate a unique name from within a test for creating objects.
+
+        Used to ensure each test generates uniquely named objects that don't
+        interfere with other tests.
+        """
+        # name of calling function
+        name = self.id().rsplit(".", 1)[1]
+
+        # remove test_ prefix
+        if name.startswith("test_"):
+            name = name[5:]
+
+        # finally, convert to camelcase
+        name = RE_CAMELCASE.sub(" ", name).title().replace(" ", "")
+        return "".join([name[0].lower(), name[1:]])
+
     def setUp(self):
         super(TestCase, self).setUp()
         test_debug_level = os.getenv("TEST_DEBUG_LEVEL")