Skip to content

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

tools: validate flamechart.py output arguments

Sandiv Dathunarachchidb64b949ae2e
diff --git a/tests/test_flamechart.py b/tests/test_flamechart.py
index 3d2f5c221..211c79f7a 100644
--- a/tests/test_flamechart.py
+++ b/tests/test_flamechart.py
@@ -159,6 +159,52 @@ class ArgumentTest(unittest.TestCase):
self.assertEqual(flamechart.parse(["a", "--out", "chart.png"]).out, "chart.png") # the checkout
+class OutputArgumentTest(unittest.TestCase):
+ def assert_rejected_before_trace_work(self, argv, out, flag):
+ with (mock.patch.dict(sys.modules, {"PIL": None, "perfetto": None}),
+ mock.patch.object(flamechart.trace_report, "opener") as opener,
+ mock.patch.object(flamechart.trace_report, "expand") as expand,
+ mock.patch("builtins.__import__", wraps=__import__) as imports,
+ contextlib.redirect_stderr(io.StringIO()) as err,
+ self.assertRaises(SystemExit) as caught):
+ flamechart.main(argv)
+ self.assertEqual(caught.exception.code, 2)
+ self.assertIn(flag, err.getvalue().splitlines()[-1])
+ self.assertFalse(out.exists())
+ opener.assert_not_called()
+ expand.assert_not_called()
+ self.assertFalse(any(call.args[0].split(".")[0] in ("PIL", "perfetto")
+ for call in imports.call_args_list))
+
+ def test_non_png_output_is_rejected_before_trace_work(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ for suffix in (".txt", ".svg", ".jpg"):
+ with self.subTest(suffix=suffix):
+ out = Path(tmp, "chart" + suffix)
+ self.assert_rejected_before_trace_work(["nav-1.pftrace", "--out", str(out)], out, "--out")
+
+ def test_unusable_width_is_rejected_before_trace_work(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ out = Path(tmp, "chart.png")
+ for width in (-1, 0, 1, 244, 245):
+ with self.subTest(width=width):
+ self.assert_rejected_before_trace_work(
+ ["nav-1.pftrace", "--out", str(out), "--width", str(width)], out, "--width")
+
+ def test_png_paths_and_usable_widths_are_accepted(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ for suffix in (".png", ".PNG"):
+ for width in (None, 246):
+ with self.subTest(suffix=suffix, width=width):
+ out = str(Path(tmp, "chart" + suffix))
+ argv = ["nav-1.pftrace", "--out", out]
+ if width is not None:
+ argv += ["--width", str(width)]
+ args = flamechart.parse(argv)
+ self.assertEqual(args.out, out)
+ self.assertEqual(args.width, 1600 if width is None else width)
+
+
@unittest.skipUnless(importlib.util.find_spec("PIL"), "drawing needs Pillow (tools/requirements-perf.txt)")
class DrawTest(unittest.TestCase):
def test_draws_both_panels_on_one_scale_with_magenta(self):
@@ -174,6 +220,26 @@ class DrawTest(unittest.TestCase):
self.assertEqual(im.size, size)
self.assertIn(flamechart.MAGENTA, set(im.getdata()))
+ def test_main_writes_png_at_default_and_minimum_width(self):
+ from PIL import Image
+ with tempfile.TemporaryDirectory() as tmp:
+ trace = Path(tmp, "nav-1.pftrace")
+ trace.write_bytes(b"")
+ for width in (None, 246):
+ with self.subTest(width=width):
+ out = Path(tmp, "chart.PNG" if width is not None else "chart.png")
+ argv = [str(trace), "--out", str(out)]
+ if width is not None:
+ argv += ["--width", str(width)]
+ open_trace = lambda path: (fake_trace(), lambda: None)
+ with mock.patch.object(flamechart.trace_report, "opener", return_value=open_trace), \
+ contextlib.redirect_stdout(io.StringIO()):
+ flamechart.main(argv)
+ with Image.open(out) as im:
+ self.assertEqual(im.format, "PNG")
+ self.assertEqual(im.width, 1600 if width is None else width)
+ im.verify()
+
if __name__ == "__main__":
unittest.main()
diff --git a/tools/flamechart.py b/tools/flamechart.py
index 8aca8308a..1ef008f0f 100644
--- a/tools/flamechart.py
+++ b/tools/flamechart.py
@@ -212,6 +212,8 @@ def output_path(value):
raise argparse.ArgumentTypeError(f"{value}: {folder} is a file, not a folder")
if not os.path.isdir(folder):
raise argparse.ArgumentTypeError(f"{value}: there is no folder {folder} - create it first")
+ if os.path.splitext(value)[1].lower() != ".png":
+ raise argparse.ArgumentTypeError(f"{value}: name the PNG with a .png extension")
return value
@@ -231,6 +233,8 @@ def parse(argv):
p.add_argument("--engine", metavar="PATH", help="a trace_processor_shell to use instead of downloading one")
p.add_argument("--width", type=int, default=1600)
args = p.parse_args(argv)
+ if args.width <= LEFT + 95:
+ p.error(f"--width must be greater than {LEFT + 95} pixels to leave room for the plot")
if len(args.traces) > 4:
p.error("at most four traces (panels)")
if not re.fullmatch(r"first-frame|all|start:\d+(\.\d+)?", args.window):