Skip to content

Commit 944125c

Browse files
committed
Report an error when UNKNOWN_TAG is found spdx#55
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>
1 parent d4d1829 commit 944125c

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spdx/parsers/tagvalue.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def p_extr_lic_id_2(self, p):
266266
self.logger.log(msg)
267267

268268
def p_uknown_tag(self, p):
269-
"""unknown_tag : UNKNOWN_TAG"""
269+
"""unknown_tag : UNKNOWN_TAG LINE"""
270270
self.error = True
271271
msg = ERROR_MESSAGES['UNKNOWN_TAG'].format(p[1], p.lineno(1))
272272
self.logger.log(msg)

tests/test_tag_value_parser.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from __future__ import print_function
1414
from __future__ import unicode_literals
1515

16+
import sys
1617
import unittest
1718
from unittest import TestCase
1819

@@ -95,6 +96,14 @@ def test_pacakage(self):
9596
self.token_assert_helper(self.l.token(), 'PKG_VERF_CODE', 'PackageVerificationCode', 3)
9697
self.token_assert_helper(self.l.token(), 'LINE', '4e3211c67a2d28fced849ee1bb76e7391b93feba (SpdxTranslatorSpdx.rdf, SpdxTranslatorSpdx.txt)', 3)
9798

99+
def test_unknown_tag(self):
100+
data = '''
101+
SomeUnknownTag: SomeUnknownValue
102+
'''
103+
self.l.input(data)
104+
self.token_assert_helper(self.l.token(), 'UNKNOWN_TAG', 'SomeUnknownTag', 2)
105+
self.token_assert_helper(self.l.token(), 'LINE', 'SomeUnknownValue', 2)
106+
98107
def token_assert_helper(self, token, ttype, value, line):
99108
assert token.type == ttype
100109
assert token.value == value
@@ -158,6 +167,8 @@ class TestParser(TestCase):
158167
'FileComment: <text>Very long file</text>'
159168
])
160169

170+
unknown_tag_str = 'SomeUnknownTag: SomeUnknownValue'
171+
161172
complete_str = '{0}\n{1}\n{2}\n{3}\n{4}'.format(document_str, creation_str, review_str, package_str, file_str)
162173

163174
def setUp(self):
@@ -207,6 +218,21 @@ def test_file(self):
207218
assert len(spdx_file.artifact_of_project_home) == 1
208219
assert len(spdx_file.artifact_of_project_uri) == 1
209220

221+
def test_unknown_tag(self):
222+
223+
try:
224+
from StringIO import StringIO
225+
except ImportError:
226+
from io import StringIO
227+
228+
saved_out = sys.stdout
229+
sys.stdout = StringIO()
230+
document, error = self.p.parse(self.unknown_tag_str)
231+
self.assertEqual(sys.stdout.getvalue(), 'Found unknown tag : SomeUnknownTag at line: 1\n')
232+
sys.stdout = saved_out
233+
assert error
234+
assert document is not None
235+
210236

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

0 commit comments

Comments
 (0)