Create argparse type "existing_dir[_empty]"
[pyi2ncommon] / test / test_argparse_helpers.py
CommitLineData
df4b14b0
CH
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
7628bc48 21Test classes and functions in argparse_helpers
df4b14b0
CH
22
23For help see :py:mod:`unittest`
24
25.. codeauthor:: Intra2net
26"""
27
df4b14b0
CH
28import unittest
29import os
f02ce4ca
CH
30from os.path import isfile, isdir
31from shutil import rmtree
32from tempfile import mkstemp, mkdtemp
df4b14b0
CH
33
34# relative import of tested module ensures we do not test installed version
35try:
36 from src import argparse_helpers
37except ImportError as ie:
38 raise RuntimeError('Failed to import tested module argparse_helpers: {}'
39 .format(ie))
40
41
42class ArgparseTester(unittest.TestCase):
43 """ only test case in this module, see module doc for more help """
44
45 def test_non_exit(self):
46 """Check that NonExitingParser raises the right exception."""
47 parser = argparse_helpers.NonExitingParser()
48 parser.add_argument('-a', type=int)
49 self.assertRaises(argparse_helpers.ArgParserWantsExit,
50 parser.parse_args, ['-a', 'blubb'])
51
52 def test_existing_file(self):
53 temp_file = None
54 temp_handle = None
55 try:
56 # create a file
57 temp_handle, temp_file = mkstemp()
58 os.write(temp_handle, b'bla')
59 os.close(temp_handle)
60 temp_handle = None
61
62 # test with existing file
63 parser = argparse_helpers.NonExitingParser()
64 parser.add_argument('--input', type=argparse_helpers.existing_file)
f02ce4ca
CH
65 parser.add_argument('--input2', type=argparse_helpers.existing_file_or_empty)
66 parser.parse_args(['--input', temp_file, '--input2', temp_file])
df4b14b0
CH
67 finally:
68 if temp_handle:
69 os.close(temp_handle)
70 if temp_file and isfile(temp_file):
71 os.remove(temp_file)
72
7628bc48
CH
73 # test with non-existing file
74 self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
75 'is not an existing file',
76 parser.parse_args,
77 ['--input', temp_file, ])
78
f02ce4ca
CH
79 def test_existing_dir(self):
80 temp_dir = None
81 try:
82 # create a dir
83 temp_dir = mkdtemp()
84
85 # test with existing dir
86 parser = argparse_helpers.NonExitingParser()
87 parser.add_argument('--input', type=argparse_helpers.existing_dir)
88 parser.add_argument('--input2', type=argparse_helpers.existing_dir_or_empty)
89 parser.parse_args(['--input', temp_dir, '--input2', temp_dir])
90 finally:
91 if temp_dir and isdir(temp_dir):
92 rmtree(temp_dir)
93
94 # test with non-existing dir
95 self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
96 'is not an existing directory',
97 parser.parse_args,
98 ['--input', temp_dir, ])
99
100 def test_allow_empty(self):
101 parser = argparse_helpers.NonExitingParser()
102 parser.add_argument('--file', type=argparse_helpers.existing_file_or_empty)
103 parser.add_argument('--dir', type=argparse_helpers.existing_dir_or_empty)
104
105 # both emtpy: no problem
106 parser.parse_args(['--file', '', '--dir', ''])
107
108 # test with non-existing file/dir
109 self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
110 'is not an existing file',
111 parser.parse_args,
112 ['--dir', '', '--file', 'not-an-existing-file'])
113 self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit,
114 'is not an existing directory',
115 parser.parse_args,
116 ['--file', '', '--dir', 'not-an-existing-dir'])
117
118
df4b14b0
CH
119if __name__ == '__main__':
120 unittest.main()