From 925d6b9ab7a1043c4a39e393618856cc9fe9c301 Mon Sep 17 00:00:00 2001
From: Gabriel Studer <gabriel.studer@unibas.ch>
Date: Mon, 11 Dec 2023 15:09:20 +0100
Subject: [PATCH] fix unit test

pm3_argparse explicitely checks for expected output. That output may
differ for different argparse versions. The fix reduces the checks
to certain key elements.
---
 core/tests/test_pm3argparse.py | 53 ++++++++++++----------------------
 1 file changed, 19 insertions(+), 34 deletions(-)

diff --git a/core/tests/test_pm3argparse.py b/core/tests/test_pm3argparse.py
index ab5270de..1f181407 100644
--- a/core/tests/test_pm3argparse.py
+++ b/core/tests/test_pm3argparse.py
@@ -90,36 +90,29 @@ class PM3ArgParseTests(unittest.TestCase):
         with self.assertRaises(SystemExit) as ecd:
             parser.Parse(['--help'])
         self.assertEqual(ecd.exception.code, 0)
-        self.assertEqual(ecd.exception.code, 0)
-        if sys.version_info >= (3,11):
-            self.assertEqual(self.log.messages['SCRIPT'],
-                             ['usage: test_pm3argparse.py [-h]\n\nTesting our '+
-                              'own little argument parser.\n\noptions:\n  -h, '+
-                              '--help  show this help message and exit'])
-        else:
-            self.assertEqual(self.log.messages['SCRIPT'],
-                             ['usage: test_pm3argparse.py [-h]\n\nTesting our '+
-                              'own little argument parser.\n\noptional '+
-                              'arguments:\n  -h, --help  show this help '+
-                              'message and exit'])
+
+        # output of self.log.messages['SCRIPT'] depends on version of argparse
+        # module observed output:
+        # 
+        # 'usage: test_pm3argparse.py [-h]\n\nTesting our '+
+        # 'own little argument parser.\n\noptions:\n  -h, '+
+        # '--help  show this help message and exit'
+        #
+        # 'usage: test_pm3argparse.py [-h]\n\nTesting our '+
+        # 'own little argument parser.\n\noptional '+
+        # 'arguments:\n  -h, --help  show this help '+
+        # 'message and exit'
+        #
+        # we just check whether it starts with "usage: "
+        self.assertTrue(self.log.messages['SCRIPT'][0].startswith("usage: "))
 
         self.log.messages = dict()
         parser = pm3argparse.PM3ArgumentParser(__doc__, action=True)
         with self.assertRaises(SystemExit) as ecd:
             parser.Parse(['--help'])
         self.assertEqual(ecd.exception.code, 0)
-        if sys.version_info >= (3,11):
-            self.assertEqual(self.log.messages['SCRIPT'],
-                             ['usage: t_pm3argparse.py [-h]\n\nTesting our '+
-                              'own little argument parser.\n\noptions:\n  '+
-                              '-h, --help  show this help message and exit'])
-        else:
-            self.assertEqual(self.log.messages['SCRIPT'],
-                             ['usage: t_pm3argparse.py [-h]\n\nTesting our '+
-                              'own little argument parser.\n\noptional '+
-                              'arguments:\n  -h, --help  show this help '+
-                              'message and exit'])
-
+        # see long comment above...
+        self.assertTrue(self.log.messages['SCRIPT'][0].startswith("usage: "))
 
     def testDescription(self):
         parser = pm3argparse.PM3ArgumentParser(action=False,
@@ -127,16 +120,8 @@ class PM3ArgParseTests(unittest.TestCase):
         with self.assertRaises(SystemExit) as ecd:
             parser.Parse(['--help'])
         self.assertEqual(ecd.exception.code, 0)
-        if sys.version_info >= (3,11):
-            self.assertEqual(self.log.messages['SCRIPT'],
-                             ['usage: test_pm3argparse.py [-h]\n\nThis is a '+
-                              'test.\n\noptions:\n  -h, --help  '+
-                              'show this help message and exit'])
-        else:
-            self.assertEqual(self.log.messages['SCRIPT'],
-                             ['usage: test_pm3argparse.py [-h]\n\nThis is a '+
-                              'test.\n\noptional arguments:\n  -h, --help  '+
-                              'show this help message and exit'])
+        tmp = ''.join(self.log.messages['SCRIPT'])
+        self.assertTrue(tmp.find("This is a test")!=-1)
 
     def testAddAlignmentNoFileArg(self):
         # check failure on missing file argument
-- 
GitLab