From df4b14b040e7153fe12ae5d394db2b9672bc83ba Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Fri, 15 Jun 2018 11:06:26 +0200 Subject: [PATCH] Create unittest for argpars_helpers --- test/test_argparse_helpers.py | 90 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 90 insertions(+), 0 deletions(-) create mode 100644 test/test_argparse_helpers.py diff --git a/test/test_argparse_helpers.py b/test/test_argparse_helpers.py new file mode 100644 index 0000000..ab70db9 --- /dev/null +++ b/test/test_argparse_helpers.py @@ -0,0 +1,90 @@ +# 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() -- 1.7.1