Skip to content

Commit 309439d

Browse files
committed
common(feat[get_version]): thread tmux_bin through version functions
why: When Server(tmux_bin="/custom/tmux") is used, version checks still query the system tmux because get_version() calls tmux_cmd("-V") without passing tmux_bin. what: - Add optional tmux_bin parameter to get_version, has_version, has_gt_version, has_gte_version, has_lte_version, has_lt_version, and has_minimum_version - Pass tmux_bin through to tmux_cmd("-V", tmux_bin=tmux_bin) - Update mock_get_version in tests to accept tmux_bin kwarg - All parameters default to None for full backward compatibility
1 parent 9afdfe0 commit 309439d

3 files changed

Lines changed: 38 additions & 19 deletions

File tree

src/libtmux/common.py

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def __init__(self, *args: t.Any, tmux_bin: str | None = None) -> None:
318318
)
319319

320320

321-
def get_version() -> LooseVersion:
321+
def get_version(tmux_bin: str | None = None) -> LooseVersion:
322322
"""Return tmux version.
323323
324324
If tmux is built from git master, the version returned will be the latest
@@ -327,12 +327,19 @@ def get_version() -> LooseVersion:
327327
If using OpenBSD's base system tmux, the version will have ``-openbsd``
328328
appended to the latest version, e.g. ``2.4-openbsd``.
329329
330+
Parameters
331+
----------
332+
tmux_bin : str, optional
333+
Path to tmux binary. If *None*, uses the system tmux from
334+
:func:`shutil.which`.
335+
330336
Returns
331337
-------
332338
:class:`distutils.version.LooseVersion`
333-
tmux version according to :func:`shtuil.which`'s tmux
339+
tmux version according to *tmux_bin* if provided, otherwise the
340+
system tmux from :func:`shutil.which`
334341
"""
335-
proc = tmux_cmd("-V")
342+
proc = tmux_cmd("-V", tmux_bin=tmux_bin)
336343
if proc.stderr:
337344
if proc.stderr[0] == "tmux: unknown option -- V":
338345
if sys.platform.startswith("openbsd"): # openbsd has no tmux -V
@@ -357,93 +364,105 @@ def get_version() -> LooseVersion:
357364
return LooseVersion(version)
358365

359366

360-
def has_version(version: str) -> bool:
367+
def has_version(version: str, tmux_bin: str | None = None) -> bool:
361368
"""Return True if tmux version installed.
362369
363370
Parameters
364371
----------
365372
version : str
366373
version number, e.g. '3.2a'
374+
tmux_bin : str, optional
375+
Path to tmux binary. If *None*, uses the system tmux.
367376
368377
Returns
369378
-------
370379
bool
371380
True if version matches
372381
"""
373-
return get_version() == LooseVersion(version)
382+
return get_version(tmux_bin=tmux_bin) == LooseVersion(version)
374383

375384

376-
def has_gt_version(min_version: str) -> bool:
385+
def has_gt_version(min_version: str, tmux_bin: str | None = None) -> bool:
377386
"""Return True if tmux version greater than minimum.
378387
379388
Parameters
380389
----------
381390
min_version : str
382391
tmux version, e.g. '3.2a'
392+
tmux_bin : str, optional
393+
Path to tmux binary. If *None*, uses the system tmux.
383394
384395
Returns
385396
-------
386397
bool
387398
True if version above min_version
388399
"""
389-
return get_version() > LooseVersion(min_version)
400+
return get_version(tmux_bin=tmux_bin) > LooseVersion(min_version)
390401

391402

392-
def has_gte_version(min_version: str) -> bool:
403+
def has_gte_version(min_version: str, tmux_bin: str | None = None) -> bool:
393404
"""Return True if tmux version greater or equal to minimum.
394405
395406
Parameters
396407
----------
397408
min_version : str
398409
tmux version, e.g. '3.2a'
410+
tmux_bin : str, optional
411+
Path to tmux binary. If *None*, uses the system tmux.
399412
400413
Returns
401414
-------
402415
bool
403416
True if version above or equal to min_version
404417
"""
405-
return get_version() >= LooseVersion(min_version)
418+
return get_version(tmux_bin=tmux_bin) >= LooseVersion(min_version)
406419

