From d1d27a1bd2abdaec4de26dc94a1188df5af34bba Mon Sep 17 00:00:00 2001 From: Akihiro Uchida Date: Sun, 4 Oct 2015 12:40:24 +0900 Subject: [PATCH] Evade ValueError exception on ShorthandParserError --- awscli/shorthand.py | 23 ++++++++++++++++------- tests/unit/test_shorthand.py | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/awscli/shorthand.py b/awscli/shorthand.py index 374b12d08f03..a0eda950fe8d 100644 --- a/awscli/shorthand.py +++ b/awscli/shorthand.py @@ -64,8 +64,9 @@ def __init__(self, value, expected, actual, index): super(ShorthandParseError, self).__init__(msg) def _construct_msg(self): - if '\n' in self.value: - # If there's newlines in the expression, we want + consumed, remaining, num_spaces = self.value, '', self.index + if '\n' in self.value[:self.index]: + # If there's newlines in the consumed expression, we want # to make sure we're only counting the spaces # from the last newline: # foo=bar,\n @@ -73,14 +74,22 @@ def _construct_msg(self): # ^ last_newline = self.value[:self.index].rindex('\n') num_spaces = self.index - last_newline - 1 - else: - num_spaces = self.index + if '\n' in self.value[self.index:]: + # If there's newline in the remaining, divide value + # into consumed and remainig + # foo==bar,\n + # ^ + # bar=baz + next_newline = self.index + self.value[self.index:].index('\n') + consumed = self.value[:next_newline] + remaining = self.value[next_newline:] msg = ( "Expected: '%s', received: '%s' for input:\n" "%s\n" - "%s\n" - ) % (self.expected, self.actual, self.value, - ' ' * num_spaces + '^') + "%s" + "%s" + ) % (self.expected, self.actual, consumed, + ' ' * num_spaces + '^', remaining) return msg diff --git a/tests/unit/test_shorthand.py b/tests/unit/test_shorthand.py index 72437eb9dc62..743922ab344c 100644 --- a/tests/unit/test_shorthand.py +++ b/tests/unit/test_shorthand.py @@ -160,7 +160,7 @@ def test_error_parsing(): yield (_is_error, "foo={bar}") yield (_is_error, "foo={bar=bar") yield (_is_error, "foo=bar,") - + yield (_is_error, "foo==bar,\nbar=baz") def _is_error(expr): try: