|
| 1 | +# Copyright (c) 2025 Leander Stephen Desouza |
| 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 os |
| 16 | +import tempfile |
| 17 | +from typing import Generator |
| 18 | + |
| 19 | +import launch |
| 20 | +from launch.substitutions import LaunchConfiguration |
| 21 | +from nav2_common.launch import RewrittenYaml |
| 22 | +import pytest |
| 23 | +import yaml |
| 24 | + |
| 25 | + |
| 26 | +class TestRewrittenYamlValueRewrites: |
| 27 | + """Test that value rewrites work correctly in RewrittenYaml.""" |
| 28 | + |
| 29 | + @pytest.fixture(autouse=True) |
| 30 | + def setup_teardown(self) -> Generator[None, None, None]: |
| 31 | + # Create a temporary YAML file for testing |
| 32 | + self.test_yaml = tempfile.NamedTemporaryFile(mode='w', delete=False) |
| 33 | + self.test_yaml.write("""\ |
| 34 | + param0: placeholder_bool |
| 35 | + param1: placeholder_string |
| 36 | + param2: placeholder_float |
| 37 | + param3: placeholder_int |
| 38 | + param4: placeholder_list |
| 39 | + param5: placeholder_dict |
| 40 | + nested: |
| 41 | + param6: placeholder_bool |
| 42 | + param7: placeholder_string |
| 43 | + param8: placeholder_float |
| 44 | + param9: placeholder_int |
| 45 | + param10: placeholder_list |
| 46 | + param11: placeholder_dict |
| 47 | + other_value: 42 |
| 48 | + list_values: |
| 49 | + - placeholder_bool |
| 50 | + - placeholder_string |
| 51 | + - placeholder_float |
| 52 | + - placeholder_int |
| 53 | + - placeholder_list |
| 54 | + - placeholder_dict |
| 55 | + - normal_value |
| 56 | + """) |
| 57 | + self.test_yaml.close() |
| 58 | + yield |
| 59 | + os.unlink(self.test_yaml.name) |
| 60 | + |
| 61 | + def test_value_rewrites(self) -> None: |
| 62 | + """Test that value rewrites work for various types.""" |
| 63 | + # Set up launch configurations for our test values |
| 64 | + launch_configurations = { |
| 65 | + 'test_bool': 'true', |
| 66 | + 'test_string': 'replaced_string', |
| 67 | + 'test_float': '3.14', |
| 68 | + 'test_int': '100', |
| 69 | + 'test_list': '["string", 42, 2.718, ["sublist_item1", "sublist_item2"]]', |
| 70 | + 'test_dict': ('{"key1": "value1", "key2": 2, "key3": 3.14, ' |
| 71 | + '"key4": ["list_item1", "list_item2"]}') |
| 72 | + } |
| 73 | + context = launch.LaunchContext() |
| 74 | + for key, value in launch_configurations.items(): |
| 75 | + context.launch_configurations[key] = value |
| 76 | + |
| 77 | + value_rewrites = { |
| 78 | + 'placeholder_bool': LaunchConfiguration('test_bool'), |
| 79 | + 'placeholder_string': LaunchConfiguration('test_string'), |
| 80 | + 'placeholder_float': LaunchConfiguration('test_float'), |
| 81 | + 'placeholder_int': LaunchConfiguration('test_int'), |
| 82 | + 'placeholder_list': LaunchConfiguration('test_list'), |
| 83 | + 'placeholder_dict': LaunchConfiguration('test_dict'), |
| 84 | + } |
| 85 | + |
| 86 | + rewriter = RewrittenYaml( |
| 87 | + source_file=self.test_yaml.name, |
| 88 | + param_rewrites={}, |
| 89 | + value_rewrites=value_rewrites, |
| 90 | + convert_types=True, |
| 91 | + ) |
| 92 | + output_file = rewriter.perform(context) |
| 93 | + |
| 94 | + try: |
| 95 | + with open(output_file) as f: |
| 96 | + result = yaml.safe_load(f) |
| 97 | + |
| 98 | + assert result['param0'] is True |
| 99 | + assert result['param1'] == 'replaced_string' |
| 100 | + assert result['param2'] == 3.14 |
| 101 | + assert result['param3'] == 100 |
| 102 | + assert result['param4'] == \ |
| 103 | + '["string", 42, 2.718, ["sublist_item1", "sublist_item2"]]' |
| 104 | + assert result['param5'] == \ |
| 105 | + '{"key1": "value1", "key2": 2, "key3": 3.14, "key4": ["list_item1", "list_item2"]}' |
| 106 | + |
| 107 | + # Check nested values |
| 108 | + assert result['nested']['param6'] is True |
| 109 | + assert result['nested']['param7'] == 'replaced_string' |
| 110 | + assert result['nested']['param8'] == 3.14 |
| 111 | + assert result['nested']['param9'] == 100 |
| 112 | + assert result['nested']['param10'] == \ |
| 113 | + '["string", 42, 2.718, ["sublist_item1", "sublist_item2"]]' |
| 114 | + assert result['nested']['param11'] == \ |
| 115 | + '{"key1": "value1", "key2": 2, "key3": 3.14, "key4": ["list_item1", "list_item2"]}' |
| 116 | + |
| 117 | + # Check list values |
| 118 | + assert result['list_values'][0] is True |
| 119 | + assert result['list_values'][1] == 'replaced_string' |
| 120 | + assert result['list_values'][2] == 3.14 |
| 121 | + assert result['list_values'][3] == 100 |
| 122 | + assert result['list_values'][4] == \ |
| 123 | + '["string", 42, 2.718, ["sublist_item1", "sublist_item2"]]' |
| 124 | + assert result['list_values'][5] == \ |
| 125 | + '{"key1": "value1", "key2": 2, "key3": 3.14, "key4": ["list_item1", "list_item2"]}' |
| 126 | + |
| 127 | + # Check other values remain unchanged |
| 128 | + assert result['nested']['other_value'] == 42 |
| 129 | + assert result['list_values'][6] == 'normal_value' |
| 130 | + |
| 131 | + finally: |
| 132 | + if os.path.exists(output_file): |
| 133 | + os.unlink(output_file) |
0 commit comments