407420

408-
def has_lte_version(max_version: str) -> bool:
421+
def has_lte_version(max_version: str, tmux_bin: str | None = None) -> bool:
409422
"""Return True if tmux version less or equal to minimum.
410423
411424
Parameters
412425
----------
413426
max_version : str
414427
tmux version, e.g. '3.2a'
428+
tmux_bin : str, optional
429+
Path to tmux binary. If *None*, uses the system tmux.
415430
416431
Returns
417432
-------
418433
bool
419434
True if version below or equal to max_version
420435
"""
421-
return get_version() <= LooseVersion(max_version)
436+
return get_version(tmux_bin=tmux_bin) <= LooseVersion(max_version)
422437

423438

424-
def has_lt_version(max_version: str) -> bool:
439+
def has_lt_version(max_version: str, tmux_bin: str | None = None) -> bool:
425440
"""Return True if tmux version less than minimum.
426441
427442
Parameters
428443
----------
429444
max_version : str
430445
tmux version, e.g. '3.2a'
446+
tmux_bin : str, optional
447+
Path to tmux binary. If *None*, uses the system tmux.
431448
432449
Returns
433450
-------
434451
bool
435452
True if version below max_version
436453
"""
437-
return get_version() < LooseVersion(max_version)
454+
return get_version(tmux_bin=tmux_bin) < LooseVersion(max_version)
438455

439456

440-
def has_minimum_version(raises: bool = True) -> bool:
457+
def has_minimum_version(raises: bool = True, tmux_bin: str | None = None) -> bool:
441458
"""Return True if tmux meets version requirement. Version >= 3.2a.
442459
443460
Parameters
444461
----------
445462
raises : bool
446463
raise exception if below minimum version requirement
464+
tmux_bin : str, optional
465+
Path to tmux binary. If *None*, uses the system tmux.
447466
448467
Returns
449468
-------
@@ -467,12 +486,12 @@ def has_minimum_version(raises: bool = True) -> bool:
467486
Versions will now remove trailing letters per
468487
`Issue 55 <https://github.com/tmux-python/tmuxp/issues/55>`_.
469488
"""
470-
if get_version() < LooseVersion(TMUX_MIN_VERSION):
489+
if get_version(tmux_bin=tmux_bin) < LooseVersion(TMUX_MIN_VERSION):
471490
if raises:
472491
msg = (
473492
f"libtmux only supports tmux {TMUX_MIN_VERSION} and greater. This "
474-
f"system has {get_version()} installed. Upgrade your tmux to use "
475-
"libtmux, or use libtmux v0.48.x for older tmux versions."
493+
f"system has {get_version(tmux_bin=tmux_bin)} installed. Upgrade your "
494+
"tmux to use libtmux, or use libtmux v0.48.x for older tmux versions."
476495
)
477496
raise exc.VersionTooLow(msg)
478497
return False

tests/legacy_api/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def test_ignores_letter_versions(monkeypatch: pytest.MonkeyPatch) -> None:
135135
def test_error_version_less_1_7(monkeypatch: pytest.MonkeyPatch) -> None:
136136
"""Test raises if tmux version less than 1.7."""
137137

138-
def mock_get_version() -> LooseVersion:
138+
def mock_get_version(tmux_bin: str | None = None) -> LooseVersion:
139139
return LooseVersion("1.7")
140140

141141
monkeypatch.setattr(libtmux.common, "get_version", mock_get_version)

tests/test_common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def test_version_validation(
494494

495495
if mock_version is not None:
496496

497-
def mock_get_version() -> LooseVersion:
497+
def mock_get_version(tmux_bin: str | None = None) -> LooseVersion:
498498
return LooseVersion(mock_version)
499499

500500
monkeypatch.setattr(libtmux.common, "get_version", mock_get_version)

0 commit comments

Comments
 (0)