Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions ament_clang_format/ament_clang_format/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,20 @@ def main(argv: list[str] = sys.argv[1:]) -> Literal[0, 1]:
report[filename] = []

xmls = output.split(b"<?xml version='1.0'?>")[1:]
if len(files) > len(xmls):
# Skip empty files since clang-format doesn't output anything for them.
for index in range(len(files)):
if os.stat(files[index]).st_size == 0:
xmls.insert(index, None)

changed_files = []
for filename, xml in zip(files, xmls):
if not xml:
print("Skipping empty file '%s'" % filename)
if not args.reformat:
print('')
continue

try:
root = ElementTree.fromstring(xml)
except ElementTree.ParseError as e:
Expand Down
54 changes: 54 additions & 0 deletions ament_clang_format/test/test_empty_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright 2026 Open Source Robotics Foundation, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from ament_clang_format.main import main


EMPTY_CONTENTS = ''
PROPER_CONTENTS = '#include <this/file/should/be/unchanged>\n'
MALFORMED_CONTENTS_BEFORE = 'void this_should_get_formatted( );\n'
MALFORMED_CONTENTS_AFTER = 'void this_should_get_formatted();\n'


def test_empty_files(tmp_path, capsys):
"""Check that formatting suggestions are correct in the presence of an empty file."""
empty_path = tmp_path / 'empty.cpp'
proper_path = tmp_path / 'proper.cpp'
malformed_path = tmp_path / 'malformed.cpp'

empty_path.write_text(EMPTY_CONTENTS, encoding='utf_8')
proper_path.write_text(PROPER_CONTENTS, encoding='utf_8')
malformed_path.write_text(MALFORMED_CONTENTS_BEFORE, encoding='utf_8')

main([str(empty_path), str(proper_path), str(malformed_path)])

captured = capsys.readouterr()
assert MALFORMED_CONTENTS_AFTER in captured.err


def test_empty_files_reformat(tmp_path):
"""Check that reformatting applies correctly in the presence of an empty file."""
empty_path = tmp_path / 'empty.cpp'
proper_path = tmp_path / 'proper.cpp'
malformed_path = tmp_path / 'malformed.cpp'

empty_path.write_text(EMPTY_CONTENTS, encoding='utf_8')
proper_path.write_text(PROPER_CONTENTS, encoding='utf_8')
malformed_path.write_text(MALFORMED_CONTENTS_BEFORE, encoding='utf_8')

main(['--reformat', str(empty_path), str(proper_path), str(malformed_path)])

assert empty_path.read_text(encoding='utf_8') == EMPTY_CONTENTS
assert proper_path.read_text(encoding='utf_8') == PROPER_CONTENTS
assert malformed_path.read_text(encoding='utf_8') == MALFORMED_CONTENTS_AFTER