Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit 3278a16

Browse files
committed
Merge pull request #73 from sk-/issue64
Print new lines between skipped comments and messages. Fixes #64.
2 parents 7363811 + 28b8fdc commit 3278a16

File tree

2 files changed

+43
-15
lines changed

2 files changed

+43
-15
lines changed

gitlint/__init__.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656

5757
__VERSION__ = '0.0.6.1'
5858

59+
ERROR = termcolor.colored('ERROR', 'red', attrs=('bold',))
60+
SKIPPED = termcolor.colored('SKIPPED', 'yellow', attrs=('bold',))
61+
OK = termcolor.colored('OK', 'green', attrs=('bold',))
62+
5963

6064
def find_invalid_filenames(filenames, repository_root):
6165
"""Find files that does not exist, are not in the repo or are directories.
@@ -179,7 +183,7 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
179183
vcs, repository_root = get_vcs_root()
180184

181185
if vcs is None:
182-
stderr.write('fatal: Not a git repository' + os.linesep)
186+
stderr.write('fatal: Not a git repository' + linesep)
183187
return 128
184188

185189
commit = None
@@ -192,7 +196,7 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
192196
if invalid_filenames:
193197
invalid_filenames.append(('', ''))
194198
stderr.write(
195-
os.linesep.join(invalid[1] for invalid in invalid_filenames))
199+
linesep.join(invalid[1] for invalid in invalid_filenames))
196200
return 2
197201

198202
changed_files = vcs.modified_files(repository_root,
@@ -213,9 +217,6 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
213217
gitlint_config = get_config(repository_root)
214218
json_result = {}
215219

216-
error = termcolor.colored('ERROR', 'red', attrs=('bold',))
217-
skipped = termcolor.colored('SKIPPED', 'yellow', attrs=('bold',))
218-
219220
for filename in sorted(modified_files.keys()):
220221
rel_filename = os.path.relpath(filename)
221222
if not json_output:
@@ -233,31 +234,30 @@ def main(argv, stdout=sys.stdout, stderr=sys.stderr):
233234
filename, modified_lines, gitlint_config)
234235
result = result[filename]
235236

236-
output = ''
237+
output_lines = []
237238
if result.get('error'):
238-
output += os.linesep.join(
239-
'%s: %s' % (error, reason) for reason in result.get('error')
239+
output_lines.extend(
240+
'%s: %s' % (ERROR, reason) for reason in result.get('error')
240241
)
241242
linter_not_found = True
242243
if result.get('skipped'):
243-
output += os.linesep.join(
244-
'%s: %s' % (skipped, reason) for reason in result.get('skipped')
244+
output_lines.extend(
245+
'%s: %s' % (SKIPPED, reason) for reason in result.get('skipped')
245246
)
246247
if result.get('comments', []) == []:
247-
if not output:
248-
output += termcolor.colored('OK', 'green', attrs=('bold',))
248+
if not output_lines:
249+
output_lines.append(OK)
249250
else:
250251
files_with_problems += 1
251-
messages = []
252252
for data in result['comments']:
253253
formatted_message = format_comment(data)
254-
messages.append(formatted_message)
254+
output_lines.append(formatted_message)
255255
data['formatted_message'] = formatted_message
256-
output += os.linesep.join(messages)
257256

258257
if json_output:
259258
json_result[filename] = result
260259
else:
260+
output = linesep.join(output_lines)
261261
stdout.write(output)
262262
stdout.write(linesep + linesep)
263263

test/unittest/test_gitlint.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,34 @@ def test_main_with_valid_files_relative(self):
449449
self.git_lint_config)]
450450
self.assertEqual(expected_calls, self.lint.call_args_list)
451451

452+
def test_main_errors_skipped_comments(self):
453+
lint_response = {
454+
self.filename: {
455+
'error': ['error1', 'error2'],
456+
'skipped': ['skipped1', 'skipped2'],
457+
'comments': [{'message': 'msg1'}, {'message': 'msg2'}],
458+
},
459+
self.filename2: {
460+
'comments': [],
461+
}
462+
}
463+
self.lint.return_value = lint_response
464+
465+
with mock.patch('gitlint.find_invalid_filenames', return_value=[]), \
466+
mock.patch('os.getcwd', return_value=self.root):
467+
self.assertEqual(
468+
1, gitlint.main(['git-lint', self.filename, self.filename2],
469+
stdout=self.stdout, stderr=None))
470+
expected_output = os.linesep.join([
471+
'\x1b[1m\x1b[31mERROR\x1b[0m: error1',
472+
'\x1b[1m\x1b[31mERROR\x1b[0m: error2',
473+
'\x1b[1m\x1b[33mSKIPPED\x1b[0m: skipped1',
474+
'\x1b[1m\x1b[33mSKIPPED\x1b[0m: skipped2',
475+
'msg1',
476+
'msg2',
477+
])
478+
self.assertIn(expected_output, self.stdout.getvalue())
479+
452480
def test_get_config(self):
453481
git_config = os.path.join(self.root, '.gitlint.yaml')
454482
config = """python:

0 commit comments

Comments
 (0)