From 78fcf33b3a77003e102401acedf5e40f4ba69f62 Mon Sep 17 00:00:00 2001 From: AramJonghu Date: Sun, 24 May 2026 03:09:19 +0200 Subject: [PATCH] refactor(cli): clean shell start/restart, drop redundant ipc check --- .../__pycache__/scheme.cpython-314.pyc | Bin 30628 -> 30626 bytes .../__pycache__/screenshot.cpython-314.pyc | Bin 1042 -> 1040 bytes .../__pycache__/shell.cpython-314.pyc | Bin 6639 -> 6472 bytes .../__pycache__/wallpaper.cpython-314.pyc | Bin 2438 -> 2436 bytes cli/src/zshell/subcommands/shell.py | 29 ++++++----- cli/tests/test_shell.py | 47 ++++++++---------- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/cli/src/zshell/subcommands/__pycache__/scheme.cpython-314.pyc b/cli/src/zshell/subcommands/__pycache__/scheme.cpython-314.pyc index e5535703c5f0e815418b2501a20b9cf701d2044c..06584ac0875cc72ca33cd0db9527c59ae4813dbb 100644 GIT binary patch delta 39 tcmZ4To^jE8Ms96BUM>b8P<9vE$gRi1ZL6P=pPQ0Ms96BUM>b8SoB_SBexz4uf2Xoer~FMc7A1kZsukembhX71CR{K diff --git a/cli/src/zshell/subcommands/__pycache__/screenshot.cpython-314.pyc b/cli/src/zshell/subcommands/__pycache__/screenshot.cpython-314.pyc index ecd3450d69ddad48296ea32af8832e861aa929c1..f1921ea0037d724a68c84ccba54f287a3d7a389b 100644 GIT binary patch delta 37 rcmbQlF@b|yn~#@^0SJ`cg*I}}W8`+!&&bbB)lV!+%-y_=F@^~Mjt~hU delta 39 tcmbQhF^Pj)n~#@^0SNN832fw^$H?ofpOK%Ns-K--nV*}vc?)9<69BdD3eo@o diff --git a/cli/src/zshell/subcommands/__pycache__/shell.cpython-314.pyc b/cli/src/zshell/subcommands/__pycache__/shell.cpython-314.pyc index a33767152f267077925eaebafc8768711f2566be..d6ca5095d3c790b30885eb76e34adb694c63a97e 100644 GIT binary patch delta 1671 zcmZ8h&u<$=6rNeH?akV|o3)+TiR~n=W5-TNoY17W6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&x-8q8!y+0@@Z0ufUmCw@T;dmF#DGJm4Iz9mCVo+t>6Z9K&wU-lpeMP%dwR~f zzkBXE@ARvl-+R?|O;rfWoj*OVZxq!A`c1Lz2z3?562;x1t|n|cNr*a`bI;Kzk3%Ua z=%Q+|+-H+@0!=hR9dL}ct4^Kl$7sdffcTmi55THW*G8kXa<-Qy6lAPA!01lU5Q%6d zhp&Sqep^|K13GBri8kDDaZIsjitMYT$IOQ9GLk(5!4+Q)t jxo;yv^)AZWHF#=HumJ0D`=20vowI7 None: + result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True) + stdout = result.stdout.decode().strip() + if stdout: + if "already running" in stdout.lower(): + raise click.ClickException(stdout) + if result.returncode != 0: + stderr = result.stderr.decode().strip() + raise click.ClickException(stderr) + + @app.command() def start(no_daemon: bool = False): - check = subprocess.run(args + ["ipc"] + ["show"], capture_output=True) - if check.returncode == 0: - raise click.ClickException("An instance of this configuration is already running.") - result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True) - if result.returncode != 0: - raise click.ClickException(result.stderr.decode().strip()) - sys.stderr.write(result.stderr.decode()) + start_instance(no_daemon) @app.command() def restart(no_daemon: bool = False): - subprocess.run(args + ["kill"]) - for _ in range(50): + subprocess.run(args + ["kill"], capture_output=True) + deadline = time.monotonic() + 2.5 + while time.monotonic() < deadline: result = subprocess.run(args + ["kill"], capture_output=True) if result.returncode == 255: break time.sleep(0.05) - result = subprocess.run(args + ["-n"] + ([] if no_daemon else ["-d"]), capture_output=True) - if result.returncode != 0: - raise click.ClickException(result.stderr.decode().strip()) - sys.stderr.write(result.stderr.decode()) + start_instance(no_daemon=no_daemon) @app.command() diff --git a/cli/tests/test_shell.py b/cli/tests/test_shell.py index 9875d99..1763247 100644 --- a/cli/tests/test_shell.py +++ b/cli/tests/test_shell.py @@ -34,35 +34,30 @@ class TestKill: class TestStart: @patch("zshell.subcommands.shell.subprocess.run") def test_start_default_daemon(self, mock_run): - mock_run.side_effect = [ - CompletedProcess([], 1, b"", b""), # ipc show → no instance - CompletedProcess([], 0, b"", b"Launching config\n"), # launch ok - ] + mock_run.return_value = CompletedProcess([], 0, b"", b"Launching config\n") invoke("start") - assert mock_run.call_args_list == [ - call(["qs", "-c", "zshell", "ipc", "show"], capture_output=True), - call(["qs", "-c", "zshell", "-n", "-d"], capture_output=True), - ] + mock_run.assert_called_once_with(["qs", "-c", "zshell", "-n", "-d"], capture_output=True) @patch("zshell.subcommands.shell.subprocess.run") def test_start_no_daemon(self, mock_run): - mock_run.side_effect = [ - CompletedProcess([], 1, b"", b""), - CompletedProcess([], 0, b"", b"Launching config\n"), - ] + mock_run.return_value = CompletedProcess([], 0, b"", b"Launching config\n") invoke("start", "--no-daemon") - assert mock_run.call_args_list == [ - call(["qs", "-c", "zshell", "ipc", "show"], capture_output=True), - call(["qs", "-c", "zshell", "-n"], capture_output=True), - ] + mock_run.assert_called_once_with(["qs", "-c", "zshell", "-n"], capture_output=True) @patch("zshell.subcommands.shell.subprocess.run") def test_start_already_running_errors(self, mock_run): - mock_run.return_value = CompletedProcess([], 0, b"", b"target visibilities\n") + mock_run.return_value = CompletedProcess([], 0, b"An instance of this configuration is already running.\n", b"") result = runner.invoke(app, ["start"]) assert result.exit_code != 0 assert "already running" in result.output + @patch("zshell.subcommands.shell.subprocess.run") + def test_start_other_failure_errors(self, mock_run): + mock_run.return_value = CompletedProcess([], 1, b"", b"Config error\n") + result = runner.invoke(app, ["start"]) + assert result.exit_code != 0 + assert "Config error" in result.output + class TestShow: @patch("zshell.subcommands.shell.subprocess.run") @@ -106,30 +101,30 @@ class TestCall: class TestRestart: + @patch("zshell.subcommands.shell.start_instance") @patch("zshell.subcommands.shell.subprocess.run") - def test_restart_kills_then_starts(self, mock_run): + def test_restart_kills_then_starts(self, mock_run, mock_start): mock_run.side_effect = [ - CompletedProcess([], 0, b"", b"Killed abc\n"), # first kill (no capture) + CompletedProcess([], 0, b"", b"Killed abc\n"), # first kill (captured) CompletedProcess([], 255, b"", b""), # poll → no instance - CompletedProcess([], 0, b"", b"Launching config\n"), # launch ok ] invoke("restart") assert mock_run.call_args_list == [ - call(["qs", "-c", "zshell", "kill"]), # no capture_output call(["qs", "-c", "zshell", "kill"], capture_output=True), - call(["qs", "-c", "zshell", "-n", "-d"], capture_output=True), + call(["qs", "-c", "zshell", "kill"], capture_output=True), ] + mock_start.assert_called_once_with(no_daemon=False) + @patch("zshell.subcommands.shell.start_instance") @patch("zshell.subcommands.shell.subprocess.run") - def test_restart_no_daemon(self, mock_run): + def test_restart_no_daemon(self, mock_run, mock_start): mock_run.side_effect = [ CompletedProcess([], 0, b"", b"Killed abc\n"), CompletedProcess([], 255, b"", b""), - CompletedProcess([], 0, b"", b"Launching config\n"), ] invoke("restart", "--no-daemon") assert mock_run.call_args_list == [ - call(["qs", "-c", "zshell", "kill"]), call(["qs", "-c", "zshell", "kill"], capture_output=True), - call(["qs", "-c", "zshell", "-n"], capture_output=True), + call(["qs", "-c", "zshell", "kill"], capture_output=True), ] + mock_start.assert_called_once_with(no_daemon=True)