Skip to content

Commit

Permalink
Add test for run command in tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
tsterbak committed Feb 8, 2024
1 parent 45b0287 commit 9a85672
Showing 1 changed file with 125 additions and 0 deletions.
125 changes: 125 additions & 0 deletions tests/test_tooling.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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."

0 comments on commit 9a85672

Please sign in to comment.