|
| 1 | +"""Tests for utility functions in libtmux.""" |
| 2 | + |
| 3 | +import re |
| 4 | +import sys |
| 5 | +import typing as t |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +import libtmux |
| 11 | +from libtmux._compat import LooseVersion |
| 12 | +from libtmux.common import ( |
| 13 | + TMUX_MAX_VERSION, |
| 14 | + TMUX_MIN_VERSION, |
| 15 | + get_libtmux_version, |
| 16 | + get_version, |
| 17 | + has_gt_version, |
| 18 | + has_gte_version, |
| 19 | + has_lt_version, |
| 20 | + has_lte_version, |
| 21 | + has_minimum_version, |
| 22 | + has_version, |
| 23 | + session_check_name, |
| 24 | + tmux_cmd, |
| 25 | +) |
| 26 | +from libtmux.exc import BadSessionName, LibTmuxException, TmuxCommandNotFound |
| 27 | +from libtmux.session import Session |
| 28 | + |
| 29 | +version_regex = re.compile(r"([0-9]\.[0-9])|(master)") |
| 30 | + |
| 31 | + |
| 32 | +def test_allows_master_version(monkeypatch: pytest.MonkeyPatch) -> None: |
| 33 | + class Hi: |
| 34 | + stdout = ["tmux master"] |
| 35 | + stderr = None |
| 36 | + |
| 37 | + def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi: |
| 38 | + return Hi() |
| 39 | + |
| 40 | + monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd) |
| 41 | + |
| 42 | + assert has_minimum_version() |
| 43 | + assert has_gte_version(TMUX_MIN_VERSION) |
| 44 | + assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version" |
| 45 | + assert ( |
| 46 | + "%s-master" % TMUX_MAX_VERSION == get_version() |
| 47 | + ), "Is the latest supported version with -master appended" |
| 48 | + |
| 49 | + |
| 50 | +def test_allows_next_version(monkeypatch: pytest.MonkeyPatch) -> None: |
| 51 | + TMUX_NEXT_VERSION = str(float(TMUX_MAX_VERSION) + 0.1) |
| 52 | + |
| 53 | + class Hi: |
| 54 | + stdout = [f"tmux next-{TMUX_NEXT_VERSION}"] |
| 55 | + stderr = None |
| 56 | + |
| 57 | + def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi: |
| 58 | + return Hi() |
| 59 | + |
| 60 | + monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd) |
| 61 | + |
| 62 | + assert has_minimum_version() |
| 63 | + assert has_gte_version(TMUX_MIN_VERSION) |
| 64 | + assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version" |
| 65 | + assert TMUX_NEXT_VERSION == get_version() |
| 66 | + |
| 67 | + |
| 68 | +def test_get_version_openbsd(monkeypatch: pytest.MonkeyPatch) -> None: |
| 69 | + class Hi: |
| 70 | + stderr = ["tmux: unknown option -- V"] |
| 71 | + |
| 72 | + def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi: |
| 73 | + return Hi() |
| 74 | + |
| 75 | + monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd) |
| 76 | + monkeypatch.setattr(sys, "platform", "openbsd 5.2") |
| 77 | + assert has_minimum_version() |
| 78 | + assert has_gte_version(TMUX_MIN_VERSION) |
| 79 | + assert has_gt_version(TMUX_MAX_VERSION), "Greater than the max-supported version" |
| 80 | + assert ( |
| 81 | + "%s-openbsd" % TMUX_MAX_VERSION == get_version() |
| 82 | + ), "Is the latest supported version with -openbsd appended" |
| 83 | + |
| 84 | + |
| 85 | +def test_get_version_too_low(monkeypatch: pytest.MonkeyPatch) -> None: |
| 86 | + class Hi: |
| 87 | + stderr = ["tmux: unknown option -- V"] |
| 88 | + |
| 89 | + def mock_tmux_cmd(*args: t.Any, **kwargs: t.Any) -> Hi: |
| 90 | + return Hi() |
| 91 | + |
| 92 | + monkeypatch.setattr(libtmux.common, "tmux_cmd", mock_tmux_cmd) |
| 93 | + with pytest.raises(LibTmuxException) as exc_info: |
| 94 | + get_version() |
| 95 | + exc_info.match("is running tmux 1.3 or earlier") |
| 96 | + |
| 97 | + |
| 98 | +def test_ignores_letter_versions(monkeypatch: pytest.MonkeyPatch) -> None: |
| 99 | + """Ignore letters such as 1.8b. |
| 100 | +
|
| 101 | + See ticket https://github.com/tmux-python/tmuxp/issues/55. |
| 102 | +
|
| 103 | + In version 0.1.7 this is adjusted to use LooseVersion, in order to |
| 104 | + allow letters. |
| 105 | +
|
| 106 | + """ |
| 107 | + monkeypatch.setattr(libtmux.common, "TMUX_MIN_VERSION", "1.9a") |
| 108 | + result = has_minimum_version() |
| 109 | + assert result |
| 110 | + |
| 111 | + monkeypatch.setattr(libtmux.common, "TMUX_MIN_VERSION", "1.8a") |
| 112 | + result = has_minimum_version() |
| 113 | + assert result |
| 114 | + |
| 115 | + # Should not throw |
| 116 | + assert type(has_version("1.8")) is bool |
| 117 | + assert type(has_version("1.8a")) is bool |
| 118 | + assert type(has_version("1.9a")) is bool |
| 119 | + |
| 120 | + |
| 121 | +def test_error_version_less_1_7(monkeypatch: pytest.MonkeyPatch) -> None: |
| 122 | + def mock_get_version() -> LooseVersion: |
| 123 | + return LooseVersion("1.7") |
| 124 | + |
| 125 | + monkeypatch.setattr(libtmux.common, "get_version", mock_get_version) |
| 126 | + with pytest.raises(LibTmuxException) as excinfo: |
| 127 | + has_minimum_version() |
| 128 | + excinfo.match(r"libtmux only supports") |
| 129 | + |
| 130 | + with pytest.raises(LibTmuxException) as excinfo: |
| 131 | + has_minimum_version() |
| 132 | + |
| 133 | + excinfo.match(r"libtmux only supports") |
| 134 | + |
| 135 | + |
| 136 | +def test_has_version() -> None: |
| 137 | + assert has_version(str(get_version())) |
| 138 | + |
| 139 | + |
| 140 | +def test_has_gt_version() -> None: |
| 141 | + assert has_gt_version("1.6") |
| 142 | + assert has_gt_version("1.6b") |
| 143 | + |
| 144 | + assert not has_gt_version("4.0") |
| 145 | + assert not has_gt_version("4.0b") |
| 146 | + |
| 147 | + |
| 148 | +def test_has_gte_version() -> None: |
| 149 | + assert has_gte_version("1.6") |
| 150 | + assert has_gte_version("1.6b") |
| 151 | + assert has_gte_version(str(get_version())) |
| 152 | + |
| 153 | + assert not has_gte_version("4.0") |
| 154 | + assert not has_gte_version("4.0b") |
| 155 | + |
| 156 | + |
| 157 | +def test_has_lt_version() -> None: |
| 158 | + assert has_lt_version("4.0a") |
| 159 | + assert has_lt_version("4.0") |
| 160 | + |
| 161 | + assert not has_lt_version("1.7") |
| 162 | + assert not has_lt_version(str(get_version())) |
| 163 | + |
| 164 | + |
| 165 | +def test_has_lte_version() -> None: |
| 166 | + assert has_lte_version("4.0a") |
| 167 | + assert has_lte_version("4.0") |
| 168 | + assert has_lte_version(str(get_version())) |
| 169 | + |
| 170 | + assert not has_lte_version("1.7") |
| 171 | + assert not has_lte_version("1.7b") |
| 172 | + |
| 173 | + |
| 174 | +def test_tmux_cmd_raises_on_not_found(monkeypatch: pytest.MonkeyPatch) -> None: |
| 175 | + monkeypatch.setenv("PATH", "") |
| 176 | + with pytest.raises(TmuxCommandNotFound): |
| 177 | + tmux_cmd("-V") |
| 178 | + |
| 179 | + |
| 180 | +def test_tmux_cmd_unicode(session: Session) -> None: |
| 181 | + session.cmd("new-window", "-t", 3, "-n", "юникод", "-F", "Ελληνικά") |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.parametrize( |
| 185 | + "session_name,raises,exc_msg_regex", |
| 186 | + [ |
| 187 | + ("", True, "may not be empty"), |
| 188 | + (None, True, "may not be empty"), |
| 189 | + ("my great session.", True, "may not contain periods"), |
| 190 | + ("name: great session", True, "may not contain colons"), |
| 191 | + ("new great session", False, None), |
| 192 | + ("ajf8a3fa83fads,,,a", False, None), |
| 193 | + ], |
| 194 | +) |
| 195 | +def test_session_check_name( |
| 196 | + session_name: Optional[str], raises: bool, exc_msg_regex: Optional[str] |
| 197 | +) -> None: |
| 198 | + if raises: |
| 199 | + with pytest.raises(BadSessionName) as exc_info: |
| 200 | + session_check_name(session_name) |
| 201 | + if exc_msg_regex is not None: |
| 202 | + assert exc_info.match(exc_msg_regex) |
| 203 | + else: |
| 204 | + session_check_name(session_name) |
| 205 | + |
| 206 | + |
| 207 | +def test_get_libtmux_version() -> None: |
| 208 | + from libtmux.__about__ import __version__ |
| 209 | + |
| 210 | + version = get_libtmux_version() |
| 211 | + assert isinstance(version, LooseVersion) |
| 212 | + assert LooseVersion(__version__) == version |
0 commit comments