Skip to content

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

tests: the list sweeps read a folder's second list, and the undo reader reads Windows, quoted and Apply lines

Mazin Kanuga1f895a54b353
diff --git a/tests/test_examples.py b/tests/test_examples.py
index e877e89f6..c76757c95 100644
--- a/tests/test_examples.py
+++ b/tests/test_examples.py
@@ -28,6 +28,12 @@ def folders():
return sorted(p for p in EXAMPLES.iterdir() if p.is_dir())
+def list_files(examples):
+ """Every list under examples: each folder's debloat.txt and a second list beside it, such as
+ google-chromecast-gtv-4k/debloat-keep-cast.txt, which tools/disable.sh applies the same way."""
+ return sorted(examples.glob("*/debloat*.txt"))
+
+
def commented_out_but_listed(lines):
"""(line number, package) for each package commented out below the first package line that an
active line still names.
@@ -55,6 +61,35 @@ def commented_out_but_listed(lines):
return found
+def format_problems(examples):
+ """`list: what` for every way a list under examples breaks the list format, debloat.txt and a
+ second list beside it alike."""
+ out = []
+ for path in list_files(examples):
+ own = path.relative_to(examples).as_posix()
+ lines = path.read_text(encoding="utf-8").split("\n")
+ if not lines[0].startswith("#"):
+ out.append(f"{own}: first line should be a comment naming the TV, Android version and build")
+ packages = []
+ for n, line in enumerate(lines, 1):
+ body = line.split("#", 1)[0].strip()
+ if not body:
+ continue
+ if not PACKAGE.search(body):
+ out.append(f"{own}: line {n}: not a package name: {line!r}")
+ continue
+ packages.append(body)
+ if not packages:
+ out.append(f"{own}: no packages: notes without a list belong in docs/devices/")
+ dupes = sorted({p for p in packages if packages.count(p) > 1})
+ if dupes:
+ out.append(f"{own}: listed twice: {dupes}")
+ kept = [f"line {n}: {p}" for n, p in commented_out_but_listed(lines)]
+ if kept:
+ out.append(f"{own}: commented out to keep it, but an active line still disables it: {kept}")
+ return out
+
+
def table_rows(path, header_prefix):
"""Rows of the first markdown table whose header starts with header_prefix."""
lines = path.read_text(encoding="utf-8").split("\n")
@@ -74,27 +109,8 @@ class ExampleListsTest(unittest.TestCase):
self.assertTrue((folder / "debloat.txt").is_file(), "no debloat.txt")
def test_list_format(self):
- for folder in folders():
- path = folder / "debloat.txt"
- if not path.is_file():
- continue
- with self.subTest(folder=folder.name):
- lines = path.read_text(encoding="utf-8").split("\n")
- self.assertTrue(lines and lines[0].startswith("#"),
- "first line should be a comment naming the TV, Android version and build")
- packages = []
- for n, line in enumerate(lines, 1):
- body = line.split("#", 1)[0].strip()
- if not body:
- continue
- self.assertRegex(body, PACKAGE, f"line {n}: not a package name: {line!r}")
- packages.append(body)
- self.assertTrue(packages, "no packages: notes without a list belong in docs/devices/")
- dupes = sorted({p for p in packages if packages.count(p) > 1})
- self.assertFalse(dupes, f"listed twice: {dupes}")
- kept = [f"line {n}: {p}" for n, p in commented_out_but_listed(lines)]
- self.assertFalse(kept, "commented out to keep it, but an active line still disables it: "
- f"{kept}")
+ problems = format_problems(EXAMPLES)
+ self.assertEqual([], problems, "\n".join(problems))
def test_every_folder_has_a_valid_tv_json(self):
try:
diff --git a/tests/test_restore_references.py b/tests/test_restore_references.py
index 4229095d6..390860def 100644
--- a/tests/test_restore_references.py
+++ b/tests/test_restore_references.py
@@ -5,6 +5,12 @@ name against the record taken before the change and prints the batch. So the nam
step is what an owner types when the TV looks wrong. A name that is not there is refused before a
single command reaches the TV, which is the worst moment to find out.
+A list is read the way the pages write it: with Windows backslashes, after the `-TvHost <tv ip>` that
+restore.ps1's usage line puts first, and in quotes. The Apply line above an undo is read the same way,
+for tools/disable.sh and disable.ps1, so a list's own Apply and Undo lines both have to name that
+list: debloat.txt, or a second list beside it such as
+examples/google-chromecast-gtv-4k/debloat-keep-cast.txt.
+
The same goes for the install and restore scripts three list folders still ship from before the rule
that a list folder holds none (AGENTS.md, "Hard rules"): a page that sends an owner to
`examples/<folder>/restore.sh` names a file the folder has to have, and the TCL 55S470G header
@@ -28,8 +34,17 @@ ROOT = Path(__file__).resolve().parents[1]
# Same walk as tests/test_markdown_links.py: the tools venv CI builds and a launcher build are not docs.
PRUNE = {".git", "venv", "node_modules", "build", ".gradle", "__pycache__"}
SUFFIXES = (".md", ".txt", ".sh", ".ps1")
-# `tools/restore.sh examples/x/debloat.txt com.stock.launcher`, and the PowerShell twin.
-REFERENCE = re.compile(r"restore\.(?:sh|ps1)\s+([A-Za-z0-9_./<>-]*\.txt)")
+# The list a command hands to the script: `tools/restore.sh examples/x/debloat.txt com.stock.launcher`,
+# `tools/restore.sh "examples/x/debloat.txt"`, and the PowerShell twin with a Windows path, also after
+# -TvHost: `tools\restore.ps1 -TvHost <tv ip> examples\x\debloat.txt`.
+LIST_ARGUMENT = r"(?:\s+-TvHost\s+(?:<[^<>]*>|\S+))?\s+[\"']?([A-Za-z0-9_./\\<>-]*\.txt)"
+REFERENCE = re.compile(r"restore\.(?:sh|ps1)" + LIST_ARGUMENT)
+# The Apply line the undo replays: `tools/disable.sh examples/x/debloat.txt`, and disable.ps1.
+APPLY = re.compile(r"disable\.(?:sh|ps1)" + LIST_ARGUMENT)
+# README.md writes the reader's own list as examples/<your tv>/debloat.txt, left out below as a
+# placeholder. Three translations (it, ja, vi) write it as a made-up folder name, in quotes.
+PLACEHOLDERS = {"examples/il-tuo-modello/debloat.txt", "examples/your-model/debloat.txt",
+ "examples/model-cua-ban/debloat.txt"}
# A helper script inside a list folder, e.g. `examples/epson-ls800/restore.sh`. Three folders still
# ship the install and restore scripts their submission came with (#265); a page or a list header
# that sends an owner to one names a file the folder has to have.
@@ -49,13 +64,16 @@ def unwrapped(token):
return token.strip(WRAPPER).rstrip(".")
-# Every list a reader can run. A page outside examples/ that needs one names that file too.
-LIST_FOLDERS = ROOT / "examples"
+# Every list a reader can run: each folder's debloat.txt and a second list beside it. A page outside
+# examples/ that needs one names that file too.
+LISTS = "examples/*/debloat*.txt"
-def references(text):
- """The list names a line of text hands to restore.sh, placeholders like <your tv> left out."""
- return [name for name in REFERENCE.findall(text) if "<" not in name and ">" not in name]
+def references(text, pattern=REFERENCE):
+ """The list names a line of text hands to restore.sh (or, with APPLY, to disable.sh), with forward
+ slashes, placeholders like <your tv> left out."""
+ names = [name.replace("\\", "/") for name in pattern.findall(text)]
+ return [name for name in names if "<" not in name and ">" not in name and name not in PLACEHOLDERS]
def walk(root):
@@ -69,15 +87,27 @@ def walk(root):
yield path.relative_to(root), path.read_text(encoding="utf-8", errors="replace")
-def read_references(root):
- """(file, name) for every restore.sh reference under root, as a reader would copy it."""
+def read_references(root, pattern=REFERENCE):
+ """(file, name) for every restore.sh reference under root (disable.sh with APPLY), as a reader
+ would copy it."""
found = []
for path, text in walk(root):
- for target in references(text):
+ for target in references(text, pattern):
found.append((path, target))
return found
+def own_commands(root, pattern=REFERENCE):
+ """(list, name) for every restore.sh reference (disable.sh with APPLY) in a list in examples/."""
+ return [(path.as_posix(), name) for path, name in read_references(root, pattern)
+ if path.match(LISTS)]
+
+
+def commands_for_another_list(root, pattern=REFERENCE):
+ """(list, name) for each of those that names a file other than the list it is in."""
+ return [(own, name) for own, name in own_commands(root, pattern) if name != own]
+
+
def script_references(text):
"""The helper scripts inside a list folder a line names, placeholders left out."""
return [name for name in FOLDER_SCRIPT.findall(text) if "<" not in name and ">" not in name]
@@ -124,14 +154,26 @@ class RestoreReferencesTest(unittest.TestCase):
def test_each_list_undo_replays_that_same_list(self):
# restore.sh replays the list disable.sh ran against, so a list's own undo names that file:
# 237 of the 238 references in examples/ already do.
- wrong = []
- for path, name in read_references(ROOT):
- own = path.as_posix()
- if own.startswith(LIST_FOLDERS.relative_to(ROOT).as_posix() + "/") and path.name == "debloat.txt" \
- and name != own:
- wrong.append((own, name))
- self.assertEqual([], wrong, "a list's undo points somewhere else, so it replays another set "
- "of packages or nothing at all")
+ self.assertEqual([], commands_for_another_list(ROOT),
+ "a list's undo points somewhere else, so it replays another set of packages "
+ "or nothing at all")
+
+ def test_every_apply_names_a_list_that_is_there(self):
+ missing = [(str(path), name) for path, name in read_references(ROOT, APPLY)
+ if not (ROOT / name).is_file()]
+ self.assertEqual([], missing,
+ "these lines apply a list that is not in the repo, so disable.sh stops on it "
+ "before anything reaches the TV")
+
+ def test_each_list_apply_runs_that_same_list(self):
+ # The Apply counterpart of the undo rule above: the owner copies the Apply line of the list
+ # they are reading, and the undo below it replays that list.
+ self.assertGreaterEqual(len(own_commands(ROOT, APPLY)), 250,
+ "the sweep should read the Apply line of nearly every list; it found "
+ "fewer, so it cannot fail on one")
+ self.assertEqual([], commands_for_another_list(ROOT, APPLY),
+ "a list's Apply line runs another file, so the TV gets another set of packages "
+ "than the list shows")
def test_a_folder_script_a_page_names_is_there(self):
# examples/epson-ls800, anker-nebula-d2426 and tcl-43c645 ship the install and restore scripts
@@ -204,6 +246,34 @@ class RestoreReferencesTest(unittest.TestCase):
self.write(root, "docs/page.md", "Undo: tools\\restore.ps1 examples/x/debloat.txt\n")
self.assertEqual([(Path("docs/page.md"), "examples/x/debloat.txt")], read_references(root))
+ def test_a_windows_path_is_read_with_forward_slashes(self):
+ # How the Windows undo lines in examples/ write it, e.g. hisense-65d62qh and sony-kd-55x8000c.
+ for line in ("tools\\restore.ps1 examples\\gone\\debloat.txt",
+ "tools\\restore.ps1 -TvHost <tv ip> examples\\gone\\debloat.txt",
+ "tools\\restore.ps1 -TvHost 192.168.1.50 examples\\gone\\debloat.txt -Stock com.x.y",
+ "$env:TV_HOST=\"<tv ip>\"; tools\\restore.ps1 examples\\gone\\debloat.txt"):
+ with self.subTest(line=line):
+ self.assertEqual(["examples/gone/debloat.txt"], references(line))
+
+ def test_a_list_in_quotes_is_read(self):
+ self.assertEqual(["examples/gone/debloat.txt"],
+ references("tools/restore.sh \"examples/gone/debloat.txt\" com.x.y"))
+ self.assertEqual(["examples/gone/debloat.txt"],
+ references("tools/restore.sh 'examples/gone/debloat.txt'"))
+
+ def test_an_apply_line_is_read_apart_from_the_undo(self):
+ for line in ("# Apply: TV_HOST=<tv ip> tools/disable.sh examples/gone/debloat.txt",
+ "# Apply: tools\\disable.ps1 -TvHost <tv ip> examples\\gone\\debloat.txt"):
+ with self.subTest(line=line):
+ self.assertEqual(["examples/gone/debloat.txt"], references(line, APPLY))
+ self.assertEqual([], references(line))
+ self.assertEqual([], references("tools/restore.sh examples/gone/debloat.txt", APPLY))
+
+ def test_a_translated_placeholder_is_not_a_reference(self):
+ # README.it.md, README.ja.md and README.vi.md: README.md's examples/<your tv>/debloat.txt.
+ self.assertEqual([], references("TV_HOST=192.168.1.50 tools/restore.sh "
+ "\"examples/il-tuo-modello/debloat.txt\" com.x.y"))
+
def test_a_placeholder_is_not_a_reference(self):
with tempfile.TemporaryDirectory() as tmp:
root = Path(tmp)
diff --git a/tests/test_stock_launcher_undo.py b/tests/test_stock_launcher_undo.py
index a976cb5c2..9761ee808 100644
--- a/tests/test_stock_launcher_undo.py
+++ b/tests/test_stock_launcher_undo.py
@@ -56,6 +56,12 @@ PASSED = re.compile(r"\s+(?:-Stock\s+)?[\"`]?(" + PACKAGE + r")")
DEFAULT_STOCK = "com.google.android.tvlauncher"
+def list_files(root):
+ """Every list under root's examples/: each folder's debloat.txt and a second list beside it, such
+ as google-chromecast-gtv-4k/debloat-keep-cast.txt, which has its own undo line."""
+ return sorted((root / "examples").glob("*/debloat*.txt"))
+
+
def active_packages(text):
"""(line number, package) for every package on an active line of a list."""
found = []
@@ -133,20 +139,27 @@ def undo_problems(text):
return problems
+def sweep(root):
+ """What the lists under root's examples/ show: how many lists, restore commands, stock-launcher
+ statements and undos that pass a package there are, and `list: problem` for each undo that misses
+ the stock launcher its list disables."""
+ lists = invocations = claims = passed_somewhere = 0
+ failures = []
+ for path in list_files(root):
+ lists += 1
+ text = path.read_text(encoding="utf-8")
+ calls = restore_invocations(text)
+ invocations += len(calls)
+ passed_somewhere += sum(1 for _, package in calls if package)
+ claims += len(stock_claims(text))
+ for problem in undo_problems(text):
+ failures.append(f"{path.relative_to(root).as_posix()}: {problem}")
+ return lists, invocations, claims, passed_somewhere, failures
+
+
class StockLauncherUndoTest(unittest.TestCase):
def test_every_list_passes_the_stock_launcher_it_disables(self):
- lists = files = invocations = claims = passed_somewhere = 0
- failures = []
- for path in sorted((ROOT / "examples").glob("*/debloat.txt")):
- files += 1
- text = path.read_text(encoding="utf-8")
- calls = restore_invocations(text)
- invocations += len(calls)
- passed_somewhere += sum(1 for _, package in calls if package)
- claims += len(stock_claims(text))
- for problem in undo_problems(text):
- failures.append(f"examples/{path.parent.name}/debloat.txt: {problem}")
- lists = files
+ lists, invocations, claims, passed_somewhere, failures = sweep(ROOT)
self.assertGreaterEqual(lists, 200, "the sweep read too few lists to be meaningful")
self.assertGreaterEqual(invocations, 150, "the sweep found too few restore commands")
self.assertGreaterEqual(claims, 150, "the sweep found too few stock-launcher statements")
diff --git a/tests/test_sweeps_read_second_lists.py b/tests/test_sweeps_read_second_lists.py
new file mode 100644
index 000000000..ad09a2bc3
--- /dev/null
+++ b/tests/test_sweeps_read_second_lists.py
@@ -0,0 +1,93 @@
+"""The sweeps over examples/ read every list there, not only each folder's debloat.txt.
+
+A folder can hold a second list beside debloat.txt:
+examples/google-chromecast-gtv-4k/debloat-keep-cast.txt is the same cleanup minus the three packages
+that cost something on that device (setupwraith for phone casting, dreamx and backdrop for the
+screensaver), with its own Apply, Undo and Check lines, and tools/disable.sh and restore.sh
+take it like any other list. The list format check (tests/test_examples.py), the stock-launcher undo
+check (tests/test_stock_launcher_undo.py) and the rule that a list's undo names that list
+(tests/test_restore_references.py) read only debloat.txt, so a second list with a package listed twice
+or an undo that names another file passed all three. tests/test_core_packages.py and
+tests/test_apply_override_caveat.py already read both.
+
+Each test below runs the function a sweep calls (format_problems, sweep, commands_for_another_list) on
+a temp tree whose second list breaks that sweep's rule, so a sweep that goes back to reading
+debloat.txt alone fails here.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+# Imported as modules so their tests do not run twice.
+import test_examples as list_format # noqa: E402
+import test_restore_references as references # noqa: E402
+import test_stock_launcher_undo as stock_undo # noqa: E402
+
+ROOT = Path(__file__).resolve().parents[1]
+KEEP_CAST = ROOT / "examples" / "google-chromecast-gtv-4k" / "debloat-keep-cast.txt"
+LAUNCHERX = "com.google.android.apps.tv.launcherx"
+
+
+def write(root, path, text):
+ target = root / path
+ target.parent.mkdir(parents=True, exist_ok=True)
+ target.write_text(text, encoding="utf-8")
+ return target
+
+
+class SecondListsTest(unittest.TestCase):
+ def test_the_keep_cast_list_is_read_by_every_sweep(self):
+ self.assertTrue(KEEP_CAST.is_file(), "this test needs the Chromecast folder's second list")
+ own = KEEP_CAST.relative_to(ROOT).as_posix()
+ self.assertIn(KEEP_CAST, list_format.list_files(ROOT / "examples"))
+ self.assertIn(KEEP_CAST, stock_undo.list_files(ROOT))
+ self.assertIn((own, own), references.own_commands(ROOT))
+ self.assertIn((own, own), references.own_commands(ROOT, references.APPLY))
+
+ def test_a_second_list_with_a_package_listed_twice_is_reported(self):
+ # test_list_format read only debloat.txt, so this passed.
+ with tempfile.TemporaryDirectory() as tmp:
+ examples = Path(tmp) / "examples"
+ write(examples, "x/debloat.txt", "# TV\ncom.example.one\n")
+ write(examples, "x/debloat-keep-two.txt", "# TV\ncom.example.one\ncom.example.one\n")
+ self.assertEqual(["x/debloat-keep-two.txt: listed twice: ['com.example.one']"],
+ list_format.format_problems(examples))
+
+ def test_a_second_list_whose_undo_misses_its_stock_launcher_is_reported(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ write(root, "examples/x/debloat.txt",
+ f"# Undo: tools/restore.sh examples/x/debloat.txt {LAUNCHERX}\n{LAUNCHERX}\n")
+ write(root, "examples/x/debloat-keep-two.txt",
+ f"# Stock launcher: {LAUNCHERX}\n"
+ "# Undo: tools/restore.sh examples/x/debloat-keep-two.txt\n"
+ f"{LAUNCHERX}\n")
+ lists, _, _, _, failures = stock_undo.sweep(root)
+ self.assertEqual(2, lists)
+ self.assertEqual(1, len(failures), failures)
+ self.assertTrue(failures[0].startswith("examples/x/debloat-keep-two.txt: "), failures[0])
+ self.assertIn("does not pass it", failures[0])
+
+ def test_a_second_list_whose_commands_name_another_file_is_reported(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp)
+ write(root, "examples/x/debloat.txt",
+ "# Apply: tools/disable.sh examples/x/debloat.txt\n"
+ "# Undo: tools/restore.sh examples/x/debloat.txt\n"
+ "com.example.one\n")
+ write(root, "examples/x/debloat-keep-two.txt",
+ "# Apply: tools/disable.sh examples/x/debloat.txt\n"
+ "# Undo: tools/restore.sh examples/x/debloat.txt\n"
+ "com.example.two\n")
+ for pattern in (references.REFERENCE, references.APPLY):
+ with self.subTest(pattern=pattern.pattern):
+ self.assertEqual([("examples/x/debloat-keep-two.txt", "examples/x/debloat.txt")],
+ references.commands_for_another_list(root, pattern))
+
+
+if __name__ == "__main__":
+ unittest.main()