Handle no newline at the end of bindep.txt

We've seen people whose editors don't add newlines to files for whatever
reason. Make bindep handle this case since users are running into it.

Note that we could theoretically modify the existing parsley grammar to
accept a missing newline and instead find EOF but adding rules like that
seems to interact with whitespace processing in ways that I don't
understand. Rather than spend a ton of time learning parsley internals I
figure the fix here is simple and easy to understand and limits
potential fallout from changes to the grammar.

Change-Id: I960e579a603d1a4612d859451df1f559e950ac31
This commit is contained in:
Clark Boylan 2021-10-20 17:28:12 -07:00
parent 0bd79b1d5d
commit c4c2bf1afe
2 changed files with 9 additions and 0 deletions

View File

@ -140,6 +140,9 @@ class Depends(object):
:param filename: The string name of the file from which requirements
were loaded.
"""
if not depends_string.endswith('\n'):
# The parsley grammar expects each line to end with a newline
depends_string += '\n'
parser = makeGrammar(grammar, {})(depends_string)
self._rules = parser.rules()
self.filename = filename

View File

@ -376,6 +376,12 @@ class TestDepends(TestCase):
depends = Depends("")
self.assertEqual([], depends._rules)
def test_no_newline(self):
depends = Depends("foo")
self.assertEqual(
[("foo", [], [])],
depends._rules)
def test_selectors(self):
depends = Depends("foo [!bar baz quux]\n")
self.assertEqual(