Beispiel #1
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 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 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
Beispiel #5
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