def test_nontrivial_reformatting(self): # If clang-format changes any lines, we want to fail-fast. # (Note the two spaces between #include and the double quote.) original_lines = ['#include "nontrivial.h"\n'] dut = IncludeFormatter("nontrivial.cc", readlines=original_lines) dut.format_includes() with self.assertRaisesRegexp(Exception, 'not just a shuffle'): dut.rewrite_file()
def _check(self, basename, original, expected, first_differing): original_lines = self._split(original) expected_lines = self._split(expected) dut = IncludeFormatter( "drake/dummy/" + basename, readlines=original_lines) dut.format_includes() self.assertEqual(dut.get_all_lines(), expected_lines) self.assertEqual(dut.get_first_differing_original_index(), first_differing)
def _check_includes(filename): """Return 0 if clang-format-includes is a no-op, and 1 otherwise.""" tool = IncludeFormatter(filename) tool.format_includes() first_difference = tool.get_first_differing_original_index() if first_difference is not None: print("error: " + filename + ":" + str(first_difference + 1) + ": " + "the #include ordering is incorrect") print("note: fix via bazel-bin/tools/lint/clang-format-includes " + filename) return 1 return 0
def _check_includes(filename): """Return 0 if clang-format-includes is a no-op, and 1 otherwise.""" tool = IncludeFormatter(filename) tool.format_includes() first_difference = tool.get_first_differing_original_index() if first_difference is not None: print("error: " + filename + ":" + str(first_difference + 1) + ": " + "the #include ordering is incorrect") print("note: fix via bazel-bin/tools/lint/clang-format-includes " + filename) return 1 return 0
def _check_includes(filename): """Return 0 if clang-format-includes is a no-op, and 1 otherwise.""" try: tool = IncludeFormatter(filename) except Exception as e: print("ERROR: " + filename + ":0: " + str(e)) return 1 tool.format_includes() first_difference = tool.get_first_differing_original_index() if first_difference is not None: print("ERROR: " + filename + ":" + str(first_difference + 1) + ": " + "the #include ordering is incorrect") print("note: fix via bazel-bin/tools/lint/clang-format-includes " + filename) print("note: if that program does not exist, " + "you might need to compile it first: " + "bazel build //tools/lint/...") return 1 return 0
def test_nontrivial_reformatting(self): # If clang-format changes any lines, we want to fail-fast. # (Note the two spaces between #include and the double quote.) original_lines = ['#include "nontrivial.h"\n'] dut = IncludeFormatter("nontrivial.cc", readlines=original_lines) dut.format_includes() with self.assertRaisesRegexp(Exception, 'not just a shuffle'): dut.rewrite_file()
def _check(self, basename, original, expected, first_differing): original_lines = self._split(original) expected_lines = self._split(expected) dut = IncludeFormatter("drake/dummy/" + basename, readlines=original_lines) dut.format_includes() self.assertEqual(dut.get_all_lines(), expected_lines) self.assertEqual(dut.get_first_differing_original_index(), first_differing)
def main(workspace_name="drake"): parser = argparse.ArgumentParser(prog='clang-format-includes', description=__doc__) parser.add_argument('filenames', metavar='filename', type=str, nargs='*', help='full path to filename to reformat in place') parser.add_argument( '--all', action='store_true', default=False, help='reformat all first-party sources within the project') parser.add_argument( '--check-only', action='store_true', default=False, help='check if the file(s) are formatted correctly; do not edit') args = parser.parse_args() if args.all: workspace_dir, relpaths = find_all_sources(workspace_name) extensions = ["cc", "h", "cpp"] filenames = [ os.path.join(workspace_dir, relpath) for relpath in relpaths if os.path.splitext(relpath)[1][1:] in extensions and not relpath.startswith("third_party") ] print(f"This will reformat {len(filenames)} files " f"within {workspace_dir}") if input("Are you sure [y/N]? ") not in ["y", "Y"]: print("... canceled") sys.exit(1) else: filenames = args.filenames num_errors = 0 for filename in filenames: tool = IncludeFormatter(filename) tool.format_includes() if tool.is_same_as_original(): continue if args.check_only: num_errors += 1 else: tool.rewrite_file() if num_errors > 0: return 1 return 0
def main(): parser = argparse.ArgumentParser(prog='clang-format-includes', description=__doc__) parser.add_argument('filenames', metavar='filename', type=str, nargs='*', help='full path to filename to reformat in place') parser.add_argument( '--all', action='store_true', default=False, help='reformat all first-party sources within the project') parser.add_argument( '--check-only', action='store_true', default=False, help='check if the file(s) are formatted correctly; do not edit') args = parser.parse_args() if args.all: # TODO(jwnimmer-tri) Consolidate this logic with the cpplint_wrapper # tree searching logic, including some way to unit test "all" search. extensions = ["cc", "h", "cpp"] pathnames = ["drake"] filenames = [ os.path.join(dirpath, filename) for pathname in pathnames for dirpath, _, filenames in os.walk(pathname) for filename in filenames if os.path.splitext(filename)[1][1:] in extensions and "/third_party/" not in dirpath and "/matlab/" not in dirpath ] else: filenames = args.filenames num_errors = 0 for filename in filenames: tool = IncludeFormatter(filename) tool.format_includes() if tool.is_same_as_original(): continue if args.check_only: num_errors += 1 else: tool.rewrite_file() if num_errors > 0: return 1 return 0
def main(): parser = argparse.ArgumentParser( prog='clang-format-includes', description=__doc__) parser.add_argument( 'filenames', metavar='filename', type=str, nargs='*', help='full path to filename to reformat in place') parser.add_argument( '--all', action='store_true', default=False, help='reformat all first-party sources within the project') parser.add_argument( '--check-only', action='store_true', default=False, help='check if the file(s) are formatted correctly; do not edit') args = parser.parse_args() if args.all: # TODO(jwnimmer-tri) Consolidate this logic with the cpplint_wrapper # tree searching logic, including some way to unit test "all" search. extensions = ["cc", "h", "cpp"] pathnames = ["drake"] filenames = [ os.path.join(dirpath, filename) for pathname in pathnames for dirpath, _, filenames in os.walk(pathname) for filename in filenames if os.path.splitext(filename)[1][1:] in extensions and "/third_party/" not in dirpath and "/matlab/" not in dirpath ] else: filenames = args.filenames num_errors = 0 for filename in filenames: tool = IncludeFormatter(filename) tool.format_includes() if tool.is_same_as_original(): continue if args.check_only: num_errors += 1 else: tool.rewrite_file() if num_errors > 0: return 1 return 0
def main(workspace_name="drake"): parser = argparse.ArgumentParser( prog='clang-format-includes', description=__doc__) parser.add_argument( 'filenames', metavar='filename', type=str, nargs='*', help='full path to filename to reformat in place') parser.add_argument( '--all', action='store_true', default=False, help='reformat all first-party sources within the project') parser.add_argument( '--check-only', action='store_true', default=False, help='check if the file(s) are formatted correctly; do not edit') args = parser.parse_args() if args.all: workspace_dir, relpaths = find_all_sources(workspace_name) extensions = ["cc", "h", "cpp"] filenames = [ os.path.join(workspace_dir, relpath) for relpath in relpaths if os.path.splitext(relpath)[1][1:] in extensions and not relpath.startswith("third_party") and not relpath.startswith("matlab") ] else: filenames = args.filenames num_errors = 0 for filename in filenames: tool = IncludeFormatter(filename) tool.format_includes() if tool.is_same_as_original(): continue if args.check_only: num_errors += 1 else: tool.rewrite_file() if num_errors > 0: return 1 return 0