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

Commit

Permalink
Merge pull request #33 from adrianno3259/issue32-improve-missing-clan…
Browse files Browse the repository at this point in the history
…g-format-message

Include error message when clang-format is not found
  • Loading branch information
prusse-martin authored Nov 1, 2018
2 parents b5e054b + 72fda4e commit d3057eb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
14 changes: 13 additions & 1 deletion esss_fix_format/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,19 @@ def _process_file(filename, check, format_code):
changed = b'<replacement ' in output
else:
mtime = os.path.getmtime(filename)
subprocess.check_output('clang-format -i "%s"' % filename, shell=True)

# checking if the terminal command is valid
# might throw an exception if clang-format is not recognized
try:
subprocess.check_output('clang-format -i "%s"' % filename, shell=True)
except subprocess.CalledProcessError as e:
msg = ': ERROR (%s: %s): ' % (type(e).__name__, e)
msg += 'Please check if "clang-format" is installed and accessible'
error_msg = click.format_filename(filename) + msg
click.secho(error_msg, fg='red')
errors.append(error_msg)
return changed, errors, formatter

changed = os.path.getmtime(filename) != mtime

return changed, errors, formatter
Expand Down
43 changes: 43 additions & 0 deletions tests/test_esss_fix_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,42 @@ def test_clang_format(tmpdir, dot_clang_format_to_tmpdir):
assert obtained == 'int a;'


def test_missing_clang_format(tmpdir, mocker, dot_clang_format_to_tmpdir):
source = 'int a; '
filename = tmpdir.join('a.cpp')
filename.write(source)

# Check for invalid format:
# File will not pass in the format check
check_invalid_file(filename, formatter='clang-format')

expected_command = 'clang-format -i "main.cpp"'
expected_error_code = 1

# The '*' is used to indicate that there may be a '.' in
# the message depending on the python version
expected_error_message = "Command '%s' returned non-zero exit status 1*" % expected_command
message_extra_details = 'Please check if "clang-format" is installed and accessible'

mocker.patch.object(
subprocess,
'check_output',
side_effect=subprocess.CalledProcessError(expected_error_code, expected_command))

# Check if the command-line instruction returned an exception
# of type CalledProcessError with the correct error message
check_cli_error_output(
filename,
expected_error_message,
message_extra_details,
formatter='clang-format'
)

# test should skip file, so no changes are made
obtained = filename.read()
assert obtained == source


def run(args, expected_exit):
from _pytest.pytester import LineMatcher
runner = CliRunner()
Expand Down Expand Up @@ -453,6 +489,13 @@ def fix_invalid_file(input_file, formatter=None):
output.fnmatch_lines(str(input_file) + ': Fixed' + _get_formatter_msg(formatter))


def check_cli_error_output(input_file, expected_error_message, message_details, formatter=None):
output = run([str(input_file)], expected_exit=1)
msg = ': ERROR (CalledProcessError: %s): ' % (expected_error_message)
msg += message_details
output.fnmatch_lines(str(input_file) + msg)


def check_invalid_file(input_file, formatter=None):
output = run(['--check', str(input_file)], expected_exit=1)
output.fnmatch_lines(str(input_file) + ': Failed' + _get_formatter_msg(formatter))
Expand Down

0 comments on commit d3057eb

Please sign in to comment.