Skip to content

Commit 5568e8f

Browse files
committed
feat: add test_helpers for temp files and add unit tests for ament_clang* execution
Signed-off-by: Tony Paulussen <[email protected]>
1 parent 414bb00 commit 5568e8f

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ament_clang_format.main import main
16+
from ament_lint.test_helpers import TempFileWriter
17+
18+
def test_clang_format_execution():
19+
"""Test that clang format can be executed on a simple C++ file, via ament_clang_format."""
20+
with TempFileWriter("int main() { return 0; }", "test.cpp") as temp_file_path:
21+
rc = main(argv=['ament_clang_format', temp_file_path])
22+
assert rc == 0, 'Clang format found issues'

ament_clang_tidy/ament_clang_tidy/main.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ def start_subprocess(full_cmd):
265265
with open(args.xunit_file, 'w') as f:
266266
f.write(xml)
267267

268+
return 0 if all(len(v) == 0 for v in report.values()) else 1
269+
268270

269271
def get_compilation_db_files(paths):
270272
files = []
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
from pathlib import Path
17+
18+
from ament_clang_tidy.main import main
19+
from ament_lint.test_helpers import TempFileWriter
20+
21+
def test_clang_tidy_execution():
22+
"""Test that clang tidy can be executed on a simple C++ file, via ament_clang_tidy."""
23+
with TempFileWriter("int main() { return 0; }", "test.cpp") as temp_file_path:
24+
temp_dir = Path(temp_file_path).parent
25+
26+
# Create compile_commands.json
27+
compile_commands = [
28+
{
29+
"directory": str(temp_dir),
30+
"command": "c++ -c test.cpp",
31+
"file": str(temp_file_path)
32+
}
33+
]
34+
compile_commands_json = json.dumps(compile_commands)
35+
with TempFileWriter(compile_commands_json, "compile_commands.json") as compile_commands_path:
36+
rc = main(argv=['ament_clang_tidy', str(compile_commands_path)])
37+
assert rc == 0, 'Clang tidy found issues'

ament_lint/ament_lint/filesystem_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 Open Source Robotics Foundation, Inc.
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2025 Open Source Robotics Foundation, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import tempfile
16+
import os
17+
18+
class TempFileWriter:
19+
"""Context manager that writes a string to a temp file and cleans up on exit."""
20+
21+
def __init__(self, content, filename):
22+
self.content = content
23+
self.filename = filename
24+
self.temp_dir = None
25+
self.temp_file = None
26+
27+
def __enter__(self):
28+
# Create a new temporary directory
29+
self.temp_dir = tempfile.mkdtemp()
30+
# Create the temp file path
31+
self.temp_file = os.path.join(self.temp_dir, self.filename)
32+
# Write content to the file
33+
with open(self.temp_file, 'w') as f:
34+
f.write(self.content)
35+
return self.temp_file
36+
37+
def __exit__(self, exc_type, exc_val, exc_tb):
38+
# Remove the file
39+
if self.temp_file and os.path.exists(self.temp_file):
40+
os.remove(self.temp_file)
41+
# Remove the directory
42+
if self.temp_dir and os.path.exists(self.temp_dir):
43+
os.rmdir(self.temp_dir)
44+
return False

0 commit comments

Comments
 (0)