Skip to content

Commit f856f6e

Browse files
committed
refactor(main): refactor the code to be simpler to test
1 parent 2082a33 commit f856f6e

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

ardupilot_methodic_configurator/__main__.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,20 @@ def create_argument_parser() -> argparse.ArgumentParser:
9191
return parser
9292

9393

94-
def setup_logging_and_check_updates(state: ApplicationState) -> bool:
94+
def setup_logging(state: ApplicationState) -> None:
9595
"""
96-
Set up logging and check for software updates.
96+
Set up logging.
97+
98+
Args:
99+
state: Application state containing parsed arguments
100+
101+
"""
102+
logging_basicConfig(level=logging_getLevelName(state.args.loglevel), format="%(asctime)s - %(levelname)s - %(message)s")
103+
104+
105+
def check_updates(state: ApplicationState) -> bool:
106+
"""
107+
Check for software updates.
97108
98109
Args:
99110
state: Application state containing parsed arguments
@@ -102,8 +113,6 @@ def setup_logging_and_check_updates(state: ApplicationState) -> bool:
102113
True if the application should exit due to updates, False otherwise
103114
104115
"""
105-
logging_basicConfig(level=logging_getLevelName(state.args.loglevel), format="%(asctime)s - %(levelname)s - %(message)s")
106-
107116
if not state.args.skip_check_for_updates and check_for_software_updates():
108117
logging_info(_("Will now exit the old software version."))
109118
return True
@@ -448,18 +457,18 @@ def main() -> None:
448457
Orchestrates the entire application startup process by calling specialized functions
449458
for each major step.
450459
"""
451-
# Parse arguments and create application state
452460
args = create_argument_parser().parse_args()
461+
453462
state = ApplicationState(args)
454463

455-
# Set up logging and check for updates
456-
if setup_logging_and_check_updates(state):
457-
sys_exit(0)
464+
setup_logging(state)
465+
466+
# Check for software updates
467+
if check_updates(state):
468+
sys_exit(0) # user asked to update, exit the old version
458469

459-
# Handle auto-opening documentation
460470
display_first_use_documentation()
461471

462-
# Initialize flight controller and filesystem
463472
initialize_flight_controller_and_filesystem(state)
464473

465474
# Handle vehicle directory selection if needed

tests/test__main__.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ardupilot_methodic_configurator.__main__ import (
2121
ApplicationState,
2222
backup_fc_parameters,
23+
check_updates,
2324
component_editor,
2425
connect_to_fc_and_set_vehicle_type,
2526
create_and_configure_component_editor,
@@ -29,7 +30,6 @@
2930
open_firmware_documentation,
3031
parameter_editor_and_uploader,
3132
process_component_editor_results,
32-
setup_logging_and_check_updates,
3333
should_open_firmware_documentation,
3434
vehicle_directory_selection,
3535
write_parameter_defaults_if_dirty,
@@ -145,15 +145,13 @@ def test_user_can_start_application_with_updates_disabled(self, application_stat
145145
application_state.args.skip_check_for_updates = True
146146

147147
with (
148-
patch("ardupilot_methodic_configurator.__main__.logging_basicConfig") as mock_logging,
149148
patch("ardupilot_methodic_configurator.__main__.check_for_software_updates") as mock_check,
150149
):
151150
# Act: User starts application
152-
should_exit = setup_logging_and_check_updates(application_state)
151+
should_exit = check_updates(application_state)
153152

154153
# Assert: Application continues normally
155154
assert should_exit is False
156-
mock_logging.assert_called_once()
157155
mock_check.assert_not_called()
158156

159157
def test_user_receives_update_notification_when_new_version_available(self, application_state: ApplicationState) -> None:
@@ -170,14 +168,12 @@ def test_user_receives_update_notification_when_new_version_available(self, appl
170168
with (
171169
patch("ardupilot_methodic_configurator.__main__.logging_basicConfig"),
172170
patch("ardupilot_methodic_configurator.__main__.check_for_software_updates", return_value=True),
173-
patch("ardupilot_methodic_configurator.__main__.logging_info") as mock_log,
174171
):
175172
# Act: User starts outdated application
176-
should_exit = setup_logging_and_check_updates(application_state)
173+
should_exit = check_updates(application_state)
177174

178175
# Assert: User informed and application exits
179176
assert should_exit is True
180-
mock_log.assert_called_once()
181177

182178
def test_user_proceeds_normally_when_application_is_current(self, application_state: ApplicationState) -> None:
183179
"""
@@ -191,11 +187,10 @@ def test_user_proceeds_normally_when_application_is_current(self, application_st
191187
application_state.args.skip_check_for_updates = False
192188

193189
with (
194-
patch("ardupilot_methodic_configurator.__main__.logging_basicConfig"),
195190
patch("ardupilot_methodic_configurator.__main__.check_for_software_updates", return_value=False),
196191
):
197192
# Act: User starts current application
198-
should_exit = setup_logging_and_check_updates(application_state)
193+
should_exit = check_updates(application_state)
199194

200195
# Assert: Application continues normally
201196
assert should_exit is False
@@ -461,7 +456,6 @@ def test_user_can_complete_standard_startup_workflow(self, mock_args: MagicMock)
461456
mock_args.skip_check_for_updates = True # Skip for test efficiency
462457

463458
with (
464-
patch("ardupilot_methodic_configurator.__main__.logging_basicConfig"),
465459
patch("ardupilot_methodic_configurator.__main__.ProgramSettings.get_setting", return_value=False),
466460
patch("ardupilot_methodic_configurator.__main__.connect_to_fc_and_set_vehicle_type") as mock_connect,
467461
patch("ardupilot_methodic_configurator.__main__.FlightControllerInfoWindow"),
@@ -479,7 +473,7 @@ def test_user_can_complete_standard_startup_workflow(self, mock_args: MagicMock)
479473
# Act: Execute complete startup workflow
480474
state = ApplicationState(mock_args)
481475

482-
should_exit = setup_logging_and_check_updates(state)
476+
should_exit = check_updates(state)
483477
assert should_exit is False
484478

485479
display_first_use_documentation() # Should complete without issues

0 commit comments

Comments
 (0)