mirror of
https://github.com/kennethreitz/pipenv.git
synced 2026-06-05 22:50:18 +00:00
Add JSONDecodeError handling for get_lockfile_hash
Now will return None when facing JSONDecodeError when loading the lockfile. do_init will remove the old lockfile and replace it.
This commit is contained in:
@@ -1257,6 +1257,26 @@ def do_init(
|
||||
),
|
||||
err=True,
|
||||
)
|
||||
elif old_hash is None:
|
||||
# Lockfile corrupted, remove it and replaced
|
||||
click.echo(
|
||||
crayons.red(
|
||||
u"Pipfile.lock is corrupted, replaced with ({0})…".format(
|
||||
new_hash[-6:]
|
||||
),
|
||||
bold=True,
|
||||
),
|
||||
err=True
|
||||
)
|
||||
os.remove(project.lockfile_location)
|
||||
do_lock(
|
||||
system=system,
|
||||
pre=pre,
|
||||
keep_outdated=keep_outdated,
|
||||
verbose=verbose,
|
||||
write=True,
|
||||
pypi_mirror=pypi_mirror,
|
||||
)
|
||||
else:
|
||||
click.echo(
|
||||
crayons.red(
|
||||
|
||||
+5
-1
@@ -815,7 +815,11 @@ class Project(object):
|
||||
if not os.path.exists(self.lockfile_location):
|
||||
return
|
||||
|
||||
lockfile = self.load_lockfile(expand_env_vars=False)
|
||||
try:
|
||||
lockfile = self.load_lockfile(expand_env_vars=False)
|
||||
except json.decoder.JSONDecodeError:
|
||||
# Lockfile corrupted
|
||||
return
|
||||
if "_meta" in lockfile and hasattr(lockfile, "keys"):
|
||||
return lockfile["_meta"].get("hash", {}).get("sha256")
|
||||
# Lockfile exists but has no hash at all
|
||||
|
||||
@@ -385,3 +385,25 @@ django = "*"
|
||||
django_version = '==2.0.6' if py_version.startswith('3') else '==1.11.13'
|
||||
assert py_version == '2.7.14'
|
||||
assert p.lockfile['default']['django']['version'] == django_version
|
||||
|
||||
|
||||
@pytest.mark.lock
|
||||
@pytest.mark.install
|
||||
def test_lockfile_corrupted(PipenvInstance):
|
||||
with PipenvInstance() as p:
|
||||
with open(p.lockfile_path, 'w') as f:
|
||||
f.write('{corrupt}')
|
||||
c = p.pipenv('install')
|
||||
assert c.return_code == 0
|
||||
assert p.lockfile['_meta']
|
||||
|
||||
|
||||
@pytest.mark.lock
|
||||
@pytest.mark.install
|
||||
def test_lockfile_with_empty_dict(PipenvInstance):
|
||||
with PipenvInstance() as p:
|
||||
with open(p.lockfile_path, 'w') as f:
|
||||
f.write('{}')
|
||||
c = p.pipenv('install')
|
||||
assert c.return_code == 0
|
||||
assert p.lockfile['_meta']
|
||||
Reference in New Issue
Block a user