Skip to content

Commit e321a60

Browse files
committed
test(components): Do not overwrite system_vehicle_components_template.json during tests
1 parent 16afcc0 commit e321a60

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

ardupilot_methodic_configurator/backend_filesystem_program_settings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
# from sys import exit as sys_exit
12+
import os
1213
from contextlib import suppress as contextlib_suppress
1314
from dataclasses import dataclass
1415
from glob import glob as glob_glob
@@ -470,6 +471,12 @@ def get_recent_vehicle_dirs() -> list[str]:
470471

471472
@staticmethod
472473
def get_templates_base_dir() -> str:
474+
# Allow tests to override the templates directory in a safe temporary location
475+
test_templates_dir = os.environ.get("AMC_COMPONENT_TEMPLATES_DIR_TEST")
476+
if test_templates_dir:
477+
logging_debug("Using test templates directory: %s", test_templates_dir)
478+
return test_templates_dir
479+
473480
package_path = importlib_files("ardupilot_methodic_configurator")
474481
logging_debug("current script directory1: %s", package_path)
475482
if platform_system() == "Windows":

ardupilot_methodic_configurator/backend_filesystem_vehicle_components.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,24 @@ def save_component_templates_to_file(self, templates_to_save: dict[str, list[dic
233233
# System templates are part of the software installation and get updated when the program
234234
# is updated and deleted when the program is un-installed.
235235
templates_filename = "system_vehicle_components_template.json"
236-
# Check if local system template file exists, use local dir if so.
236+
# Check if local system template file exists, use local templates_dir if so.
237237
try:
238-
local_dir = importlib_files("ardupilot_methodic_configurator") / "vehicle_templates"
239-
filepath = str(local_dir / templates_filename)
238+
templates_dir = ProgramSettings.get_templates_base_dir()
239+
filepath = os_path.join(templates_dir, templates_filename)
240+
240241
if os_path.exists(filepath):
241242
existing_templates = self._load_system_templates(filepath)
243+
else:
244+
# Fallback to package path when system-wide path is unavailable
245+
local_dir = importlib_files("ardupilot_methodic_configurator") / "vehicle_templates"
246+
filepath = str(local_dir / templates_filename)
247+
if os_path.exists(filepath):
248+
existing_templates = self._load_system_templates(filepath)
249+
250+
# Ensure folder exists for safe writing in test and normal mode
251+
dir_name = os_path.dirname(filepath)
252+
if dir_name:
253+
os_makedirs(dir_name, exist_ok=True)
242254
except (OSError, ValueError) as e:
243255
logging_debug("Failed to check local template file: %s", e)
244256
# Fall back to default templates_dir

tests/unit_backend_filesystem_vehicle_components.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ def test_save_component_templates_to_file_os_error(self, mock_get_base_dir, mock
160160
@patch("ardupilot_methodic_configurator.backend_filesystem_vehicle_components.os_path.exists")
161161
@patch("builtins.open", new_callable=mock_open)
162162
@patch("ardupilot_methodic_configurator.backend_filesystem_vehicle_components.os_makedirs")
163+
@patch("ardupilot_methodic_configurator.backend_filesystem_vehicle_components.safe_write")
163164
@patch("ardupilot_methodic_configurator.backend_filesystem_program_settings.ProgramSettings.get_templates_base_dir")
164-
def test_save_component_templates_to_file_uses_local_dir_when_local_file_exists(
165-
self, mock_get_base_dir, mock_makedirs, mock_file, mock_exists
165+
def test_save_component_templates_to_file_uses_local_dir_when_local_file_exists( # pylint: disable=too-many-arguments,too-many-positional-arguments
166+
self, mock_get_base_dir, mock_safe_write, mock_makedirs, mock_file, mock_exists
166167
) -> None:
167168
"""
168169
Test that system templates use local directory when local file exists.
@@ -174,15 +175,17 @@ def test_save_component_templates_to_file_uses_local_dir_when_local_file_exists(
174175
mock_get_base_dir.return_value = "/system/templates"
175176
mock_makedirs.return_value = None
176177
mock_exists.return_value = True # Local file exists
178+
mock_safe_write.return_value = None
177179

178180
vehicle_components_system = VehicleComponents(save_component_to_system_templates=True)
179181
templates_to_save = {"Component1": [{"name": "Test", "data": {}}]}
180182

181183
error, msg = vehicle_components_system.save_component_templates_to_file(templates_to_save)
182184

183185
assert error is False
184-
# Should use local dir, which contains "ardupilot_methodic_configurator/vehicle_templates"
185-
assert "ardupilot_methodic_configurator/vehicle_templates/system_vehicle_components_template.json" in msg
186+
# Should use local dir, which contains system template filename (path may vary by environment)
187+
assert "system_vehicle_components_template.json" in msg
188+
assert "/system/templates" in msg or "ardupilot_methodic_configurator/vehicle_templates" in msg
186189

187190
@patch.object(VehicleComponents, "_load_system_templates")
188191
@patch.object(VehicleComponents, "save_component_templates_to_file")

0 commit comments

Comments
 (0)