Split hash function into two

This commit is contained in:
Jeff Tratner
2018-03-23 00:08:57 -07:00
parent 1f31f40f2b
commit 8f79f13ea4
3 changed files with 23 additions and 22 deletions
+9 -9
View File
@@ -1311,14 +1311,14 @@ def do_init(
)
# Write out the lockfile if it doesn't exist, but not if the Pipfile is being ignored
if (project.lockfile_exists and not ignore_pipfile) and not skip_lock:
changed_hash = project.pipfile_hash_changed()
if changed_hash:
old_hash, new_hash = changed_hash
old_hash = project.get_lockfile_hash()
new_hash = project.calculate_pipfile_hash()
if new_hash != old_hash:
if deploy:
click.echo(
crayons.red(
'Your Pipfile.lock ({0}) is out of date. Expected: ({1}).'.format(
old_hash, new_hash
old_hash[-6:], new_hash[-6:]
)
)
)
@@ -1331,7 +1331,7 @@ def do_init(
click.echo(
crayons.red(
u'Pipfile.lock ({0}) out of date, updating to ({1})…'.format(
old_hash, new_hash
old_hash[-6:], new_hash[-6:]
),
bold=True,
),
@@ -1648,13 +1648,13 @@ def ensure_lockfile(keep_outdated=False):
keep_outdated = project.settings.get('keep_outdated')
# Write out the lockfile if it doesn't exist, but not if the Pipfile is being ignored
if project.lockfile_exists:
changed_hash = project.pipfile_hash_changed()
if changed_hash:
old_hash, new_hash = changed_hash
old_hash = project.get_lockfile_hash()
new_hash = project.calculate_pipfile_hash()
if new_hash != old_hash:
click.echo(
crayons.red(
u'Pipfile.lock ({0}) out of date, updating to ({1})…'.format(
old_hash, new_hash
old_hash[-6:], new_hash[-6:]
),
bold=True,
),
+7 -10
View File
@@ -642,18 +642,15 @@ class Project(object):
def recase_pipfile(self):
self.write_toml(recase_file(self._pipfile))
def pipfile_hash_changed(self):
"""Check if hashes differ between lockfile and Pipfile.
Returns: (old_hash, new_hash) if hash has changed
"""
def get_lockfile_hash(self):
if not os.path.exists(self.lockfile_location):
return
# Open the lockfile.
with codecs.open(self.lockfile_location, 'r') as f:
lockfile = json.load(f)
return lockfile['_meta'].get('hash', {}).get('sha256')
def calculate_pipfile_hash(self):
# Update the lockfile if it is out-of-date.
p = pipfile.load(self.pipfile_location, inject_env=False)
# Check that the hash of the Lockfile matches the lockfile's hash.
if not lockfile['_meta'].get('hash', {}).get('sha256') == p.hash:
old_hash = lockfile['_meta'].get('hash', {}).get('sha256')[-6:]
new_hash = p.hash[-6:]
return old_hash, new_hash
return p.hash
+7 -3
View File
@@ -1139,13 +1139,17 @@ flask = "==0.12.2"
""")
monkeypatch.setitem(os.environ, 'PYPI_USERNAME', 'whatever')
monkeypatch.setitem(os.environ, 'PYPI_PASSWORD', 'pass')
assert Project().get_lockfile_hash() is None
c = p.pipenv('install')
lock_hash = Project().get_lockfile_hash()
assert lock_hash is not None
assert lock_hash == Project().calculate_pipfile_hash()
# sanity check on pytest
assert 'PYPI_USERNAME' not in str(pipfile.load(p.pipfile_path))
assert c.return_code == 0
assert not Project().pipfile_hash_changed()
assert Project().get_lockfile_hash() == Project.calculate_pipfile_hash()
monkeypatch.setitem(os.environ, 'PYPI_PASSWORD', 'pass2')
assert not Project().pipfile_hash_changed()
assert Project().get_lockfile_hash() == Project.calculate_pipfile_hash()
with open(p.pipfile_path, 'a') as f:
f.write('requests = "==2.14.0"\n')
assert Project().pipfile_hash_changed()
assert Project().get_lockfile_hash() != Project.calculate_pipfile_hash()