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
This commit is contained in:
Jaisen Mathai 2017-12-06 15:46:00 -08:00 committed by GitHub
parent 4cd91e9f2d
commit 8950f756fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 12 deletions

View File

@ -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 File

@ -14,6 +14,7 @@ import urllib
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))
location_db = constants.location_db
if os.path.isfile(location_db):
os.rename(location_db, '{}-test'.format(location_db))
def restore_dbs():
""" Back up 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', ''))
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 = '{}-test'.format(constants.location_db)
if os.path.isfile(location_db):
os.rename(location_db, location_db.replace('-test', ''))
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):
_rename(hash_db, hash_db.replace('-test', ''))
location_db = '{}-test'.format(constants.location_db)
if os.path.isfile(location_db):
_rename(location_db, location_db.replace('-test', ''))