# 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 Test classes and functions in argparse_helpers For help see :py:mod:`unittest` .. codeauthor:: Intra2net """ import unittest import os from os.path import isfile, isdir from shutil import rmtree from tempfile import mkstemp, mkdtemp # 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.add_argument('--input2', type=argparse_helpers.existing_file_or_empty) parser.parse_args(['--input', temp_file, '--input2', temp_file]) finally: if temp_handle: os.close(temp_handle) if temp_file and isfile(temp_file): os.remove(temp_file) # test with non-existing file self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit, 'is not an existing file', parser.parse_args, ['--input', temp_file, ]) def test_existing_dir(self): temp_dir = None try: # create a dir temp_dir = mkdtemp() # test with existing dir parser = argparse_helpers.NonExitingParser() parser.add_argument('--input', type=argparse_helpers.existing_dir) parser.add_argument('--input2', type=argparse_helpers.existing_dir_or_empty) parser.parse_args(['--input', temp_dir, '--input2', temp_dir]) finally: if temp_dir and isdir(temp_dir): rmtree(temp_dir) # test with non-existing dir self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit, 'is not an existing directory', parser.parse_args, ['--input', temp_dir, ]) def test_allow_empty(self): parser = argparse_helpers.NonExitingParser() parser.add_argument('--file', type=argparse_helpers.existing_file_or_empty) parser.add_argument('--dir', type=argparse_helpers.existing_dir_or_empty) # both emtpy: no problem parser.parse_args(['--file', '', '--dir', '']) # test with non-existing file/dir self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit, 'is not an existing file', parser.parse_args, ['--dir', '', '--file', 'not-an-existing-file']) self.assertRaisesRegex(argparse_helpers.ArgParserWantsExit, 'is not an existing directory', parser.parse_args, ['--file', '', '--dir', 'not-an-existing-dir']) if __name__ == '__main__': unittest.main()