Skip to content

Commit 8db9096

Browse files
authored
Merge pull request #33 from ralphbean/trailing-whitespace
Add a rule to prohibit (and fix) trailing whitespace
2 parents 13a5073 + 57f25bc commit 8db9096

8 files changed

Lines changed: 61 additions & 6 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ repos:
33
rev: v5.0.0
44
hooks:
55
- id: trailing-whitespace
6+
exclude: ^tests/resources/trailing_whitespace_input.txt$
67
- id: check-yaml
78
- id: double-quote-string-fixer
89
- repo: https://github.com/asottile/setup-cfg-fmt

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ gitlab-codeowners-linter makes sure that the CODEOWNERS file is formatted respec
99
- there must be no empty lines between paths
1010
- paths must exist
1111
- there must not be duplicated sections
12+
- there must not be trailing whitespace
1213

1314
The linter can run in check or autofix mode.
1415

gitlab_codeowners_linter/autofix.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,15 @@ def _update_codeowners_file(codeowners_data, file_path):
144144
f.write('\n')
145145
if section.comments:
146146
for comment_line in section.comments:
147-
f.write(f'{comment_line}\n')
147+
f.write(f'{comment_line.rstrip()}\n')
148148
if section.codeowner_section != DEFAULT_SECTION:
149-
f.write(f'{section.codeowner_section}')
149+
f.write(f'{section.codeowner_section.rstrip()}')
150150
if section.entries:
151151
f.write('\n')
152152
for entry in section.entries:
153153
if entry.comments:
154154
for comment_line in entry.comments:
155-
f.write(f'{comment_line}\n')
155+
f.write(f'{comment_line.rstrip()}\n')
156156
owners = ' '.join(str(x) for x in entry.owners)
157-
f.write(f'{entry.path} {owners}\n')
157+
line_to_write = f'{entry.path} {owners}'
158+
f.write(f'{line_to_write.rstrip()}\n')

gitlab_codeowners_linter/checks.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,24 @@ def __init__(self):
1919
self.sections_with_non_existing_paths = []
2020
self.non_existing_paths = {}
2121
self.duplicated_sections = []
22+
self.lines_with_trailing_whitespace = []
2223

2324

24-
def check(codeowners_data):
25+
def check(codeowners_data, file_path):
2526
violations = CodeownersViolations()
2627

2728
if _is_codeowners_empty(codeowners_data):
2829
return violations
2930

31+
# Are there trailing whitespaces
32+
violations.lines_with_trailing_whitespace = _get_lines_with_trailing_whitespace(
33+
file_path,
34+
)
35+
if violations.lines_with_trailing_whitespace:
36+
violations.violation_error_messages.append(
37+
f'Lines with trailing whitespace: {violations.lines_with_trailing_whitespace}',
38+
)
39+
3040
# Are custom section names sorted?
3141
sort_sections_names_key = cmp_to_key(sort_section_names)
3242
if codeowners_data[1:] != sorted(codeowners_data[1:], key=sort_sections_names_key):
@@ -82,6 +92,15 @@ def _is_codeowners_empty(codeowners_data):
8292
return empty
8393

8494

95+
def _get_lines_with_trailing_whitespace(file_path):
96+
lines_with_trailing_whitespace = []
97+
with open(file_path) as f:
98+
for i, line in enumerate(f, 1):
99+
if line.endswith(' \n') or line.endswith('\t\n'):
100+
lines_with_trailing_whitespace.append(i)
101+
return lines_with_trailing_whitespace
102+
103+
85104
def _get_duplicated_sections(codeowners_data):
86105
all_sections_name = list(
87106
section.codeowner_section for section in codeowners_data)

gitlab_codeowners_linter/codeowners_linter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# - paths in a section must be unique
88
# - paths must exist
99
# - there must be no duplicated sections
10+
# - there must not be trailing whitespace
1011
#
1112
from __future__ import annotations
1213

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

3435
def lint(self):
35-
violations = check(self.codeowners_data)
36+
violations = check(self.codeowners_data, self.file_path)
3637
if self.autofix:
3738
fix(self.codeowners_data, violations, self.file_path)
3839
return violations

tests/codeowners_linter_tests.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,20 @@ class TestCase:
387387
'resources/empty_autofix.txt',
388388
),
389389
),
390+
TestCase(
391+
name='trailing_whitespace',
392+
input=os.path.join(
393+
os.path.dirname(os.path.abspath(__file__)),
394+
'resources/trailing_whitespace_input.txt',
395+
),
396+
expected_check=[
397+
'Lines with trailing whitespace: [6, 8]',
398+
],
399+
expected_fix=os.path.join(
400+
os.path.dirname(os.path.abspath(__file__)),
401+
'resources/trailing_whitespace_autofix.txt',
402+
),
403+
),
390404
]
391405
for case in testcases:
392406
actual = os.path.join(
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### CODEOWNERS ###
2+
#
3+
# This is a test case for trailing whitespace
4+
#
5+
6+
# comment with trailing whitespace
7+
[SECTION1]
8+
path1 @owner
9+
path2 @owner
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### CODEOWNERS ###
2+
#
3+
# This is a test case for trailing whitespace
4+
#
5+
6+
# comment with trailing whitespace
7+
[SECTION1]
8+
path1 @owner
9+
path2 @owner

0 commit comments

Comments
 (0)