Skip to content

Commit

Permalink
Raise error if yarn.lock contains a key with quoted comma
Browse files Browse the repository at this point in the history
CLOUDBLD-4216

Signed-off-by: Sumin Cho <sucho@redhat.com>
  • Loading branch information
sumincho22 authored and chmeliik committed Jun 27, 2022
1 parent 41fd2dd commit 9f09e40
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pyarn/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,25 @@ def p_block_comment(p):
def p_title(p):
"""title : STRING COLON INDENT
| list COLON INDENT"""
if isinstance(p[1], str):
if ',' in p[1]:
raise ValueError(f'Following key has a quoted comma: "{p[1]}"')
else:
for key in p[1]:
if ',' in key:
raise ValueError(f'Following key has a quoted comma: "{key}"')
p[1] = ', '.join(p[1])
p[0] = p[1]


def p_list(p):
"""list : STRING COMMA STRING
| list COMMA STRING"""
p[0] = ', '.join([p[1], p[3]])
if isinstance(p[1], str):
p[0] = [p[1], p[3]]
else:
p[1].append(p[3])
p[0] = p[1]


def p_members(p):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_lockfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ def test_from_file():
assert lock.data == expected


@pytest.mark.parametrize(
'key, lockfile_str',
[
('"foo, bar"', '"foo, bar":\n version "1.0.0"'),
('"foo, bar"', '"foo, bar", foo2:\n version "1.0.0"'),
('"foo, bar"', 'foo1, foo2, "foo, bar", foo3:\n version "1.0.0"'),
('"foo, bar"', 'foo:\n version "1.0.0"\n"foo, bar":\n version "1.0.0"'),
]
)
def test_key_with_quoted_comma(key, lockfile_str):
with pytest.raises(ValueError, match=f'Following key has a quoted comma: {key}'):
lockfile.Lockfile.from_str(lockfile_str)


def test_v1():
lock = lockfile.Lockfile.from_str('# yarn lockfile v1\n')
assert lock.version == '1'
Expand Down

0 comments on commit 9f09e40

Please sign in to comment.