Create unittest for argpars_helpers
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 15 Jun 2018 09:06:26 +0000 (11:06 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 8 Oct 2018 07:33:59 +0000 (09:33 +0200)
test/test_argparse_helpers.py [new file with mode: 0644]

diff --git a/test/test_argparse_helpers.py b/test/test_argparse_helpers.py
new file mode 100644 (file)
index 0000000..ab70db9
--- /dev/null
@@ -0,0 +1,90 @@
+# The software in this package is distributed under the GNU General
+# Public License version 2 (with a special exception described below).
+#
+# A copy of GNU General Public License (GPL) is included in this distribution,
+# in the file COPYING.GPL.
+#
+# As a special exception, if other files instantiate templates or use macros
+# or inline functions from this file, or you compile this file and link it
+# with other works to produce a work based on this file, this file
+# does not by itself cause the resulting work to be covered
+# by the GNU General Public License.
+#
+# However the source code for this file must still be made available
+# in accordance with section (3) of the GNU General Public License.
+#
+# This exception does not invalidate any other reasons why a work based
+# on this file might be covered by the GNU General Public License.
+
+""" test_argparse_helpers.py: unit tests for argparse_helpers
+
+Tests classes and functions in argparse_helpers
+
+For help see :py:mod:`unittest`
+
+.. codeauthor:: Intra2net
+"""
+
+from __future__ import print_function
+from __future__ import absolute_import
+
+import unittest
+import os
+from os.path import isfile
+from tempfile import mkstemp
+import sys
+
+# relative import of tested module ensures we do not test installed version
+try:
+    from src import argparse_helpers
+except ImportError as ie:
+    raise RuntimeError('Failed to import tested module argparse_helpers: {}'
+                       .format(ie))
+
+
+class ArgparseTester(unittest.TestCase):
+    """ only test case in this module, see module doc for more help """
+
+    def test_non_exit(self):
+        """Check that NonExitingParser raises the right exception."""
+        parser = argparse_helpers.NonExitingParser()
+        parser.add_argument('-a', type=int)
+        self.assertRaises(argparse_helpers.ArgParserWantsExit,
+                          parser.parse_args, ['-a', 'blubb'])
+
+    def test_existing_file(self):
+        temp_file = None
+        temp_handle = None
+        try:
+            # create a file
+            temp_handle, temp_file = mkstemp()
+            os.write(temp_handle, b'bla')
+            os.close(temp_handle)
+            temp_handle = None
+
+            # test with existing file
+            parser = argparse_helpers.NonExitingParser()
+            parser.add_argument('--input', type=argparse_helpers.existing_file)
+            parser.parse_args(['--input', temp_file, ])
+
+            # remove file
+            os.remove(temp_file)
+
+            # test with non-existing file
+            if sys.version_info.major < 3:
+                self.assertRaises(argparse_helpers.ArgParserWantsExit,
+                                  parser.parse_args, ['--input', temp_file, ])
+            else:
+                self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
+                                       'is not an existing file',
+                                       parser.parse_args,
+                                       ['--input', temp_file, ])
+            temp_file = None
+        finally:
+            if temp_handle:
+                os.close(temp_handle)
+            if temp_file and isfile(temp_file):
+                os.remove(temp_file)
+
+if __name__ == '__main__':
+    unittest.main()