Skip to content

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

Merge PR #2692: tools: manage.py answers 2 for a TV it never reached, instead of 1

Gathin238b785518c466
diff --cc tests/test_manage_windows.py
index dd0b6d029,000000000..470a1994f
mode 100644,000000..100644
--- a/tests/test_manage_windows.py
+++ b/tests/test_manage_windows.py
@@@ -1,164 -1,0 +1,167 @@@
+"""Theme operations use the existing PowerShell script on Windows, without needing Bash."""
+import contextlib
+import io
+import json
+import os
+from pathlib import Path
+import shutil
+import subprocess
+import sys
+import tempfile
+import unittest
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT / "tools"))
+import manage # noqa: E402
+
+
+class ThemeDispatchTest(unittest.TestCase):
+ def install(self, platform, executables, extra=(), result=0):
+ out, err = io.StringIO(), io.StringIO()
+ with mock.patch.object(sys, "platform", platform), \
+ mock.patch("shutil.which", side_effect=executables.get), \
+ mock.patch.object(manage.subprocess, "run",
+ return_value=subprocess.CompletedProcess([], result)) as runner, \
+ contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
+ code = manage.main(["install", "aurora", *extra])
+ return code, runner, out.getvalue(), err.getvalue()
+
+ def test_windows_installs_with_powershell_without_a_profile_or_policy_bypass(self):
+ code, runner, _, _ = self.install("win32", {"pwsh": r"C:\PowerShell\pwsh.exe"})
+ self.assertEqual(code, 0)
+ runner.assert_called_once_with(
+ [r"C:\PowerShell\pwsh.exe", "-NoProfile", "-NonInteractive", "-File",
+ str(manage.TOOLS / "theme.ps1"), "-Theme", str(ROOT / "themes" / "aurora")],
+ cwd=ROOT)
+
+ def test_windows_can_use_the_bundled_windows_powershell(self):
+ code, runner, _, _ = self.install("win32", {"powershell": r"C:\Windows\powershell.exe"})
+ self.assertEqual(code, 0)
+ self.assertEqual(runner.call_args.args[0][0], r"C:\Windows\powershell.exe")
+
+ def test_unix_keeps_the_existing_bash_command(self):
+ code, runner, _, _ = self.install("linux", {})
+ self.assertEqual(code, 0)
+ runner.assert_called_once_with(
+ ["bash", str(manage.TOOLS / "theme.sh"), str(ROOT / "themes" / "aurora")], cwd=ROOT)
+
+ def test_a_failed_installer_keeps_its_exit_status(self):
+ code, _, _, _ = self.install("win32", {"pwsh": "pwsh"}, result=17)
+ self.assertEqual(code, 17)
+
+ def test_extra_theme_arguments_are_refused_before_any_script_runs(self):
+ for platform in ("win32", "linux"):
+ with self.subTest(platform=platform):
+ code, runner, _, err = self.install(platform, {}, extra=["--reset"])
+ self.assertEqual(code, 2)
+ self.assertIn("--reset", err)
+ runner.assert_not_called()
+
+ def test_an_executable_that_cannot_start_is_reported_without_a_traceback(self):
+ err = io.StringIO()
+ with mock.patch.object(sys, "platform", "win32"), \
+ mock.patch("shutil.which", return_value="pwsh"), \
+ mock.patch.object(manage.subprocess, "run", side_effect=FileNotFoundError("gone")), \
+ contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err):
+ self.assertEqual(manage.main(["install", "aurora"]), 1)
+ self.assertIn("cannot start", err.getvalue())
+ self.assertIn("gone", err.getvalue())
+
+ def test_missing_powershell_does_not_rewrite_a_personal_theme(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ config = Path(tmp) / "config.json"
+ original = '{"columns": 4}\n'
+ config.write_text(original, encoding="utf-8")
+ with mock.patch.object(sys, "platform", "win32"), \
+ mock.patch("shutil.which", return_value=None), \
+ mock.patch.object(manage.subprocess, "run") as runner, \
+ self.assertRaisesRegex(SystemExit, "PowerShell"):
+ manage.main(["set", "columns", "6", "--theme", tmp])
+ self.assertEqual(config.read_text(encoding="utf-8"), original)
+ runner.assert_not_called()
+
+ def test_failed_set_keeps_the_local_edit_and_explains_the_failed_push(self):
+ with tempfile.TemporaryDirectory(prefix="theme with spaces ") as tmp:
+ folder = Path(tmp).resolve()
+ config = folder / "config.json"
+ config.write_text('{"dock_band": true}\n', encoding="utf-8")
+ out, err = io.StringIO(), io.StringIO()
+ with mock.patch.object(sys, "platform", "win32"), \
+ mock.patch("shutil.which", return_value="pwsh"), \
+ mock.patch.object(manage.subprocess, "run",
+ return_value=subprocess.CompletedProcess([], 17)) as runner, \
+ contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
+ code = manage.main(["set", "columns", "6", "--theme", str(folder)])
+ self.assertEqual(code, 17)
+ self.assertEqual(json.loads(config.read_text(encoding="utf-8")),
+ {"dock_band": True, "columns": 6})
+ runner.assert_called_once_with(
+ ["pwsh", "-NoProfile", "-NonInteractive", "-File",
+ str(manage.TOOLS / "theme.ps1"), "-Theme", str(folder)], cwd=ROOT)
+ self.assertIn(str(config), err.getvalue())
+ self.assertIn("locally", err.getvalue())
+ self.assertIn("failed", err.getvalue())
+ self.assertIn("17", err.getvalue())
+
+
+class WindowsGuidanceTest(unittest.TestCase):
+ def test_help_and_show_print_windows_commands(self):
+ for args in (["--help"], ["show", "aurora"], ["show", "columns"], ["show", "weather"]):
+ with self.subTest(args=args):
+ out = io.StringIO()
+ with mock.patch.object(sys, "platform", "win32"), contextlib.redirect_stdout(out):
+ self.assertEqual(manage.main(args), 0)
+ text = out.getvalue()
+ self.assertIn(r".\tools\venv\Scripts\python.exe", text)
+ self.assertIn("$env:TV_HOST", text)
+ self.assertNotIn("TV_HOST=<tv ip>", text)
+ self.assertNotIn("tools/venv/bin/python", text)
+
+ def test_a_missing_venv_names_the_windows_setup_commands(self):
++ err = io.StringIO()
+ with tempfile.TemporaryDirectory() as tmp, \
+ mock.patch.object(manage, "TOOLS", Path(tmp)), \
+ mock.patch.object(sys, "platform", "win32"), \
++ contextlib.redirect_stderr(err), \
+ self.assertRaises(SystemExit) as stop:
+ manage.tv_python()
- self.assertIn(r"tools\venv\Scripts\python.exe", str(stop.exception.code))
- self.assertNotIn("bin/pip", str(stop.exception.code))
++ self.assertEqual(stop.exception.code, 2)
++ self.assertIn(r"tools\venv\Scripts\python.exe", err.getvalue())
++ self.assertNotIn("bin/pip", err.getvalue())
+
+
[email protected](os.name == "nt", "real Windows PowerShell subprocesses")
+class WindowsSubprocessTest(unittest.TestCase):
+ def test_each_available_powershell_receives_the_folder_and_returns_failures(self):
+ shells = [path for name in ("pwsh", "powershell") if (path := shutil.which(name))]
+ self.assertTrue(shells, "Windows theme operations need PowerShell on PATH")
+ with tempfile.TemporaryDirectory(prefix="manage scripts ") as tmp:
+ tools = Path(tmp).resolve()
+ theme = tools / "personal theme ' & $name"
+ theme.mkdir()
+ receipt = tools / "received.json"
+ (tools / "theme.ps1").write_text(
+ "param([string]$Theme)\n"
+ "$data = @{ theme = $Theme; extra = @($args) } | ConvertTo-Json -Compress\n"
+ "[System.IO.File]::WriteAllText($env:MANAGE_TEST_RECEIPT, $data)\n"
+ "exit ([int]$env:MANAGE_TEST_EXIT)\n", encoding="utf-8")
+ for shell in shells:
+ for status in (0, 17):
+ with self.subTest(shell=shell, status=status), \
+ mock.patch.object(manage, "TOOLS", tools), \
+ mock.patch("shutil.which", return_value=shell), \
+ mock.patch.dict(os.environ, {"MANAGE_TEST_RECEIPT": str(receipt),
+ "MANAGE_TEST_EXIT": str(status)}), \
+ contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(io.StringIO()):
+ receipt.unlink(missing_ok=True)
+ self.assertEqual(manage.main(
+ ["set", "columns", "6", "--theme", str(theme)]), status)
+ self.assertEqual(json.loads(receipt.read_text(encoding="utf-8")),
+ {"theme": str(theme), "extra": []})
+ self.assertEqual(json.loads((theme / "config.json").read_text(encoding="utf-8")),
+ {"columns": 6})
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --cc tools/manage.py
index 781806ba5,ab0426eae..f15ccfadc
--- a/tools/manage.py
+++ b/tools/manage.py
@@@ -329,17 -300,25 +332,31 @@@ def tv_python()
for p in (TOOLS / "venv" / "bin" / "python", TOOLS / "venv" / "Scripts" / "python.exe"):
if p.is_file():
return str(p)
+ # Nothing reached the TV: this checkout has no tools/venv, so tools/tv.py cannot even be imported by a
+ # Python that has adb_shell. sys.exit(str) answers 1, which the docstring keeps for a command on the TV
+ # or a script of this checkout that failed, so a clone with no venv read as an install or a removal that
+ # ran and failed - and status/install/remove would have changed nothing either way. b64push.py answers
+ # this shape, and check.py the same and the one below, with 2.
- sys.stderr.write("cannot load tools/tv.py (tools/venv is not there).\n"
- "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt\n"
- "Nothing was asked of the TV.\n")
+ if sys.platform == "win32":
- sys.exit(r"run: python -m venv tools\venv; "
- r".\tools\venv\Scripts\python.exe -m pip install -r tools\requirements.txt")
- sys.exit("run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt")
++ run = (r"run: python -m venv tools\venv; "
++ r".\tools\venv\Scripts\python.exe -m pip install -r tools\requirements.txt")
++ else:
++ run = "run: python3 -m venv tools/venv && tools/venv/bin/pip install -r tools/requirements.txt"
++ sys.stderr.write(f"cannot load tools/tv.py (tools/venv is not there).\n{run}\nNothing was asked of the TV.\n")
+ raise SystemExit(2)
def tv(command):
if not os.environ.get("TV_HOST"):
+ # The same two lines tools/tv.py prints for this, and the status check.py answers it with: without
+ # TV_HOST there is no TV to ask anything, which is not the same as a command that failed on one.
- sys.stderr.write("set TV_HOST to the TV IP address, e.g. TV_HOST=192.168.1.50 tools/tv.py --probe\n"
- "(find it under Settings → Network, or look for the TV in `arp -a`)\n"
- "Nothing was asked of the TV.\n")
+ if sys.platform == "win32":
- sys.exit("set $env:TV_HOST to the TV IP address")
- sys.exit("set TV_HOST to the TV IP address")
++ sys.stderr.write("set $env:TV_HOST to the TV IP address\n")
++ else:
++ sys.stderr.write("set TV_HOST to the TV IP address, e.g. TV_HOST=192.168.1.50 tools/tv.py --probe\n"
++ "(find it under Settings → Network, or look for the TV in `arp -a`)\n")
++ sys.stderr.write("Nothing was asked of the TV.\n")
+ raise SystemExit(2)
r = subprocess.run([tv_python(), "-I", str(TOOLS / "tv.py"), command], capture_output=True, text=True)
# A failed read is unknown, not empty: never report "not installed" for a TV that didn't answer.
if r.returncode: