1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
7 # As a special exception, if other files instantiate templates or use macros
8 # or inline functions from this file, or you compile this file and link it
9 # with other works to produce a work based on this file, this file
10 # does not by itself cause the resulting work to be covered
11 # by the GNU General Public License.
13 # However the source code for this file must still be made available
14 # in accordance with section (3) of the GNU General Public License.
16 # This exception does not invalidate any other reasons why a work based
17 # on this file might be covered by the GNU General Public License.
19 """ test_argparse_helpers.py: unit tests for argparse_helpers
21 Test classes and functions in argparse_helpers
23 For help see :py:mod:`unittest`
25 .. codeauthor:: Intra2net
30 from os.path import isfile
31 from tempfile import mkstemp
33 # relative import of tested module ensures we do not test installed version
35 from src import argparse_helpers
36 except ImportError as ie:
37 raise RuntimeError('Failed to import tested module argparse_helpers: {}'
41 class ArgparseTester(unittest.TestCase):
42 """ only test case in this module, see module doc for more help """
44 def test_non_exit(self):
45 """Check that NonExitingParser raises the right exception."""
46 parser = argparse_helpers.NonExitingParser()
47 parser.add_argument('-a', type=int)
48 self.assertRaises(argparse_helpers.ArgParserWantsExit,
49 parser.parse_args, ['-a', 'blubb'])
51 def test_existing_file(self):
56 temp_handle, temp_file = mkstemp()
57 os.write(temp_handle, b'bla')
61 # test with existing file
62 parser = argparse_helpers.NonExitingParser()
63 parser.add_argument('--input', type=argparse_helpers.existing_file)
67 if temp_file and isfile(temp_file):
70 # test with non-existing file
71 self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
72 'is not an existing file',
74 ['--input', temp_file, ])
76 if __name__ == '__main__':