Skip to content

refactor(main): make the code more testable and easier to read#1347

Merged
amilcarlucas merged 3 commits intomasterfrom
refactor_main
Mar 4, 2026
Merged

refactor(main): make the code more testable and easier to read#1347
amilcarlucas merged 3 commits intomasterfrom
refactor_main

Conversation

@amilcarlucas
Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI review requested due to automatic review settings March 4, 2026 00:16
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the main entrypoint to improve readability and testability by extracting connection and vehicle-directory resolution logic into dedicated functions.

Changes:

  • Split FC connection + vehicle type resolution into connect_to_fc() and resolve_vehicle_type().
  • Extracted vehicle directory selection/writability logic into get_preferred_vehicle_dir() and resolve_writable_vehicle_dir_for_initial_download().
  • Added tests covering the new behavior and updated existing connection tests to patch the new progress context manager.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
tests/test__main__.py Adds/updates unit tests for new connection progress patching and vehicle directory resolution helpers.
ardupilot_methodic_configurator/main.py Refactors FC connection/vehicle type logic and extracts vehicle directory resolution + writability checks into helpers.

Comment on lines +287 to +292
path.mkdir(parents=True, exist_ok=True)
test_file = path / ".write_test.tmp"
with open(test_file, "w", encoding="utf-8") as tmp:
tmp.write("test")
test_file.unlink(missing_ok=True)
return True
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a fixed filename (.write_test.tmp) can cause collisions if multiple instances/threads/processes run this check concurrently in the same directory (one call can delete/overwrite another’s file and produce flaky results). Consider generating a unique temp filename (e.g., via tempfile with the target directory) and ensure cleanup happens in a finally block so the temp file is removed even if the write fails mid-way.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

result = get_preferred_vehicle_dir(mock_args)

assert result == Path("C:/cwd")

Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case models an args object without a vehicle_dir attribute, but in real usage argparse.Namespace typically includes the attribute (often set to None). Add a test where args.vehicle_dir exists but is None (and/or empty) to cover the realistic fallback behavior and prevent regressions like Path(None) raising.

Suggested change
def test_falls_back_to_cwd_when_vehicle_dir_is_none(self) -> None:
"""Given args.vehicle_dir is None, preferred directory should be current working directory."""
mock_args = MagicMock()
mock_args.vehicle_dir = None
with patch("ardupilot_methodic_configurator.__main__.Path.cwd", return_value=Path("C:/cwd")):
result = get_preferred_vehicle_dir(mock_args)
assert result == Path("C:/cwd")

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Amilcar Lucas <amilcar.lucas@iav.de>
Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 4, 2026

@amilcarlucas I've opened a new pull request, #1349, to work on those changes. Once the pull request is ready, I'll request review from you.

…leanup in _is_directory_writable

Co-authored-by: amilcarlucas <24453563+amilcarlucas@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 4, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
11564 10528 91% 89% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
ardupilot_methodic_configurator/main.py 75% 🟢
TOTAL 75% 🟢

updated for commit: d6fedc7 by action🐍

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 4, 2026

Test Results

    2 files      2 suites   28m 30s ⏱️
3 032 tests 3 027 ✅  5 💤 0 ❌
6 064 runs  6 054 ✅ 10 💤 0 ❌

Results for commit d6fedc7.

@amilcarlucas amilcarlucas merged commit 98e2712 into master Mar 4, 2026
31 of 32 checks passed
@amilcarlucas amilcarlucas deleted the refactor_main branch March 4, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants