Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit d3057eb

Browse files
Merge pull request #33 from adrianno3259/issue32-improve-missing-clang-format-message
Include error message when clang-format is not found
2 parents b5e054b + 72fda4e commit d3057eb

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

esss_fix_format/cli.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,19 @@ def _process_file(filename, check, format_code):
186186
changed = b'<replacement ' in output
187187
else:
188188
mtime = os.path.getmtime(filename)
189-
subprocess.check_output('clang-format -i "%s"' % filename, shell=True)
189+
190+
# checking if the terminal command is valid
191+
# might throw an exception if clang-format is not recognized
192+
try:
193+
subprocess.check_output('clang-format -i "%s"' % filename, shell=True)
194+
except subprocess.CalledProcessError as e:
195+
msg = ': ERROR (%s: %s): ' % (type(e).__name__, e)
196+
msg += 'Please check if "clang-format" is installed and accessible'
197+
error_msg = click.format_filename(filename) + msg
198+
click.secho(error_msg, fg='red')
199+
errors.append(error_msg)
200+
return changed, errors, formatter
201+
190202
changed = os.path.getmtime(filename) != mtime
191203

192204
return changed, errors, formatter

tests/test_esss_fix_format.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,42 @@ def test_clang_format(tmpdir, dot_clang_format_to_tmpdir):
425425
assert obtained == 'int a;'
426426

427427

428+
def test_missing_clang_format(tmpdir, mocker, dot_clang_format_to_tmpdir):
429+
source = 'int a; '
430+
filename = tmpdir.join('a.cpp')
431+
filename.write(source)
432+
433+
# Check for invalid format:
434+
# File will not pass in the format check
435+
check_invalid_file(filename, formatter='clang-format')
436+
437+
expected_command = 'clang-format -i "main.cpp"'
438+
expected_error_code = 1
439+
440+
# The '*' is used to indicate that there may be a '.' in
441+
# the message depending on the python version
442+
expected_error_message = "Command '%s' returned non-zero exit status 1*" % expected_command
443+
message_extra_details = 'Please check if "clang-format" is installed and accessible'
444+
445+
mocker.patch.object(
446+
subprocess,
447+
'check_output',
448+
side_effect=subprocess.CalledProcessError(expected_error_code, expected_command))
449+
450+
# Check if the command-line instruction returned an exception
451+
# of type CalledProcessError with the correct error message
452+
check_cli_error_output(
453+
filename,
454+
expected_error_message,
455+
message_extra_details,
456+
formatter='clang-format'
457+
)
458+
459+
# test should skip file, so no changes are made
460+
obtained = filename.read()
461+
assert obtained == source
462+
463+
428464
def run(args, expected_exit):
429465
from _pytest.pytester import LineMatcher
430466
runner = CliRunner()
@@ -453,6 +489,13 @@ def fix_invalid_file(input_file, formatter=None):
453489
output.fnmatch_lines(str(input_file) + ': Fixed' + _get_formatter_msg(formatter))
454490

455491

492+
def check_cli_error_output(input_file, expected_error_message, message_details, formatter=None):
493+
output = run([str(input_file)], expected_exit=1)
494+
msg = ': ERROR (CalledProcessError: %s): ' % (expected_error_message)
495+
msg += message_details
496+
output.fnmatch_lines(str(input_file) + msg)
497+
498+
456499
def check_invalid_file(input_file, formatter=None):
457500
output = run(['--check', str(input_file)], expected_exit=1)
458501
output.fnmatch_lines(str(input_file) + ': Failed' + _get_formatter_msg(formatter))

0 commit comments

Comments
 (0)