Skip to content

Commit 9a85672

Browse files
committed
Add test for run command in tooling
1 parent 45b0287 commit 9a85672

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

tests/test_tooling.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test interactions with tools like adb and fastboot"""
2+
23
# This file is part of OpenAndroidInstaller.
34
# OpenAndroidInstaller is free software: you can redistribute it and/or modify it under the terms of
45
# the GNU General Public License as published by the Free Software Foundation,
@@ -12,6 +13,8 @@
1213
from pathlib import Path
1314
from subprocess import CalledProcessError
1415

16+
from openandroidinstaller.tooling import run_command
17+
1518
from openandroidinstaller.tooling import adb_reboot
1619
from openandroidinstaller.tooling import search_device
1720

@@ -89,3 +92,125 @@ def patched_check_output(*args, **kwargs):
8992
)
9093

9194
assert device_code is None
95+
96+
97+
def test_run_command_success(mocker):
98+
"""Test if running a command with a tool works fine."""
99+
100+
def patched_popen(*args, **kwargs):
101+
class MockProcess:
102+
stdout = [
103+
"Output line 1",
104+
"Output line 2",
105+
"Output line 3",
106+
]
107+
returncode = 0
108+
109+
def __enter__(self):
110+
return self
111+
112+
def __exit__(self, exc_type, exc_val, exc_tb):
113+
pass
114+
115+
def communicate(self):
116+
return self.stdout, None
117+
118+
return MockProcess()
119+
120+
mocker.patch("openandroidinstaller.tooling.subprocess.Popen", patched_popen)
121+
122+
bin_path = Path("test/path/to/tools")
123+
full_command = "adb reboot"
124+
target = "device"
125+
enable_logging = True
126+
127+
expected_output = [
128+
"$adb reboot",
129+
"Output line 1",
130+
"Output line 2",
131+
"Output line 3",
132+
True,
133+
]
134+
135+
output = list(
136+
run_command(
137+
full_command=full_command,
138+
bin_path=bin_path,
139+
target=target,
140+
enable_logging=enable_logging,
141+
)
142+
)
143+
144+
assert output == expected_output
145+
146+
147+
def test_run_command_failure(mocker):
148+
"""Test if a failure in running a command with a tool is handled properly."""
149+
150+
def patched_popen(*args, **kwargs):
151+
class MockProcess:
152+
stdout = [
153+
"Error line 1",
154+
"Error line 2",
155+
"Error line 3",
156+
]
157+
returncode = 1
158+
159+
def __enter__(self):
160+
return self
161+
162+
def __exit__(self, exc_type, exc_val, exc_tb):
163+
pass
164+
165+
def communicate(self):
166+
return self.stdout, None
167+
168+
return MockProcess()
169+
170+
mocker.patch("openandroidinstaller.tooling.subprocess.Popen", patched_popen)
171+
172+
bin_path = Path("test/path/to/tools")
173+
full_command = "adb reboot"
174+
target = "device"
175+
enable_logging = True
176+
177+
expected_output = [
178+
"$adb reboot",
179+
"Error line 1",
180+
"Error line 2",
181+
"Error line 3",
182+
False,
183+
]
184+
185+
output = list(
186+
run_command(
187+
full_command=full_command,
188+
bin_path=bin_path,
189+
target=target,
190+
enable_logging=enable_logging,
191+
)
192+
)
193+
194+
assert output == expected_output
195+
196+
197+
def test_run_command_unknown_tool():
198+
"""Test if an exception is raised for an unknown tool."""
199+
200+
bin_path = Path("test/path/to/tools")
201+
full_command = "unknown_tool command"
202+
target = "device"
203+
enable_logging = True
204+
205+
try:
206+
list(
207+
run_command(
208+
full_command=full_command,
209+
bin_path=bin_path,
210+
target=target,
211+
enable_logging=enable_logging,
212+
)
213+
)
214+
assert False, "Exception not raised"
215+
except Exception as e:
216+
assert str(e) == "Unknown tool unknown_tool. Use adb, fastboot or heimdall."

0 commit comments

Comments
 (0)