Skip to content

Commit

Permalink
refactor: helper/platform_code: Fix WSL GUI exit status related tests.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rsashank authored and neiljp committed Jul 5, 2024
1 parent fa05267 commit 39cc322
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 20 deletions.
31 changes: 28 additions & 3 deletions tests/helper/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,28 +602,53 @@ def test_process_media_empty_url(


@pytest.mark.parametrize(
"returncode, error",
"platform, returncode, tool, error",
[
(0, []),
("Linux", 0, "xdg-open", []),
("MacOS", 0, "open", []),
("WSL", 1, "explorer.exe", []),
(
"Linux",
1,
"xdg-open",
[
" The tool ",
("footer_contrast", "xdg-open"),
" did not run successfully" ". Exited with ",
("footer_contrast", "1"),
],
),
(
"MacOS",
1,
"open",
[
" The tool ",
("footer_contrast", "open"),
" did not run successfully" ". Exited with ",
("footer_contrast", "1"),
],
),
# NOTE: WSL always returns a non-zero exit code (1), so we do not test for it.
],
ids=[
"Linux_os_user",
"Mac_os_user",
"WSL_os_user",
"Linux_os_error",
"Mac_os_error",
],
)
def test_open_media(
mocker: MockerFixture,
platform: str,
returncode: int,
tool: str,
error: List[Any],
tool: str = "xdg-open",
media_path: str = "/tmp/zt-somerandomtext-image.png",
) -> None:
mocked_run = mocker.patch(MODULE + ".subprocess.run")
mocker.patch("zulipterminal.platform_code.PLATFORM", platform)
mocked_run.return_value.returncode = returncode
controller = mocker.Mock()

Expand Down
27 changes: 19 additions & 8 deletions tests/platform_code/test_platform_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
SupportedPlatforms,
normalized_file_path,
notify,
successful_GUI_return_code,
validate_GUI_exit_status,
)


Expand Down Expand Up @@ -76,20 +76,31 @@ def test_notify_quotes(


@pytest.mark.parametrize(
"platform, expected_return_code",
"platform, return_code, expected_return_value",
[
("Linux", 0),
("MacOS", 0),
("WSL", 1),
("Linux", 0, "success"),
("MacOS", 0, "success"),
("WSL", 1, "success"),
("Linux", 1, "failure"),
("MacOS", 1, "failure"),
# NOTE: WSL always returns a non-zero exit code (1), so we do not test for it.
],
ids=[
"Linux_0_return_code",
"MacOS_0_return_code",
"WSL_1_return_code",
"Linux_1_return_code",
"MacOS_1_return_code",
],
)
def test_successful_GUI_return_code(
def test_validate_GUI_exit_status(
mocker: MockerFixture,
platform: SupportedPlatforms,
expected_return_code: int,
return_code: int,
expected_return_value: str,
) -> None:
mocker.patch(MODULE + ".PLATFORM", platform)
assert successful_GUI_return_code() == expected_return_code
assert validate_GUI_exit_status(return_code) == expected_return_value


@pytest.mark.parametrize(
Expand Down
4 changes: 2 additions & 2 deletions zulipterminal/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from zulipterminal.platform_code import (
PLATFORM,
normalized_file_path,
successful_GUI_return_code,
validate_GUI_exit_status,
)


Expand Down Expand Up @@ -841,7 +841,7 @@ def open_media(controller: Any, tool: str, media_path: str) -> None:
command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
)
exit_status = process.returncode
if exit_status != successful_GUI_return_code():
if validate_GUI_exit_status(exit_status) == "failure":
error = [
" The tool ",
("footer_contrast", tool),
Expand Down
15 changes: 8 additions & 7 deletions zulipterminal/platform_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,19 @@ def notify(title: str, text: str) -> str:
return ""


def successful_GUI_return_code() -> int: # noqa: N802 (allow upper case)
def validate_GUI_exit_status( # noqa: N802 (allow upper case)
exit_status: int,
) -> str:
"""
Returns success return code for GUI commands, which are OS specific.
"""
# WSL uses GUI return code as 1. Refer below link to know more:
# https://stackoverflow.com/questions/52423031/
# why-does-opening-an-explorer-window-and-selecting-a-file-through-pythons-subpro/
# 52423798#52423798
# NOTE: WSL always returns a non-zero exit code (1) for GUI commands.
# This is a known issue. Therefore, we consider it a success if the exit code is 1.
# For more information, see: https://github.com/microsoft/WSL/issues/6565
if PLATFORM == "WSL":
return 1
return "success" if exit_status == 1 else "failure"

return 0
return "success" if exit_status == 0 else "failure"


def normalized_file_path(path: str) -> str:
Expand Down

0 comments on commit 39cc322

Please sign in to comment.