Skip to content

Commit c20a0e3

Browse files
committed
FIX: Only consider the file exists if size > 0. Only condider donwload complete if size > 0
1 parent 0e1d068 commit c20a0e3

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

ARCHITECTURE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ the following system design requirements were derived:
9494
- The software must provide feedback to the user, such as success or error messages, after each action.
9595
- The software must handle errors gracefully and provide clear error messages to the user.
9696
- The software must log events and errors for debugging and auditing purposes to the console.
97+
- if files are empty flag them as non-existing [PR #135](https://github.com/ArduPilot/MethodicConfigurator/pull/135)
98+
- if a downloaded file is empty flag it as download failed [PR #135](https://github.com/ArduPilot/MethodicConfigurator/pull/135)
9799

98100
#### 6. Parameter File Management
99101

ardupilot_methodic_configurator/backend_filesystem.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,11 @@ def vehicle_configuration_file_exists(self, filename: str) -> bool:
292292
filename (str): The name of the file to check.
293293
294294
Returns:
295-
bool: True if the file exists and is a file (not a directory), False otherwise.
295+
bool: True if the file exists and is a file (not a directory) and is not empty, False otherwise.
296296
297297
"""
298-
return os_path.exists(os_path.join(self.vehicle_dir, filename)) and os_path.isfile(
299-
os_path.join(self.vehicle_dir, filename)
300-
)
298+
file_path = os_path.join(self.vehicle_dir, filename)
299+
return os_path.exists(file_path) and os_path.isfile(file_path) and os_path.getsize(file_path) > 0
301300

302301
def __all_intermediate_parameter_file_comments(self) -> dict[str, str]:
303302
"""

ardupilot_methodic_configurator/backend_internet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def download_file_from_url(
7676

7777
if progress_callback:
7878
progress_callback(100.0, _("Download complete"))
79-
return True
79+
return bool(downloaded > 0)
8080

8181
except requests_Timeout:
8282
logging_error(_("Download timed out"))

tests/test_backend_filesystem.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,43 @@ def test_rename_parameter_files(self) -> None:
110110
expected_new = os_path.join("/mock/dir", "new_file.param")
111111
mock_rename.assert_called_once_with(expected_old, expected_new)
112112

113-
def test_vehicle_configuration_file_exists(self) -> None:
114-
"""Test checking if a specific configuration file exists."""
113+
def test_vehicle_configuration_file_exists_comprehensive(self) -> None:
114+
"""Test checking if a specific configuration file exists with comprehensive path and size checks."""
115115
mock_vehicle_dir = "/mock/dir"
116-
filesystem = LocalFilesystem(mock_vehicle_dir, "ArduCopter", "4.3.0", False) # noqa: FBT003
116+
filesystem = LocalFilesystem(mock_vehicle_dir, "ArduCopter", "4.3.0", allow_editing_template_files=False)
117117

118-
with patch("os.path.exists", return_value=True), patch("os.path.isfile", return_value=True):
118+
# Test with all conditions met (file exists, is a file, and has size > 0)
119+
with (
120+
patch("os.path.exists", return_value=True) as mock_exists,
121+
patch("os.path.isfile", return_value=True) as mock_isfile,
122+
patch("os.path.getsize", return_value=100) as mock_getsize,
123+
patch("os.path.join", side_effect=os_path.join) as mock_join,
124+
):
119125
assert filesystem.vehicle_configuration_file_exists("test.param")
126+
mock_exists.assert_called_once()
127+
mock_isfile.assert_called_once()
128+
mock_getsize.assert_called_once()
129+
mock_join.assert_called_once_with(mock_vehicle_dir, "test.param")
130+
131+
# Test with file that exists but is empty
132+
with (
133+
patch("os.path.exists", return_value=True),
134+
patch("os.path.isfile", return_value=True),
135+
patch("os.path.getsize", return_value=0),
136+
patch("os.path.join", side_effect=os_path.join),
137+
):
138+
assert not filesystem.vehicle_configuration_file_exists("empty.param")
139+
140+
# Test with directory instead of file
141+
with (
142+
patch("os.path.exists", return_value=True),
143+
patch("os.path.isfile", return_value=False),
144+
patch("os.path.join", side_effect=os_path.join),
145+
):
146+
assert not filesystem.vehicle_configuration_file_exists("dir.param")
120147

121-
with patch("os.path.exists", return_value=False):
148+
# Test with nonexistent file
149+
with patch("os.path.exists", return_value=False), patch("os.path.join", side_effect=os_path.join):
122150
assert not filesystem.vehicle_configuration_file_exists("nonexistent.param")
123151

124152
@patch("os.path.exists")

tests/test_backend_internet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_download_file_empty_response(mock_get) -> None:
9393
mock_response.headers = {"content-length": "0"}
9494
mock_response.iter_content.return_value = []
9595
mock_get.return_value = mock_response
96-
assert download_file_from_url("http://test.com", "test.txt")
96+
assert not download_file_from_url("http://test.com", "test.txt")
9797

9898

9999
@patch("ardupilot_methodic_configurator.backend_internet.requests_get")

0 commit comments

Comments
 (0)