ab70db968c04ecbe837b7235ab86af050ba8ad56
[pyi2ncommon] / test / test_argparse_helpers.py
1 # The software in this package is distributed under the GNU General
2 # Public License version 2 (with a special exception described below).
3 #
4 # A copy of GNU General Public License (GPL) is included in this distribution,
5 # in the file COPYING.GPL.
6 #
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.
12 #
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.
15 #
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.
18
19 """ test_argparse_helpers.py: unit tests for argparse_helpers
20
21 Tests classes and functions in argparse_helpers
22
23 For help see :py:mod:`unittest`
24
25 .. codeauthor:: Intra2net
26 """
27
28 from __future__ import print_function
29 from __future__ import absolute_import
30
31 import unittest
32 import os
33 from os.path import isfile
34 from tempfile import mkstemp
35 import sys
36
37 # relative import of tested module ensures we do not test installed version
38 try:
39     from src import argparse_helpers
40 except ImportError as ie:
41     raise RuntimeError('Failed to import tested module argparse_helpers: {}'
42                        .format(ie))
43
44
45 class ArgparseTester(unittest.TestCase):
46     """ only test case in this module, see module doc for more help """
47
48     def test_non_exit(self):
49         """Check that NonExitingParser raises the right exception."""
50         parser = argparse_helpers.NonExitingParser()
51         parser.add_argument('-a', type=int)
52         self.assertRaises(argparse_helpers.ArgParserWantsExit,
53                           parser.parse_args, ['-a', 'blubb'])
54
55     def test_existing_file(self):
56         temp_file = None
57         temp_handle = None
58         try:
59             # create a file
60             temp_handle, temp_file = mkstemp()
61             os.write(temp_handle, b'bla')
62             os.close(temp_handle)
63             temp_handle = None
64
65             # test with existing file
66             parser = argparse_helpers.NonExitingParser()
67             parser.add_argument('--input', type=argparse_helpers.existing_file)
68             parser.parse_args(['--input', temp_file, ])
69
70             # remove file
71             os.remove(temp_file)
72
73             # test with non-existing file
74             if sys.version_info.major < 3:
75                 self.assertRaises(argparse_helpers.ArgParserWantsExit,
76                                   parser.parse_args, ['--input', temp_file, ])
77             else:
78                 self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
79                                        'is not an existing file',
80                                        parser.parse_args,
81                                        ['--input', temp_file, ])
82             temp_file = None
83         finally:
84             if temp_handle:
85                 os.close(temp_handle)
86             if temp_file and isfile(temp_file):
87                 os.remove(temp_file)
88
89 if __name__ == '__main__':
90     unittest.main()