Skip to content

Commit

Permalink
Add some more tests for tooling functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tsterbak committed Feb 9, 2023
1 parent 1c37547 commit 2544e39
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.
4 changes: 2 additions & 2 deletions openandroidinstaller/tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from pathlib import Path
import subprocess
from subprocess import (
Popen,
PIPE,
STDOUT,
CalledProcessError,
Expand Down Expand Up @@ -52,7 +51,7 @@ def run_command(
if enable_logging:
logger.info(f"Run command: {full_command}")
# run the command
with Popen(
with subprocess.Popen(
full_command,
stdout=PIPE,
stderr=STDOUT,
Expand Down Expand Up @@ -82,6 +81,7 @@ def logging(*args, **kwargs):
if (type(line) == bool) and not line:
logger.error(f"{step_desc} Failed!")
if return_if_fail:
yield False
return
yield line

Expand Down
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ py7zr = "^0.20.0"
pytest-cov = "^4.0.0"
pytest-mock = "^3.10.0"
bandit = "^1.7.4"
pytest-subprocess = "^1.5.0"

[tool.poetry.dev-dependencies]

Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
# If not, see <https://www.gnu.org/licenses/>."""
# Author: Tobias Sterbak

import os
import pytest
import subprocess
from pathlib import Path


Expand Down
77 changes: 76 additions & 1 deletion tests/test_tooling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,42 @@
from pathlib import Path
from subprocess import CalledProcessError

from openandroidinstaller.tooling import search_device
from openandroidinstaller.tooling import adb_reboot, search_device, check_ab_partition


def test_adb_reboot_success(fp):
"""Test if rebooting with adb works fine."""

with fp.context() as nested_process:
nested_process.register(
["test/path/to/tools/adb", "reboot"], stdout=bytes.fromhex("00")
)
for line in adb_reboot(bin_path=Path("test/path/to/tools")):
print(line)
for_later = "error: no devices/emulators found"
assert line


def test_adb_reboot_failure(fp):
"""Test if a fail in rebooting with adb is handled properly."""

def callback_function_with_kwargs(process, return_code):
process.returncode = return_code

return_code = 1

with fp.context() as nested_process:
nested_process.register(
["test/path/to/tools/adb", "reboot"],
stdout=[
bytes("error: no devices/emulators found", encoding="utf-8"),
],
callback=callback_function_with_kwargs,
callback_kwargs={"return_code": return_code},
)
for line in adb_reboot(bin_path=Path("test/path/to/tools")):
print(line)
assert not line


def test_search_device_success(mocker):
Expand Down Expand Up @@ -57,3 +92,43 @@ def patched_check_output(*args, **kwargs):
)

assert device_code == None


def test_check_ab_device_is_ab(mocker):
"""Test if checking for ab device works fine."""
mocker.patch(
"openandroidinstaller.tooling.check_output",
return_value=b"[ro.boot.slot_suffix]: [_b]",
)

# test linux
is_ab = check_ab_partition(
platform="linux", bin_path=Path("openandroidinstaller/bin/")
)

assert is_ab

# test windows
is_ab = check_ab_partition(
platform="windows", bin_path=Path("openandroidinstaller/bin/")
)

assert is_ab


def test_check_ab_device_not_ab(mocker):
"""Test if checking for ab device returns False if it fails."""

def patched_check_output(*args, **kwargs):
raise CalledProcessError(returncode=1, cmd="output is None")

mocker.patch(
"openandroidinstaller.tooling.check_output",
patched_check_output,
)

is_ab = check_ab_partition(
platform="linux", bin_path=Path("openandroidinstaller/bin/")
)

assert not is_ab

0 comments on commit 2544e39

Please sign in to comment.