1+ #!/usr/bin/env python3
2+
3+ # Copyright 2025 Open Source Robotics Foundation, Inc.
4+ #
5+ # Licensed under the Apache License, Version 2.0 (the "License");
6+ # you may not use this file except in compliance with the License.
7+ # You may obtain a copy of the License at
8+ #
9+ # http://www.apache.org/licenses/LICENSE-2.0
10+ #
11+ # Unless required by applicable law or agreed to in writing, software
12+ # distributed under the License is distributed on an "AS IS" BASIS,
13+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ # See the License for the specific language governing permissions and
15+ # limitations under the License.
16+
17+ import unittest
18+ import subprocess
19+
20+ class TestNamespaceArgument (unittest .TestCase ):
21+ """Test the --namespace command line argument for ros2 launch."""
22+
23+ def test_namespace_argument (self ):
24+ """Test that the --namespace argument correctly namespaces topics."""
25+ try :
26+ # Start the talker_listener launch file with namespace argument
27+ launch_proc_remapped = subprocess .Popen (
28+ ['ros2' , 'launch' , 'demo_nodes_cpp' , 'talker_listener_launch.py' ,
29+ '--namespace' , 'test_namespace' ],
30+ stdout = subprocess .PIPE ,
31+ stderr = subprocess .PIPE
32+ )
33+
34+ # Run ros2 topic list to get the remapped topics
35+ result = subprocess .run (['ros2' , 'topic' , 'list' ],
36+ stdout = subprocess .PIPE ,
37+ stderr = subprocess .PIPE ,
38+ text = True ,
39+ check = True )
40+ remapped_topics = result .stdout .strip ().split ('\n ' )
41+
42+ # Verify /chatter is NOT in the list
43+ self .assertNotIn ('/chatter' , remapped_topics ,
44+ f"Did not expect to find /chatter after remapping. Topics: { remapped_topics } " )
45+
46+ # Verify /test_namespace/chatter IS in the list
47+ self .assertIn ('/test_namespace/chatter' , remapped_topics ,
48+ f"Expected to find /test_namespace/chatter after remapping. Topics: { remapped_topics } " )
49+
50+ finally :
51+ # Clean up
52+ if 'launch_proc_remapped' in locals ():
53+ launch_proc_remapped .terminate ()
54+ try :
55+ launch_proc_remapped .wait (timeout = 5 )
56+ except subprocess .TimeoutExpired :
57+ launch_proc_remapped .kill ()
58+ launch_proc_remapped .wait ()
59+
60+
61+ if __name__ == '__main__' :
62+ unittest .main ()
0 commit comments