From 5c62ed59d2a3901931d01b27388d7c0037178a8e Mon Sep 17 00:00:00 2001 From: Edward Lesmes Date: Thu, 19 Apr 2018 16:47:15 -0400 Subject: [PATCH] owners: Add support for comments at the end of the line. Bug: 789773 Change-Id: Ic6fff7e4eba5e86dc77cf2b8e213228ec394dccf Reviewed-on: https://chromium-review.googlesource.com/1019612 Commit-Queue: Edward Lesmes Reviewed-by: Dirk Pranke --- owners.py | 13 +++++++++---- tests/owners_unittest.py | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/owners.py b/owners.py index faa019439..1b572bf1d 100644 --- a/owners.py +++ b/owners.py @@ -11,11 +11,10 @@ so you may need approval from both an OWNER and a reviewer in many cases. The syntax of the OWNERS file is, roughly: -lines := (\s* line? \s* "\n")* +lines := (\s* line? \s* comment? \s* "\n")* line := directive | "per-file" \s+ glob \s* "=" \s* directive - | comment directive := "set noparent" | "file:" glob @@ -276,6 +275,12 @@ class Database(object): previous_line_was_blank = True continue + # If the line ends with a comment, strip the comment and store it for this + # line only. + line, _, line_comment = line.partition('#') + line = line.strip() + line_comment = [line_comment.strip()] if line_comment else [] + previous_line_was_blank = False if line == 'set noparent': self._stop_looking.add(dirpath) @@ -292,7 +297,7 @@ class Database(object): line) relative_glob_string = self.os_path.relpath(full_glob_string, self.root) self._add_entry(relative_glob_string, directive, owners_path, - lineno, '\n'.join(comment)) + lineno, '\n'.join(comment + line_comment)) if reset_comment_after_use: comment = [] continue @@ -302,7 +307,7 @@ class Database(object): 'unknown option: "%s"' % line[4:].strip()) self._add_entry(dirpath, line, owners_path, lineno, - ' '.join(comment)) + ' '.join(comment + line_comment)) if reset_comment_after_use: comment = [] diff --git a/tests/owners_unittest.py b/tests/owners_unittest.py index 4f6e7661a..bac4955a8 100755 --- a/tests/owners_unittest.py +++ b/tests/owners_unittest.py @@ -349,7 +349,7 @@ class OwnersDatabaseTest(_BaseTestCase): self.files['/OWNERS'] = '\n'.join([ '# first comment', ben, - brett, + brett + ' # inline comment', '', darin, '', @@ -367,7 +367,7 @@ class OwnersDatabaseTest(_BaseTestCase): self.assertEqual(db.comments, { ben: {'': 'first comment'}, - brett: {'': 'first comment'}, + brett: {'': 'first comment inline comment'}, jochen: {'bar.*': 'comment preceeded by empty line'}, john: {'': 'comment preceeded by empty line'}, peter: {'': 'comment in the middle'}})