Permalink
Browse files

Make reset_dbs() / restore_dbs() more robust #260 (#263)

* Make reset_dbs() / restore_dbs() more robust

If reset_dbs() is called and the test errors restore_dbs() is not called. On Windows this leads to an error the next time they're called because ~/hash.json-test and ~/location.json-test still exist.

* Add rename function to compatability module for 2.7 support
  • Loading branch information...
jmathai committed Dec 6, 2017
1 parent 4cd91e9 commit 8950f756fe03774d8a34e6fd997372328c056438
Showing with 29 additions and 7 deletions.
  1. +10 −0 elodie/compatability.py
  2. +19 −7 elodie/tests/helper.py
View
@@ -63,3 +63,13 @@ def _copyfile(src, dst):
os.close(fout)
except:
pass
# If you want cross-platform overwriting of the destination,
# use os.replace() instead of rename().
# https://docs.python.org/3/library/os.html#os.rename
def _rename(src, dst):
if (constants.python_version == 3):
return os.replace(src, dst)
else:
return os.rename(src, dst)
View
@@ -14,6 +14,7 @@
from datetime import datetime
from datetime import timedelta
from elodie.compatability import _rename
from elodie import constants
def checksum(file_path, blocksize=65536):
@@ -148,19 +149,30 @@ def isclose(a, b, rel_tol = 1e-8):
diff <= abs(rel_tol * b))
def reset_dbs():
hash_db = constants.hash_db
if os.path.isfile(hash_db):
os.rename(hash_db, '{}-test'.format(hash_db))
""" Back up hash_db and location_db """
hash_db = '{}-test'.format(constants.hash_db)
if not os.path.isfile(hash_db):
hash_db = constants.hash_db
if os.path.isfile(hash_db):
_rename(hash_db, '{}-test'.format(hash_db))
#else restore_dbs wasn't called by a previous test, keep the
#existing hash_db backup
location_db = constants.location_db
location_db = '{}-test'.format(constants.location_db)
if os.path.isfile(location_db):
os.rename(location_db, '{}-test'.format(location_db))
location_db = constants.location_db
if os.path.isfile(location_db):
_rename(location_db, '{}-test'.format(location_db))
#else restore_dbs wasn't called by a previous test, keep the
#existing location_db backup
def restore_dbs():
""" Restore back ups of hash_db and location_db """
hash_db = '{}-test'.format(constants.hash_db)
if os.path.isfile(hash_db):
os.rename(hash_db, hash_db.replace('-test', ''))
_rename(hash_db, hash_db.replace('-test', ''))
location_db = '{}-test'.format(constants.location_db)
if os.path.isfile(location_db):
os.rename(location_db, location_db.replace('-test', ''))
_rename(location_db, location_db.replace('-test', ''))

0 comments on commit 8950f75

Please sign in to comment.