diff --git a/glymur/test/test_config.py b/glymur/test/test_config.py index ac24b89..feb3235 100644 --- a/glymur/test/test_config.py +++ b/glymur/test/test_config.py @@ -10,6 +10,7 @@ OPENJP2 may be present in some form or other. # unittest.mock only in Python 3.3 (python2.7/pylint import issue) # pylint: disable=E0611,F0401 +import contextlib import ctypes import imp import os @@ -42,6 +43,26 @@ def openjpeg_not_found_by_ctypes(): return False +@contextlib.contextmanager +def chdir(dirname=None): + """ + This context manager restores the value of the current working directory + (cwd) after the enclosed code block completes or raises an exception. If a + directory name is supplied to the context manager then the cwd is changed + prior to running the code block. + + Shamelessly lifted from + http://www.astropython.org/snippet/2009/10/chdir-context-manager + """ + curdir = os.getcwd() + try: + if dirname is not None: + os.chdir(dirname) + yield + finally: + os.chdir(curdir) + + @unittest.skipIf(sys.hexversion < 0x03020000, "TemporaryDirectory introduced in 3.2.") @unittest.skipIf(glymur.lib.openjp2.OPENJP2 is None, @@ -142,3 +163,19 @@ class TestSuite(unittest.TestCase): # configuration file being empty. imp.reload(glymur.lib.openjpeg) self.assertIsNotNone(glymur.lib.openjp2.OPENJPEG) + + @unittest.skipIf(os.name == "nt", WINDOWS_TMP_FILE_MSG) + def test_config_file_in_current_directory(self): + """A configuration file in the current directory should be honored.""" + libloc = glymur.lib.openjp2.OPENJP2._name + with tempfile.TemporaryDirectory() as tdir1: + fname = os.path.join(tdir1, 'glymurrc') + with open(fname, 'w') as fptr: + fptr.write('[library]\n') + fptr.write('openjp2: {0}\n'.format(libloc)) + fptr.flush() + with chdir(tdir1): + # Should be able to load openjp2 as before. + imp.reload(glymur.lib.openjp2) + self.assertEqual(glymur.lib.openjp2.OPENJP2._name, libloc) +