--- /dev/null
+# 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()