diff --git a/dist/pioneer-uploader.exe b/dist/pioneer-uploader.exe index 300478b..89d9d1f 100644 Binary files a/dist/pioneer-uploader.exe and b/dist/pioneer-uploader.exe differ diff --git a/pioneer_uploader.py b/pioneer_uploader.py index c18d748..81f9b5c 100644 --- a/pioneer_uploader.py +++ b/pioneer_uploader.py @@ -4,6 +4,7 @@ import os import sys import time +import re from pioneer_sdk import Pioneer import logging @@ -11,27 +12,46 @@ def main(): try: file_path, ip, port, mode = sys.argv[1:5] - except ValueError: - logging.error("Invalid arguments") - return - if not os.path.exists(file_path): - logging.error("Can not find program file") - return + if not os.path.exists(file_path): + raise FileNotFoundError("Can not find program file") - drone = Pioneer( - ip=ip, - mavlink_port=port, - connection_method="udpout" if mode == "wifi" else "serial", - logger=True, - ) + ipv4_pattern = ( + r"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\." + r"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" + ) + + if not re.match(ipv4_pattern, ip): + raise ValueError("Invalid IP address") + + if not 0 < int(port) <= 65535: + raise ValueError("Invalid port") + + if mode not in ["wifi", "usb"]: + raise ValueError("Invalid mode. Choose 'wifi' or 'usb'") + + drone = Pioneer( + ip=ip, + mavlink_port=port, + connection_method="udpout" if mode == "wifi" else "serial", + logger=True, + ) + + if file_path.split(".")[-1] != "lua": + file_name = file_path + "/" + file_path.split("/")[-1] + ".lua" + drone.lua_script_upload(file_name) + else: + drone.lua_script_upload(file_path) - try: - drone.lua_script_upload(file_path) time.sleep(2) drone.close_connection() + except Exception as e: logging.error(e) + logging.info( + "Please download uploader from " + ' site' + ) return diff --git a/requirements.txt b/requirements.txt index 1443fc5..fce33d8 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/test/test_file.lua b/test/test_file.lua new file mode 100644 index 0000000..e69de29 diff --git a/test/test_uploader.py b/test/test_uploader.py index 6ca51ce..bb17a56 100644 --- a/test/test_uploader.py +++ b/test/test_uploader.py @@ -1,31 +1,112 @@ import pytest import repackage +import os from unittest.mock import patch repackage.up() import pioneer_uploader +current_directory = os.path.dirname(os.path.abspath(__file__)) +test_file_path = os.path.join(current_directory, "test_file.lua") + + @patch("logging.error") -def test_without_file(mock_logging_error): +def test_few_arguments(mock_logging_error): test_args = [ - "parameter", - "invalid_file_path", - "invalid_ip", - "invalid_port", - "invalid_mode", + ["less then", "five", "arguments"], + ["parameter", "still", "less", "arguments"], + [""], + ["parameter", "test_file.lua"], ] - with patch("sys.argv", test_args): - pioneer_uploader.main() - mock_logging_error.assert_called_once_with("Can not find program file") + expected_errors = [ + "not enough values to unpack (expected 4, got 2)", + "not enough values to unpack (expected 4, got 3)", + "not enough values to unpack (expected 4, got 0)", + "not enough values to unpack (expected 4, got 1)", + ] + for i in range(len(test_args)): + with patch("sys.argv", test_args[i]): + pioneer_uploader.main() + args, _ = mock_logging_error.call_args + assert isinstance(args[0], ValueError) + assert str(args[0]) == expected_errors[i] @patch("logging.error") -def test_few_arguments(mock_logging_error): - test_args = ["less then", "five", "arguments"] - with patch("sys.argv", test_args): - pioneer_uploader.main() - mock_logging_error.assert_called_once_with("Invalid arguments") +def test_missing_file(mock_logging_error): + test_args = [ + [ + "parameter", + "invalid_file_path.aaa", + "invalid_ip", + "invalid_port", + "invalid_mode", + ], + ["parameter", "some_file.lua", "invalid_ip", "invalid_port", "invalid_mode"], + ["parameter", "8888", "invalid_ip", "invalid_port", "invalid_mode"], + ["parameter", "some_file.luac", "invalid_ip", "invalid_port", "invalid_mode"], + ] + for i in range(len(test_args)): + with patch("sys.argv", test_args[i]): + pioneer_uploader.main() + args, _ = mock_logging_error.call_args + assert isinstance(args[0], FileNotFoundError) + assert str(args[0]) == "Can not find program file" + + +@patch("logging.error") +def test_invalid_ip(mock_logging_error): + test_args = [ + ["parameter", test_file_path, "127.3.not.ip", "port", "mode"], + ["parameter", test_file_path, "127.3.43254.00", "port", "mode"], + ["parameter", test_file_path, "8888", "port", "mode"], + ["parameter", test_file_path, "serial", "port", "mode"], + ] + for i in range(len(test_args)): + with patch("sys.argv", test_args[i]): + pioneer_uploader.main() + args, _ = mock_logging_error.call_args + assert isinstance(args[0], ValueError) + assert str(args[0]) == "Invalid IP address" + + +@patch("logging.error") +def test_invalid_port(mock_logging_error): + test_args = [ + ["parameter", test_file_path, "127.0.0.1", "43242342", "mode"], + ["parameter", test_file_path, "127.0.0.1", "127.0.0.1", "mode"], + ["parameter", test_file_path, "127.0.0.1", "udpin", "mode"], + ["parameter", test_file_path, "127.0.0.1", "port", "mode"], + ] + expected_errors = [ + "Invalid port", + "invalid literal for int() with base 10: '127.0.0.1'", + "invalid literal for int() with base 10: 'udpin'", + "invalid literal for int() with base 10: 'port'", + ] + for i in range(len(test_args)): + with patch("sys.argv", test_args[i]): + pioneer_uploader.main() + args, _ = mock_logging_error.call_args + assert isinstance(args[0], ValueError) + assert str(args[0]) == expected_errors[i] + + +@patch("logging.error") +def test_invalid_mode(mock_logging_error): + test_args = [ + ["parameter", test_file_path, "127.0.0.1", "8888", "8888"], + ["parameter", test_file_path, "127.0.0.1", "8888", "udpin"], + ["parameter", test_file_path, "127.0.0.1", "8888", "serial"], + ["parameter", test_file_path, "127.0.0.1", "8888", "127.0.0.1"], + ] + for i in range(len(test_args)): + with patch("sys.argv", test_args[i]): + pioneer_uploader.main() + args, _ = mock_logging_error.call_args + assert isinstance(args[0], ValueError) + assert str(args[0]) == "Invalid mode. Choose 'wifi' or 'usb'" if __name__ == "__main__": diff --git a/tox.ini b/tox.ini index b204276..aa37e28 100644 --- a/tox.ini +++ b/tox.ini @@ -10,8 +10,8 @@ deps = flake8-black commands = - black - flake8 + black --check . + flake8 . [testenv] deps =