Skip to content

Commit

Permalink
Simplify bash unsupported markers
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard committed Mar 26, 2024
1 parent 11c21d5 commit 1861844
Showing 1 changed file with 30 additions and 40 deletions.
70 changes: 30 additions & 40 deletions tests/test_activate.py
Expand Up @@ -54,7 +54,7 @@
from conda.utils import quote_for_shell

if TYPE_CHECKING:
from typing import Callable, Iterable
from typing import Callable, Iterable, Literal

from pytest import MonkeyPatch

Expand Down Expand Up @@ -122,41 +122,35 @@


@lru_cache(maxsize=None)
def bash_unsupported_because():
bash = which("bash")
reason = ""
if not bash:
reason = "bash: was not found on PATH"
def bash_unsupported() -> str | Literal[False]:
if not (bash := which("bash")):
return "bash: was not found on PATH"
elif on_win:
try:
output = check_output(bash + " -c " + '"uname -v"')
output = check_output(f'{bash} -c "uname -v"')
except CalledProcessError as exc:
reason = f"bash: something went wrong while running bash, output:\n{exc.output}\n"
return f"bash: something went wrong while running bash, output:\n{exc.output}\n"
else:
if b"Microsoft" in output:
reason = "bash: WSL is not yet supported. Pull requests welcome."
return "bash: WSL is not yet supported. Pull requests welcome."
else:
output = check_output(bash + " --version")
output = check_output(f"{bash} --version")
if b"msys" not in output and b"cygwin" not in output:
reason = f"bash: Only MSYS2 and Cygwin bash are supported on Windows, found:\n{output}\n"
return reason
return f"bash: Only MSYS2 and Cygwin bash are supported on Windows, found:\n{output!r}\n"
return False


def bash_unsupported():
return True if bash_unsupported_because() else False


def bash_unsupported_win_because():
if on_win:
return (
"You are using Windows. These tests involve setting PATH to POSIX values\n"
"but our Python is a Windows program and Windows doesn't understand POSIX values."
)
return bash_unsupported_because()


def bash_unsupported_win():
return True if bash_unsupported_win_because() else False
skipif_bash_unsupported = pytest.mark.skipif(
bash_unsupported(),
reason=bash_unsupported(),
)
skipif_posix_path_unsupported = pytest.mark.skipif(
on_win,
reason=(
"You are using Windows. These tests involve setting PATH to POSIX values\n"
"but our Python is a Windows program and Windows doesn't understand POSIX values."
),
)


@pytest.fixture
Expand Down Expand Up @@ -524,7 +518,7 @@ def test_build_activate_shlvl_0(
assert builder["deactivate_scripts"] == ()


@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
@skipif_posix_path_unsupported
def test_build_activate_shlvl_1(
reset_environ: None,
monkeypatch: MonkeyPatch,
Expand Down Expand Up @@ -624,7 +618,7 @@ def test_build_activate_shlvl_1(
assert builder["deactivate_scripts"] == ()


@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
@skipif_posix_path_unsupported
def test_build_stack_shlvl_1(
reset_environ: None,
monkeypatch: MonkeyPatch,
Expand Down Expand Up @@ -767,7 +761,7 @@ def test_activate_same_environment(
)


@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
@skipif_posix_path_unsupported
def test_build_deactivate_shlvl_2_from_stack(
reset_environ: None,
monkeypatch: MonkeyPatch,
Expand Down Expand Up @@ -872,7 +866,7 @@ def test_build_deactivate_shlvl_2_from_stack(
)


@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
@skipif_posix_path_unsupported
def test_build_deactivate_shlvl_2_from_activate(
reset_environ: None,
monkeypatch: MonkeyPatch,
Expand Down Expand Up @@ -1055,7 +1049,7 @@ def test_get_env_vars_empty_file(reset_environ: None):
assert env_vars == {}


@pytest.mark.skipif(bash_unsupported_win(), reason=bash_unsupported_win_because())
@skipif_posix_path_unsupported
def test_build_activate_restore_unset_env_vars(
reset_environ: None,
monkeypatch: MonkeyPatch,
Expand Down Expand Up @@ -2611,9 +2605,7 @@ def basic_csh(shell, prefix, prefix2, prefix3):
"bash",
basic_posix,
marks=[
pytest.mark.skipif(
bash_unsupported(), reason=bash_unsupported_because()
),
skipif_bash_unsupported,
pytest.mark.skipif(
on_win, reason="Temporary skip, larger refactor necessary"
),
Expand Down Expand Up @@ -2862,7 +2854,7 @@ def test_cmd_exe_basic_integration(shell_wrapper_integration: tuple[str, str, st
shell.assert_env_var("CONDA_SHLVL", "0")


@pytest.mark.skipif(bash_unsupported(), reason=bash_unsupported_because())
@skipif_bash_unsupported
@pytest.mark.skipif(on_win, reason="Temporary skip, larger refactor necessary")
@pytest.mark.integration
def test_bash_activate_error(shell_wrapper_integration: tuple[str, str, str]):
Expand Down Expand Up @@ -2900,7 +2892,7 @@ def test_cmd_exe_activate_error(shell_wrapper_integration: tuple[str, str, str])
shell.expect("usage: conda activate")


@pytest.mark.skipif(bash_unsupported(), reason=bash_unsupported_because())
@skipif_bash_unsupported
@pytest.mark.integration
def test_legacy_activate_deactivate_bash(
shell_wrapper_integration: tuple[str, str, str],
Expand Down Expand Up @@ -3009,9 +3001,7 @@ def prefix():
[
pytest.param(
"bash",
marks=pytest.mark.skipif(
bash_unsupported(), reason=bash_unsupported_because()
),
marks=skipif_bash_unsupported,
),
pytest.param(
"cmd.exe",
Expand Down

0 comments on commit 1861844

Please sign in to comment.