Skip to content

Commit 39cc322

Browse files
rsashankneiljp
authored andcommitted
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 fa05267 commit 39cc322

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

tests/helper/test_helper.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,28 +602,53 @@ def test_process_media_empty_url(
602602

603603

604604
@pytest.mark.parametrize(
605-
"returncode, error",
605+
"platform, returncode, tool, error",
606606
[
607-
(0, []),
607+
("Linux", 0, "xdg-open", []),
608+
("MacOS", 0, "open", []),
609+
("WSL", 1, "explorer.exe", []),
608610
(
611+
"Linux",
609612
1,
613+
"xdg-open",
610614
[
611615
" The tool ",
612616
("footer_contrast", "xdg-open"),
613617
" did not run successfully" ". Exited with ",
614618
("footer_contrast", "1"),
615619
],
616620
),
621+
(
622+
"MacOS",
623+
1,
624+
"open",
625+
[
626+
" The tool ",
627+
("footer_contrast", "open"),
628+
" did not run successfully" ". Exited with ",
629+
("footer_contrast", "1"),
630+
],
631+
),
632+
# NOTE: WSL always returns a non-zero exit code (1), so we do not test for it.
633+
],
634+
ids=[
635+
"Linux_os_user",
636+
"Mac_os_user",
637+
"WSL_os_user",
638+
"Linux_os_error",
639+
"Mac_os_error",
617640
],
618641
)
619642
def test_open_media(
620643
mocker: MockerFixture,
644+
platform: str,
621645
returncode: int,
646+
tool: str,
622647
error: List[Any],
623-
tool: str = "xdg-open",
624648
media_path: str = "/tmp/zt-somerandomtext-image.png",
625649
) -> None:
626650
mocked_run = mocker.patch(MODULE + ".subprocess.run")
651+
mocker.patch("zulipterminal.platform_code.PLATFORM", platform)
627652
mocked_run.return_value.returncode = returncode
628653
controller = mocker.Mock()
629654

tests/platform_code/test_platform_code.py

Lines changed: 19 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,31 @@ 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: WSL always returns a non-zero exit code (1), so we do not test for it.
87+
],
88+
ids=[
89+
"Linux_0_return_code",
90+
"MacOS_0_return_code",
91+
"WSL_1_return_code",
92+
"Linux_1_return_code",
93+
"MacOS_1_return_code",
8494
],
8595
)
86-
def test_successful_GUI_return_code(
96+
def test_validate_GUI_exit_status(
8797
mocker: MockerFixture,
8898
platform: SupportedPlatforms,
89-
expected_return_code: int,
99+
return_code: int,
100+
expected_return_value: str,
90101
) -> None:
91102
mocker.patch(MODULE + ".PLATFORM", platform)
92-
assert successful_GUI_return_code() == expected_return_code
103+
assert validate_GUI_exit_status(return_code) == expected_return_value
93104

94105

95106
@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+
) -> str:
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 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" if exit_status == 1 else "failure"
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)