Skip to content

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

tools: gen_tables.py names a path it cannot read, and exits 2, not "out of date"

xbsheng3d21d3ce7166
diff --git a/tests/test_gen_tables_inputs.py b/tests/test_gen_tables_inputs.py
new file mode 100644
index 000000000..445f71378
--- /dev/null
+++ b/tests/test_gen_tables_inputs.py
@@ -0,0 +1,162 @@
+"""tools/gen_tables.py refuses a path it cannot read instead of dying in a traceback, the way it refuses a font.
+
+Exit 1 is what main's "README tables" job reads as "the tables are out of date", so the run that never read a
+byte must not come back as 1: `--root` on a folder that is not a checkout, and `--adopt` on a patch that is not
+there, both ended in a FileNotFoundError traceback - exit 1 - before, which is the same answer a stale README.md
+gives. The script's own docstring names 2 for "what it was given cannot be read", and `--font` on a path that is
+not there already exits 2 through ap.error, so this is one rule applied to its other two paths.
+
+Every test runs the real script in a subprocess, since the exit status and the line on stderr are what a
+maintainer sees; the trees come from tests/test_gen_tables.py, so a valid checkout is built the same way there.
+
+Run from the repository root: python3 -m unittest discover -s tests -v
+"""
+import subprocess
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT / "tests"))
+sys.path.insert(0, str(ROOT / "tools"))
+from test_gen_tables import add, tree, tv # noqa: E402 (the small checkout the other gen_tables tests use)
+
+SCRIPT = ROOT / "tools" / "gen_tables.py"
+
+
+def run(*args, cwd=None):
+ """(exit status, stdout, stderr) of the real script."""
+ done = subprocess.run([sys.executable, str(SCRIPT), *args], cwd=cwd, capture_output=True, text=True)
+ return done.returncode, done.stdout, done.stderr
+
+
+def valid(tmp):
+ """The small checkout tests/test_gen_tables.py builds: examples/ with two folders and the three files."""
+ return tree(tmp, [("tcl-a", tv("TCL A", "TCL")), ("sony-b", tv("Sony B", "Sony"))])
+
+
+class RootTest(unittest.TestCase):
+ def test_a_root_that_is_not_a_folder_exits_2(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp) / "nope"
+ code, out, err = run("--root", str(root), "--check")
+ self.assertEqual(code, 2, err)
+ self.assertIn("not a folder", err)
+ self.assertIn(str(root), err)
+ self.assertNotIn("Traceback", err)
+ self.assertEqual(out, "")
+
+ def test_a_root_that_is_a_file_exits_2(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ path = Path(tmp) / "README.md"
+ path.write_text("# not a checkout\n", encoding="utf-8")
+ code, out, err = run("--root", str(path), "--check")
+ self.assertEqual(code, 2, err)
+ self.assertIn("not a folder", err)
+ self.assertEqual(out, "")
+
+ def test_a_folder_that_is_not_a_checkout_names_what_is_missing(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = Path(tmp) / "empty"
+ root.mkdir()
+ code, out, err = run("--root", str(root), "--check")
+ self.assertEqual(code, 2, err)
+ for name in ("examples/", "README.md", "examples/README.md", "tools/readme_stats.py"):
+ with self.subTest(missing=name):
+ self.assertIn(name, err)
+ self.assertEqual(out, "")
+
+ def test_one_missing_file_is_named_and_the_others_are_not(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ (root / "tools" / "readme_stats.py").unlink()
+ code, out, err = run("--root", str(root), "--check")
+ self.assertEqual(code, 2, err)
+ self.assertIn("no tools/readme_stats.py in it", err)
+ self.assertNotIn("examples/", err)
+
+ def test_a_refused_root_is_never_the_out_of_date_exit(self):
+ # The one thing a weekly job and the README-tables job both read as a real finding.
+ with tempfile.TemporaryDirectory() as tmp:
+ (Path(tmp) / "repo").mkdir()
+ code, _, err = run("--root", str(Path(tmp) / "repo"), "--check")
+ self.assertNotEqual(code, 1, err)
+ self.assertEqual(code, 2)
+
+ def test_a_refused_root_writes_nothing(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ # A tree whose tables are stale: a run that got as far as render() would rewrite README.md.
+ add(root, "tcl-c", tv("TCL C", "TCL"))
+ before = (root / "README.md").read_bytes()
+ (root / "examples" / "README.md").unlink()
+ code, _, err = run("--root", str(root))
+ self.assertEqual(code, 2, err)
+ self.assertEqual((root / "README.md").read_bytes(), before)
+
+
+class AdoptTest(unittest.TestCase):
+ def test_a_patch_that_is_not_there_exits_2(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ patch = Path(tmp) / "pr.diff"
+ code, out, err = run("--root", str(root), "--adopt", str(patch))
+ self.assertEqual(code, 2, err)
+ self.assertIn(str(patch), err)
+ self.assertIn("No such file or directory", err)
+ self.assertIn("Nothing was written", err)
+ self.assertNotIn("Traceback", err)
+ self.assertEqual(out, "")
+
+ def test_a_patch_that_is_a_folder_exits_2(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ code, out, err = run("--root", str(root), "--adopt", tmp)
+ self.assertEqual(code, 2, err)
+ self.assertIn("Is a directory", err)
+ self.assertIn("Nothing was written", err)
+ self.assertEqual(out, "")
+
+ def test_the_root_is_judged_before_the_patch_is_applied(self):
+ # A readable patch and a root with nothing of the repository in it: no tv.json may appear.
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ add(root, "tcl-d", None) # a folder with a list and no tv.json: what --adopt exists to write
+ patch = Path(tmp) / "pr.diff"
+ patch.write_text("| `tcl-d/` | TCL D | Android TV 11 | ads off |\n", encoding="utf-8")
+ (root / "README.md").unlink()
+ code, out, err = run("--root", str(root), "--adopt", str(patch))
+ self.assertEqual(code, 2, err)
+ self.assertIn("README.md", err)
+ self.assertFalse((root / "examples" / "tcl-d" / "tv.json").exists(), "the patch was applied anyway")
+ self.assertEqual(out, "")
+
+
+class UnchangedTest(unittest.TestCase):
+ def test_a_checkout_exits_0_and_a_stale_one_still_1(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ code, out, err = run("--root", str(root))
+ self.assertEqual(code, 0, err)
+ code, out, err = run("--root", str(root), "--check")
+ self.assertEqual(code, 0, err)
+ add(root, "tcl-c", tv("TCL C", "TCL"))
+ code, out, err = run("--root", str(root), "--check")
+ self.assertEqual(code, 1, err)
+ self.assertIn("Out of date", out)
+
+ def test_a_readable_patch_is_still_applied(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ root = valid(tmp)
+ add(root, "tcl-d", None)
+ patch = Path(tmp) / "pr.diff"
+ patch.write_text("| `tcl-d/` | TCL D | Android TV 11 | ads off |\n", encoding="utf-8")
+ code, out, err = run("--root", str(root), "--adopt", str(patch))
+ self.assertEqual(code, 0, err)
+ self.assertIn("examples/tcl-d/tv.json", out)
+ self.assertTrue((root / "examples" / "tcl-d" / "tv.json").is_file())
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/gen_tables.py b/tools/gen_tables.py
index dd28a27c3..3a1bf42a2 100644
--- a/tools/gen_tables.py
+++ b/tools/gen_tables.py
@@ -26,7 +26,8 @@ The tables live between <!-- tv-table:start ... --> and <!-- tv-table:end --> li
between <!-- tv-brands:start ... --> and <!-- tv-brands:end -->; everything outside them is hand-written.
The counts are the card's alt text ("N TV models · B brands · L languages · "), the "| **L languages** |"
feature row and STATS in tools/readme_stats.py. Exit codes: 0 up to date (or written), 1 out of date
-(--check), 2 a tv.json or a marker is wrong.
+(--check), 2 what it was given cannot be read: a tv.json, a marker, or the path in --root or --adopt
+(nothing is written then).
"""
import argparse
import json
@@ -277,6 +278,32 @@ def adopt(root, patch_text):
return out
+def read_inputs(root, patch):
+ """(the patch's text or None, the reason this run cannot start or None).
+
+ Nothing is read or written when this returns a reason. Exit 1 is the code main's "README tables" job
+ reads as "the tables are out of date", so a path this script cannot read must not come back as 1: a
+ `--root` that is not a checkout (a typo, a worktree that was removed) and an `--adopt` patch that is
+ not there both ended in a FileNotFoundError traceback before, which is exit 1 - the one answer that
+ says the generated files are stale when in fact nothing was read at all.
+ """
+ if not root.is_dir():
+ return None, f"--root {root}: not a folder. Nothing was read or written."
+ # examples/ is what it reads; the three OWNED files are the parts of the checkout it writes.
+ needs = ["examples"] + list(OWNED)
+ missing = [f"{n}/" if n == "examples" else n for n in needs if not (root / n).exists()]
+ if missing:
+ return None, (f"--root {root}: no {', no '.join(missing)} in it. Point --root at a checkout of this "
+ f"repository: nothing was read or written.")
+ if patch is None:
+ return None, None
+ try:
+ return Path(patch).read_text(encoding="utf-8"), None
+ except OSError as e:
+ # A path that is a directory is OSError too (IsADirectoryError), so this covers a patch folder.
+ return None, f"--adopt: cannot read {patch}: {e.strerror}. Nothing was written."
+
+
def main(argv=None):
ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
ap.add_argument("--root", default=str(Path(__file__).resolve().parents[1]))
@@ -290,9 +317,13 @@ def main(argv=None):
# then turn that into a CalledProcessError traceback, which names neither the file nor where it is.
ap.error(f"cannot open the font at {a.font}: Fredoka (variable TTF, Google Fonts) is not in this repo")
root = Path(a.root)
+ patch, problem = read_inputs(root, a.adopt)
+ if problem:
+ print(problem, file=sys.stderr)
+ return 2
try:
if a.adopt:
- for name, what in adopt(root, Path(a.adopt).read_text(encoding="utf-8")):
+ for name, what in adopt(root, patch):
print(f"examples/{name}/tv.json: {what}")
if a.check:
out = stale(root)