From 9a856725d107c48e8270dc7cbcd3ae4117bccffb Mon Sep 17 00:00:00 2001 From: Tobias Sterbak Date: Thu, 8 Feb 2024 17:06:22 +0000 Subject: [PATCH] Add test for run command in tooling --- tests/test_tooling.py | 125 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/tests/test_tooling.py b/tests/test_tooling.py index d42238ad..1a7b3396 100644 --- a/tests/test_tooling.py +++ b/tests/test_tooling.py @@ -1,4 +1,5 @@ """Test interactions with tools like adb and fastboot""" + # This file is part of OpenAndroidInstaller. # OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of # the GNU General Public License as published by the Free Software Foundation, @@ -12,6 +13,8 @@ from pathlib import Path from subprocess import CalledProcessError +from openandroidinstaller.tooling import run_command + from openandroidinstaller.tooling import adb_reboot from openandroidinstaller.tooling import search_device @@ -89,3 +92,125 @@ def patched_check_output(*args, **kwargs): ) assert device_code is None + + +def test_run_command_success(mocker): + """Test if running a command with a tool works fine.""" + + def patched_popen(*args, **kwargs): + class MockProcess: + stdout = [ + "Output line 1", + "Output line 2", + "Output line 3", + ] + returncode = 0 + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + pass + + def communicate(self): + return self.stdout, None + + return MockProcess() + + mocker.patch("openandroidinstaller.tooling.subprocess.Popen", patched_popen) + + bin_path = Path("test/path/to/tools") + full_command = "adb reboot" + target = "device" + enable_logging = True + + expected_output = [ + "$adb reboot", + "Output line 1", + "Output line 2", + "Output line 3", + True, + ] + + output = list( + run_command( + full_command=full_command, + bin_path=bin_path, + target=target, + enable_logging=enable_logging, + ) + ) + + assert output == expected_output + + +def test_run_command_failure(mocker): + """Test if a failure in running a command with a tool is handled properly.""" + + def patched_popen(*args, **kwargs): + class MockProcess: + stdout = [ + "Error line 1", + "Error line 2", + "Error line 3", + ] + returncode = 1 + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + pass + + def communicate(self): + return self.stdout, None + + return MockProcess() + + mocker.patch("openandroidinstaller.tooling.subprocess.Popen", patched_popen) + + bin_path = Path("test/path/to/tools") + full_command = "adb reboot" + target = "device" + enable_logging = True + + expected_output = [ + "$adb reboot", + "Error line 1", + "Error line 2", + "Error line 3", + False, + ] + + output = list( + run_command( + full_command=full_command, + bin_path=bin_path, + target=target, + enable_logging=enable_logging, + ) + ) + + assert output == expected_output + + +def test_run_command_unknown_tool(): + """Test if an exception is raised for an unknown tool.""" + + bin_path = Path("test/path/to/tools") + full_command = "unknown_tool command" + target = "device" + enable_logging = True + + try: + list( + run_command( + full_command=full_command, + bin_path=bin_path, + target=target, + enable_logging=enable_logging, + ) + ) + assert False, "Exception not raised" + except Exception as e: + assert str(e) == "Unknown tool unknown_tool. Use adb, fastboot or heimdall."