@@ -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+
428464def 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+
456499def 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