Skip to content

Commit efa23bb

Browse files
committed
bugfix: tests: 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. Fix open_media test to validate both the tool and the error code across various platforms. Refactor process_media to shift platform dependent code to platform_code.py.
1 parent 3b52fcd commit efa23bb

File tree

4 files changed

+61
-38
lines changed

4 files changed

+61
-38
lines changed

tests/helper/test_helper.py

+33-8
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ def test_download_media(
548548
("Linux", True, True, "xdg-open", "/path/to/media"),
549549
("MacOS", True, True, "open", "/path/to/media"),
550550
("WSL", True, True, "explorer.exe", "\\path\\to\\media"),
551-
("UnknownOS", True, False, "unknown-tool", "/path/to/media"),
551+
("UnknownOS", True, False, "invalid", "/path/to/media"),
552552
],
553553
ids=[
554554
"Linux_os_user",
@@ -572,9 +572,13 @@ def test_process_media(
572572
MODULE + ".download_media", return_value=media_path
573573
)
574574
mocked_open_media = mocker.patch(MODULE + ".open_media")
575-
mocker.patch(MODULE + ".PLATFORM", platform)
576-
mocker.patch("zulipterminal.platform_code.PLATFORM", platform)
577-
mocker.patch("zulipterminal.core.Controller.show_media_confirmation_popup")
575+
576+
# Mocking the PLATFORM variable in both platform_code and the main module
577+
# to ensure consistency during tests that rely on platform detection.
578+
mocker.patch("zulipterminal.platform_code" + ".PLATFORM", platform)
579+
mocker.patch(
580+
"zulipterminal.platform_code" + ".process_media_tool", return_value=tool
581+
)
578582

579583
process_media(controller, link)
580584

@@ -603,28 +607,49 @@ def test_process_media_empty_url(
603607

604608

605609
@pytest.mark.parametrize(
606-
"returncode, error",
610+
"platform, returncode, tool, error",
607611
[
608-
(0, []),
609-
(
612+
case("Linux", 0, "xdg-open", [], id="Linux:success"),
613+
case("MacOS", 0, "open", [], id="MacOS:success"),
614+
case("WSL", 1, "explorer.exe", [], id="WSL:success"),
615+
case(
616+
"Linux",
610617
1,
618+
"xdg-open",
611619
[
612620
" The tool ",
613621
("footer_contrast", "xdg-open"),
614622
" did not run successfully" ". Exited with ",
615623
("footer_contrast", "1"),
616624
],
625+
id="Linux:error",
617626
),
627+
case(
628+
"MacOS",
629+
1,
630+
"open",
631+
[
632+
" The tool ",
633+
("footer_contrast", "open"),
634+
" did not run successfully" ". Exited with ",
635+
("footer_contrast", "1"),
636+
],
637+
id="Mac:error",
638+
),
639+
# NOTE: explorer.exe (WSL) always returns a non-zero exit code (1)
640+
# so we do not test for it.
618641
],
619642
)
620643
def test_open_media(
621644
mocker: MockerFixture,
645+
platform: str,
622646
returncode: int,
647+
tool: str,
623648
error: List[Any],
624-
tool: str = "xdg-open",
625649
media_path: str = "/tmp/zt-somerandomtext-image.png",
626650
) -> None:
627651
mocked_run = mocker.patch(MODULE + ".subprocess.run")
652+
mocker.patch("zulipterminal.platform_code" + ".PLATFORM", platform)
628653
mocked_run.return_value.returncode = returncode
629654
controller = mocker.Mock()
630655

tests/platform_code/test_platform_code.py

+13-8
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

+7-15
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
REGEX_QUOTED_FENCE_LENGTH,
4141
)
4242
from zulipterminal.platform_code import (
43-
PLATFORM,
43+
process_media_tool,
4444
normalized_file_path,
45-
successful_GUI_return_code,
45+
validate_GUI_exit_status,
4646
)
4747

4848

@@ -785,18 +785,10 @@ def process_media(controller: Any, link: str) -> None:
785785
)
786786
media_path = download_media(controller, link, show_download_status)
787787
media_path = normalized_file_path(media_path)
788-
tool = ""
789-
790-
# TODO: Add support for other platforms as well.
791-
if PLATFORM == "WSL":
792-
tool = "explorer.exe"
793-
# Modifying path to backward slashes instead of forward slashes
794-
media_path = media_path.replace("/", "\\")
795-
elif PLATFORM == "Linux":
796-
tool = "xdg-open"
797-
elif PLATFORM == "MacOS":
798-
tool = "open"
799-
else:
788+
789+
tool = process_media_tool()
790+
791+
if tool == "invalid":
800792
controller.report_error("Media not supported for this platform")
801793
return
802794

@@ -841,7 +833,7 @@ def open_media(controller: Any, tool: str, media_path: str) -> None:
841833
command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
842834
)
843835
exit_status = process.returncode
844-
if exit_status != successful_GUI_return_code():
836+
if validate_GUI_exit_status(exit_status) == "failure":
845837
error = [
846838
" The tool ",
847839
("footer_contrast", tool),

zulipterminal/platform_code.py

+8-7
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)