Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ repos:
rev: v5.0.0
hooks:
- id: trailing-whitespace
exclude: ^tests/resources/trailing_whitespace_input.txt$
- id: check-yaml
- id: double-quote-string-fixer
- repo: https://github.com/asottile/setup-cfg-fmt
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gitlab-codeowners-linter makes sure that the CODEOWNERS file is formatted respec
- there must be no empty lines between paths
- paths must exist
- there must not be duplicated sections
- there must not be trailing whitespace

The linter can run in check or autofix mode.

Expand Down
9 changes: 5 additions & 4 deletions gitlab_codeowners_linter/autofix.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ def _update_codeowners_file(codeowners_data, file_path):
f.write('\n')
if section.comments:
for comment_line in section.comments:
f.write(f'{comment_line}\n')
f.write(f'{comment_line.rstrip()}\n')
if section.codeowner_section != DEFAULT_SECTION:
f.write(f'{section.codeowner_section}')
f.write(f'{section.codeowner_section.rstrip()}')
if section.entries:
f.write('\n')
for entry in section.entries:
if entry.comments:
for comment_line in entry.comments:
f.write(f'{comment_line}\n')
f.write(f'{comment_line.rstrip()}\n')
owners = ' '.join(str(x) for x in entry.owners)
f.write(f'{entry.path} {owners}\n')
line_to_write = f'{entry.path} {owners}'
f.write(f'{line_to_write.rstrip()}\n')
21 changes: 20 additions & 1 deletion gitlab_codeowners_linter/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,24 @@ def __init__(self):
self.sections_with_non_existing_paths = []
self.non_existing_paths = {}
self.duplicated_sections = []
self.lines_with_trailing_whitespace = []


def check(codeowners_data):
def check(codeowners_data, file_path):
violations = CodeownersViolations()

if _is_codeowners_empty(codeowners_data):
return violations

# Are there trailing whitespaces
violations.lines_with_trailing_whitespace = _get_lines_with_trailing_whitespace(
file_path,
)
if violations.lines_with_trailing_whitespace:
violations.violation_error_messages.append(
f'Lines with trailing whitespace: {violations.lines_with_trailing_whitespace}',
)

# Are custom section names sorted?
sort_sections_names_key = cmp_to_key(sort_section_names)
if codeowners_data[1:] != sorted(codeowners_data[1:], key=sort_sections_names_key):
Expand Down Expand Up @@ -82,6 +92,15 @@ def _is_codeowners_empty(codeowners_data):
return empty


def _get_lines_with_trailing_whitespace(file_path):
lines_with_trailing_whitespace = []
with open(file_path) as f:
for i, line in enumerate(f, 1):
if line.endswith(' \n') or line.endswith('\t\n'):
lines_with_trailing_whitespace.append(i)
return lines_with_trailing_whitespace


def _get_duplicated_sections(codeowners_data):
all_sections_name = list(
section.codeowner_section for section in codeowners_data)
Expand Down
3 changes: 2 additions & 1 deletion gitlab_codeowners_linter/codeowners_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# - paths in a section must be unique
# - paths must exist
# - there must be no duplicated sections
# - there must not be trailing whitespace
#
from __future__ import annotations

Expand All @@ -32,7 +33,7 @@ def __init__(self, file_path, no_autofix):
self.autofix = not no_autofix

def lint(self):
violations = check(self.codeowners_data)
violations = check(self.codeowners_data, self.file_path)
if self.autofix:
fix(self.codeowners_data, violations, self.file_path)
return violations
Expand Down
14 changes: 14 additions & 0 deletions tests/codeowners_linter_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,20 @@ class TestCase:
'resources/empty_autofix.txt',
),
),
TestCase(
name='trailing_whitespace',
input=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'resources/trailing_whitespace_input.txt',
),
expected_check=[
'Lines with trailing whitespace: [6, 8]',
],
expected_fix=os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'resources/trailing_whitespace_autofix.txt',
),
),
]
for case in testcases:
actual = os.path.join(
Expand Down
9 changes: 9 additions & 0 deletions tests/resources/trailing_whitespace_autofix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### CODEOWNERS ###
#
# This is a test case for trailing whitespace
#

# comment with trailing whitespace
[SECTION1]
path1 @owner
path2 @owner
9 changes: 9 additions & 0 deletions tests/resources/trailing_whitespace_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### CODEOWNERS ###
#
# This is a test case for trailing whitespace
#

# comment with trailing whitespace
[SECTION1]
path1 @owner
path2 @owner