Skip to content

Commit 2544e39

Browse files
committed
Add some more tests for tooling functions
1 parent 1c37547 commit 2544e39

5 files changed

Lines changed: 102 additions & 4 deletions

File tree

openandroidinstaller/tooling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from pathlib import Path
1818
import subprocess
1919
from subprocess import (
20-
Popen,
2120
PIPE,
2221
STDOUT,
2322
CalledProcessError,
@@ -52,7 +51,7 @@ def run_command(
5251
if enable_logging:
5352
logger.info(f"Run command: {full_command}")
5453
# run the command
55-
with Popen(
54+
with subprocess.Popen(
5655
full_command,
5756
stdout=PIPE,
5857
stderr=STDOUT,
@@ -82,6 +81,7 @@ def logging(*args, **kwargs):
8281
if (type(line) == bool) and not line:
8382
logger.error(f"{step_desc} Failed!")
8483
if return_if_fail:
84+
yield False
8585
return
8686
yield line
8787

poetry.lock

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ py7zr = "^0.20.0"
2424
pytest-cov = "^4.0.0"
2525
pytest-mock = "^3.10.0"
2626
bandit = "^1.7.4"
27+
pytest-subprocess = "^1.5.0"
2728

2829
[tool.poetry.dev-dependencies]
2930

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
# If not, see <https://www.gnu.org/licenses/>."""
1212
# Author: Tobias Sterbak
1313

14+
import os
1415
import pytest
16+
import subprocess
1517
from pathlib import Path
1618

1719

tests/test_tooling.py

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,42 @@
1616
from pathlib import Path
1717
from subprocess import CalledProcessError
1818

19-
from openandroidinstaller.tooling import search_device
19+
from openandroidinstaller.tooling import adb_reboot, search_device, check_ab_partition
20+
21+
22+
def test_adb_reboot_success(fp):
23+
"""Test if rebooting with adb works fine."""
24+
25+
with fp.context() as nested_process:
26+
nested_process.register(
27+
["test/path/to/tools/adb", "reboot"], stdout=bytes.fromhex("00")
28+
)
29+
for line in adb_reboot(bin_path=Path("test/path/to/tools")):
30+
print(line)
31+
for_later = "error: no devices/emulators found"
32+
assert line
33+
34+
35+
def test_adb_reboot_failure(fp):
36+
"""Test if a fail in rebooting with adb is handled properly."""
37+
38+
def callback_function_with_kwargs(process, return_code):
39+
process.returncode = return_code
40+
41+
return_code = 1
42+
43+
with fp.context() as nested_process:
44+
nested_process.register(
45+
["test/path/to/tools/adb", "reboot"],
46+
stdout=[
47+
bytes("error: no devices/emulators found", encoding="utf-8"),
48+
],
49+
callback=callback_function_with_kwargs,
50+
callback_kwargs={"return_code": return_code},
51+
)
52+
for line in adb_reboot(bin_path=Path("test/path/to/tools")):
53+
print(line)
54+
assert not line
2055

2156

2257
def test_search_device_success(mocker):
@@ -57,3 +92,43 @@ def patched_check_output(*args, **kwargs):
5792
)
5893

5994
assert device_code == None
95+
96+
97+
def test_check_ab_device_is_ab(mocker):
98+
"""Test if checking for ab device works fine."""
99+
mocker.patch(
100+
"openandroidinstaller.tooling.check_output",
101+
return_value=b"[ro.boot.slot_suffix]: [_b]",
102+
)
103+
104+
# test linux
105+
is_ab = check_ab_partition(
106+
platform="linux", bin_path=Path("openandroidinstaller/bin/")
107+
)
108+
109+
assert is_ab
110+
111+
# test windows
112+
is_ab = check_ab_partition(
113+
platform="windows", bin_path=Path("openandroidinstaller/bin/")
114+
)
115+
116+
assert is_ab
117+
118+
119+
def test_check_ab_device_not_ab(mocker):
120+
"""Test if checking for ab device returns False if it fails."""
121+
122+
def patched_check_output(*args, **kwargs):
123+
raise CalledProcessError(returncode=1, cmd="output is None")
124+
125+
mocker.patch(
126+
"openandroidinstaller.tooling.check_output",
127+
patched_check_output,
128+
)
129+
130+
is_ab = check_ab_partition(
131+
platform="linux", bin_path=Path("openandroidinstaller/bin/")
132+
)
133+
134+
assert not is_ab

0 commit comments

Comments
 (0)