Skip to content

Commit

Permalink
Report an error when UNKNOWN_TAG is found spdx#55
Browse files Browse the repository at this point in the history
The cause of the error was the method ​`p_unknown_tag`
in `parsers/tagvalue.py`. Due to the incorrect context-free
grammar specification defined in the method, the line after
the `unknown_tag` was not taken into consideration. The
context-free grammar specification was rectified and tests
were corresponding tests were added.

Signed-off-by: Yash Nisar <yash.nisar@somaiya.edu>
  • Loading branch information
yash-nisar committed May 7, 2018
1 parent 354eaf6 commit 3700acc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion spdx/parsers/tagvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def p_extr_lic_id_2(self, p):
self.logger.log(msg)

def p_uknown_tag(self, p):
"""unknown_tag : UNKNOWN_TAG"""
"""unknown_tag : UNKNOWN_TAG LINE"""
self.error = True
msg = ERROR_MESSAGES['UNKNOWN_TAG'].format(p[1], p.lineno(1))
self.logger.log(msg)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_tag_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from __future__ import print_function
from __future__ import unicode_literals

import sys
import unittest
from unittest import TestCase

Expand Down Expand Up @@ -94,6 +95,14 @@ def test_pacakage(self):
self.token_assert_helper(self.l.token(), 'PKG_VERF_CODE', 'PackageVerificationCode', 3)
self.token_assert_helper(self.l.token(), 'LINE', '4e3211c67a2d28fced849ee1bb76e7391b93feba (SpdxTranslatorSpdx.rdf, SpdxTranslatorSpdx.txt)', 3)

def test_unknown_tag(self):
data = '''
SomeUnknownTag: SomeUnknownValue
'''
self.l.input(data)
self.token_assert_helper(self.l.token(), 'UNKNOWN_TAG', 'SomeUnknownTag', 2)
self.token_assert_helper(self.l.token(), 'LINE', 'SomeUnknownValue', 2)

def token_assert_helper(self, token, ttype, value, line):
assert token.type == ttype
assert token.value == value
Expand Down Expand Up @@ -157,6 +166,8 @@ class TestParser(TestCase):
'FileComment: <text>Very long file</text>'
])

unknown_tag_str = 'SomeUnknownTag: SomeUnknownValue'

complete_str = '{0}\n{1}\n{2}\n{3}\n{4}'.format(document_str, creation_str, review_str, package_str, file_str)

def setUp(self):
Expand Down Expand Up @@ -206,6 +217,21 @@ def test_file(self):
assert len(spdx_file.artifact_of_project_home) == 1
assert len(spdx_file.artifact_of_project_uri) == 1

def test_unknown_tag(self):

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

saved_out = sys.stdout
sys.stdout = StringIO()
document, error = self.p.parse(self.unknown_tag_str)
self.assertEqual(sys.stdout.getvalue(), 'Found unknown tag : SomeUnknownTag at line: 1\n')
sys.stdout = saved_out
assert error
assert document is not None


if __name__ == '__main__':
unittest.main()

0 comments on commit 3700acc

Please sign in to comment.