#!/local/bin/python
'''
----------------
standard_argparse.py
A module containing tests for the standard argparser options I'm specifying
in 'standard_argparse.py'. These tests use the standard `unittest
<http://docs.python.org/2/library/unittest.html>`_ package and extend the
:py:class`unittest.TestCase` class.
.. moduleauthor:: Nick Schurch <nschurch@dundee.ac.uk>
:created_on: 2013-04-08
----------------
'''
__version__ = "1.0"
import unittest, os, tempfile, shutil, warnings
import script_options.standard_parsers as sp
[docs]class TestStandardArgparse(unittest.TestCase):
'''Tests for the standard argparse parser and options I like'''
[docs] def setUp(self):
prog = "TestParser"
desc = "A test parser for testing!"
self.parser = sp.standard_parser(1.0, prog=prog, description=desc)
# set up an existing path and file
self.existing_path = tempfile.mkdtemp()
self.existing_file = "%s/thisfileexists.txt" % self.existing_path
temp_file = open(self.existing_file, "w")
temp_file.write("this exists")
temp_file.close()
# make up a mythical path and file based on the existing dir
self.nonexisting_path = self.existing_path+"/testfornonexistingdir"
self.nonexisting_file = self.nonexisting_path+"/thisdoesntexists.txt"
[docs] def tearDown(self):
shutil.rmtree(self.existing_path)
[docs] def test_standard_parser_infile(self):
# test infile with existing file
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file
]
msg = "existing infile not being handled correctly"
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.infile, self.existing_file, msg)
# test infile with non-existent file
fakeargs = [self.nonexisting_file,
self.existing_file,
"-l", self.nonexisting_file
]
self.assertRaises(SystemExit,
self.parser.parse_args,
fakeargs)
[docs] def test_standard_parser_outfile(self):
# test outfile with existing file
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file
]
msg = "existing outfile not being handled correctly"
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.outfile, self.existing_file, msg)
# test outfile with non-existent file
fakeargs = [self.existing_file,
self.nonexisting_file,
"-l", self.existing_file
]
msg = "non-existent outfile and dir not being handled correctly"
with warnings.catch_warnings():
warnings.simplefilter("ignore")
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.outfile, self.nonexisting_file, msg)
[docs] def test_standard_parser_log(self):
# test logfile with existing file
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file
]
msg = "existing logfile not being handled correctly"
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.log, self.existing_file, msg)
# test logfile with non-existant dir
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.nonexisting_file
]
msg = "non-existant log file and dir not being handled correctly"
with warnings.catch_warnings():
warnings.simplefilter("ignore")
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.log, self.nonexisting_file, msg)
[docs] def test_standard_parser_verbosity(self):
# test verbosity
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file
]
msg = "verbosity not being parsed correctly"
opts = self.parser.parse_args(fakeargs)
self.assertFalse(opts.verbose, msg)
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file,
"-v"
]
opts = self.parser.parse_args(fakeargs)
self.assertTrue(opts.verbose, msg)
[docs] def test_standard_parser_tmpdir(self):
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file
]
msg = "omitted tmpdir arg not being handled correctly"
opts = self.parser.parse_args(fakeargs)
self.assertTrue(os.path.exists(opts.tmpdir), msg)
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file,
"--tmpdir"
]
msg = "unspecified tmpdir arg not being handled correctly"
opts = self.parser.parse_args(fakeargs)
self.assertTrue(os.path.exists(opts.tmpdir), msg)
# test tmpdir with existing but non-random dir
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file,
"--tmpdir", self.existing_path
]
msg = "existing tmpdir not being handled correctly"
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.tmpdir, self.existing_path, msg)
# test tmpdir with non-existant dir
fakeargs = [self.existing_file,
self.existing_file,
"-l", self.existing_file,
"--tmpdir", self.nonexisting_path
]
msg = "non-existent tmpdir not being handled correctly"
with warnings.catch_warnings():
warnings.simplefilter("ignore")
opts = self.parser.parse_args(fakeargs)
self.assertEqual(opts.tmpdir, self.nonexisting_path, msg)
if __name__ == '__main__':
unittest.main()