Skip to content

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

tools: refuse extra install arguments for scriptless add-ons

Jernej Pangersicd2b0804544a7
diff --git a/tests/test_manage_install_arguments.py b/tests/test_manage_install_arguments.py
new file mode 100644
index 000000000..8f0e8ca9f
--- /dev/null
+++ b/tests/test_manage_install_arguments.py
@@ -0,0 +1,130 @@
+"""Scriptless add-ons refuse extra install arguments before building or contacting the TV."""
+import contextlib
+import io
+import os
+import subprocess
+import sys
+import unittest
+from pathlib import Path
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parent.parent
+sys.path.insert(0, str(ROOT / "tools"))
+import manage # noqa: E402
+
+
+class InstallArgumentsTest(unittest.TestCase):
+ def setUp(self):
+ patcher = mock.patch.object(
+ manage.subprocess, "run",
+ return_value=subprocess.CompletedProcess([], 0, "Success\n", ""),
+ )
+ self.process = patcher.start()
+ self.addCleanup(patcher.stop)
+ patcher = mock.patch.object(manage, "tv_python", return_value="fake-python")
+ self.python = patcher.start()
+ self.addCleanup(patcher.stop)
+ patcher = mock.patch.dict(os.environ, {"TV_HOST": "192.0.2.1"})
+ patcher.start()
+ self.addCleanup(patcher.stop)
+
+ def call(self, operation, *args):
+ out, err = io.StringIO(), io.StringIO()
+ with contextlib.redirect_stdout(out), contextlib.redirect_stderr(err):
+ status = operation(*args)
+ return status, out.getvalue(), err.getvalue()
+
+ def test_scriptless_addon_refuses_every_extra_word_before_side_effects(self):
+ for extra in (["--dry-run"], ["--unknown"], ["unexpected"], ["--"], ["--unknown", "value"]):
+ with self.subTest(extra=extra):
+ self.process.reset_mock()
+ self.python.reset_mock()
+ with mock.patch.object(manage, "tv", side_effect=AssertionError("TV was asked")) as tv:
+ status, out, err = self.call(manage.main, ["install", "jewish-calendar"] + extra)
+ self.assertEqual(status, 2, err)
+ self.assertEqual(out, "")
+ self.assertIn("jewish-calendar", err)
+ for word in extra:
+ self.assertIn(word, err)
+ self.assertIn("install <add-on or theme>", err)
+ self.process.assert_not_called()
+ self.python.assert_not_called()
+ tv.assert_not_called()
+
+ def test_direct_scriptless_install_also_refuses_extra_arguments(self):
+ item = manage.find("jewish-calendar")
+ self.assertIsNone(item["script"])
+ with mock.patch.object(manage, "run", side_effect=AssertionError("build was started")) as run, \
+ mock.patch.object(manage, "tv", side_effect=AssertionError("TV was asked")) as tv:
+ status, out, err = self.call(manage.install_addon, item, ["--dry-run"])
+ self.assertEqual(status, 2, err)
+ self.assertEqual(out, "")
+ self.assertIn("--dry-run", err)
+ run.assert_not_called()
+ self.process.assert_not_called()
+ self.python.assert_not_called()
+ tv.assert_not_called()
+
+ def test_custom_installers_keep_arguments_and_exit_status(self):
+ for script in ("install.sh", "build-and-install.sh"):
+ for extra in ([], ["--dry-run", "--name", "two words"]):
+ with self.subTest(script=script, extra=extra):
+ self.process.reset_mock()
+ self.process.return_value = subprocess.CompletedProcess([], 7)
+ item = {"kind": "add-on", "id": "custom", "script": script}
+ with mock.patch.object(manage, "find", return_value=item):
+ status, out, err = self.call(manage.main, ["install", "custom"] + extra)
+ self.assertEqual(status, 7)
+ self.assertEqual(err, "")
+ self.process.assert_called_once_with(
+ ["bash", str(ROOT / "addons" / "custom" / script)] + extra, cwd=ROOT,
+ )
+ self.python.assert_not_called()
+
+ def test_scriptless_install_without_arguments_keeps_build_push_and_install(self):
+ folder = ROOT / "addons" / "jewish-calendar"
+ gradlew = str(folder / "gradlew.bat") if os.name == "nt" else "./gradlew"
+ apk = folder / "app" / "build" / "outputs" / "apk" / "release" / "app-release.apk"
+ status, out, err = self.call(manage.main, ["install", "jewish-calendar"])
+ self.assertEqual(status, 0)
+ self.assertEqual(err, "")
+ self.assertIn("Success", out)
+ self.assertEqual(self.process.call_args_list, [
+ mock.call([gradlew, "assembleRelease"], cwd=folder),
+ mock.call(
+ ["fake-python", "-I", str(ROOT / "tools" / "tv.py"), "--push",
+ str(apk), "/data/local/tmp/addon.apk"], cwd=ROOT,
+ ),
+ mock.call(
+ ["fake-python", "-I", str(ROOT / "tools" / "tv.py"),
+ "pm install -r /data/local/tmp/addon.apk; rm -f /data/local/tmp/addon.apk"],
+ capture_output=True, text=True,
+ ),
+ ])
+
+ def test_scriptless_install_stops_at_build_or_push_failure(self):
+ for codes in ([7], [0, 9]):
+ with self.subTest(codes=codes):
+ self.process.reset_mock()
+ self.process.side_effect = [subprocess.CompletedProcess([], code) for code in codes]
+ with mock.patch.object(manage, "tv", side_effect=AssertionError("TV was asked")) as tv:
+ status, out, err = self.call(manage.main, ["install", "jewish-calendar"])
+ self.assertEqual(status, codes[-1])
+ self.assertEqual(self.process.call_count, len(codes))
+ self.assertEqual(err, "")
+ tv.assert_not_called()
+
+ def test_scriptless_install_reports_package_install_failure(self):
+ self.process.side_effect = [
+ subprocess.CompletedProcess([], 0),
+ subprocess.CompletedProcess([], 0),
+ subprocess.CompletedProcess([], 0, "Failure [INSTALL_FAILED_VERSION_DOWNGRADE]\n", ""),
+ ]
+ status, out, err = self.call(manage.main, ["install", "jewish-calendar"])
+ self.assertEqual(status, 1)
+ self.assertIn("INSTALL_FAILED_VERSION_DOWNGRADE", out)
+ self.assertEqual(err, "")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/manage.py b/tools/manage.py
index f15ccfadc..77216caf2 100644
--- a/tools/manage.py
+++ b/tools/manage.py
@@ -21,7 +21,9 @@ JSON object is named before the key is written, and a write it cannot make says
set's own arguments are read first: a switch without a value, a --theme without the folder behind it, and a word
left over are answered with the usage line and exit 2 before a theme folder is read or written. list, status,
show, install and remove read their own words the same way, before anything is read from this checkout or asked
-of the TV. A --theme folder that is not there is refused by name, not made: set starts a config.json in a folder
+of the TV. install then reads the add-on's entry: extra arguments are passed to its custom installer, but an
+add-on without one takes only its name and refuses extra arguments before anything is built, pushed or installed.
+A --theme folder that is not there is refused by name, not made: set starts a config.json in a folder
of your own that exists, and the push it ends with replaces the theme on the TV and empties that theme's
wallpapers, so a mistyped path would leave a one-key theme behind.
@@ -32,7 +34,7 @@ reports that separately: the TV may have received only part of the theme.
Exit status: 0 the command ran, 1 a command on the TV or a script this checkout runs failed, 2 the command line
asked for something this script does not do - a command it does not have, or a word the command would drop - which
-is answered before the READMEs are read or the TV is asked anything. 2 is also a run that ends with nothing asked of
+is answered before a script is started or the TV is asked anything. 2 is also a run that ends with nothing asked of
the TV: no TV_HOST, a checkout with no tools/venv, and a TV tools/tv.py answered 2 for because it never reached it
(a refused port, a Wireless debugging port that answers TLS, an address with no route to it). check.py answers the
first two of those with 2 as well, and b64push.py the second.
@@ -403,6 +405,11 @@ def install_addon(item, extra):
if item["script"]:
# The creator's own script, as their README says to run it.
return run(["bash", str(folder / item["script"])] + extra, ROOT)
+ if extra:
+ sys.stderr.write(f"{item['id']} has no custom installer, so install takes only its name, "
+ f"not {' '.join(extra)!r}: {USAGE_LINES['install']}. "
+ "Nothing was built, pushed or installed.\n")
+ return 2
# No script: the steps addons/README.md gives for every add-on.
# Windows resolves a relative program against this process's directory, not cwd, so give the full path.
gradlew = str(folder / "gradlew.bat") if os.name == "nt" else "./gradlew"