Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: pass benchmark host and port to tv.py

SlivenRede71b6c845f23
diff --git a/tests/test_bench_target.py b/tests/test_bench_target.py
new file mode 100644
index 000000000..dcedb3687
--- /dev/null
+++ b/tests/test_bench_target.py
@@ -0,0 +1,69 @@
+"""bench.py passes its selected host and port to the tv.py subprocess.
+
+A local tv.py records its environment and returns no Home component, so the CLI stops
+before measuring anything. No TV, network connection or ADB key is needed.
+"""
+import json
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import tempfile
+import unittest
+
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+class BenchTargetTest(unittest.TestCase):
+ def run_bench(self, args, target_env=None):
+ env = os.environ.copy()
+ for key in ("TV_HOST", "TV_PORT"):
+ env.pop(key, None)
+ env.update(target_env or {})
+ env["BENCH_TARGET_TEST"] = "keep me"
+ with tempfile.TemporaryDirectory() as tmp:
+ folder = Path(tmp)
+ shutil.copyfile(ROOT / "tools" / "bench.py", folder / "bench.py")
+ (folder / "tv.py").write_text(
+ "import json, os\n"
+ "from pathlib import Path\n"
+ "target = {key: os.environ.get(key) for key in "
+ "('TV_HOST', 'TV_PORT', 'BENCH_TARGET_TEST')}\n"
+ "Path(__file__).with_name('target.json').write_text(json.dumps(target))\n",
+ encoding="utf-8")
+ result = subprocess.run([sys.executable, str(folder / "bench.py"), *args],
+ cwd=tmp, env=env, capture_output=True, text=True, timeout=10)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("no component answers HOME on this TV", result.stderr)
+ target = json.loads((folder / "target.json").read_text(encoding="utf-8"))
+ self.assertEqual(target.pop("BENCH_TARGET_TEST"), "keep me")
+ return target
+
+ def test_host_option_without_environment_uses_default_port(self):
+ target = self.run_bench(["--host", "192.0.2.20"])
+ self.assertEqual(target, {"TV_HOST": "192.0.2.20", "TV_PORT": "5555"})
+
+ def test_options_override_environment(self):
+ target = self.run_bench(["--host", "127.0.0.1", "--port", "15555"],
+ {"TV_HOST": "192.0.2.10", "TV_PORT": "5555"})
+ self.assertEqual(target, {"TV_HOST": "127.0.0.1", "TV_PORT": "15555"})
+
+ def test_environment_defaults_reach_child(self):
+ target = self.run_bench([], {"TV_HOST": "192.0.2.20", "TV_PORT": "15555"})
+ self.assertEqual(target, {"TV_HOST": "192.0.2.20", "TV_PORT": "15555"})
+
+ def test_port_option_keeps_environment_host(self):
+ target = self.run_bench(["--port", "15555"],
+ {"TV_HOST": "127.0.0.1", "TV_PORT": "5555"})
+ self.assertEqual(target, {"TV_HOST": "127.0.0.1", "TV_PORT": "15555"})
+
+ def test_host_option_keeps_environment_port(self):
+ target = self.run_bench(["--host", "192.0.2.20"],
+ {"TV_HOST": "192.0.2.10", "TV_PORT": "15555"})
+ self.assertEqual(target, {"TV_HOST": "192.0.2.20", "TV_PORT": "15555"})
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/bench.py b/tools/bench.py
index 44f135a5d..c803f1bfc 100644
--- a/tools/bench.py
+++ b/tools/bench.py
@@ -55,6 +55,8 @@ class TV:
"""
def __init__(self, host, port=5555, binary=None):
+ self.host = host
+ self.port = port
self.serial = f"{host}:{port}"
self.binary = binary
self.client = None
@@ -81,8 +83,11 @@ class TV:
def _run(self, *args, capture=True):
"""Run tools/tv.py with the given arguments."""
cmd = [self._python(), "-I", self._tv_py(), *args]
+ # tv.py reads its target from the environment, including CLI overrides here.
+ env = os.environ.copy()
+ env.update(TV_HOST=self.host, TV_PORT=str(self.port))
result = subprocess.run(cmd, capture_output=capture, text=True,
- errors="replace" if capture else None)
+ errors="replace" if capture else None, env=env)
if result and result.returncode != 0 and not result.stdout.strip():
raise SystemExit(f"tools/tv.py failed: {result.stderr.strip()}")
return result