Fix for "Environment variables in mediapath"

This commit is contained in:
Pierre Bélissent 2015-08-19 20:24:40 +02:00
parent 5d654d402a
commit 4bbae0e46a
2 changed files with 25 additions and 2 deletions

View File

@ -152,6 +152,7 @@ def expanded_vars_path(path):
$GRAMPSHOME is set and restored afterwards, $GRAMPSHOME is set and restored afterwards,
because undefined $GRAMPSHOME has a special meaning (see const.py). because undefined $GRAMPSHOME has a special meaning (see const.py).
""" """
grampshome_added = False
if not 'GRAMPSHOME' in os.environ: if not 'GRAMPSHOME' in os.environ:
os.environ['GRAMPSHOME'] = USER_HOME os.environ['GRAMPSHOME'] = USER_HOME
grampshome_added = True grampshome_added = True

View File

@ -49,6 +49,7 @@ from gramps.version import VERSION
class FileTest(unittest.TestCase): class FileTest(unittest.TestCase):
def test_mediapath(self): def test_mediapath(self):
# Create database # Create database
dbstate = DbState() dbstate = DbState()
db = dbstate.make_database("bsddb") db = dbstate.make_database("bsddb")
@ -56,23 +57,44 @@ class FileTest(unittest.TestCase):
db.write_version(path) db.write_version(path)
db.load(path) db.load(path)
dbstate.change_database(db) dbstate.change_database(db)
# Test without db.mediapath set # Test without db.mediapath set
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME)))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME))))
self.assertTrue(os.path.exists(media_path(db))) self.assertTrue(os.path.exists(media_path(db)))
# Test with absolute db.mediapath # Test with absolute db.mediapath
db.set_mediapath(os.path.abspath(USER_HOME) + "/test_abs") db.set_mediapath(os.path.abspath(USER_HOME) + "/test_abs")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_abs")))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_abs"))))
# Test with relative db.mediapath # Test with relative db.mediapath
db.set_mediapath("test_rel") db.set_mediapath("test_rel")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(TEMP_DIR + "/utils_file_test/test_rel")))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(TEMP_DIR + "/utils_file_test/test_rel"))))
# Test with environment variable # Test with environment variable
db.set_mediapath("$GRAMPSHOME/test_var")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_var"))))
db.set_mediapath("/test/$GRAMPS_VERSION/test_var") db.set_mediapath("/test/$GRAMPS_VERSION/test_var")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/test/" + VERSION + "/test_var")))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/test/" + VERSION + "/test_var"))))
db.set_mediapath("${GRAMPS_USER_PLUGINS}/test_var") db.set_mediapath("${GRAMPS_USER_PLUGINS}/test_var")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_PLUGINS + "/test_var")))) self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_PLUGINS + "/test_var"))))
# Test with $GRAMPSHOME environment variable not set
grampshome = None
if 'GRAMPSHOME' in os.environ:
grampshome = os.environ['GRAMPSHOME']
del os.environ['GRAMPSHOME']
db.set_mediapath("$GRAMPSHOME/test_var")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath(USER_HOME + "/test_var"))))
# Test with $GRAMPSHOME environment variable set
os.environ['GRAMPSHOME'] = "/this/is/a/test"
db.set_mediapath("$GRAMPSHOME/test_var")
self.assertEqual(media_path(db), os.path.normcase(os.path.normpath(os.path.abspath("/this/is/a/test/test_var"))))
# Restore $GRAMPSHOME
if grampshome:
os.environ['GRAMPSHOME'] = grampshome
else:
del os.environ['GRAMPSHOME']
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #