python:tests: Ensure that we don’t overwrite tests
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Thu, 25 May 2023 05:03:48 +0000 (17:03 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Mon, 29 May 2023 22:32:28 +0000 (22:32 +0000)
If the file iterator returns two entries with the same name, one may
overwrite the other.

script_iterator() currently ensures this won’t happen, but it pays to be
safe.

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/usage.py

index c75f18cb43b36ef8d0a46d426f97c5985a6bb1cc..8441ea03b7361eb1edbed9de9395da16ffbe5ffd 100644 (file)
@@ -255,7 +255,10 @@ class PythonScriptUsageTests(TestCase):
                 self.assertIn('usage', out.lower() + err.lower(),
                               'stdout:\n%s\nstderr:\n%s' % (out, err))
 
-            setattr(cls, 'test_%s' % name, _f)
+            attr = 'test_%s' % name
+            if hasattr(cls, attr):
+                raise RuntimeError(f'Usage test ‘{attr}’ already exists!')
+            setattr(cls, attr, _f)
 
 
 class HelpTestSuper(TestCase):
@@ -344,7 +347,10 @@ class HelpTestSuper(TestCase):
                     if self.check_multiline:
                         self.assertIn('\n', out, 'expected multi-line output')
 
-            setattr(cls, 'test_%s' % name, _f)
+            attr = 'test_%s' % name
+            if hasattr(cls, attr):
+                raise RuntimeError(f'Usage test ‘{attr}’ already exists!')
+            setattr(cls, attr, _f)
 
 
 class PythonScriptHelpTests(HelpTestSuper):