message: tools: Test for external tools in check_format_test_helper.py
  Add a test which temporarily changes PATH to get an error about
  clang-format-7 being not available, then expects that error.
  This is a followup to
  Risk Level: low
  Testing: new checker test
  Docs Changes: n/a
  Release Notes: n/a
diff: @@ -10,13 +10,14 @@ import os
  import shutil
  import logging
  import subprocess
  +import sys
  os.putenv("BUILDIFIER_BIN", "/usr/local/bin/buildifier")
  tools = os.path.dirname(os.path.realpath(__file__))
  tmp = os.path.join(os.getenv('TEST_TMPDIR', "/tmp"), "check_format_test")
  src = os.path.join(tools, 'testdata', 'check_format')
  -check_format = os.path.join(tools, 'check_format.py')
  +check_format = sys.executable + " " + os.path.join(tools, 'check_format.py')
  errors = 0
  # Echoes and runs an OS command, returning exit status and the captured
  @@ -110,6 +111,16 @@ def checkAndFixError(filename, expected_substring):
  errors += fixFileExpectingSuccess(filename)
  return errors
  +def checkToolNotFoundError():
  + # Temporarily change PATH to test the error about lack of external tools.
  + oldPath = os.environ["PATH"]
  + os.environ["PATH"] = "/sbin:/usr/sbin"
  + clang_format = os.getenv("CLANG_FORMAT", "clang-format-7")
  + errors = checkFileExpectingError("no_namespace_envoy.cc",
  + "Command %s not found." % clang_format)
  + os.environ["PATH"] = oldPath
  + return errors
  +
  def checkUnfixableError(filename, expected_substring):
  errors = checkFileExpectingError(filename, expected_substring)
  errors += fixFileExpectingFailure(filename, expected_substring)
  @@ -137,6 +148,9 @@ if __name__ == "__main__":
  os.makedirs(tmp)
  os.chdir(tmp)
  + # The following error is the error about unavailability of external tools.
  + errors += checkToolNotFoundError()
  +
  # The following errors can be detected but not fixed automatically.
  errors += checkUnfixableError("no_namespace_envoy.cc",
  "Unable to find Envoy namespace or NOLINT(namespace-envoy)")
  