Skip to content

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

tools: restore.sh refuses a run with no list, or a flag it does not have, in its own words and 2

xbsheng3e633b2ae8f7
diff --git a/tests/test_restore_command_line.py b/tests/test_restore_command_line.py
index 52075f86e..f7848b377 100644
--- a/tests/test_restore_command_line.py
+++ b/tests/test_restore_command_line.py
@@ -1,8 +1,17 @@
-"""restore.sh reads its own command line before it moves: a list that is not there, and a third word.
+"""restore.sh reads its own command line before it moves: no list, a flag, a list that is not there, a third word.
The undo is the script an owner reaches for when the TV already looks wrong, and it read its first two
words only:
+- `restore.sh` with no word at all - the list forgotten, or a variable that was empty - was answered by
+ bash's own `${1:?}`: `tools/restore.sh: line 11: 1: usage: restore.sh <debloat list> [stock launcher
+ package]`, a script line number and a stray `1:` where the reader expected the script to speak, and exit
+ 1, which this script keeps for a restore that started and did not finish. The header gives that shape 2.
+- `restore.sh --help` (`-h`, `--reset` copied over from theme.sh) was handed to recovery.py as the list:
+ "could not read --help (No such file or directory). Nothing was sent to the TV", and the run went on to
+ contact the TV, so the status was recovery.py's 1 when it answered and tv.py's connection failure when it
+ did not - for a reader asking the script for its usage. restore.ps1 refuses these on its own - PowerShell
+ reads a dash-led word as a parameter name.
- `restore.sh examples/xiaomi-mi-tv-4/debloat.txt` - one character short of a folder that exists -
ended inside the script's folder resolution: bash printed `cd: examples/xiaomi-mi-tv-4: No such
file or directory` with restore.sh's own line number, named a folder where the reader had typed a
@@ -14,11 +23,12 @@ words only:
recovery.py refuses three words with its usage line and exit 2 when a caller hands them over.
restore.ps1 refuses them on its own: PowerShell binds two positional arguments.
-Both now answer before anything is sent: the folder that is not there keeps the path as typed, so
-recovery.py's refusal is what the reader reads, and a word left over exits 2 saying it would not have
-been used. What the script does with the two words it takes is unchanged - the list is still resolved
-against the caller's directory in the `pwd -W` form Git Bash needs for the record's key, and the batch
-still goes through recovery.py and tv.py --batch.
+All four now answer before anything is sent, in the script's own words with its usage line: no list and a
+dash-led word exit 2 (the command line is not one it can read, and the header says so), the folder that is
+not there keeps the path as typed so recovery.py's refusal is what the reader reads, and a word left over
+exits 2 saying it would not have been used. What the script does with the two words it takes is unchanged -
+the list is still resolved against the caller's directory in the `pwd -W` form Git Bash needs for the
+record's key, and the batch still goes through recovery.py and tv.py --batch.
tools/tv.py is a fake that records the batch, so "nothing was sent" is asserted and the documented
command lines are still shown to restore. The restore.ps1 cases need `pwsh` on PATH and skip without
@@ -132,6 +142,32 @@ class RestoreCommandLineTest(unittest.TestCase):
self.assertNotIn("would not have been used", output)
self.assertIn("pm enable com.example.ads", self.sent())
+ def test_no_list_at_all_is_the_scripts_own_refusal(self):
+ for args, why in ([(), "no word at all"], (("",), "an empty word, e.g. a variable that was not set"),
+ (("", "com.google.android.tvlauncher"), "an empty list and the stock launcher")):
+ with self.subTest(why):
+ self.state_path.write_text(json.dumps({"commands": []}))
+ result, output = self.run_script("restore.sh", *args)
+ self.assertEqual(result.returncode, 2, output)
+ self.assertIn("restore.sh takes a debloat list", result.stderr, "the script says what it needs")
+ self.assertIn("usage: TV_HOST=<tv ip> tools/restore.sh", result.stderr)
+ self.assertIn("Nothing was sent to the TV", result.stderr)
+ self.assertNotIn("restore.sh: line", output, "bash's own ${1:?} named a script line, not the mistake")
+ self.assertEqual(result.stdout, "")
+ self.assertEqual(self.sent(), [])
+
+ def test_a_dash_led_word_is_a_flag_it_does_not_have(self):
+ for word in ("--help", "-h", "--reset"):
+ with self.subTest(word):
+ result, output = self.run_script("restore.sh", word)
+ self.assertEqual(result.returncode, 2, output)
+ self.assertIn(word + " is a flag it does not have", result.stderr)
+ self.assertIn("usage: TV_HOST=<tv ip> tools/restore.sh", result.stderr)
+ self.assertNotIn("could not read " + word, output,
+ "recovery.py read the flag as a list called " + word)
+ self.assertEqual(result.stdout, "")
+ self.assertEqual(self.sent(), [])
+
def test_restore_ps1_already_answers_both_shapes(self):
extra = "com.google.android.tvlauncher"
cases = [((str(self.list_path), extra, extra), extra),
diff --git a/tests/test_restore_references.py b/tests/test_restore_references.py
index dd5d9b9a6..99a6e1826 100644
--- a/tests/test_restore_references.py
+++ b/tests/test_restore_references.py
@@ -59,7 +59,7 @@ PLACEHOLDERS = {"examples/il-tuo-modello/debloat.txt", "examples/your-model/debl
FOLDER_SCRIPT = re.compile(r"(examples/[A-Za-z0-9._-]+/(?:restore|disable|install|build-and-install)\."
r"(?:sh|ps1))")
# `tools/restore.sh <list> <stock launcher>`: the list is restore.sh's first argument, the stock
-# launcher its second (see its usage line and LIST=${1:?} / STOCK=${2:-...}). Both arguments are
+# launcher its second (see its usage line and LIST="${1:-}" / STOCK="${2:-...}"). Both arguments are
# matched with no `.txt` required in the pattern, so a command that leaves the list out is read too.
PAIR = re.compile(r"tools[/\\]restore\.sh\s+(\S+)\s+(\S+)")
PACKAGE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z0-9_]+)+")
diff --git a/tools/restore.sh b/tools/restore.sh
index 4d2573f97..f2a3aabf2 100755
--- a/tools/restore.sh
+++ b/tools/restore.sh
@@ -6,9 +6,26 @@
# Usage: TV_HOST=192.168.1.50 tools/restore.sh examples/xiaomi-mi-tv-4a/debloat.txt [stock.launcher.package]
#
# Exit status: 0 the recorded states are back; 1 the restore did not finish; 2 the command line is not a
-# list plus an optional stock launcher package, so nothing was sent to the TV.
+# list plus an optional stock launcher package - no list word at all, an empty one, or a dash-led word
+# that is a flag somewhere else - so nothing was sent to the TV.
set -euo pipefail
-LIST="${1:?usage: restore.sh <debloat list> [stock launcher package]}"
+LIST="${1:-}"
+if [ -z "$LIST" ]; then
+ echo "restore.sh takes a debloat list: pass the same one you ran disable.sh with. Nothing was sent to the TV." >&2
+ echo "usage: TV_HOST=<tv ip> tools/restore.sh <debloat list> [stock launcher package]" >&2
+ exit 2
+fi
+# A dash-led word is a flag this script does not have - `--help`, `-h`, `--reset` copied over from
+# theme.sh - and it was read as the list: recovery.py named a file called --help, and the run went on to
+# contact the TV anyway, so the reader asking for the usage got "could not read --help (No such file or
+# directory)" and a status that depended on whether the TV answered. The twin refuses these on its own
+# (PowerShell reads a dash-led word as a parameter name), so this is the script that had to say it.
+case "$LIST" in
+ -*)
+ echo "restore.sh takes a debloat list and an optional stock launcher package, and $LIST is a flag it does not have: nothing was sent to the TV." >&2
+ echo "usage: TV_HOST=<tv ip> tools/restore.sh <debloat list> [stock launcher package]" >&2
+ exit 2 ;;
+esac
# A list and the optional stock launcher, and nothing else. Only the first two words were read, so a
# line with a word appended - the stock package named twice, a second list path, `--reset` copied over
# from theme.sh - restored the TV while that word was dropped without a word, on the one script an