Skip to content

Commit

Permalink
fix for when no blank lines between rule sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Hall committed Oct 16, 2024
1 parent 23c5c7d commit cf0f440
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion codeowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,23 @@ def does_match(self, path: Path) -> bool:
def parse_code_owners(codeowners_file_path: Path, codeowners_content: str) -> Iterable[CodeOwnerSpecification]:
last_comment = None
line_number = 0
prev_line_was_a_rule = False
for line in codeowners_content.splitlines():
line_number += 1
if line.startswith('#'):
if last_comment is None:
if last_comment is None or prev_line_was_a_rule:
last_comment = line
else:
last_comment += '\n' + line
prev_line_was_a_rule = False
continue

if line.strip() == '':
last_comment = None
prev_line_was_a_rule = False
continue

prev_line_was_a_rule = True
inline_comment_pos = line.find('#')
if inline_comment_pos > -1:
line = line[0:inline_comment_pos]
Expand Down
27 changes: 27 additions & 0 deletions test/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
# subdirectory, as this subdirectory has its own owner @doctocat
#/apps/ @octocat
/apps/github2 @doctocat
# test
z @z
# another test with no blank lines between these
# comments and the prev rule
x @y
""")
codeowners = list(parse_code_owners(fake_path, codeowners_content))

Expand Down Expand Up @@ -182,6 +188,27 @@
fake_path,
),
),
(
'z',
CodeOwnerSpecification(
'# test',
'z',
['@z'],
70,
fake_path,
),
),
(
'x',
CodeOwnerSpecification(
'# another test with no blank lines between these\n' +
'# comments and the prev rule',
'x',
['@y'],
73,
fake_path,
),
),
]
)
def test_parsing(path: str, expected_owner: Optional[CodeOwnerSpecification]) -> None:
Expand Down

0 comments on commit cf0f440

Please sign in to comment.