Skip to content

Commit

Permalink
handle properly even json requirement was multiple lines (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
jupe authored Nov 6, 2023
1 parent 746e1f0 commit e51c8d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lockable/lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ def parse_requirements(requirements_str: (str or dict)) -> dict:
return {}
if isinstance(requirements_str, dict):
return requirements_str
try:
return json.loads(requirements_str)
except json.decoder.JSONDecodeError as error:
# if the first char is not {, try to parse as string requirements
if error.colno > 1:
# expecting requirements_str to be a json format
assert isinstance(requirements_str, str), 'requirements must be string or dict'
requirements_str = requirements_str.strip() # remove leading and trailing spaces
if requirements_str.startswith('{'):
try:
return json.loads(requirements_str)
except json.decoder.JSONDecodeError as error:
raise ValueError(str(error)) from error
return Lockable.parse_str_requirements(requirements_str)

Expand Down
6 changes: 6 additions & 0 deletions tests/test_Lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,18 @@ def test_parse_requirements(self):
self.assertEqual(Lockable.parse_requirements('a=False'), {"a": False})
self.assertEqual(Lockable.parse_requirements('a=b&c=d'), {"a": "b", "c": "d"})
self.assertEqual(Lockable.parse_requirements('{"a":"b","c":"d"}'), {"a": "b", "c": "d"})
with self.assertRaises(ValueError):
Lockable.parse_requirements('{')
with self.assertRaises(AssertionError):
Lockable.parse_requirements(1)
with self.assertRaises(ValueError):
Lockable.parse_requirements('a')
with self.assertRaises(ValueError):
Lockable.parse_requirements('a=')
with self.assertRaises(ValueError):
Lockable.parse_requirements('{"a":"b","c":"d}')
with self.assertRaises(ValueError):
Lockable.parse_requirements('{\na=b}')

def test_lock_resource_not_found(self):
with create_lockable([]) as lockable:
Expand Down

0 comments on commit e51c8d6

Please sign in to comment.