Skip to content

Commit 6853166

Browse files
committed
refactor: helper/platform_code: Fix WSL GUI exit status related tests.
WSL always returns a non-zero exit code. The `successful_GUI_return_code()` function has been refactored to directly compare exit statuses and return "success" or "failure" accordingly.
1 parent 40d4425 commit 6853166

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

tests/helper/test_helper.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,28 +603,49 @@ def test_process_media_empty_url(
603603

604604

605605
@pytest.mark.parametrize(
606-
"returncode, error",
606+
"platform, returncode, tool, error",
607607
[
608-
(0, []),
609-
(
608+
case("Linux", 0, "xdg-open", [], id="Linux:success"),
609+
case("MacOS", 0, "open", [], id="MacOS:success"),
610+
case("WSL", 1, "explorer.exe", [], id="WSL:success"),
611+
case(
612+
"Linux",
610613
1,
614+
"xdg-open",
611615
[
612616
" The tool ",
613617
("footer_contrast", "xdg-open"),
614618
" did not run successfully" ". Exited with ",
615619
("footer_contrast", "1"),
616620
],
621+
id="Linux:error",
617622
),
623+
case(
624+
"MacOS",
625+
1,
626+
"open",
627+
[
628+
" The tool ",
629+
("footer_contrast", "open"),
630+
" did not run successfully" ". Exited with ",
631+
("footer_contrast", "1"),
632+
],
633+
id="Mac:error",
634+
),
635+
# NOTE: explorer.exe (WSL) always returns a non-zero exit code (1)
636+
# so we do not test for it.
618637
],
619638
)
620639
def test_open_media(
621640
mocker: MockerFixture,
641+
platform: str,
622642
returncode: int,
643+
tool: str,
623644
error: List[Any],
624-
tool: str = "xdg-open",
625645
media_path: str = "/tmp/zt-somerandomtext-image.png",
626646
) -> None:
627647
mocked_run = mocker.patch(MODULE + ".subprocess.run")
648+
mocker.patch("zulipterminal.platform_code.PLATFORM", platform)
628649
mocked_run.return_value.returncode = returncode
629650
controller = mocker.Mock()
630651

tests/platform_code/test_platform_code.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
SupportedPlatforms,
77
normalized_file_path,
88
notify,
9-
successful_GUI_return_code,
9+
validate_GUI_exit_status,
1010
)
1111

1212

@@ -76,20 +76,25 @@ def test_notify_quotes(
7676

7777

7878
@pytest.mark.parametrize(
79-
"platform, expected_return_code",
79+
"platform, return_code, expected_return_value",
8080
[
81-
("Linux", 0),
82-
("MacOS", 0),
83-
("WSL", 1),
81+
("Linux", 0, "success"),
82+
("MacOS", 0, "success"),
83+
("WSL", 1, "success"),
84+
("Linux", 1, "failure"),
85+
("MacOS", 1, "failure"),
86+
# NOTE: explorer.exe (WSL) always returns a non-zero exit code (1),
87+
# so we do not test for it.
8488
],
8589
)
86-
def test_successful_GUI_return_code(
90+
def test_validate_GUI_exit_status(
8791
mocker: MockerFixture,
8892
platform: SupportedPlatforms,
89-
expected_return_code: int,
93+
return_code: int,
94+
expected_return_value: str,
9095
) -> None:
9196
mocker.patch(MODULE + ".PLATFORM", platform)
92-
assert successful_GUI_return_code() == expected_return_code
97+
assert validate_GUI_exit_status(return_code) == expected_return_value
9398

9499

95100
@pytest.mark.parametrize(

zulipterminal/helper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from zulipterminal.platform_code import (
4343
PLATFORM,
4444
normalized_file_path,
45-
successful_GUI_return_code,
45+
validate_GUI_exit_status,
4646
)
4747

4848

@@ -841,7 +841,7 @@ def open_media(controller: Any, tool: str, media_path: str) -> None:
841841
command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
842842
)
843843
exit_status = process.returncode
844-
if exit_status != successful_GUI_return_code():
844+
if validate_GUI_exit_status(exit_status) == "failure":
845845
error = [
846846
" The tool ",
847847
("footer_contrast", tool),

zulipterminal/platform_code.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,19 @@ def notify(title: str, text: str) -> str:
8989
return ""
9090

9191

92-
def successful_GUI_return_code() -> int: # noqa: N802 (allow upper case)
92+
def validate_GUI_exit_status( # noqa: N802 (allow upper case)
93+
exit_status: int,
94+
) -> Literal["success", "failure"]:
9395
"""
9496
Returns success return code for GUI commands, which are OS specific.
9597
"""
96-
# WSL uses GUI return code as 1. Refer below link to know more:
97-
# https://stackoverflow.com/questions/52423031/
98-
# why-does-opening-an-explorer-window-and-selecting-a-file-through-pythons-subpro/
99-
# 52423798#52423798
98+
# NOTE: WSL (explorer.exe) always returns a non-zero exit code (1) for GUI commands.
99+
# This is a known issue. Therefore, we consider it a success if the exit code is 1.
100+
# For more information, see: https://github.com/microsoft/WSL/issues/6565
100101
if PLATFORM == "WSL":
101-
return 1
102+
return "success"
102103

103-
return 0
104+
return "success" if exit_status == 0 else "failure"
104105

105106

106107
def normalized_file_path(path: str) -> str:

0 commit comments

Comments
 (0)