Skip to content

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

tools: tv.py refuses a blank --screenshot destination, and a trailing separator on a folder that is not there yet

xbshenge50948930242
diff --git a/tests/test_tv_screenshot_target.py b/tests/test_tv_screenshot_target.py
index 4b2fe652a..007e71eb5 100644
--- a/tests/test_tv_screenshot_target.py
+++ b/tests/test_tv_screenshot_target.py
@@ -18,6 +18,12 @@ opened, so "nothing was captured" is observed rather than assumed.
A folder that is not there is still made (#364) and an existing PNG is still overwritten; both are
pinned here so the refusal cannot start refusing the runs the pages describe.
+Two shapes of a destination were left after that: a word that is only whitespace -
+`--screenshot "$SHOT"` with SHOT holding a space - was accepted and its pull wrote a file named
+`" "`, and a trailing separator on a folder that is not there yet slipped past the isdir() check,
+after which that folder was made and the pull wrote to a folder name. Both are refused now, by the
+same judge, before the TV is opened; each has a test below.
+
Run from the repository root: python3 -m unittest discover -s tests -v
"""
import os
@@ -155,6 +161,20 @@ class ScreenshotTargetTest(unittest.TestCase):
def test_a_folder_with_its_trailing_separator_is_refused(self):
self.assertRefused(["--screenshot", str(self.shots) + os.sep], f"{self.shots}{os.sep} is a folder")
+ def test_a_trailing_separator_for_a_folder_that_is_not_there_is_refused(self):
+ # The isdir() check above only answers a folder that exists: with the separator on a name that is
+ # not there yet, os.path.abspath() drops it, the folder was made, and the run went on to
+ # `screencap` before the pull wrote to a name that is a folder.
+ target = os.path.join(str(self.work()), "newshots") + os.sep
+ self.assertRefused(["--screenshot", target], "ends with a separator", "01-home.png",
+ "Nothing was captured.")
+
+ def test_a_blank_destination_is_refused(self):
+ # `--screenshot "$SHOT"` with SHOT holding a space: accepted, the pull wrote a file named " ",
+ # and the run reported a screenshot at a path that is not the one the reader asked for.
+ err = self.assertRefused(["--screenshot", " "], "blank")
+ self.assertIn("tv.py --screenshot", err, "a blank argument still carries the usage")
+
def test_an_empty_destination_is_a_usage_error(self):
# `--screenshot "$SHOT"` with SHOT never set: adb_shell's pull answered FileNotFoundError: ''.
err = self.assertRefused(["--screenshot", ""], "--screenshot needs the file to save to")
diff --git a/tools/tv.py b/tools/tv.py
index 8a73c78a7..f7a8507f6 100755
--- a/tools/tv.py
+++ b/tools/tv.py
@@ -508,13 +508,25 @@ def screenshot_target(local):
have been judged this way since #2039; this is the flag the pages hand out most.
A folder that is not there is still made rather than refused: that is the one shape the command is
- expected to fix for itself.
+ expected to fix for itself. Two words are not a file name and are refused here: one that is only
+ whitespace, which `--screenshot "$SHOT"` sends when SHOT holds a space (the pull wrote a file named
+ " " and the run printed `screenshot (N bytes)`, exit 0, while the path the reader meant was
+ never made), and one that ends with a separator while that folder is not there yet - the check below
+ only answers a folder that exists, and os.path.abspath() drops the separator, so the folder was made
+ and `screencap` ran on the TV before the pull wrote to a name that is a folder.
"""
if not local:
usage_error("--screenshot needs the file to save to")
+ if not local.strip():
+ usage_error(f"--screenshot's file is blank ({local!r}): name the file to save to, such as "
+ f"shots/01-home.png")
if os.path.isdir(local):
path_error(f"{local} is a folder, and --screenshot saves the PNG to a file: give a file name, such "
f"as {os.path.join(local, '01-home.png')}. Nothing was captured.")
+ if local.endswith(("/", os.sep)):
+ path_error(f"{local} ends with a separator, so it names a folder, and --screenshot saves the PNG "
+ f"to a file: give a file name such as {os.path.join(local, '01-home.png')}. "
+ f"Nothing was captured.")
folder = os.path.dirname(os.path.abspath(local))
if os.path.exists(folder) and not os.path.isdir(folder):
path_error(f"{folder} is a file, not a folder: --screenshot cannot save {local} inside it. "